Ejemplo n.º 1
0
def extract(fpath):

    fdir = os.path.split(fpath)[0]
    fname = os.path.splitext(os.path.split(fpath)[1])[0]

    sima_mc_path = os.path.join(fdir, fname + '_mc.sima')

    if not os.path.exists(sima_mc_path):
        raise Exception(
            'Data not motion corrected yet; can\'t extract ROI data')

    rois = ROIList.load(
        os.path.join(fdir, fname + '_RoiSet.zip'),
        fmt='ImageJ')  # load ROIs as sima polygon objects (list)
    dataset = sima.ImagingDataset.load(os.path.join(
        fdir, fname + '_mc.sima'))  # reload motion-corrected dataset
    dataset.add_ROIs(rois, 'from_ImageJ')
    print('Extracting roi signals from %s' % fdir)
    signals = dataset.extract(rois)
    extracted_signals = np.asarray(
        signals['raw'])  # turn signals list into an np array
    np.save(os.path.join(fdir, fname + '_extractedsignals.npy'),
            extracted_signals)

    print('Done with extracting roi signals')
Ejemplo n.º 2
0
def getRoi():
    ds_path = request.form.get('path')
    label = request.form.get('label')
    roi_id = request.form.get('id')

    dataset = ImagingDataset.load(ds_path)
    convertedRois = {}
    try:
        rois = ROIList.load(os.path.join(dataset.savedir, 'rois.pkl'),
                            label=label)
    except:
        return jsonify({})

    for i, roi in enumerate(rois):
        if roi.id == roi_id:
            break

    roi_points = []
    try:
        for i in xrange(roi.im_shape[0]):
            roi_points.append([])
    except:
        for i in xrange(np.max(np.array(roi.coords)[:, :, 2])):
            roi_points.append([])
    for poly in roi.polygons:
        coords = np.array(poly.exterior.coords)
        if np.all(coords[-1] == coords[0]):
            coords = coords[:-1]
        plane = int(coords[0, -1])
        coords = coords[:, :2].astype(int).tolist()
        roi_points[plane].append(coords)

    return jsonify({roi.id: {'label': roi.label, 'points': roi_points}})
    def test_extract_roi(self):
        # make sure number of loaded ROIs (from zip and extracted signals) are correct
        rois = ROIList.load(
            os.path.join(self.fdir, self.fname + '_RoiSet.zip'),
            fmt='ImageJ')  # load ROIs as sima polygon objects (list)

        assert self.sig.shape[1] == 11  # CZ manually drew 11 ROIs
        assert len(rois) == 11
Ejemplo n.º 4
0
def extract_rois((full_path_to_file, individualframename)):
    # Create SIMA Sequence & ImagingDataset objects from image file(s) or
    # motion correction if action == 'extract', assume that motion
    # correction was done on EC2 previously
    if action == 'both':
        try:
            motion_correction((full_path_to_file, individualframename))
        except Exception as e:
            print('Motion correction failed')
            print e
            logging.Exception('Motion correction failed')

    filename = os.path.splitext(os.path.basename(full_path_to_file))[0]
    dataset = sima.ImagingDataset.load(filename + '_mc.sima')

    # Obtain ROIs
    if to_segment:
        logging.info("Segmenting images for %s..." % filename)

        # Automated segmentation
        # Define segmentation method and post processing.
        segment_approach = sima.segment.PlaneNormalizedCuts()
        segment_approach.append(sima.segment.SparseROIsFromMasks())
        segment_approach.append(sima.segment.SmoothROIBoundaries())
        segment_approach.append(sima.segment.MergeOverlapping(threshold=0.5))

        # Apply segmentation to dataset
        rois = dataset.segment(segment_approach)

        logging.info("Done segmenting images for %s" % filename)

        print("Done segmenting images for %s" % filename)
    else:
        logging.info("Importing ROIs from ImageJ for %s..." % filename)
        print("Importing ROIs from ImageJ for %s..." % filename)

        # Load ROIs from ImageJ
        rois = ROIList.load(filename + '_mc_' + roi_filename, fmt='ImageJ')
        dataset.add_ROIs(rois, 'from_ImageJ')

        logging.info("Done importing ROIs for %s" % filename)
        print("Done importing ROIs for %s" % filename)

    # Extract signals from ROIs into numpy file
    signals = dataset.extract(rois)
    extracted_signals = np.asarray(signals['raw'])
    np.save(filename + '_extractedsignals', extracted_signals)

    logging.info("Done extracting signals")
    print("Done extracting signals")
Ejemplo n.º 5
0
def extract_rois((full_path_to_file, individualframename)):
    # Create SIMA Sequence & ImagingDataset objects from image file(s) or
    # motion correction if action == 'extract', assume that motion
    # correction was done on EC2 previously
    if action == 'both':
        try:
            motion_correction((full_path_to_file, individualframename))
        except Exception as e:
            print('Motion correction failed')
            print e
            logging.Exception('Motion correction failed')

    filename = os.path.splitext(os.path.basename(full_path_to_file))[0]
    dataset = sima.ImagingDataset.load(filename + '_mc.sima')

    # Obtain ROIs
    if to_segment:
        logging.info("Segmenting images for %s..." % filename)

        # Automated segmentation
        # Define segmentation method and post processing.
        segment_approach = sima.segment.PlaneNormalizedCuts()
        segment_approach.append(sima.segment.SparseROIsFromMasks())
        segment_approach.append(sima.segment.SmoothROIBoundaries())
        segment_approach.append(sima.segment.MergeOverlapping(threshold=0.5))

        # Apply segmentation to dataset
        rois = dataset.segment(segment_approach)

        logging.info("Done segmenting images for %s" % filename)

        print("Done segmenting images for %s" % filename)
    else:
        logging.info("Importing ROIs from ImageJ for %s..." % filename)
        print("Importing ROIs from ImageJ for %s..." % filename)

        # Load ROIs from ImageJ
        rois = ROIList.load(filename + '_mc_' + roi_filename, fmt='ImageJ')
        dataset.add_ROIs(rois, 'from_ImageJ')

        logging.info("Done importing ROIs for %s" % filename)
        print("Done importing ROIs for %s" % filename)

    # Extract signals from ROIs into numpy file
    signals = dataset.extract(rois)
    extracted_signals = np.asarray(signals['raw'])
    np.save(filename + '_extractedsignals', extracted_signals)

    logging.info("Done extracting signals")
    print("Done extracting signals")
