Пример #1
0
def nps_finder_tp_cluster(image_file, z_extent, filter_kernel, min_sep):
    files, metadata = preprocessor.data_org(tifdir)

    columns = ('x', 'y', 'z', 'a_total', 'intensity', 'particle', 'com_dist',
               'trial')
    all_parts = pd.DataFrame([], columns=columns)

    for i in range(0, len(files)):
        print(i)
        print(files[i])
        frames = pims.TiffStack('./tifs/' + folders[m] + '/tifs/' + files[i])
        # nps = np.array(frames[::2])
        # nuclei = np.array(frames[1::2])
        features = tp.batch(nps[:], diameter=filter_kernel, separation=min_sep)

        # Option to add a mass cut
        # features = features[features['raw_mass'] > 0]

        if len(features) == 0:
            features['particle'] = []
        if len(features) == 1:
            features['particle'] = 1
        elif len(features) > 1:
            # Clustering to eliminate maxima that are too close together
            positions = features[['x', 'y', 'frame']].values
            # Distance matrix is n-particles x n-particles in size - reckon it gives the interparticle separation
            # This gives the upper triangle of the distance matrix
            dist_mat = dist.pdist(positions)
            link_mat = hier.linkage(dist_mat)
            # fcluster assigns each of the particles in positions a cluster to which it belongs
            cluster_idx = hier.fcluster(link_mat, 5, criterion='distance')
            features['particle'] = cluster_idx

        n_parts = np.unique(features['particle'].values)
        values_clustered = []
        for j in n_parts:
            current = features[features['particle'] == j]
            if len(current) > z_extent:
                norm = np.sum(current['raw_mass'])
                x_av = np.mean(current['x'])
                y_av = np.mean(current['y'])
                # z-value is the intensity-weighted value
                z_av = np.sum(current['raw_mass'] * current['frame']) / norm
                a_total = np.sum(current['size'])
                # Geometric cut-off to particle size
                if a_total > 1:
                    values_clustered.append(
                        [x_av, y_av, z_av, a_total, norm, j])

        columns_clustered = ('x', 'y', 'z', 'a_total', 'intensity', 'particle')
        particles_clustered = pd.DataFrame(values_clustered,
                                           columns=columns_clustered)
Пример #2
0
def main():

    frequencies = np.zeros(28)
    displacement = np.zeros(28)
    error = np.zeros(28)
    i = 0
    k = 0
    directory = os.path.dirname(os.path.realpath(__file__)) + '/Samples/'

    for filename in os.listdir(directory):
        tempStr = ''
        print('i')
        if ('Hz' in filename):
            displacement[k], error[k] = Find_Gauss_Intersect(
                pims.TiffStack('./Samples/' + filename)[0])

            i = 0
            while (filename[i] != 'H'):

                tempStr += filename[i]
                i += 1

            frequencies[k] = int(tempStr)
            k += 1

    save = open('Data.txt', 'w')
    for i in range(0, 28):
        save.write(
            str(frequencies[i]) + '\t' + str(displacement[i]) + '\t' +
            str(error[i]) + '\n')

    save.close()
    plt.errorbar(frequencies,
                 displacement,
                 yerr=error,
                 xerr=5,
                 fmt='none',
                 label='Raw Data')
    plt.xlabel("Frequency of Rotating Mirror(Hz)")
    plt.ylabel("Pixel Position (pixel)")

    coef, cov = np.polyfit(frequencies, displacement, 1, cov=True)
    print(cov)
    newEquation = np.poly1d(coef)
    fitData = newEquation(frequencies)
    plt.plot(frequencies, fitData, label='Linear Fit')
    plt.title("Slope = " + str(coef[0]))
    plt.legend()
    print(cov)
    plt.show()
    def __init__(self, dir_path):

        self.imgSeq = pims.TiffStack(dir_path)  # this is what Trackpy uses
        self.img = io.imread(dir_path, plugin='tifffile')
        # self.__normalize_img()
        self.shape = self.img.shape
        meta = dir_path.split('/')[-1].split('_')
        self.date = meta[0]
        self.coverslip = meta[1]
        self.exp = meta[2]
        self.name = meta[3]
        self.exposure = meta[4]
        self.frames = meta[5][0:-4]
        self.fname = dir_path.split('/')[-1][0:-4]
