Example #1
0
 def change_anime(self):
     """ Load an anime. """
     if args.check is not None:
         # load manga
         self.change_manga()
         path = anime_path
         self.anime = pims.open(path)
         # exclude certain ranges of indexes
         exclude = set(x for r in anime_config["exclude"]
                       for x in range(r[0], r[1] + 1))
         indexes = sorted(set(range(len(self.anime))) - set(exclude))
         self.anime = self.anime[indexes]
         self.ratio = (len(self.manga) - 1) / (len(self.anime) - 1)
         print(f"frames per page: {1/self.ratio:.3f}")
         # tags
         if os.path.exists(anime_folder + f"/{anime_name}.json"):
             print("loaded tags from file")
             with open(anime_folder + f"/{anime_name}.json") as f:
                 self.tags = json.load(f)
         else:
             self.tags = [len(self.anime)] * len(self.manga)
     else:
         print(f"Select an anime file")
         path = config["anime_path"] if "anime_path" in config else \
             fd.askopenfilename(initialdir=DATA)
         print(f"The file you selected was: {path}")
         self.anime = pims.open(path)
     self.anime_path = path
     self.anime_btn["text"] = f"anime_path: {path}"
     self.anime_i = 0
     set_image(self.anime_img, Image.fromarray(self.anime[0]))
     set_frame(self.anime_box, 0)
     self.anime_frame["text"] = f"/{len(self.anime)}"
Example #2
0
 def test_open_pngs(self):
     self.filepath = os.path.join(path, 'image_sequence')
     self.filenames = ['T76S3F00001.png', 'T76S3F00002.png',
                       'T76S3F00003.png', 'T76S3F00004.png',
                       'T76S3F00005.png']
     shape = (10, 11)
     save_dummy_png(self.filepath, self.filenames, shape)
     pims.open(os.path.join(path, 'image_sequence', '*.png'))
     clean_dummy_png(self.filepath, self.filenames)
Example #3
0
 def test_open_pngs(self):
     self.filepath = os.path.join(path, 'image_sequence')
     self.filenames = ['T76S3F00001.png', 'T76S3F00002.png',
                       'T76S3F00003.png', 'T76S3F00004.png',
                       'T76S3F00005.png']
     shape = (10, 11)
     save_dummy_png(self.filepath, self.filenames, shape)
     pims.open(os.path.join(path, 'image_sequence', '*.png'))
     clean_dummy_png(self.filepath, self.filenames)
Example #4
0
 def _pims_open_no_warn(self, f):
     pth = self.data_dir / f
     if pth.suffix.lower() == ".spe":
         # Disable warnings about file size being wrong which is caused
         # by SDT-control not dumping the whole file
         with warnings.catch_warnings():
             warnings.simplefilter("ignore", UserWarning)
             return pims.open(str(pth))
     return pims.open(str(pth))
Example #5
0
    def filter_and_track(self):

        print("######################################")
        print("Filter and Linking")
        print("######################################")

        if osp.exists(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-regi.tif'):
            frames = pims.open(self.config.OUTPUT_PATH +
                               self.config.ROOT_NAME + '-regi.tif')
        else:
            frames = pims.open(self.config.OUTPUT_PATH +
                               self.config.ROOT_NAME + '-raw.tif')

        psf_df = pd.read_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                             '-fittData.csv')

        blobs_df, im = track_blobs(psf_df,
                                   search_range=self.config.SEARCH_RANGE,
                                   memory=self.config.MEMORY,
                                   pixel_size=self.config.PIXEL_SIZE,
                                   frame_rate=self.config.FRAME_RATE,
                                   divide_num=self.config.DIVIDE_NUM,
                                   filters=None,
                                   do_filter=False)

        traj_num_before = blobs_df['particle'].nunique()

        if self.config.DO_FILTER:
            blobs_df, im = track_blobs(blobs_df,
                                       search_range=self.config.SEARCH_RANGE,
                                       memory=self.config.MEMORY,
                                       pixel_size=self.config.PIXEL_SIZE,
                                       frame_rate=self.config.FRAME_RATE,
                                       divide_num=self.config.DIVIDE_NUM,
                                       filters=self.config.FILTERS,
                                       do_filter=True)

        blobs_df.to_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                        '-physData.csv')

        plot_msd_batch(blobs_df,
                       image=frames[0],
                       output_path=self.config.OUTPUT_PATH,
                       root_name=self.config.ROOT_NAME,
                       pixel_size=self.config.PIXEL_SIZE,
                       frame_rate=self.config.FRAME_RATE,
                       divide_num=self.config.DIVIDE_NUM,
                       pltshow=True,
                       check_traj_msd=True)

        print("######################################")
        print("Trajectory number before filters: \t%d" % traj_num_before)
        print("Trajectory number after filters: \t%d" %
              blobs_df['particle'].nunique())
        print("######################################")