Ejemplo n.º 6
0
def extract_dff(directory, plot=False, load_signal=False):
	hd, tl = os.path.split(directory)
	if load_signal:
		dff = np.load(os.path.join(hd, 'DFF'))		
	else:
		dataset = sima.ImagingDataset.load(directory)
		rois = ROIList.load(os.path.join(hd, 'manual', 'RoiSet.zip'), fmt='ImageJ')
		dataset.add_ROIs(rois, 'from_ImageJ')  # this immediately saves the ROIs
		signals = dataset.extract(rois, signal_channel='0', label='0') 
		signals = dataset.signals(channel='0')['0']

		# signals are in signals['raw']
		# first list index is for each cycle
		# second index is for each roi within a cycle (third is signal at time t)
		## convert each signal into a df/f trace
		k = 30
		baseline_ix = np.array([[0, 90], [240, 320]])
		traces = np.stack(signals['raw'], axis=1)
		dff = np.zeros_like(traces)
		nroi, ncycles, t = traces.shape

		# normalize the signal
		for i, roi in enumerate(traces):
			for j, cycle in enumerate(roi):
				cur_mins = []
				for b in baseline_ix:
					ITI = cycle[b[0]:b[1]]
					sroi = movmean(ITI, 30)
					cur_mins.append(np.min(sroi))
				bsl = np.min(cur_mins)
				roi_df = (cycle - bsl) / bsl
				dff[i,j,:] = roi_df  # rois x cycles x time

				# smooth the dff
				# sroi_df = sp.signal.savgol_filter(roi_df, 29, 3)
				# sroi_df = sp.ndimage.filters.gaussian_filter1d(roi_df,3)
				# dff[i,j,:] = sroi_df  # rois x cycles x time

		np.save(os.path.join(hd, 'manual', 'DFF'), dff)

	# check by plotting
	if plot:
		plt.subplots()
		plt.plot(traces[0,0,:], label='raw signal')
		plt.plot(dff[0,0,:], label='dF/F')
		plt.legend()
		plt.show()
Ejemplo n.º 7
0
def selectRoi():
    ds_path = request.form.get('path')
    label = request.form.get('label')
    plane = float(request.form.get('z'))

    point = Point(float(request.form.get('x')), float(request.form.get('y')))

    dataset = ImagingDataset.load(ds_path)
    rois = ROIList.load(os.path.join(dataset.savedir, 'rois.pkl'), label=label)

    for roi in rois:
        for poly in roi.polygons:
            z_coord = np.array(poly.exterior.coords)[0, 2]
            if z_coord == plane or plane == -1:
                if poly.contains(point):
                    return jsonify(label=roi.label, id=roi.id)

    return jsonify({'error': 'roi not found'})
Ejemplo n.º 8
0
def getRois():
    ds_path = request.form.get("path")
    label = request.form.get("label")

    dataset = ImagingDataset.load(ds_path)
    convertedRois = {}
    rois = ROIList.load(os.path.join(dataset.savedir, "rois.pkl"), label=label)

    for i, roi in enumerate(rois):
        if roi.label is None:
            roi.label = i
        convertedRois[roi.label] = {}
        for poly in roi.polygons:
            coords = np.array(poly.exterior.coords)
            plane = int(coords[0, -1])
            # coords = list(coords[:,:2].ravel())
            coords = coords[:, :2].tolist()
            try:
                convertedRois[roi.label][plane].append(coords)
            except KeyError:
                convertedRois[roi.label][plane] = [coords]

    return jsonify(**convertedRois)
Ejemplo n.º 9
0
def getRois():
    ds_path = request.form.get('path')
    label = request.form.get('label')

    dataset = ImagingDataset.load(ds_path)
    convertedRois = {}
    rois = ROIList.load(os.path.join(dataset.savedir, 'rois.pkl'), label=label)

    for i, roi in enumerate(rois):
        if roi.label is None:
            roi.label = i
        convertedRois[roi.label] = {}
        for poly in roi.polygons:
            coords = np.array(poly.exterior.coords)
            plane = int(coords[0, -1])
            #coords = list(coords[:,:2].ravel())
            coords = coords[:, :2].tolist()
            try:
                convertedRois[roi.label][plane].append(coords)
            except KeyError:
                convertedRois[roi.label][plane] = [coords]

    return jsonify(**convertedRois)
Ejemplo n.º 10
0
def ROILoadFromDir(_path, _dir):
    fullRAP_roi_list = []
    for i in range(0, len(_dir)):
        fullRAP_roi_list.append(ROIList.load(_path + _dir[i], fmt='ImageJ'))
    return fullRAP_roi_list