<commit_before>#!/usr/bin/python
'''Part of VAMP project, only for import.

Extracts feature positions from images of aspirated vesicles
(as of now only for Phase Contrast images) - extracts a brightness profile
along the system axis, finds positions of pipette tip,
vesicle and aspirated vesicle tip.

more details are documented in vamp.tex

prerequisites - installed numpy, scipy
'''
#for arrays and math
from numpy import sqrt, square, sum  # these are the most common ones just for convenience
import numpy as np
from scipy import ndimage
from fitting import fit_err
import smooth

from common import PIX_ERR

def section_profile(img, point1, point2):
    '''define the brightness profile along the section between 2 points

    coordinates of end points with their errors are supplied as numpy arrays 
    in notation array((y,x),(dy,dx))!
    #TODO:might as well submit other options to map_coordinates function

    '''
    # define the line given by 2 ends
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    #fast solving of trivial cases
    if x1==x2:
        return 1, 0, img[y1:y2,x1]
    if y1==y2:
        return 1, 0, img[y1,x1:x2]
    
    deltax = np.fabs(x2-x1)
    deltay = np.fabs(y2-y1)
    ddeltax = np.sqrt(dx1**2+dx2**2)
    ddeltay = np.sqrt(dy1**2+dy2**2)

    longleg = max(deltax, deltay)
    shortleg = min(deltax, deltay)
    
    # number of points for profile
    nPoints = int(longleg)

    #coordinates of points in the profile
    x = np.linspace(x1, x2, nPoints)
    y = np.linspace(y1, y2, nPoints)
    #interpolated values at points of profile
    profile = ndimage.map_coordinates(img, [y, x], output = float)
    
    #calculate profile metric - coefficient for lengths in profile vs pixels
    k = np.sqrt(deltax**2+deltay**2) / longleg
    
    metric = sqrt(1-k**2)

    metric_err = PIX_ERR
    return metric, metric_err, profile

def line_profile2(img, point1, point2):
    '''define the brightness profile along the line defined by 2 points
        across the whole image

    coordinates of points with their errors are supplied as numpy arrays 
    in notation array((y,x),(dy,dx))!
    might as well submit other options to map_coordinates function

    '''
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    #fast solving of trivial cases
    if x1==x2:
        return 1, 0, img[:,x1]
    if y1==y2:
        return 1, 0, img[y1,:]
    
    k = (y2 - y1) / (x2 - x1)
    b = y1 - k*x1
    
    dk = sqrt(dy1*dy1 + dy2*dy2 + k*k*(dx1*dx1+dx2*dx2) )/np.fabs(x2-x1)
    
    #find ponits of intersection with image borders
    sizey, sizex = img.shape
    ends = {'yleft':(b,0),
            'yright':((sizex-1)*k+b,sizex-1),
            'xbottom':(0,-b/k),
            'xtop':(sizey-1,(sizey-1-b)/k)}
    for key, value in ends:
        x,y = value
        if 'x' in key:
            if not (0 <= x <= sizex-1):
                ends.pop(key)
        elif 'y' in key:
            if not (0 < y < sizey-1):
                ends.pop(key)
    assert len(ends) == 2
    end1, end2 = ends.values
    
def line_profile(img, point1, point2):
    '''define the brightness profile along the line defined by 2 points
        across the whole image

    coordinates of points with their errors are supplied as numpy arrays 
    in notation array((y,x),(dy,dx))!
    might as well submit other options to map_coordinates function

    it is assumed that pipette is more or less horizontal
    so that axis intersects left and right image sides
    '''
    # define the line going though 2 points
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    k = (y2 - y1) / (x2 - x1)
    b = y1 - k*x1
    
    dk = sqrt(dy1*dy1 + dy2*dy2 + k*k*(dx1*dx1+dx2*dx2) )/np.fabs(x2-x1)

    # number of points for profile
    # it is assumed that pipette is more or less horizontal
    # so that axis intersects left and right image sides
    nPoints = max(np.fabs(k) * (img.shape[1] - 1) + 1, img.shape[1])

    #coordinates of points in the profile
    x = np.linspace(0, img.shape[1] - 1, nPoints)
    y = np.linspace(b, k * (img.shape[1] - 1) + b, nPoints)

    #calculate profile metric - coefficient for lengths in profile vs pixels
    if np.fabs(k) <=1:
        metric = sqrt(1 + k*k)
        metric_err = np.fabs(k)*dk/metric
    else:
        metric = sqrt(1 + 1/(k*k))
        metric_err = dk/np.fabs(metric * k*k*k)
    #output interpolated values at points of profile and profile metric
    return metric, metric_err, ndimage.map_coordinates(img, [y, x], output = float)

