def time_equalize(src_dir, dst_dir, img_width, img_height, new_time_depth):
    '''Make image stacks in src_dir have the same number of frames.  Place results in dst_dir'''
    do_copy = src_dir != dst_dir
    for f in os.listdir(src_dir):
        ext = os.path.splitext(f)[1].lower()

        # copy zip roi files without modification
        if ext == '.zip' and do_copy:
            shutil.copy(src_dir + f, dst_dir)

        # time equalize indvidual videos
        elif ext == '.tif' or ext == '.tiff':
            data = load_stack(src_dir + f)
            resized = zoom(data, (float(new_time_depth) / data.shape[0], 1, 1))
            tifffile.imsave(dst_dir + f, resized.squeeze())
def downsample_helper(files_list, img_width, img_height, mean_proj_bins,
                      max_proj_bins):
    '''Mean and max project to covert image files in list to single downsampled numpy array'''
    mean_stack = np.zeros((0, img_width, img_height), dtype=np.float32)
    full_stack = np.zeros((0, img_width, img_height), dtype=np.float32)

    # mean projections
    for f in files_list:
        full_stack = np.vstack((full_stack, load_stack(f)))
        for i in range(0, full_stack.shape[0], mean_proj_bins):
            if full_stack.shape[0] - (
                    i +
                    mean_proj_bins) < mean_proj_bins and f == files_list[-1]:
                m = np.mean(full_stack[i:, :, :], axis=0)
                m = np.expand_dims(m, axis=0)
                mean_stack = np.vstack((mean_stack, m))
                break
            elif full_stack.shape[0] - i < mean_proj_bins:
                full_stack = full_stack[i:, :, :]
            else:
                m = np.mean(full_stack[i:i + mean_proj_bins, :, :], axis=0)
                m = np.expand_dims(m, axis=0)
                mean_stack = np.vstack((mean_stack, m))

    # max projections
    max_stack = np.zeros((0, img_width, img_height), dtype=np.float32)
    for i in range(0, mean_stack.shape[0], max_proj_bins):
        if mean_stack.shape[0] - (i + max_proj_bins) < max_proj_bins:
            m = np.max(mean_stack[i:, :, :], axis=0)
            m = np.expand_dims(m, axis=0)
            max_stack = np.vstack((max_stack, m))
            break
        else:
            m = np.max(mean_stack[i:i + max_proj_bins, :, :], axis=0)
            m = np.expand_dims(m, axis=0)
            max_stack = np.vstack((max_stack, m))
    return max_stack
    def load_files(self):
        '''load a new image and corresponding ROIs'''
        current_files = self.files[self.files_keys[self.current_index]]
        self.filename_label.config(text=self.files_keys[self.current_index])
        self.image = load_stack(current_files[0])
        self.image_slider.config(to=self.image.shape[0] - 1)
        self.image_slider.set(0)
        self.image_index = 0

        self.convnet_rois = np.load(current_files[1])['rois']
        self.convnet_roi_probs = np.load(current_files[1])['roi_probabilities']
        self.indexed_roi_probs = sorted(
            [(v, i) for i, v in enumerate(self.convnet_roi_probs)],
            reverse=True)
        assert (self.convnet_rois.shape[0] == self.convnet_roi_probs.shape[0])
        self.roi_slider.config(from_=self.convnet_rois.shape[0])

        if self.manual_thresh.get().strip() == "":
            self.roi_slider.set(self.convnet_rois.shape[0])
            self.roi_index = self.convnet_rois.shape[0]
        else:
            new_index = self.convnet_rois.shape[0]
            for j, (p, ind) in enumerate(self.indexed_roi_probs):
                if p < float(self.manual_thresh.get()):
                    new_index = j
                    break
            self.roi_index = new_index
            self.just_set_thresh = True
            self.roi_slider.set(new_index)

        if current_files[2] is not None:
            self.gt_rois = load_rois(current_files[2], self.img_width,
                                     self.img_height)
            self.gt_rois = self.gt_rois.max(axis=0)

        self.draw_canvas()