Пример #4
0
def stackloader(filename, dir_in='', plot=True):
    # Load up a stack - assumes that PLA signal on channel 1, DAPI on channel 2
    if dir_in == '':
        data = pims.TiffStack('./' + filename)
    if dir_in != '':
        data = pims.TiffStack(dir_in + filename)
    # This may need removing to run on the Mac / non-Conda Python
    # data = data[0]

    nuclei = np.array(data[1::2])
    nps = np.array(data[::2])

    z_size, y_size, x_size = np.shape(nps)

    nrows = np.int(np.ceil(np.sqrt(z_size)))
    ncols = np.int(z_size // nrows + 1)

    # print z_size, nrows, ncols

    if plot == True:
        fig, axes = plt.subplots(nrows, ncols, figsize=(3 * ncols, 3 * nrows))

        for n in range(z_size):
            i = n // ncols
            j = n % ncols
            axes[i, j].imshow(nuclei[n], interpolation='nearest', cmap='gray')

        for ax in axes.ravel():
            if not (len(ax.images)):
                fig.delaxes(ax)
        fig.tight_layout()

        plt.show()
        plt.close()

    return nuclei, nps, nrows, ncols
Пример #5
0
def cell_detect(file, var, opt):
    print('Detecting cells')
    # http://soft-matter.github.io/trackpy/v0.3.2/tutorial/walkthrough.html

    # load, run object detection and track (then clean up)
    raw_frames = pims.TiffStack(file, as_grey=True)  # load

    # only analyse n frames
    if var['frames_keep'] is not 0:
        raw_frames = raw_frames[0:var['frames_keep']]

    # object detect
    cellsdf = tp.batch(raw_frames,
                       var['diameter'],
                       minmass=var['minFluroMass'],
                       separation=var['separation'],
                       engine='numba',
                       max_iterations=1,
                       characterize=False,
                       noise_size=var['noise_smooth'])

    # remove brightest objects
    cellsdf = cellsdf.drop(cellsdf[cellsdf.mass > var['maxFluroMass']].index)

    if opt['plot_inter_static']:
        annotate_args = {"vmin": 0, "vmax": 200}
        # Tweak styles
        plt.ion()
        plt.show()
        fig_dims = np.multiply(0.01, raw_frames[0].shape)
        mpl.rc('figure',
               figsize=(fig_dims[1].astype(int), fig_dims[0].astype(int)))
        mpl.rc('image', cmap='gray')

        # plot final particles chosen
        plt.figure()
        tp.annotate(cellsdf[cellsdf.frame == var['frame_plot']],
                    raw_frames[var['frame_plot']],
                    imshow_style=annotate_args)
        plt.title('Particles included in analysis'
                  '(at t=' + str(var['frame_plot']) + ')')
        plt.draw()
        plt.pause(0.001)

    return cellsdf, raw_frames
Пример #6
0
def predict_videos(path, num_videos):
    videos = os.listdir(join(path))
    pred_videos=[]
    for i in range(num_videos):
        pims_sequence = pims.TiffStack(join(path, videos[i]), process_func=None)
        frames = np.stack([frame.copy() for frame in pims_sequence], axis=2)
        pred_frames=np.zeros((768,768,frames.shape[2]))
        for j in range(frames.shape[2]):
            aux=frames[:, :, j]
            aux = resize_image(aux, [768, 768])
            patches = split_image(aux, (256,256))
            patches = patches.reshape(patches.shape+(1,))
            resultado = model.predict(patches, verbose=2)
            resultado = np.uint8(np.argmax(resultado, axis=3))
            imagen = reconstruction(resultado, (768, 768))
            pred_frames[:, :, j] = imagen
        pred_videos.append(pred_frames)
    return pred_videos
    
Пример #7
0
    return args.boundaries_fn, args.tif_fn


if __name__ == '__main__':

    ### Parse command line arguments
    boundaries_fn, tif_fn = parse_command_line_args()

    ### Load data
    all_boundary_pts = np.load(boundaries_fn)
    try:
        cell_label = re.findall(r'\d+', boundaries_fn)[0]
    except IndexError:
        cell_label = -1

    frames = pims.TiffStack(tif_fn)
    frame = frames[0]

    ### smooth and plot cell trajectory ###
    masks = [grid_points_in_poly(frame.shape, pts) for pts in all_boundary_pts]
    centers = np.array([regionprops(mask)[0].centroid for mask in masks])

    # estimate initial velocity via regression on first few timepoints
    init_pts = 5
    vx0, _, _, _, _ = linregress(range(init_pts), centers[:init_pts, 0])
    vy0, _, _, _, _ = linregress(range(init_pts), centers[:init_pts, 1])
    initial_state = np.array([centers[0, 0], centers[0, 1], vx0, vy0])

    # smooth the cell center positions
    position_noise = 15.0  # higher noise -> heavier smoothing
    smoother = KalmanSmoother2D(position_noise, position_noise)
from pims import Frame

# In[ ]:

import trackpy
import trackpy.diag

trackpy.__version__
#trackpy.diag.dependencies()

# In[ ]:

shot = 235
pic = 18333
v = pims.TiffStack(
    '/Users/pinghanchu/Documents/Git/Data/Shot{}/Shot{}_Cam_{}.tif'.format(
        shot, shot, pic))
size = len(v)
v0 = tp.bandpass(v[0], 0, 300, threshold=5, truncate=4)
imsave(
    "/Users/pinghanchu/Documents/Git/Data/Shot{}/Clean_Data_Shot{}_Cam_{}/FrameL0.tif"
    .format(shot, shot, pic), v0)
zero = tp.bandpass(v[300], 0, 300, threshold=5, truncate=10) - v0
zero[zero > 0] = 0
zero[zero < 0] = 0
a = zero
b = zero
comb_index = 1
run_range = int(size / comb_index)
for iv1 in range(0, run_range):
    b = zero
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import DataFrame, Series
import pims
import trackpy as tp
from uncertainties import ufloat

T = ufloat(293,1)
eta = 1/1000
a = ufloat(0.00000099, 0.00000003)




frames = pims.TiffStack('C:/Users/Ewout van der Velde/Downloads/Untitled4.tif', as_grey=False)

f = tp.locate(frames[-250:], 21, invert = True, minmass=1000)

f.head()

plt.figure()
tp.annotate(f, frames[0])


fig,ax = plt.subplots()
ax.hist(f['mass'], bins=40)

f = tp.batch(frames[-100:], 21, minmass = 1000, invert=True)
t = tp.link_df(f, 5, memory=3)
t.head()
Пример #10
0
def input_to_highlighted_png(input_file_name, dist_0=0, use_green_channel=True, **kwargs):
	'''if a blue figure is returnd, check that vmin == - vmax'''
	#get frame number, frm, for a given file_name
	# from cv2 import drawMarker
	lst = input_file_name.split('.')
	assert(len(lst)>2)
	frm = int(eval(lst[-2]))
	#only consider frames with a history, so that we may calculate optical flow
	if frm>1:
		out = None
	else:
		return False
	
	os.chdir(kwargs['data_dir'])
	df = pd.read_csv(kwargs['cluster_dir'])
	#raw dic frames
	frames = pims.TiffStack_libtiff(kwargs['dic_dir'])

	# compute outward flow
	os.chdir(kwargs['tmp_dir'])
	current, previous, previous_previous = load_data(input_file_name)
	# current = current[...,1].astype('uint8')
	# previous = previous[...,1].astype('uint8')
	# previous_previous = previous_previous[...,1].astype('uint8')
	current = current.astype('uint8')
	previous = previous.astype('uint8')
	previous_previous = previous_previous.astype('uint8')
	flow = compute_flow(current, previous, previous_previous, **kwargs)

	position = df.loc[df.frame==frm][['x','y']].values.T
	area_channel = current.copy()

	flow_radial = compute_flow_out(flow, position, area_channel = area_channel, frm=frm, **kwargs)

	image = get_image(frames, frm)

	if use_green_channel:
		#get the green channel
		os.chdir(kwargs['data_dir'])
		imgs = pims.TiffStack(kwargs['fret_dir'])
		input_array = imgs[frm]
		green_channel = get_green_channel_from(input_array, baseline = 1000,  x_shiftby = 0, rotateby = 0)#, x_shiftby = 2, rotateby = 15)
		boo=~np.isfinite(green_channel)
		green_channel[boo]=0.
		image[...,1] *= green_channel/4+1
		image[...,0] /= green_channel/4+1
		image[...,2] /= green_channel/4+1

	# flow_out = flow_radial[...,1]
	flow_in = flow_radial[...,0]
	# lograt_ = get_lograt_out(-1.*flow_in, area_channel, baseline = 0.01)
	# lograt_[boo] = 0.
	signal = -1.*flow_in #lograt_

	#set signal values in distances within a distance dist_0 to zero
	r0 = dist_0/kwargs['lamda'] #pixels
	# if of is None:
	of = OpticalFlowClient(dt=kwargs['dt'])
		# kwargs['of'] = of
	rhat,rmat = of.get_r_hat_mat(position)
	boo = rmat<r0
	signal[boo] = 0.
	fig = highlight_flow_bwr(image, signal, vmin = kwargs['vmin'], vmax = kwargs['vmax'], figsize = (10,10), mydpi = 512/10)

	#save fig as .png in tmp2
	if input_file_name.find('tmp/preprocessed_snapshot')==-1:
		return False
	save_marked_fig_dir = input_file_name.replace('tmp/preprocessed_snapshot','tmp2/highlighted_snapshot')
	assert(save_marked_fig_dir != input_file_name)
	#if ^this assert fails, check that input_file_name is an absolute directory
	#     save_figure(fig, save_marked_fig_dir=save_marked_fig_dir)
	img_fn = save_marked_fig_dir
	# fig.tight_layout()
	plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
	fig.savefig(img_fn, dpi = 512/10)
	plt.close()
	return img_fn
Пример #11
0
def my_input_file_name_to_highlight(input_file_name, dist_0=0, use_green_channel = True, **kwargs):
	'''if a blue figure is returnd, check that vmin == - vmax'''
	#initialize for a given frame
	dt    = kwargs['dt']
	lamda = kwargs['lamda']
	worker = Worker(lamda=lamda, dt=dt)
	of  = worker.optical_flow_client()
	dis = worker.dis_client()
	asserting = worker.asserting

	#get frame number, frm, for a given file_name
	lst = input_file_name.split('.')
	assert(len(lst)>2)
	frm = int(eval(lst[-2]))
	#only consider frames with a history, so that we may calculate optical flow
	if frm>1:
		out = None
	else:
		return False
	# import the aggregate's trajectory
	os.chdir(kwargs['data_dir'])
	df = pd.read_csv(kwargs['cluster_dir'])	
	position = df.loc[df.frame==frm][['x','y']].values.T

	#raw dic frames
	frames = pims.TiffStack_libtiff(kwargs['dic_dir'])
	
	#     file_name = f"{tmp_folder}/preprocessed_snapshot.{int(frm)}.png"
	if asserting:
		assert ( os.path.exists(input_file_name) ) 

	#import from a folder of preprocessed frames stored as .png's
	current, previous, previous_previous = load_input_grayscale_data_second_order(input_file_name)
	#convert to grayscale uint8 (as required by dis.calc *sad face* )
	current = current.astype('uint8')
	previous = previous.astype('uint8')
	previous_previous = previous_previous.astype('uint8')	
	
	try:
		#compute the flow (redunant)
		current_flow = dis.calc(previous_previous, previous, flow = None)
		new_flow     = dis.calc(previous, current, flow = current_flow)
	except Exception as e: 
		current = current[...,0].astype('uint8')
		previous = previous[...,0].astype('uint8')
		previous_previous = previous_previous[...,0].astype('uint8')

		current_flow = dis.calc(previous_previous, previous, flow = None)
		new_flow     = dis.calc(previous, current, flow = current_flow)
	
	#  compute the flow_out and the flow_in
	flow_radial = calc_sharp_optical_flow(img=current,img_before=previous, 
										  position = position, dt=dt, frm = frm, 
										  area_channel = current, 
										  img_before_before = previous_previous, of=of)
	flow_out = flow_radial[...,1]
	flow_in = flow_radial[...,0]
	area_channel = current.copy()

	image = get_image(frames, frm)

	if use_green_channel:
		#get the green channel
		os.chdir(kwargs['data_dir'])
		imgs = pims.TiffStack(kwargs['fret_dir'])
		input_array = imgs[frm]
		green_channel = get_green_channel_from(input_array, baseline = 1000,  x_shiftby = 0, rotateby = 0)#, x_shiftby = 2, rotateby = 15)
		boo=~np.isfinite(green_channel)
		green_channel[boo]=0.
		image[...,1] *= green_channel/2+1


	fig = highlight_flow_bwr(image, flow_in, kwargs['vmin'], kwargs['vmax'], figsize = (10,10), mydpi = 512/10)


	#save fig as .png in tmp2
	if input_file_name.find('tmp/preprocessed_snapshot')==-1:
		return False
	save_marked_fig_dir = input_file_name.replace('tmp/preprocessed_snapshot','tmp2/highlighted_snapshot')
	assert(save_marked_fig_dir != input_file_name)
	#if ^this assert fails, check that input_file_name is an absolute directory
	#     save_figure(fig, save_marked_fig_dir=save_marked_fig_dir)
	img_fn = save_marked_fig_dir
	fig.tight_layout()
	fig.savefig(img_fn, dpi =  512/10)
	plt.close()
	return img_fn