Example #6
0
def test_frame(movie_path, parameters_path, frame_no):
    if os.path.exists(movie_path) and os.path.exists(parameters_path):
        # if the path exists
        # check file extension and source from parameters
        # if it is tiff - use tifffile
        # else use pims
        if movie_path.endswith('nd2'):
            movie = pims.open(movie_path)
            if 'c' in movie.sizes:
                # it is a multichannel movie
                movie.bundle_axes = 'cyx'
        elif movie_path.endswith('tif') or movie_path.endswith('tiff'):
            pass

        if frame_no > 0 and frame_no < len(movie):
            frame = movie[frame_no]
            # parse the parameters
            parameters = parse_parameters(parameters_path)
            camera_pixel = parameters.camera_pixel

            pf = PeakFinder(parameters, notebook)
            results = pf.locate(frame)

            if np.any(results):
                from matplotlib import pyplot as plt

                plt.figure()
                plt.imshow(frame)
                for mol in results:
                    plt.plot(mol.y / camera_pixel, mol.x / camera_pixel, 'rx')
                plt.show()
        else:
            raise ValueError('Frame not in movie')
    else:
        raise ValueError('movie or parameters file not found')
Example #7
0
def example_with_trackpy_and_twv(filename):
    """
    Example usecase from input file to particle and trap positions in .dat file.
    """
    frames = pims.open(filename)
    # Open file with pims. Works with many file extensions.
    # This example assumes .twv file.

    # metadata = frames.get_all_metadata()
    # Optional access to additional metadata.

    times, laser_powers, traps = frames.get_all_tweezer_positions()
    # Obtain frame times, laser power at each frame time and
    # traps powers and positions at each frame.

    features = tp.batch(frames, 25, minmass=1000, invert=False)
    # Obtain features (particle positions) using trackpy's batch function.
    # It is verbose.
    # The 25 in arguments is diameter. It be odd number.
    # It is recommended to obtain parameters using GUI.

    tracks = tp.link_df(features, 15, memory=10)
    # Joins particles positions to tracks (connects them in time).
    # See trackpy documentation for parameters.

    save_tracked_data_pandas(filename[:-4] + '_out.dat', frames, tracks, times,
                             laser_powers, traps)
Example #8
0
 def process_nd2(self):
     """Loads the nd2 file into the class and obtains and displays the metadata from the file"""
     if self.parameters['filetype'] == 'tif':
         self.stack = pims.ImageSequenceND(self.parameters['filename'], axes_identifiers=['c','z'])
         self.parameters['channels'] = [f"channel {i}" for i in range(self.stack.sizes['c'])]
     else:
         self.stack = pims.open(self.parameters['filename'])
         self.stack.default_coords['t'] = 0
         self.parameters['channels'] = self.stack[0].metadata['channels']
         self.parameters['pixel_microns'] = self.stack[0].metadata['pixel_microns']
     if "v" in self.stack.sizes:
         self.stack.bundle_axes = 'vyx'
         self.has_multiple_series = True
     else:
         self.stack.bundle_axes = 'yx'
         self.has_multiple_series = False
     self.stack.iter_axes = 'z'
     tvMeta = ttk.Treeview(self.window)
     tvMeta['columns'] = ("metaval")
     tvMeta.column("#0", width=250)
     tvMeta.column("metaval", minwidth=250)
     tvMeta.heading("#0", text="Key", anchor=tk.W)
     tvMeta.heading("metaval", text="Value", anchor=tk.W)
     for metakey, metaval in self.stack[0].metadata.items():
         if not metaval:
             metaval = '' # replace attributes that can't be parsed with an empty string
         tvMeta.insert('', "end", text=metakey, values=(metaval))
     tvMeta.pack(side=tk.TOP, fill=tk.BOTH,expand=True)
     self.widgets['btnNext'] = tk.Button(self.window, text="Select channel >", command=self.open_channelselector)
     self.widgets['btnNext'].pack(side=tk.TOP)
Example #9
0
    def __init__(self, work_inp_path, val, ext):
        stack.val = val

        # Import stack
        im_path = work_inp_path + '_' + stack.val + '.' + ext
        self.im_stack = pims.open(im_path)
        stack.siz1, stack.siz2 = self.im_stack.frame_shape
Example #10
0
def find_intro(args):
    """ Finds the spot where the image occurs in the video. """
    folder = "/".join(args.path.split("/")[:-1])
    config = json.load(open(f"{folder}/config.json"))
    t, info = args.type == "intro", config[args.type]
    l, r = info["left"], info["right"]
    img = np.asarray(Image.open(f"{folder}/{info['image']}")).astype(np.float)
    for fname in glob.glob(args.path):
        name = fname.split("/")[-1].split(".")[0]
        video = pims.open(fname)
        best, besti = -float("inf"), 0
        for i, frame in enumerate(video[:DURATION] if t else video[-DURATION:]):
            frame = frame.astype(np.float)
            # dot product between two matrices, flattened to a vector
            k = np.sum(img*frame)/np.sqrt(np.sum(frame*frame))
            if k > best:
                best, besti = k, i
        besti += 0 if t else len(video) - DURATION
        Image.fromarray(video[besti]).show()
        print(f"cosine similarity: {best/np.sqrt(np.sum(img*img)):.3f}")
        ans = input(f"correct for {fname}? (y/n) ")
        if ans[0].lower() == "y":
            if name not in config:
                config[name] = {"exclude": []}
            config[name]["exclude"].append([besti - l, min(besti + r, len(video))])

    with open(f"{folder}/config.json", "w") as f:
        json.dump(config, f, indent=4, sort_keys=True)
