'nb_patch': 0,
                'ssub_B': 2,
                'init_iter': 2,
                'ring_size_factor': 1.4,
                'method_init': 'corr_pnr',
                'method_deconvolution': 'oasis',
                'update_background_components': True,
                'center_psf': True,
                'border_pix': 0,
                'normalize_init': False,
                'del_duplicates': True,
                'only_init': True
            }
            mouse_row_new = main_source_extraction(
                mouse_row,
                parameters_source_extraction,
                dview,
                session_wise=True)
            states_df = db.append_to_or_merge_with_states_df(
                states_df, mouse_row_new)
            db.save_analysis_states_database(
                states_df,
                path=analysis_states_database_path,
                backup_path=backup_path)

selected_rows = db.select(states_df,
                          'source_extraction',
                          mouse=mouse_number,
                          session=session,
                          is_rest=is_rest,
                          decoding_v=decoding_version,
        selected_rows = db.select(
            states_df,
            'source_extraction',
            mouse=mouse_number,
            session=session,
            is_rest=is_rest,
            decoding_v=decoding_version,
            cropping_v=cropping_version,
            motion_correction_v=motion_correction_version,
            alignment_v=alignment_version,
            source_extraction_v=0)

        ### all aligned videos are save in one file
        aligned_row = selected_rows.iloc[0]
        mouse_row_new = main_source_extraction(aligned_row,
                                               parameters_source_extraction,
                                               dview)
        states_df = db.append_to_or_merge_with_states_df(
            states_df, mouse_row_new)
        db.save_analysis_states_database(states_df,
                                         path=analysis_states_database_path,
                                         backup_path=backup_path)
        source_extraction_version = mouse_row_new.name[8]

#%% run separately component evaluation

# evaluation
dview.terminate()
for session in [1, 2, 4]:
    print(session)
    # Run decoding for group of data tha have the same cropping parameters (same mouse)
    'nb': 0,
    'nb_patch': 0,
    'ssub_B': 2,
    'init_iter': 2,
    'ring_size_factor': 1.4,
    'method_init': 'corr_pnr',
    'method_deconvolution': 'oasis',
    'update_background_components': True,
    'center_psf': True,
    'border_pix': 0,
    'normalize_init': False,
    'del_duplicates': True,
    'only_init': True
}

main_source_extraction(index, row, parameters_source_extraction, dview)

#%%
## plot countours

source_extraction_output = eval(row.loc['source_extraction_output'])
corr_path, pnr_path = source_extraction_output['meta']['corr'][
    'main'], source_extraction_output['meta']['pnr']['main']
source_extraction_parameters = db.get_parameters('source_extraction',
                                                 index[0],
                                                 index[1],
                                                 index[2],
                                                 index[3],
                                                 download_=False)
cn_filter = np.load(db.get_file(corr_path))
pnr = np.load(db.get_file(pnr_path))