-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
147 lines (122 loc) · 3.9 KB
/
__main__.py
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
import glob
import pprint
import re
import traceback
from datetime import datetime
from os.path import dirname
import pyabf
from firing import firing
from first_ap import first_ap
from iv_plot import iv_plot
from rm import rm
from rmp import rmp
from sag_tau_cm import sag_tau_cm
DATA_FOLDER = "E:\\Записи обновленные"
RESULTS_FOLDER = f"{dirname(__file__)}\\results"
EXPERIMENT_DATE_FORMAT = r"%Y-%m-%d"
def create_protocol_re(*protocol_names):
return re.compile(
rf"(?:\d\s*-\s*)+(?:{'|'.join([re.escape(name) for name in protocol_names])})"
)
def parse_ap_steps(*args):
return {
"RMP": rmp(*args),
"First AP props": first_ap(*args),
"Firing props": firing(*args),
}
patterns = (
(
"Rm",
create_protocol_re(
"CC_Rm_from_-70mV",
"CC_props_from_-70mV",
"CC_steps_props",
),
rm,
),
(
"Sag_tau_Cm",
create_protocol_re(
"CC_Ih_from_-70mV",
"CC_Ih_from_RMP_and_-70mV",
"CC_sag_from_-70mV",
"CC_steps_Ih",
),
sag_tau_cm,
),
(
"APs",
create_protocol_re(
"CC_AP_thrh",
"CC_steps_from_RMP",
"CC_steps_from_RMP_and_-70mV",
"CC_steps_AP",
),
parse_ap_steps,
),
(
"IV",
create_protocol_re(
"VC_IV_plot",
"VC_standard",
"VC_steps",
),
iv_plot,
),
)
starting_date = datetime.strptime(
input("Enter the experiment date from which to analyze cells (yyyy-mm-dd): "),
EXPERIMENT_DATE_FORMAT,
)
all_patterns = [pattern[0] for pattern in patterns]
chosen_patterns = input(
f"Choose patterns that will be used from {', '.join(all_patterns)}. "
f"Separate by one whitespace. Type * to choose everything. "
)
chosen_patterns = chosen_patterns.split(" ") if chosen_patterns != "*" else all_patterns
show_plots = input('Show plots ("y" for "yes", anything for "no")? ').lower() == "y"
year_path = f"{DATA_FOLDER}\\{starting_date.year}\\"
r_cell_id = re.compile(rf".+?\\{starting_date.year}\\(.+?)\\\d+\.abf")
r_experiment_date = re.compile(rf".+?\\{starting_date.year}\\([^\\]+)")
results = {pattern: {} for pattern in chosen_patterns}
analysis_start_time = datetime.now()
evaluation_count = 0
exception_count = 0
for filepath in glob.iglob(f"{year_path}**\\*.abf", recursive=True):
if (
datetime.strptime(r_experiment_date.match(filepath)[1], EXPERIMENT_DATE_FORMAT)
>= starting_date
):
abf = pyabf.ABF(filepath, False)
for pattern in patterns:
if pattern[0] in results and pattern[1].match(abf.protocol):
cell_id = r_cell_id.match(filepath)[1]
if cell_id not in results[pattern[0]]:
results[pattern[0]][cell_id] = []
try:
results[pattern[0]][cell_id].append(pattern[2](abf, show_plots))
except Exception:
print(
f"\nException occurred with cell {cell_id}, file path: {filepath}\n"
)
print(traceback.format_exc())
exception_count += 1
else:
evaluation_count += 1
print(
f"Analysis finished in {(datetime.now() - analysis_start_time).total_seconds()} s. "
f"Evaluations done: {evaluation_count}. "
f"Exceptions occurred: {exception_count}"
)
results_for_file = []
for prop_name, prop_data in results.items():
results_for_file.append(f"Property: {prop_name}")
for cell_id, cell_data in prop_data.items():
results_for_file.extend(
(f"\nCell: {cell_id}", pprint.pformat(cell_data, sort_dicts=False))
)
with open(
f"{RESULTS_FOLDER}\\{str(datetime.now()).replace(":", "-")}.txt",
"w",
) as file:
file.write("\n".join(results_for_file))