Example #11
0
    def anim_fitData(self):

        print("######################################")
        print("Anim fitData")
        print("######################################")

        frames = pims.open(self.settings['Output path'] + self.root_name + \
          '.tif')
        blobs_df = pd.read_csv(self.settings['Output path'] + self.root_name + \
          '-fitData.csv')

        blobs_df = blobs_df[
            blobs_df['dist_err'] < self.settings['max_dist_err']]
        blobs_df = blobs_df[
            blobs_df['sigx_to_sigraw'] < self.settings['max_sig_to_sigraw']]
        blobs_df = blobs_df[
            blobs_df['sigy_to_sigraw'] < self.settings['max_sig_to_sigraw']]
        blobs_df = blobs_df[blobs_df['slope'] > self.settings['min_slope']]
        blobs_df = blobs_df[blobs_df['mass'] > self.settings['min_mass']]

        fit_plt_array = anim_blob(
            blobs_df,
            frames,
            pixel_size=self.settings['Pixel size'],
            blob_markersize=10,
        )
        imsave(
            self.settings['Output path'] + self.root_name + '-fittVideo.tif',
            fit_plt_array)
Example #12
0
def Detect(estimateFeatureSize, CameraName, minMass = None, dynamicMinMass = False):
    ImagePath = 'E:/BubbleRisingUltimate/4mmGasBubbleRising/4mmGasBubbleRising'
    Path = os.path.join(ImagePath, CameraName)
    frames = pims.open(Path)
    print('Valid frames length is %d' %len(frames))

    # find five brightest
    if not minMass:
        f = tp.locate(frames[testFrame], estimateFeatureSize)
        mass = list(f['mass']); mass.sort()
        minMass = int(mass[-3]*0.9 + mass[-1]*0.1)
        print(minMass)
    #TopTen = np.argsort(f['mass'])[-5:]
    #TopTenArray = f['mass'][TopTen]
    # show mass histogram
    # show subpixel accuracy of the detection 
    #minMass = list(TopTenArray)[0]
    f = tp.locate(frames[testFrame], estimateFeatureSize, minmass= minMass)
    plt.figure()
    tp.annotate(f, frames[testFrame]);
    # run batch processing for all frames
    if dynamicMinMass:
        f = f[0:0]
        for i in range(len(frames)):
            f_ele = tp.locate(frames[i], estimateFeatureSize)
            mass = list(f_ele['mass']); mass.sort()
            minMass = int(mass[-2]*0.9 + mass[-1]*0.1)
            f_ele = tp.locate(frames[i], estimateFeatureSize, minmass = minMass)
            f = f.append(f_ele)
    else:
        f = tp.batch(frames, estimateFeatureSize, minmass = minMass)
    return f, frames
Example #13
0
    def open_file(self, filename=None, reader_cls=None):
        """Open image file and update the viewer's reader"""
        if filename is None:
            try:
                cur_dir = os.path.dirname(self.reader.filename)
            except AttributeError:
                cur_dir = ''
            filename = QtWidgets.QFileDialog.getOpenFileName(directory=cur_dir)
            if isinstance(filename, tuple):
                # Handle discrepancy between PyQt4 and PySide APIs.
                filename = filename[0]
        if filename is None or len(filename) == 0:
            return

        if reader_cls is None:
            try:
                reader = pims.open(filename)
            except pims.api.UnknownFormatError:
                reader = None
        else:
            reader = reader_cls(filename)

        if self.reader is not None:
            self.close_reader()

        if reader is not None:
            self.update_reader(reader)
            self.status = 'Opened {}'.format(filename)
            self.filename = filename
        else:
            self.status = 'No suitable reader was found to open {}'.format(filename)
Example #14
0
    def plot_foci_dynamics2(self):
        frames = pims.open(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \
              '-raw.tif')
        fitt_df = pd.read_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                              '-fittData.csv')

        fitt_df = fitt_df[
            fitt_df['dist_err'] < self.config.DICT['Foci filt max_dist_err']]
        fitt_df = fitt_df[fitt_df['sigx_to_sigraw'] <
                          self.config.DICT['Foci filt max_sig_to_sigraw']]
        fitt_df = fitt_df[fitt_df['sigy_to_sigraw'] <
                          self.config.DICT['Foci filt max_sig_to_sigraw']]

        fitt_df = fitt_df[fitt_df['frame'] != 246]

        foci_dynamics_fig = plot_foci_dynamics(fitt_df)
        foci_dynamics_fig.savefig(self.config.OUTPUT_PATH + \
            self.config.ROOT_NAME + '-foci-dynamics2.pdf')

        fitt_plt_array = anim_blob(
            fitt_df,
            frames,
            pixel_size=self.config.PIXEL_SIZE,
            blob_markersize=5,
        )
        try:
            imsave(
                self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                '-fittVideo.tif', fitt_plt_array)
        except:
            pass
Example #15
0
    def check_detect_fit(self):

        print("######################################")
        print("Check detection and fitting")
        print("######################################")

        check_frame_ind = [0, 100]

        frames = pims.open(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \
               '-raw.tif')

        for ind in check_frame_ind:
            blobs_df, det_plt_array = detect_blobs(
                frames[ind],
                min_sig=self.config.MIN_SIGMA,
                max_sig=self.config.MAX_SIGMA,
                num_sig=self.config.NUM_SIGMA,
                blob_thres_rel=self.config.THRESHOLD,
                overlap=0.5,
                peak_thres_rel=self.config.PEAK_THRESH_REL,
                r_to_sigraw=1,
                pixel_size=self.config.PIXEL_SIZE,
                diagnostic=True,
                pltshow=True,
                plot_r=True,
                truth_df=None)
