@@ -20,8 +20,8 @@ def boolean_string(s):
2020
2121def main (args = None ):
2222 """Parses directory argument supplied by user and conducts NMF
23- decomposition analysis (computes NMF decomposition and shows the weights
24- over time)."""
23+ decomposition analysis (computes NMF decomposition and shows the
24+ weights over time)."""
2525
2626 _BANNER = """
2727 This is a package which takes a directory of 1D diffraction files
@@ -38,13 +38,17 @@ def main(args=None):
3838
3939 def tup (s ):
4040 if not isinstance (s , str ):
41- raise TypeError ("Input must be a string of two integers separated by a comma." )
41+ raise TypeError (
42+ "Input must be a string of two integers separated by a comma."
43+ )
4244
4345 try :
4446 l , h = map (int , s .split ("," ))
4547 return l , h
4648 except ValueError :
47- raise ValueError ("Input must be two integers separated by a comma (e.g., '1,5')" )
49+ raise ValueError (
50+ "Input must be two integers separated by a comma (e.g., '1,5')"
51+ )
4852
4953 # args
5054 parser .add_argument (
@@ -94,15 +98,19 @@ def tup(s):
9498 "--xrd" ,
9599 default = False ,
96100 type = boolean_string ,
97- help = "whether to look for .xy files rather than .gr files\n " "default: False\n " "e.g. --xrd True" ,
101+ help = "whether to look for .xy files rather than .gr files\n "
102+ "default: False\n "
103+ "e.g. --xrd True" ,
98104 )
99105 parser .add_argument (
100106 "--x_units" ,
101107 default = None ,
102108 type = str ,
103109 choices = ["twotheta" , "q" ],
104110 required = "--xrd" in sys .argv ,
105- help = "x axis units for XRD data\n " "default: None\n " "e.g. --x_units twotheta" ,
111+ help = "x axis units for XRD data\n "
112+ "default: None\n "
113+ "e.g. --x_units twotheta" ,
106114 )
107115 parser .add_argument (
108116 "--xrange" ,
@@ -152,7 +160,9 @@ def tup(s):
152160
153161 print (f"Number of components: { len (df_components .columns )} " )
154162
155- fig1 = nmf .component_plot (df_components , args1 .xrd , args1 .x_units , args1 .show )
163+ fig1 = nmf .component_plot (
164+ df_components , args1 .xrd , args1 .x_units , args1 .show
165+ )
156166 fig2 = nmf .component_ratio_plot (df_component_weight_timeseries , args1 .show )
157167 fig3 = nmf .reconstruction_error_plot (df_reconstruction_error , args1 .show )
158168 if args1 .pca_thresh :
@@ -161,10 +171,18 @@ def tup(s):
161171 if args1 .save_files :
162172 if not os .path .exists (os .path .join (os .getcwd (), "nmf_result" )):
163173 os .mkdir (os .path .join (os .getcwd (), "nmf_result" ))
164- output_fn = datetime .fromtimestamp (time .time ()).strftime ("%Y%m%d%H%M%S%f" )
165- df_components .to_json (os .path .join (os .getcwd (), "nmf_result" , "x_index_vs_y_col_components.json" ))
174+ output_fn = datetime .fromtimestamp (time .time ()).strftime (
175+ "%Y%m%d%H%M%S%f"
176+ )
177+ df_components .to_json (
178+ os .path .join (
179+ os .getcwd (), "nmf_result" , "x_index_vs_y_col_components.json"
180+ )
181+ )
166182 df_component_weight_timeseries .to_json (
167- os .path .join (os .getcwd (), "nmf_result" , "component_index_vs_pratio_col.json" )
183+ os .path .join (
184+ os .getcwd (), "nmf_result" , "component_index_vs_pratio_col.json"
185+ )
168186 )
169187 df_component_weight_timeseries .to_csv (
170188 os .path .join (
@@ -178,27 +196,47 @@ def tup(s):
178196 mode = "a" ,
179197 )
180198 df_reconstruction_error .to_json (
181- os .path .join (os .getcwd (), "nmf_result" , "component_index_vs_RE_value.json" )
199+ os .path .join (
200+ os .getcwd (), "nmf_result" , "component_index_vs_RE_value.json"
201+ )
202+ )
203+ plot_file1 = os .path .join (
204+ os .getcwd (), "nmf_result" , output_fn + "comp_plot.png"
205+ )
206+ plot_file2 = os .path .join (
207+ os .getcwd (), "nmf_result" , output_fn + "ratio_plot.png"
208+ )
209+ plot_file3 = os .path .join (
210+ os .getcwd (), "nmf_result" , output_fn + "loss_plot.png"
182211 )
183- plot_file1 = os .path .join (os .getcwd (), "nmf_result" , output_fn + "comp_plot.png" )
184- plot_file2 = os .path .join (os .getcwd (), "nmf_result" , output_fn + "ratio_plot.png" )
185- plot_file3 = os .path .join (os .getcwd (), "nmf_result" , output_fn + "loss_plot.png" )
186212 if args1 .pca_thresh :
187- plot_file7 = os .path .join (os .getcwd (), "nmf_result" , output_fn + "pca_var_plot.png" )
213+ plot_file7 = os .path .join (
214+ os .getcwd (), "nmf_result" , output_fn + "pca_var_plot.png"
215+ )
188216 plot_file4 = os .path .splitext (plot_file1 )[0 ] + ".pdf"
189217 plot_file5 = os .path .splitext (plot_file2 )[0 ] + ".pdf"
190218 plot_file6 = os .path .splitext (plot_file3 )[0 ] + ".pdf"
191219 if args1 .pca_thresh :
192220 plot_file8 = os .path .splitext (plot_file7 )[0 ] + ".pdf"
193- txt_file = os .path .join (os .getcwd (), "nmf_result" , output_fn + "_meta" + ".txt" )
221+ txt_file = os .path .join (
222+ os .getcwd (), "nmf_result" , output_fn + "_meta" + ".txt"
223+ )
194224 with open (txt_file , "w+" ) as fi :
195225 fi .write ("NMF Analysis\n \n " )
196- fi .write (f"{ len (df_component_weight_timeseries .columns )} files uploaded for analysis.\n \n " )
226+ fi .write (
227+ f"{ len (df_component_weight_timeseries .columns )} files uploaded for analysis.\n \n "
228+ )
197229 fi .write (f"The selected active r ranges are: { args1 .xrange } \n \n " )
198230 fi .write ("Thesholding:\n " )
199- fi .write (f"\t The input component threshold was: { args1 .threshold } \n " )
200- fi .write (f"\t The input improvement threshold was: { args1 .improve_thresh } \n " )
201- fi .write (f"\t The input # of iterations to run was: { args1 .n_iter } \n " )
231+ fi .write (
232+ f"\t The input component threshold was: { args1 .threshold } \n "
233+ )
234+ fi .write (
235+ f"\t The input improvement threshold was: { args1 .improve_thresh } \n "
236+ )
237+ fi .write (
238+ f"\t The input # of iterations to run was: { args1 .n_iter } \n "
239+ )
202240 fi .write (f"\t Was PCA thresholding used?: { args1 .pca_thresh } \n " )
203241 fi .write (f"{ len (df_components .columns )} components were extracted" )
204242
@@ -214,7 +252,9 @@ def tup(s):
214252 fig4 .savefig (plot_file8 )
215253 columns = df_components .columns
216254 for i , col in enumerate (columns ):
217- data = np .column_stack ([df_components .index .to_list (), df_components [col ].to_list ()])
255+ data = np .column_stack (
256+ [df_components .index .to_list (), df_components [col ].to_list ()]
257+ )
218258
219259 if args1 .xrd :
220260 np .savetxt (
@@ -237,7 +277,8 @@ def tup(s):
237277 output_fn + f"_comp{ i } " + ".cgr" ,
238278 ),
239279 data ,
240- header = f"NMF Generated PDF\n Source: nmfMapping\n " f"Date: { output_fn } \n r g" ,
280+ header = f"NMF Generated PDF\n Source: nmfMapping\n "
281+ f"Date: { output_fn } \n r g" ,
241282 fmt = "%s" ,
242283 )
243284
0 commit comments