def __init__(self, batch_path, UUID, viewer_ref): vi = ViewerUtils(viewer_ref) if not vi.discard_workEnv(): return vi.viewer.status_bar_label.showMessage('Please wait, loading image data...') pik_path = os.path.join(batch_path, f'{UUID}_input.pik') tiff_path = os.path.join(batch_path, f'{UUID}_input.tiff') vi.viewer.workEnv = ViewerWorkEnv.from_pickle(pik_path, tiff_path) vi.update_workEnv() vi.viewer.status_bar_label.showMessage('Finished loading image data...') input_params_path = os.path.join(batch_path, f'{UUID}.params') input_params = pickle.load(open(input_params_path, 'rb')) if input_params['is_3d']: roi_manager_gui = vi.viewer.parent().get_module('roi_manager') manager = roi_manager_gui.start_backend('VolMultiCNMFROI') cnmf_data_dicts = \ [ load_dict_from_hdf5( os.path.join(batch_path, f'{UUID}_results_z{z}.hdf5') ) # for z in range(5) for z in range(vi.viewer.workEnv.imgdata.z_max + 1) ] manager.add_all_components( cnmf_data_dicts, input_params ) else: cnmf_data = load_dict_from_hdf5( os.path.join(batch_path, f'{UUID}_results.hdf5') ) roi_manager_gui = vi.viewer.parent().get_module('roi_manager') roi_manager_gui.start_backend('CNMFROI') roi_manager_gui.manager.add_all_components( cnmf_data, input_params_dict=input_params, ) name = input_params['item_name'] vi.viewer.ui.label_curr_img_seq_name.setText(f'CNMF: {name}') vi.viewer.workEnv.history_trace.append({'cnmf': input_params}) vi.enable_ui(True)
def import_cnmfe_into_viewer(self): vi = ViewerUtils(self.viewer_ref) if not vi.discard_workEnv(): return cnmf_data = load_dict_from_hdf5( os.path.join(self.batch_dir, f'{self.UUID}_results.hdf5')) vi.viewer.status_bar_label.showMessage( 'Loading CNMFE data, please wait...') pickle_file_path = os.path.join(self.batch_dir, f'{self.UUID}_input.pik') tiff_path = os.path.join(self.batch_dir, f'{self.UUID}_input.tiff') vi.viewer.workEnv = ViewerWorkEnv.from_pickle(pickle_file_path, tiff_path) vi.update_workEnv() input_params = pickle.load( open(os.path.join(self.batch_dir, f'{self.UUID}.params'), 'rb')) vi.viewer.workEnv.history_trace.append({'cnmfe': input_params}) roi_manager_gui = vi.viewer.parent().get_module('roi_manager') roi_manager_gui.start_backend('CNMFROI') roi_manager_gui.manager.add_all_components( cnmf_data, input_params_dict=input_params, calc_raw_min_max=self.checkbox_calc_raw_min_max.isChecked()) vi.viewer.ui.label_curr_img_seq_name.setText('CNMFE of: ' + input_params['item_name']) self.close()
def run(batch_dir: str, UUID: str): logging.basicConfig( stream=sys.stdout, level=logging.DEBUG, format= "%(relativeCreated)12d [%(filename)s:%(funcName)20s():%(lineno)s] [%(process)d] %(message)s" ) start_time = time() output = {'status': 0, 'output_info': ''} n_processes = os.environ['_MESMERIZE_N_THREADS'] n_processes = int(n_processes) file_path = os.path.join(batch_dir, UUID) filename = [file_path + '_input.tiff'] input_params = pickle.load(open(file_path + '.params', 'rb')) # If Ain is specified if input_params['do_cnmfe']: Ain = None item_uuid = input_params['cnmfe_kwargs'].pop('Ain') if item_uuid: print('>> Ain specified, looking for cnm-A file <<') parent_batch_dir = os.environ['CURR_BATCH_DIR'] item_out_file = os.path.join(parent_batch_dir, f'{item_uuid}.out') t0 = time() timeout = 60 while not os.path.isfile(item_out_file): print('>>> cnm-A not found, waiting for 15 seconds <<<') sleep(15) if time() - t0 > timeout: output.update({ 'status': 0, 'output_info': 'Timeout exceeding in waiting for Ain input file' }) raise TimeoutError( 'Timeout exceeding in waiting for Ain input file') if os.path.isfile(item_out_file): if json.load(open(item_out_file, 'r'))['status']: Ain_path = os.path.join(parent_batch_dir, item_uuid + '_results.hdf5') Ain = load_dict_from_hdf5(Ain_path)['estimates']['A'] print('>>> Found Ain file <<<') else: raise FileNotFoundError( '>>> Could not find specified Ain file <<<') print('*********** Creating Process Pool ***********') c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=n_processes, single_thread=False #ignore_preexisting=True ) try: print('Creating memmap') # memmap_path = cm.save_memmap_each( # filename, # base_name='memmap-' + UUID, # order='C', # border_to_0=input_params['border_pix'], # dview=dview) # memmap_path = cm.save_memmap_join(memmap_path, base_name='memmap-' + UUID, dview=dview) # # load memory mappable file # Yr, dims, T = cm.load_memmap(memmap_path) # Y = Yr.T.reshape((T,) + dims, order='F') memmap_path = cm.save_memmap( filename, base_name=f'memmap-', order='C', dview=dview, border_to_0=input_params['border_pix'], ) Yr, dims, T = cm.load_memmap(memmap_path) Y = np.reshape(Yr.T, [T] + list(dims), order='F') if input_params['do_cnmfe']: gSig = input_params['cnmfe_kwargs']['gSig'][0] else: gSig = input_params['corr_pnr_kwargs']['gSig'] cn_filter, pnr = cm.summary_images.correlation_pnr(Y, swap_dim=False, gSig=gSig) if not input_params['do_cnmfe'] and input_params['do_corr_pnr']: pickle.dump(cn_filter, open(UUID + '_cn_filter.pikl', 'wb'), protocol=4) pickle.dump(pnr, open(UUID + '_pnr.pikl', 'wb'), protocol=4) output_file_list = \ [ UUID + '_pnr.pikl', UUID + '_cn_filter.pikl', ] output.update({ 'output': UUID, 'status': 1, 'output_info': 'inspect correlation & pnr', 'output_files': output_file_list }) dview.terminate() for mf in glob(batch_dir + '/memmap-*'): os.remove(mf) end_time = time() processing_time = (end_time - start_time) / 60 output.update({'processing_time': processing_time}) json.dump(output, open(file_path + '.out', 'w')) return cnm = cnmf.CNMF( n_processes=n_processes, method_init='corr_pnr', dview=dview, Ain=Ain, only_init_patch=True, # just leave it as is normalize_init=False, center_psf=True, **input_params['cnmfe_kwargs'], ) cnm.fit(Y) # DISCARD LOW QUALITY COMPONENTS cnm.params.set('quality', { 'use_cnn': False, **input_params['eval_kwargs'] }) cnm.estimates.evaluate_components(Y, cnm.params, dview=dview) out_filename = f'{UUID}_results.hdf5' cnm.save(out_filename) pickle.dump(pnr, open(UUID + '_pnr.pikl', 'wb'), protocol=4) pickle.dump(cn_filter, open(UUID + '_cn_filter.pikl', 'wb'), protocol=4) output.update({ 'output': filename[:-5], 'status': 1, 'output_files': [ out_filename, UUID + '_pnr.pikl', UUID + '_cn_filter.pikl', ] }) except Exception as e: output.update({ 'status': 0, 'Y.shape': Y.shape, 'output_info': traceback.format_exc() }) dview.terminate() for mf in glob(batch_dir + '/memmap-*'): try: os.remove(mf) except: # Windows doesn't like removing the memmaps pass end_time = time() processing_time = (end_time - start_time) / 60 output.update({'processing_time': processing_time}) json.dump(output, open(file_path + '.out', 'w'))