Example #16
0
    def fit(self):

        print("######################################")
        print("Fit")
        print("######################################")

        blobs_df = pd.read_csv(self.config.OUTPUT_PATH + \
           self.config.ROOT_NAME + '-detData.csv')
        frames_deno = pims.open(self.config.OUTPUT_PATH + \
           self.config.ROOT_NAME + '-deno.tif')

        psf_df, fit_plt_array = fit_psf_batch(
            frames_deno,
            blobs_df,
            diagnostic=False,
            pltshow=False,
            diag_max_dist_err=self.config.FILTERS['MAX_DIST_ERROR'],
            diag_max_sig_to_sigraw=self.config.FILTERS['SIG_TO_SIGRAW'],
            truth_df=None,
            segm_df=None)

        psf_df = psf_df.apply(pd.to_numeric)
        psf_df['slope'] = psf_df['A'] / (9 * np.pi * psf_df['sig_x'] *
                                         psf_df['sig_y'])
        psf_df.round(6).to_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \
            '-fittData.csv', index=False)

        foci_prop_hist_fig = plot_foci_prop_hist(psf_df)
        foci_prop_hist_fig.savefig(self.config.OUTPUT_PATH + \
            self.config.ROOT_NAME + '-foci-prop-hist.pdf')
Example #17
0
    def __init__(self,
                 images,
                 frames=slice(None),
                 pixels=(slice(None), slice(None))):
        """Instanciate the images."""
        if type(images) is str:
            if os.path.isfile(images) and os.path.splitext(images)[1] in [
                    '.tif', '.tiff'
            ]:
                self.base = imageio.imread(images)
                self.base.path = images
            else:
                self.base = pims.open(images)
        else:
            self.base = images

        # For now, this is how we'll deal with slicing:
        # Frames are stored as an array of the frame numbers. They can be directly sliced using advanced indexing.
        # x and y are just going to be applied to the loaded image every time an image is loaded.
        # In the end, all I'm doing is letting numpy deal with the slicing :)
        self.frame_index = np.arange(len(self.base))[frames]
        self.pixel_slice = pixels

        # Store for future pickling and unpickling in a base-less context.
        # These are commonly used and calculated from the base, so they are worth saving.
        self.path
        self.shape
        self.dtype
        self.itemsize
Example #18
0
    def check_foci_detection(self):

        print("######################################")
        print("Check foci detection")
        print("######################################")

        check_frame_ind = [0, 50, 100, 144]

        frames = pims.open(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \
              '-raw.tif')

        for ind in check_frame_ind:
            blobs_df, det_plt_array = detect_blobs(
                frames[ind],
                min_sig=self.config.MIN_SIGMA,
                max_sig=self.config.MAX_SIGMA,
                num_sig=self.config.NUM_SIGMA,
                blob_thres_rel=self.config.THRESHOLD,
                peak_min=self.config.PEAK_MIN,
                num_peaks=self.config.NUM_PEAKS,
                peak_thres_rel=self.config.PEAK_THRESH_REL,
                mass_thres_rel=self.config.MASS_THRESH_REL,
                peak_r_rel=self.config.PEAK_R_REL,
                mass_r_rel=self.config.MASS_R_REL,
                r_to_sigraw=1,
                pixel_size=self.config.PIXEL_SIZE,
                diagnostic=True,
                pltshow=True,
                blob_markersize=5,
                plot_r=False,
                truth_df=None)
Example #19
0
def analyse_movie(movie_path, parameters_path, notebook=False):
    if os.path.exists(movie_path) and os.path.exists(parameters_path):
        # if the path exists
        if notebook:
            pbar = tqdm.tqdm_notebook
        # check file extension and source from parameters
        # if it is tiff - use tifffile
        # else use pims
        if movie_path.endswith('nd2'):
            movie = pims.open(movie_path)
            if 'c' in movie.sizes:
                # it is a multichannel movie
                movie.bundle_axes = 'cyx'
        elif movie_path.endswith('tif') or movie_path.endswith('tiff'):
            pass

        # parse the parameters
        parameters = parse_parameters(parameters_path)

        # after loading movie run the analysis
        try:
            results = run(movie, parameters, notebook)
        except KeyboardInterrupt:
            print("Execution stopped")
    else:
        raise ValueError('movie or parameters file not found')
Example #20
0
    def check_cell_detection(self):
        print("######################################")
        print("Check cell detection")
        print("######################################")

        check_frame_ind = [0, 50, 100]

        frames = pims.open(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \
              '-celldeno.tif')

        for ind in check_frame_ind:
            blobs_df, det_plt_array = detect_blobs(
                frames[ind],
                min_sig=self.config.CELL_MIN_SIGMA,
                max_sig=self.config.CELL_MAX_SIGMA,
                num_sig=self.config.CELL_NUM_SIGMA,
                blob_thres_rel=self.config.CELL_THRESHOLD,
                peak_thres_rel=self.config.CELL_PEAK_THRESH_REL,
                overlap=0.5,
                r_to_sigraw=self.config.CELL_R_TO_SIGRAW,
                pixel_size=self.config.PIXEL_SIZE,
                diagnostic=True,
                pltshow=True,
                blob_markersize=10,
                plot_r=True,
                truth_df=None,
            )
