99import multiprocessing
1010import os
1111import string
12- import subprocess
1312import sys
1413
1514import cycler
@@ -48,18 +47,22 @@ def run_from_command_line():
4847
4948 parser .add_argument ('data_directory' , help = 'The path to read input from' )
5049 parser .add_argument ('-o' , '--output_dir' , default = 'output' ,
51- help = 'The path to write output to' )
50+ help = 'The path to write output to' )
5251
53- parser .add_argument ('-w' , '--wells' , nargs = '+' ,
52+ parser .add_argument (
53+ '-w' , '--wells' , nargs = '+' ,
5454 help = 'A space separated list of wells to include. If not given, all'
5555 'wells are loaded.' )
5656
57- parser .add_argument ('--output_traces' , action = 'store_true' ,
57+ parser .add_argument (
58+ '--output_traces' , action = 'store_true' ,
5859 help = 'Add this flag to store (raw and processed) traces, as .csv files' )
59- parser .add_argument ('--export_failed' , action = 'store_true' ,
60+ parser .add_argument (
61+ '--export_failed' , action = 'store_true' ,
6062 help = 'Add this flag to produce full output even for wells failing QC' )
6163
62- parser .add_argument ('--Erev' , default = - 90.71 , type = float ,
64+ parser .add_argument (
65+ '--Erev' , default = - 90.71 , type = float ,
6366 help = 'The calculated or estimated reversal potential.' )
6467 parser .add_argument (
6568 '--reversal_spread_threshold' , type = float , default = 10 ,
@@ -74,7 +77,6 @@ def run_from_command_line():
7477 parser .add_argument ('--debug' , action = 'store_true' )
7578 parser .add_argument ('--log_level' , default = 'INFO' )
7679
77-
7880 args = parser .parse_args ()
7981
8082 # Import options from configuration file
@@ -142,6 +144,9 @@ def run(data_path, output_path, qc_map, wells=None,
142144 # TODO reversal_spread_threshold should be specified the same way as all
143145 # other QC thresholds & parameters.
144146
147+ # Create output path if necessary, and write info file
148+ output_path = setup_output_directory (output_path )
149+
145150 # TODO Remove protocol selection here: this is done via the export file!
146151 # Only protocols listed there are accepted
147152
@@ -388,13 +393,12 @@ def run(data_path, output_path, qc_map, wells=None,
388393 logging .info (f"passed_QC_Erev_spread { passed_QC_Erev_spread } " )
389394
390395 # R_leftover only considered for protocols used for QC (i.e. staircase protocols)
391- passed_QC_R_leftover = np .all (sub_df [sub_df .protocol .isin (args .D2SQC .values ())]
392- ["QC.R_leftover" ].values
393- )
396+ passed_QC_R_leftover = np .all (sub_df [sub_df .protocol .isin (qc_map .values ())]
397+ ['QC.R_leftover' ].values )
394398
395399 logging .info (f"passed_QC_R_leftover { passed_QC_R_leftover } " )
396400
397- passed_QC_Erev_spread = E_rev_spread <= args . reversal_spread_threshold
401+ passed_QC_Erev_spread = E_rev_spread <= reversal_spread_threshold
398402
399403 qc_erev_spread [well ] = passed_QC_Erev_spread
400404 erev_spreads [well ] = E_rev_spread
@@ -821,7 +825,7 @@ def extract_protocol(readname, savename, time_strs, selected_wells, savedir,
821825 savedir , 'debug' , '-120mV time constant' ,
822826 f'{ savename } -{ well } -sweep{ sweep } -time-constant-fit.pdf' ),
823827 figure_size
824- )
828+ )
825829
826830 row_dict ['-120mV decay time constant 1' ] = res [0 ][0 ]
827831 row_dict ['-120mV decay time constant 2' ] = res [0 ][1 ]
@@ -859,7 +863,7 @@ def extract_protocol(readname, savename, time_strs, selected_wells, savedir,
859863 protocol = savename )
860864
861865 fig .savefig (os .path .join (subtraction_plots_dir ,
862- f" { save_id } -{ savename } -{ well } -sweep{ sweep } -subtraction" ))
866+ f' { save_id } -{ savename } -{ well } -sweep{ sweep } -subtraction' ))
863867 fig .clf ()
864868
865869 plt .close (fig )
@@ -874,11 +878,11 @@ def extract_protocol(readname, savename, time_strs, selected_wells, savedir,
874878 # extract protocol
875879 protocol = before_trace .get_voltage_protocol ()
876880 protocol .export_txt (os .path .join (protocol_dir ,
877- f" { save_id } -{ savename } .txt" ))
881+ f' { save_id } -{ savename } .txt' ))
878882
879883 json_protocol = before_trace .get_voltage_protocol_json ()
880884
881- with open (os .path .join (protocol_dir , f" { save_id } -{ savename } .json" ), 'w' ) as fout :
885+ with open (os .path .join (protocol_dir , f' { save_id } -{ savename } .json' ), 'w' ) as fout :
882886 json .dump (json_protocol , fout )
883887
884888 return extract_df
@@ -898,7 +902,7 @@ def run_qc_for_protocol(readname, savename, time_strs, output_path,
898902 filepath_before = os .path .join (data_path , f'{ readname } _{ time_strs [0 ]} ' )
899903 json_file_before = f"{ readname } _{ time_strs [0 ]} "
900904
901- filepath_after = os .path .join (data_path , f'{ readname } _{ time_strs [1 ]} ' )
905+ filepath_after = os .path .join (data_path , f'{ readname } _{ time_strs [1 ]} ' )
902906 json_file_after = f"{ readname } _{ time_strs [1 ]} "
903907
904908 logging .debug (f"loading { json_file_after } and { json_file_before } " )
@@ -910,7 +914,7 @@ def run_qc_for_protocol(readname, savename, time_strs, output_path,
910914 # Convert to s
911915 sampling_rate = before_trace .sampling_rate
912916
913- savedir = os .path .join (output_path , "QC" )
917+ savedir = os .path .join (output_path , 'QC' )
914918 leak_correction_dir = os .path .join (savedir , "leak_correction" )
915919
916920 if not os .path .exists (savedir ):
@@ -1031,7 +1035,7 @@ def run_qc_for_protocol(readname, savename, time_strs, output_path,
10311035 for i in range (nsweeps ):
10321036
10331037 savepath = os .path .join (savedir ,
1034- f" { save_id } -{ savename } -{ well } -sweep{ i } .csv" )
1038+ f' { save_id } -{ savename } -{ well } -sweep{ i } .csv' )
10351039 subtracted_current = before_currents_corrected [i , :] - after_currents_corrected [i , :]
10361040
10371041 if write_traces :
@@ -1125,25 +1129,18 @@ def qc3_bookend(readname, savename, time_strs, wells, output_path,
11251129 save_fname = f"{ well } _{ savename } _before0.pdf"
11261130
11271131 if debug :
1128- qc3_output_dir = os .path .join (output_path ,
1129- "QC" ,
1130- "debug" ,
1131- f"debug_{ well } _{ savename } " ,
1132- "qc3_bookend" )
1132+ qc3_output_dir = os .path .join (
1133+ output_path , 'QC' , 'debug' , f'{ well } _{ savename } ' , 'qc3_bookend' )
11331134
11341135 if not os .path .exists (qc3_output_dir ):
11351136 os .makedirs (qc3_output_dir )
11361137
1137- leak_correct_dir = os .path .join (qc3_output_dir ,
1138- "leak_correction" )
1138+ leak_correct_dir = os .path .join (qc3_output_dir , 'leak_correction' )
11391139
11401140 # Plot subtraction
1141- if debug :
1142- get_leak_corrected (first_before_current ,
1143- voltage , times ,
1144- * ramp_bounds ,
1145- save_fname = save_fname ,
1146- output_dir = leak_correct_dir )
1141+ get_leak_corrected (
1142+ first_before_current , voltage , times , * ramp_bounds ,
1143+ save_fname = save_fname , output_dir = leak_correct_dir )
11471144
11481145 before_traces_first [well ] = get_leak_corrected (first_before_current ,
11491146 voltage , times ,
@@ -1166,9 +1163,8 @@ def qc3_bookend(readname, savename, time_strs, wells, output_path,
11661163
11671164 voltage_protocol = VoltageProtocol .from_voltage_trace (voltage , times )
11681165
1169-
11701166 plot_dir = os .path .join (output_path , output_path ,
1171- f" { save_id } -{ savename } -qc3-bookend" )
1167+ f' { save_id } -{ savename } -qc3-bookend' )
11721168 if not os .path .exists (plot_dir ):
11731169 os .makedirs (plot_dir )
11741170 hergqc = hERGQC (sampling_rate = first_before_trace .sampling_rate ,
@@ -1197,8 +1193,7 @@ def qc3_bookend(readname, savename, time_strs, wells, output_path,
11971193
11981194 res_dict [well ] = passed
11991195
1200- save_fname = os .path .join (output_path ,
1201- 'qc3_bookend' )
1196+ save_fname = os .path .join (output_path , 'qc3_bookend' )
12021197
12031198 ax .plot (times , trace1 )
12041199 ax .plot (times , trace2 )
0 commit comments