def point_to_line_dist(point, point1, point2):
    '''Point to line distance.
    Finds distance (unsigned) from point to line defined by 2 points
    point - point from which distance is calculated (with errors)
    point1, point2 - 2 points forming the line from which distance is calculated (with errors)

    all arguments are in numpy-array notation, i.e. array((y,x),(dy,dx))!
    '''
    y0,x0,dy0,dx0 = point.flatten()
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    
    k = (y2-y1)/(x2-x1)
    b = y1-k*x1
    dist = (k*x0-y0+b)/sqrt(1+k*k)
    
    dk = sqrt(dy1*dy1 + dy2*dy2 + k*k*(dx1*dx1+dx2*dx2) )/np.fabs(x2-x1)
    db = sqrt(dy1*dy1 + x1*x1*dk*dk + k*k*dx1*dx1)
    dist_err = sqrt(k*k*dx0*dx0 + dk*dk*x0*x0 + db*db + dy0*dy0 + 
                    k*k*dk*dk*(k*x0+b-y0)**2/(1+k*k))/(1+k*k)
    return np.asarray((np.fabs(dist), dist_err))

def wall_points_subpix(img, refsx, mode):
#    return refssub, refs_err, extra_walls
    return<commit_msg>added passing of additional arguments to scipy.ndimage.map_coordinates inside line profile extraction routines<commit_after>#!/usr/bin/python
'''Part of VAMP project, only for import.

Extracts feature positions from images of aspirated vesicles
(as of now only for Phase Contrast images) - extracts a brightness profile
along the system axis, finds positions of pipette tip,
vesicle and aspirated vesicle tip.

more details are documented in vamp.tex

prerequisites - installed numpy, scipy
'''
#for arrays and math
from numpy import sqrt, square, sum  # these are the most common ones just for convenience
import numpy as np
from scipy import ndimage
from fitting import fit_err
import smooth

from common import PIX_ERR

def section_profile(img, point1, point2, **mapkwargs):
    '''define the brightness profile along the section between 2 points

    coordinates of end points with their errors are supplied as numpy arrays 
    in notation array((y,x),(dy,dx))!

    '''
    # define the line given by 2 ends
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    #fast solving of trivial cases
    if x1==x2:
        return 1, 0, img[y1:y2,x1]
    if y1==y2:
        return 1, 0, img[y1,x1:x2]
    
    deltax = np.fabs(x2-x1)
    deltay = np.fabs(y2-y1)
    ddeltax = np.sqrt(dx1**2+dx2**2)
    ddeltay = np.sqrt(dy1**2+dy2**2)

    longleg = max(deltax, deltay)
    shortleg = min(deltax, deltay)
    
    # number of points for profile
    nPoints = int(longleg)

    #coordinates of points in the profile
    x = np.linspace(x1, x2, nPoints)
    y = np.linspace(y1, y2, nPoints)
    #interpolated values at points of profile
    mapkwargs['output'] = float
    profile = ndimage.map_coordinates(img, [y, x], **mapkwargs)
    
    #calculate profile metric - coefficient for lengths in profile vs pixels
    k = np.sqrt(deltax**2+deltay**2) / longleg
    
    metric = sqrt(1-k**2)

    metric_err = PIX_ERR
    return metric, metric_err, profile