Example #21
0
    def __read_raw_image(self):
        if self.input_path.suffix == '.nd2':
            images = ND2Reader(str(self.input_path.absolute()))
            self.shape[:2] = images.frame_shape
            self.shape[2] = images.sizes['z']
            self.positions[2] = np.array(images.metadata['z_coordinates'])
            self.positions[2] += - min(self.positions[2])
            for n in (0, 1):
                self.positions[n] = np.linspace(0, images.metadata['pixel_microns']*self.shape[n],
                                                num=self.shape[n], endpoint=False)
            if 'c' in images.default_coords:
                if type(self.channel) == int:
                    images.default_coords['c'] = self.channel
                else:
                    images.default_coords['c'] = images.metadata['channels'].index(self.channel)

            self.z_spacing = np.average(np.diff(self.positions[2]))
            self.xy_spacing = images.metadata['pixel_microns']
        else:
            images = pims.open(str(self.input_path.absolute()))
            self.shape[:2] = images.frame_shape
            self.shape[2] = len(images)
            for n in range(3):
                self.positions[n] = np.linspace(0, self.voxel_size[n]*self.shape[n],
                                                num=self.shape[n], endpoint=False)
            self.z_spacing = self.voxel_size[2]
            self.xy_spacing = self.voxel_size[0]
        # scale all images from 0 to 1 and store it at the selected resolution
        raw_image = np.zeros((self.shape[2], self.shape[0], self.shape[1]), dtype=self.dtype)
        raw_image[:] = (images - np.min(images))/(np.max(images)-np.min(images))
        images.close()
        return raw_image
Example #22
0
    def __init__(self, filePath):

        self.filePath = filePath

        video = pims.open(self.filePath)

        self.currentFrame_save = 0  # When saving, keeps track of progress
        self.currentFrame_play = 0  # When playing, keeps track of progress
        self.height = video.frame_shape[1]
        self.width = video.frame_shape[0]

        self.minHeight = 0
        self.maxHeight = self.height
        self.minWidth = 0
        self.maxWidth = self.width

        self.recordingSpeed = video.frame_rate
        try:
            self.frameCount = video.len()
            self.realRecordedTime = video.get_time(video.len() - 1)
            self.recordingDate = video.frame_time_stamps[0][0]
        except:
            # Support for more video formats is needed
            self.frameCount = 0
            self.realRecordedTime = 0
            self.recordingDate = 0
    def check_detect(self):

        print("######################################")
        print("Check detection")
        print("######################################")

        frames = pims.open(self.settings['Input path'] + self.root_name + \
               '.tif')

        check_frame_ind = [0, len(frames) - 1]

        for ind in check_frame_ind:
            blobs_df, det_plt_array = detect_blobs(
                frames[ind],
                min_sig=self.settings['Blob_min_sigma'],
                max_sig=self.settings['Blob_max_sigma'],
                num_sig=self.settings['Blob_num_sigma'],
                blob_thres_rel=self.settings['Blob_thres_rel'],
                overlap=0,
                peak_thres_rel=self.settings['Blob_pk_thresh_rel'],
                r_to_sigraw=1.4,
                show_scalebar=False,
                pixel_size=self.settings['Pixel size'],
                diagnostic=True,
                pltshow=True,
                plot_r=True,
                truth_df=None)
