-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine_analysed_data_sptPALM.py
More file actions
162 lines (122 loc) · 7.39 KB
/
combine_analysed_data_sptPALM.py
File metadata and controls
162 lines (122 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This work is licensed under the CC BY 4.0 License.
You are free to share and adapt this work, even for commercial purposes,
as long as you provide appropriate credit to the original creator.
Original Creator: Johannes Hohlbein (Wageningen University & Research)
Date of Creation: September, 2024
Full license details can be found at https://creativecommons.org/licenses/by/4.0/
"""
import pandas as pd
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from tkinter.simpledialog import askstring
import os
import pickle
from set_parameters_sptPALM import set_parameters_sptPALM
from set_parameters_sptPALM_GUI import set_parameters_sptPALM_GUI
from helper_functions import string_input_with_default
def combine_analysed_data_sptPALM(data=None, input_parameter=None):
print('\nRun combine_analysed_data_sptPALM.py')
"""
Following lines can be used for loading data bypassing the GUIs
"""
# # # TEMPORARY For bugfixing - Replace the following line with your file path if needed
# print(" TEMP! SPECIFIC FILE is being loaded: input_parameter.pkl!")
# filename = '/Users/hohlbein/Documents/WORK-DATA-local/Data_Finland/input_parameter.pkl'
# with open(filename, 'rb') as f:
# input_parameter = pickle.load(f)
# print(" TEMP! SPECIFIC FILE is being loaded: sptData_combined_movies.pkl!")
# # filename = '/Users/hohlbein/Documents/WORK-DATA-local/Cas12a-data-JH/output_python/sptData_combined_movies.pkl'
# filename = '/Users/hohlbein/Documents/WORK-DATA-local/Data_Finland/output_python/sptData_movies.pkl'
# with open(filename, 'rb') as f:
# data = pickle.load(f)
if not input_parameter:
input_parameter = set_parameters_sptPALM()
input_parameter = set_parameters_sptPALM_GUI(input_parameter)
filename = []
# Check whether DATA was passed to the function
if not data:
Tk().withdraw() # Close root window
starting_directory = os.path.join(input_parameter['data_dir'],
input_parameter['default_output_dir'])
filename = askopenfilename(initialdir = starting_directory,
filetypes = [("pickle file", "*.pkl")],
title = "Select *.pkl file from sptPALM_analyse_movies.py")
if filename:
with open(filename, 'rb') as f:
data = pickle.load(f)
else:
raise ValueError("No file selected!")
else:
print(' Careful, data taken from memory!')
comb_data = {}
comb_data['#_movies_loaded'] = len(data['movies'])
print(f" ... there were in total {comb_data['#_movies_loaded']} movie(s) found")
comb_data['#_conditions'] = len(data['input_parameter']['condition_names']) # number of conditions
comb_data['#_movies_per_condition'] = [len(movies) for movies in data['input_parameter']['condition_files']] # movies per condition
comb_data['condition_names'] = data['input_parameter']['condition_names']
comb_data['condition_files'] = data['input_parameter']['condition_files']
# Check whether any combining is possible
if comb_data['#_conditions'] == 0:
raise ValueError('Please set a number of combinable files in define_input_parameters.py (Option 1)')
# # Assign data to a combined table
tmp_max_track_id = 0
tmp_max_frame = 0
condi_table = {}
condi_table['cell_data'] = {}
condi_table['diff_data'] = {}
condi_table['anaDDA_tracks'] = {}
# condi_table['anaDDA_condis'] = []
condi_table['condis_#_cells'] = {}
# For each condition
for ff in range(comb_data['#_conditions']):
tmp_scta_table = data['movies'][ff]['scta_table'].iloc[0:0].copy()
tmp_dcoef_table = data['movies'][ff]['diff_coeffs_filtered_list'].iloc[0:0].copy()
tmp_tracks_table = data['movies'][ff]['tracks'].iloc[0:0].copy()
# breakpoint()
# Check whether data is loaded that doesn't exist
if max(comb_data['condition_files'][ff]) > comb_data['#_movies_loaded']:
raise ValueError('Please check: You are trying to load a non-existing movie!')
# For each movie per condition
for jj in range(comb_data['#_movies_per_condition'][ff]):
tmp_ParaNr = comb_data['condition_files'][ff][jj] # current movie ID
temp = data['movies'][tmp_ParaNr]['scta_table']
temp['movie']=jj
tmp_scta_table = pd.concat(
[tmp_scta_table, data['movies'][tmp_ParaNr]['scta_table']], ignore_index=True) # append cellwise data
tmp_dcoef_table = pd.concat(
[tmp_dcoef_table, data['movies'][tmp_ParaNr]['diff_coeffs_filtered_list']], ignore_index=True) # append diffcoefficients
# --- append tracks of condition for anaDDA:
# A. Use all localisations in valid cells; not filtered for 'DiffHistSteps' and 'numberTracksPerCell'
tmp_tracks = data['movies'][tmp_ParaNr]['tracks'].copy()
# B. Use localisations in valid cells, filtered for 'diff_hist_steps' and '#_tracks_per_cell'
#tmp_tracks = data['movies'][tmp_ParaNr]['tracks_filtered'].copy()
tmp_tracks.loc[:, 'track_id'] += tmp_max_track_id # update track_id for continuous assignments
# breakpoint()
tmp_tracks.loc[:, 'frame'] += tmp_max_frame # update frame_id for continuous assignments
tmp_tracks_table = pd.concat([tmp_tracks_table, tmp_tracks], ignore_index=True) # append diffcoefficients
tmp_max_track_id = max(tmp_tracks_table['track_id']) # tmp_id for shifting track_id
tmp_max_frame = max(tmp_tracks_table['frame']) # tmp_id for shifting frame_id
# Store important variables
condi_table['cell_data'][ff] = tmp_scta_table # a
condi_table['diff_data'][ff] = tmp_dcoef_table # all diffcoefficients per condition
anaDDA_tracks = tmp_tracks_table[['x [µm]', 'y [µm]', 'frame', 'track_id']]
anaDDA_tracks['frametime'] = data['input_parameter']['frametime'] #warning on win: using .loc[row_indexer, col_indexer] instead
# anaDDA_tracks.loc[:,'frametime'] = data['input_parameter']['frametime'] #warning on win: using .loc[row_indexer, col_indexer] instead
condi_table['anaDDA_tracks'][ff] = anaDDA_tracks
# condi_table['anaDDA_condis'].append(f"anaDDA: {comb_data['condition_names'][ff]}")
condi_table['condis_#_cells'][ff] = len(tmp_scta_table) # how many valid cells per condition
comb_data['cell_data'] = condi_table['cell_data']
comb_data['diff_data'] = condi_table['diff_data']
comb_data['anaDDA_tracks'] = condi_table['anaDDA_tracks']
# comb_data['anaDDA_condis']= condi_table['anaDDA_condis']
comb_data['condis_#_cells'] = condi_table['condis_#_cells']
comb_data['input_parameter'] = data['input_parameter']
# 3. Save comb_data dictionary
temp_path = os.path.join(data['input_parameter']['data_dir'], data['input_parameter']['default_output_dir'])
with open(temp_path + data['input_parameter']['fn_combined_movies'], 'wb') as f:
pickle.dump(comb_data, f)
print(f" Analysis of infividual movie(s) saved as pickle file: {data['input_parameter']['fn_combined_movies']}")
return comb_data, input_parameter