def line_profile2(img, point1, point2):
    '''define the brightness profile along the line defined by 2 points
        across the whole image

    coordinates of points with their errors are supplied as numpy arrays 
    in notation array((y,x),(dy,dx))!
    might as well submit other options to map_coordinates function

    '''
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    #fast solving of trivial cases
    if x1==x2:
        return 1, 0, img[:,x1]
    if y1==y2:
        return 1, 0, img[y1,:]
    
    k = (y2 - y1) / (x2 - x1)
    b = y1 - k*x1
    
    dk = sqrt(dy1*dy1 + dy2*dy2 + k*k*(dx1*dx1+dx2*dx2) )/np.fabs(x2-x1)
    
    #find ponits of intersection with image borders
    sizey, sizex = img.shape
    ends = {'yleft':(b,0),
            'yright':((sizex-1)*k+b,sizex-1),
            'xbottom':(0,-b/k),
            'xtop':(sizey-1,(sizey-1-b)/k)}
    for key, value in ends:
        x,y = value
        if 'x' in key:
            if not (0 <= x <= sizex-1):
                ends.pop(key)
        elif 'y' in key:
            if not (0 < y < sizey-1):
                ends.pop(key)
    assert len(ends) == 2
    end1, end2 = ends.values
    
def line_profile(img, point1, point2, **mapkwargs):
    '''define the brightness profile along the line defined by 2 points
        across the whole image

    coordinates of points with their errors are supplied as numpy arrays 
    in notation array((y,x),(dy,dx))!
    might as well submit other options to map_coordinates function

    it is assumed that pipette is more or less horizontal
    so that axis intersects left and right image sides
    '''
    # define the line going though 2 points
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    k = (y2 - y1) / (x2 - x1)
    b = y1 - k*x1
    
    dk = sqrt(dy1*dy1 + dy2*dy2 + k*k*(dx1*dx1+dx2*dx2) )/np.fabs(x2-x1)

    # number of points for profile
    # it is assumed that pipette is more or less horizontal
    # so that axis intersects left and right image sides
    nPoints = max(np.fabs(k) * (img.shape[1] - 1) + 1, img.shape[1])

    #coordinates of points in the profile
    x = np.linspace(0, img.shape[1] - 1, nPoints)
    y = np.linspace(b, k * (img.shape[1] - 1) + b, nPoints)

    #calculate profile metric - coefficient for lengths in profile vs pixels
    if np.fabs(k) <=1:
        metric = sqrt(1 + k*k)
        metric_err = np.fabs(k)*dk/metric
    else:
        metric = sqrt(1 + 1/(k*k))
        metric_err = dk/np.fabs(metric * k*k*k)
    #output interpolated values at points of profile and profile metric
    mapkwargs['output'] = float
    return metric, metric_err, ndimage.map_coordinates(img, [y, x], **mapkwargs)

def point_to_line_dist(point, point1, point2):
    '''Point to line distance.
    Finds distance (unsigned) from point to line defined by 2 points
    point - point from which distance is calculated (with errors)
    point1, point2 - 2 points forming the line from which distance is calculated (with errors)

    all arguments are in numpy-array notation, i.e. array((y,x),(dy,dx))!
    '''
    y0,x0,dy0,dx0 = point.flatten()
    y1,x1,dy1,dx1 = point1.flatten()
    y2,x2,dy2,dx2 = point2.flatten()
    
    k = (y2-y1)/(x2-x1)
    b = y1-k*x1
    dist = (k*x0-y0+b)/sqrt(1+k*k)
    
    dk = sqrt(dy1*dy1 + dy2*dy2 + k*k*(dx1*dx1+dx2*dx2) )/np.fabs(x2-x1)
    db = sqrt(dy1*dy1 + x1*x1*dk*dk + k*k*dx1*dx1)
    dist_err = sqrt(k*k*dx0*dx0 + dk*dk*x0*x0 + db*db + dy0*dy0 + 
                    k*k*dk*dk*(k*x0+b-y0)**2/(1+k*k))/(1+k*k)
    return np.asarray((np.fabs(dist), dist_err))

def wall_points_subpix(img, refsx, mode):
#    return refssub, refs_err, extra_walls
    return
{'fraction_non_alphanumeric': 0.11911947452511983, 'fraction_numerical': 0.03550505947097461, 'mean_word_length': 2.8180277871907826, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441541', 'n_tokens_mistral': 4203, 'n_tokens_neox': 4061, 'n_words': 1229}