Example #24
0
    def sort_and_plot(self):

        print("######################################")
        print("Sort and PlotMSD")
        print("######################################")

        if osp.exists(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-regi.tif'):
            frames = pims.open(self.config.OUTPUT_PATH +
                               self.config.ROOT_NAME + '-regi.tif')
        else:
            frames = pims.open(self.config.OUTPUT_PATH +
                               self.config.ROOT_NAME + '-raw.tif')

        phys_df = pd.read_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                              '-physData.csv')

        if self.config.DO_SORT:
            phys_df = sort_phys(phys_df, self.config.SORTERS)
            phys_df.to_csv(self.config.OUTPUT_PATH + self.config.ROOT_NAME + \
               '-physData.csv', index=False)
        else:
            sorter_list = get_sorter_list(phys_df)
            phys_df = phys_df.drop(columns=sorter_list[1:-1])

        plot_msd_batch(phys_df,
                       image=frames[0],
                       output_path=self.config.OUTPUT_PATH,
                       root_name=self.config.ROOT_NAME,
                       pixel_size=self.config.PIXEL_SIZE,
                       frame_rate=self.config.FRAME_RATE,
                       divide_num=self.config.DIVIDE_NUM,
                       pltshow=True)

        self.config.save_config()
        if osp.exists(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-active.tif'):
            os.remove(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-active.tif')
        if osp.exists(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-boundaryMask.tif'):
            os.remove(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-boundaryMask.tif')
        if osp.exists(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-53bp1Mask.tif'):
            os.remove(self.config.OUTPUT_PATH + self.config.ROOT_NAME +
                      '-53bp1Mask.tif')
Example #25
0
def analyse_movie(movie_path, parameters_path, notebook=True):
    """
    Estimate the position of bright spots in each frame of ND2 movie.

    Parameters
    ----------
    movie_path : str
        Path to movie in ND2 format to be processed.
    parameters_path : str
        Path to file in yaml format holding the paramters for position
        estimation.
    notebook : bool
        True if the script is run from a Jupyter notebook.

    Raises
    ------
    FileNotFoundError
        If `movie_path` does not exist.
    FileNotFoundError
        If `parameters_path` does not exist.
    ValueError
        If the movie being analysed is not in ND2 format.
    """
    if not os.path.exists(movie_path):
        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
                                movie_path)

    if not os.path.exists(parameters_path):
        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
                                parameters_path)

    if movie_path.endswith('nd2'):
        movie = pims.open(movie_path)
        if 'c' in movie.sizes:
            # it is a multichannel movie
            movie.bundle_axes = 'cyx'
    else:
        raise ValueError('ND2 format movies only')

    # parse the parameters
    parameters = parse_parameters(parameters_path)
    start = parameters.start_frame
    stop = parameters.stop_frame
    samples = parameters.samples
    threshold_method = parameters.threshold_method
    threshold = parameters.threshold

    # run the analysis
    try:
        results = process(movie, start, stop, samples, threshold_method,
                          threshold, notebook)

        if results:
            # write them to file
            basename = os.path.splitext(movie_path)[0]
            results_path = basename + '.csv'
            save_positions(results_path, results)
    except KeyboardInterrupt:
        print("Execution stopped")
Example #26
0
    def __init__(self, roi, masking='outside', use_z=True, image=None):
        """
        Create an ROI object using an roi imported using ijroi classes
        Inputs:
        1. masking: determine whether inside or outside pixels will be masked
        2. z, t, f: use roi's stored z, t (time), or f (frame) coordinate for roi determination.
        By default, z is true and t and f are false
        
        image can be an image or a path to image
        """
        # Import statements for this module
        from read_roi import read_roi_file, read_roi_zip  # ImageJ ROI import
        from skimage.draw import polygon, ellipse  # enable drawing of polygon and elliptical ROIs
        import numpy as np
        import numpy.ma as ma
        import pims

        # Initialize instance variables
        self.name = roi['name']
        #self.str = "ROI"
        self.kind = roi['type']
        self.position = roi['position']  # int if single slice, dict if z or c
        self.mask_type = masking
        self.mask = None  # stores the mask
        self.masked_image = None  # stores the masked image
        self.attribs = roi  # bucket to hold all the variables that can be called under specialized circumstances
        # set attached image
        if type(image) == "str":
            print("image type str")  # debug line
            self.image = pims.open(image)  # open image
        else:
            self.image = image  # pin image

        # define custom positionals (z, t, c)
        # if only a single position is defined (single slice)
        self.z = None  # initialize as None
        self.c = None  # initialize as None
        self.t = None  # initialize as None
        if type(self.position) == 'dict':
            for item in self.position:
                if item == "slice":
                    self.z = self.position["slice"]
                elif item == "frame":
                    self.t = self.position["frame"]
                elif item == "channel":
                    self.c = self.position["channel"]

        # Define ROI-inclusive pixels using contained methods
        self.pixels = self.__setPixels()

        # Define masking operations (debugging)
        if self.mask_type == 'inside':
            print('inside mask requested')
        elif self.mask_type == 'outside':
            print("outside mask requested")
        else:
            print("No mask requested")

        return
Example #27
0
def imread(fname, nframes=1, *, arraytype="numpy"):
    """
    Read image data into a Dask Array.

    Provides a simple, fast mechanism to ingest image data into a
    Dask Array.

    Parameters
    ----------
    fname : str
        A glob like string that may match one or multiple filenames.
    nframes : int, optional
        Number of the frames to include in each chunk (default: 1).
    arraytype : str, optional
        Array type for dask chunks. Available options: "numpy", "cupy".

    Returns
    -------
    array : dask.array.Array
        A Dask Array representing the contents of all image files.
    """

    if not isinstance(nframes, numbers.Integral):
        raise ValueError("`nframes` must be an integer.")
    if (nframes != -1) and not (nframes > 0):
        raise ValueError("`nframes` must be greater than zero.")

    if arraytype == "numpy":
        arrayfunc = numpy.asanyarray
    elif arraytype == "cupy":  # pragma: no cover
        import cupy
        arrayfunc = cupy.asanyarray

    with pims.open(fname) as imgs:
        shape = (len(imgs), ) + imgs.frame_shape
        dtype = numpy.dtype(imgs.pixel_type)

    if nframes == -1:
        nframes = shape[0]

    if nframes > shape[0]:
        warnings.warn(
            "`nframes` larger than number of frames in file."
            " Will truncate to number of frames in file.", RuntimeWarning)
    elif shape[0] % nframes != 0:
        warnings.warn(
            "`nframes` does not nicely divide number of frames in file."
            " Last chunk will contain the remainder.", RuntimeWarning)

    a = dask.array.map_blocks(
        _map_read_frame,
        chunks=dask.array.core.normalize_chunks((nframes, ) + shape[1:],
                                                shape),
        fn=fname,
        arrayfunc=arrayfunc,
        meta=arrayfunc([]).astype(dtype),  # meta overwrites `dtype` argument
    )

    return a
Example #28
0
def test():
    frames = gray(pims.open('data/png/crack_tip/*.png'))
    print(frames)
    print(frames[0][100, :])
    plt.imshow(frames[0])
    f = tp.locate(frames[0], diameter=15, invert=False, minmass=1)
    f.head()
    tp.annotate(f, frames[0])
Example #29
0
 def background_from_dark_img(self, files_re):
     files = io.get_files(files_re, self.data_dir)[0]
     bg = []
     for f in files:
         with pims.open(str(self.data_dir / f)) as img:
             img_don = self.channel_roi(self.exc_img_filter(img, "d"))
             bg.append(img_don[0])
     self.background = np.mean(bg, axis=0)
def find_min_frame(data):
    min_frame = np.inf 
    for _, f_list in data['data'].items():
        for f_name in f_list:
            vid = pims.open(f_name)
            if(len(vid) < min_frame):
                min_frame = len(vid)
    return min_frame
Example #31
0
	def detect(self):

		print("######################################")
		print("Detect")
		print("######################################")

		frames = pims.open(self.settings['Output path'] + self.root_name + \
				'.tif')
		ind = self.settings['Frame number']

		# for ind in self.settings['Frame number']:
		frame = frames[ind]
		blobs_df, det_plt_array = detect_blobs(frame,
					min_sig=self.settings['Blob_min_sigma'],
					max_sig=self.settings['Blob_max_sigma'],
					num_sig=self.settings['Blob_num_sigma'],
					blob_thres_rel=self.settings['Blob_thres_rel'],
					overlap=0.5,

					peak_thres_rel=self.settings['Blob_pk_thresh_rel'],
					r_to_sigraw=1,
					pixel_size=self.settings['Pixel size'],

					diagnostic=False,
					pltshow=True,
					plot_r=True,
					truth_df=None)

		if self.settings['If_fit']:
			blobs_df, fit_plt_array = fit_psf(frame,
		            blobs_df,
		            diagnostic=False,
		            pltshow=True,
		            diag_max_dist_err=self.settings['max_dist_err'],
		            diag_max_sig_to_sigraw=self.settings['max_sig_to_sigraw'],
					diag_min_slope=self.settings['min_slope'],
					diag_min_mass=self.settings['min_mass'],
		            truth_df=None,
		            segm_df=blobs_df)

			blobs_df = blobs_df[ blobs_df['dist_err']<self.settings['max_dist_err'] ]
			blobs_df = blobs_df[ blobs_df['sigx_to_sigraw']<self.settings['max_sig_to_sigraw'] ]
			blobs_df = blobs_df[ blobs_df['sigy_to_sigraw']<self.settings['max_sig_to_sigraw'] ]
			blobs_df = blobs_df[ blobs_df['slope']>self.settings['min_slope'] ]
			blobs_df = blobs_df[ blobs_df['mass']>self.settings['min_mass'] ]
			blobs_df = blobs_df[ blobs_df['r']<=self.settings['Blob_max_sigma'] ]
			blobs_df['r'] = (blobs_df['sig_x'] + blobs_df['sig_y']) / 2

		fig, ax = plt.subplots(figsize=(6,6))
		ax.imshow(frame, cmap="gray", aspect='equal')
		anno_blob(ax, blobs_df, marker='^', markersize=10,
				plot_r=True, color=(0,0,1,0.8))
		plt.show()

		blobs_df = blobs_df.apply(pd.to_numeric)
		blobs_df.round(3).to_csv(self.settings['Output path'] + self.root_name + \
				'-detData-frame' + str(ind) + '.csv',
				index=False)
Example #32
0
 def test_rawvideo_export(self):
     """Exported frames must equal the input exactly"""
     self.export_func(self.sequence, self.tempfile, codec='rawvideo',
                      pixel_format='bgr24')
     with pims.open(self.tempfile) as reader:
         assert_equal(len(reader), self.expected_len)
         assert_equal(reader.frame_shape, self.expected_shape)
         for a, b in zip(self.sequence, reader):
             assert_array_equal(a, b)
Example #33
0
 def test_rgb_memorder(self):
     """Fortran memory order must be converted."""
     self.export_func(self.sequence.astype(self.sequence.dtype,
                                           order='F'),
                      self.tempfile, codec='rawvideo',
                      pixel_format='bgr24')
     with pims.open(self.tempfile) as reader:
         assert_equal(len(reader), self.expected_len)
         assert_equal(reader.frame_shape, self.expected_shape)
         for a, b in zip(self.sequence, reader):
             assert_array_equal(a, b)
Example #34
0
 def test_rgba_h264(self):
     """Remove alpha channel."""
     # Start with smoke test for H.264
     self.export_func(self.sequence_rgba, self.tempfile, codec='libx264')
     # Check that RGB channels are preserved
     self.export_func(self.sequence_rgba, self.tempfile, codec='rawvideo',
                      pixel_format='bgr24')
     sequence_rgba_stripped = self.sequence_rgba[:, :, :, :3]
     with pims.open(self.tempfile) as reader:
         assert_equal(len(reader), self.expected_len)
         assert_equal(reader.frame_shape, self.expected_shape)
         for a, b in zip(sequence_rgba_stripped, reader):
             assert_array_equal(a, b)
Example #35
0
 def test_open_tiff(self):
     pims.open(os.path.join(path, 'stuck.tif'))
Example #36
0
 def test_open_mov(self):
     _skip_if_no_PyAV()
     pims.open(os.path.join(path, 'bulk-water.mov'))
Example #37
0
 def __init__(self, file_path):
     self.file_path = file_path
     with pims.open(self.file_path) as tif_file:
         self.data = np.swapaxes(np.array(tif_file, dtype=np.float32), 1, 2)[:,:,::-1]
Example #38
0
def load(file_name,fr=None,start_time=0,meta_data=None,subindices=None):
    '''
    load movie from file. 
    
    Parameters
    -----------
    file_name: name of file. Possible extensions are tif, avi, npy, (npz and hdf5 are usable only if saved by calblitz)
    fr=None,start_time=0,meta_data=None, same as for calblitz.movie
    subindices=None: iterable indexes, for loading only portion of the movie
    '''  
    
    # case we load movie from file
    if os.path.exists(file_name):        
            
        name,extension = os.path.splitext(file_name)[:2]

        if extension == '.tif': # load avi file
            
            with pims.open(file_name) as f:
                if len(f.frame_shape)==3:
                    for ext_fr in f:
                        if subindices is None:
                            input_arr = np.array(ext_fr)    
                        else:
                            input_arr = np.array([ext_fr[j] for j in subindices])
                elif len(f.frame_shape)==2:                    
                        if subindices is None:
                            input_arr = np.array(f)    
                        else:
                            input_arr = np.array([f[j] for j in subindices])
                else:
                    raise Exception('The input file has an unknown numberof dimensions')
                    
            # necessary for the way pims work with tiffs      
#            input_arr = input_arr[:,::-1,:]

        elif extension == '.avi': # load avi file
            #raise Exception('Use sintax mov=cb.load(filename)')
            if subindices is not None:
                raise Exception('Subindices not implemented')
            cap = cv2.VideoCapture(file_name)
            length = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
            width  = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
            height = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
            
            input_arr=np.zeros((length, height,width),dtype=np.uint8)
            counter=0
            ret=True
            while True:
                # Capture frame-by-frame
                ret, frame = cap.read()
                if not ret:
                    break
                input_arr[counter]=frame[:,:,0]
                counter=counter+1
                if not counter%100:
                    print counter
            
            # When everything done, release the capture
            cap.release()
            cv2.destroyAllWindows()   
            
        elif extension == '.npy': # load npy file     
            if subindices is not None:
                input_arr=np.load(file_name)[subindices]     
            else:                   
                input_arr=np.load(file_name)
            
        elif extension == '.npz': # load movie from saved file                          
            if subindices is not None:
                raise Exception('Subindices not implemented')
            with np.load(file_name) as f:
                return movie(**f)  
            
        elif extension== '.hdf5':
            
            with h5py.File(file_name, "r") as f:     
                attrs=dict(f['mov'].attrs)
                attrs['meta_data']=cpk.loads(attrs['meta_data'])
                if subindices is None:
#                    fr=f['fr'],start_time=f['start_time'],file_name=f['file_name']
                    return movie(f['mov'],**attrs)   
                else:
                    return movie(f['mov'][subindices],**attrs)         

        else:
            raise Exception('Unknown file type')    
    else:
        raise Exception('File not found!')
        
    return movie(input_arr,fr=fr,start_time=start_time,file_name=file_name, meta_data=meta_data)
Example #39
0
def test_open_pngs():
    pims.open(os.path.join(path, 'image_sequence', '*.png'))
Example #40
0
def test_open_mov():
    _skip_if_no_ffmpeg()
    pims.open(os.path.join(path, 'bulk-water.mov'))
Example #41
0
 def test_open_png(self):
     self.filenames = ['dummy_png.png']
     shape = (10, 11)
     save_dummy_png(path, self.filenames, shape)
     pims.open(os.path.join(path, 'dummy_png.png'))
     clean_dummy_png(path, self.filenames)
Example #42
0
 def setUp(self):
     self.seq = pims.open(self.sample_filename, **self.options)
@author: epnevmatikakis
"""
#%%
import pims
import numpy as np
import scipy.io as sio
from greedyROI2d import greedyROI2d
from arpfit import arpfit
from sklearn.decomposition import ProjectedGradientNMF
from update_spatial_components import update_spatial_components
from update_temporal_components import update_temporal_components
from matplotlib import pyplot as plt
from time import time
from merge_rois import mergeROIS
#%%
Yp =(pims.open('demoMovie.tif')) 
Y=[]
for idx,y in enumerate(Yp):
   Y.append(np.rot90(y,k=-1))

Y=np.transpose(np.array(Y),(1,2,0))

#%%
#Ymat = sio.loadmat('Y.mat')
Y1 = Ymat['Y']*1.

#%%
d1,d2,T = np.shape(Y)
#%% greedy initialization
nr = 30
t1 = time()
Example #44
0
 def test_open_tiff(self):
     _skip_if_no_tifffile()
     pims.open(os.path.join(path, 'stuck.tif'))
Example #45
0
 def test_open(self):
     self.v.close()
     self.v = pims.open(self.filename)