## Open thw data base with all data
states_df = db.open_analysis_states_database()

##Select one specific row of the data base specified by mouse, session, trial, is_rest, decoding_v, cropping_v, etc.
## If one experimental parameters is not specifies, it chooses al previos one with same id
## If one analysis version is not specified it selects the latest one
selected_row = db.select('decoding', 56165, 1, 1)

##Select one row from selected_rows
row = selected_row.iloc[0]
##This gives an array with the experimental details and analysis version.
index = row.name

#%%
##run decoding on data specified by index (one row in the data base)
main_decoding(index, row)

#%%
## Define parameters from cropping the movie.
## paramenters is given to the funcion or can be retreived from the data base (I think)

parameters_cropping = {
    'crop_spatial': True,
    'cropping_points_spatial': [80, 450, 210, 680],
    'crop_temporal': False,
    'cropping_points_temporal': []
}

## Call main cropping funcion
main_cropping(index, row, parameters_cropping)
#%% Paths
analysis_states_database_path = 'references/analysis/analysis_states_database.xlsx'
backup_path = 'references/analysis/backup/'
#parameters_path = 'references/analysis/parameters_database.xlsx'

## Open thw data base with all data
states_df = db.open_analysis_states_database()


#%% DECODING
# Select all the data corresponding to a particular mouse. Ex: 56165.

selected_rows = db.select(states_df,'decoding',56165)
mouse_row = selected_rows.iloc[0]
mouse_row = main_decoding(mouse_row)
states_df = db.append_to_or_merge_with_states_df(states_df, mouse_row)
db.save_analysis_states_database(states_df, analysis_states_database_path, backup_path)

#%% CROPPING
# Select the rows for cropping
selected_rows = db.select(states_df,'cropping',56165)

mouse_row = selected_rows.iloc[0]
plot_movie_frame(mouse_row)
#%%
parameters_cropping = cropping_interval() #check whether it is better to do it like this or to use the functions get
# and set parameters from the data_base_manipulation file
mouse_row = main_cropping(mouse_row, parameters_cropping)
plot_movie_frame_cropped(mouse_row) # verify that the cropping is the desired one
# Now cropping parameters had been selected. Next step is selection version analysis.
r_values_min = 0.85  # threshold on space consistency (if you lower more components
#                        will be accepted, potentially with worst quality)
parameters_component_evaluation = {
    'min_SNR': min_SNR,
    'rval_thr': r_values_min,
    'use_cnn': False
}

#%%
for i in range(0, len(selected_rows)):

    # Get the row from the selected rows as a series using simple indexing
    row = selected_rows.iloc[i]
    index = row.name
    # Get the index from the row
    row = main_decoding(row)
    print('Decoding for mouse' + str(index[0]) + 'session' + str(index[1]) +
          'trial' + str(index[2]))
    states_df = db.append_to_or_merge_with_states_df(states_df, row)
    db.save_analysis_states_database(states_df, analysis_states_database_path,
                                     backup_path)

    row = main_cropping(row, parameters_cropping)
    #upload_to_server_cropped_movie(index,row)
    print('Cropping for mouse' + str(index[0]) + 'session' + str(index[1]) +
          'trial' + str(index[2]))
    n_processes = psutil.cpu_count()
    cm.cluster.stop_server()
    # Start a new cluster
    c, dview, n_processes = cm.cluster.setup_cluster(
        backend='local',