This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
705 lines (535 loc) · 19.3 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
"""Runs all data analyses, including splitting, calculations, and plotting."""
import argparse
import os
from enum import Enum, auto
from sys import exit
from time import time
import analysis as anl
#region Internals
class Commands(Enum):
"""CLI commands."""
PREPARSE = auto()
RESONANCE = auto()
SPATIAL = auto()
SPATIALLINE = auto()
def __validate_date(value: list[str], arg_obj):
if "..." in value and (value.count("...") > 1 or value.index("...") != 1 or len(value) != 3):
raise argparse.ArgumentError(
arg_obj, "Ranged dates must be in the format 'DATE_1 ... DATE_2'")
def __validate_arg_list(value: list, arg_obj, accept_vals: list):
if any(i not in accept_vals for i in value):
rejected_mag_vars = list(set(value) - set(accept_vals))
raise argparse.ArgumentError(
arg_obj,
f"{', '.join(rejected_mag_vars)}"
+ f" {'does not' if len(rejected_mag_vars) == 1 else 'do not'}"
+ f" match valid values: {', '.join(accept_vals)}"
)
def __validate_arg_option(value, arg_obj, accept_vals: str):
if value not in accept_vals:
raise argparse.ArgumentError(
arg_obj, f"'{value}' does not match valid_values: {', '.join(accept_vals)}")
def __resolve_dates(values: list[str]) -> list[str]:
# Checks that simulation date exists.
for val in values:
if val != "..." and (not os.path.exists(anl.paths.top.result_dir(val))):
exit(f"Error: no result was found for '{val}'.")
if "..." in values:
start_date, end_date = values[0], values[2]
all_dates = sorted(os.listdir(anl.paths.top.results_root()))
return all_dates[all_dates.index(start_date) : all_dates.index(end_date) + 1]
else:
return values
def __timed_run(anl_funcs: list):
"""Runs all analyses."""
t_init = time()
if not CLI_TEST:
for func in anl_funcs:
t_start = time()
func()
print(f" Done in {time() - t_start:.1f}s.")
print(f"Finished {len(anl_funcs)} {'analysis' if len(anl_funcs) == 1 else 'analyses'} in",
f"{time() - t_init:.1f}s.\n")
else:
print(
"CLI test mode is active.",
f"Analysis functions: {[func.__name__ for func in anl_funcs]}\n"
)
#endregion
#region Command line
def __parse_cli_input() -> tuple:
"""Parses and validates the command line interface.
Returns:
list: A list of length 3. The 0th item is the subcommand. The 1st item
is a tuple of arguments for the subcommand. The 2nd item is a tuple
of top-level arguments.
"""
parser = argparse.ArgumentParser(prog="analysis", description="Runs analyses of mumax3 data.")
subparser = parser.add_subparsers(dest="command", required=True)
comm_resonance = subparser.add_parser(
"resonance",
description=(
"Analyses for characterizing resonance characteristics of the nano-constriction.")
)
comm_spatial = subparser.add_parser(
"spatial",
description="Analyses of spatial magnetization data."
)
comm_spatialline = subparser.add_parser(
"spatialline",
description="Plots a line of values in spatial data."
)
comm_preparse = subparser.add_parser(
"preparse",
description="Readies mumax3 output before being uploaded."
)
# Top-level args
parser.add_argument(
"--cli-test",
dest="cli_test",
action="store_true",
help="activates CLI test mode; analysis functions will not be run."
)
# resonance args
argobj_resonance_dates = comm_resonance.add_argument(
"date",
type=str,
nargs='*',
default=[anl.paths.top.latest_date()],
help="the list of dates to analyze (YYYY-MM-DD_hhmm), defaults to latest"
)
argobj_mag_vars = comm_resonance.add_argument(
"--mag-vars",
dest="mag_vars",
type=str,
nargs='+',
required=False,
default=["mx", "my", "mz"],
help="magnetization components to plot and analyze, any of: mx my mz, defaults to all"
)
comm_resonance.add_argument(
"--plot-depth",
dest="plot_depth",
type=int,
required=False,
default=1,
help="the number of split levels to plot, defaults to 1"
)
# spatial args
argobj_spatial_dates = comm_spatial.add_argument(
"date",
type=str,
nargs='*',
default=[anl.paths.top.latest_date()],
help="the list of dates to analyze (YYYY-MM-DD_hhmm), defaults to latest"
)
argobj_spatial_components = comm_spatial.add_argument(
"--components",
dest="components",
type=str,
nargs='+',
required=False,
default=["x", "y", "z"],
help="components to plot and analyze, any of: x y z, defaults to all"
)
comm_spatial.add_argument(
"--quantities",
dest="quantities",
type=str,
nargs='+',
required=False,
default=None,
help="quantities to plot, defaults to all"
)
# spatialline args
argobj_spatialline_dates = comm_spatialline.add_argument(
"date",
type=str,
nargs='*',
default=[anl.paths.top.latest_date()],
help="the list of dates to analyze (YYYY-MM-DD_hhmm), defaults to latest"
)
comm_spatialline.add_argument(
"--quantity",
type=str,
required=True,
help="the quantity to be plotted"
)
argobj_spatialline_components = comm_spatialline.add_argument(
"--components",
dest="components",
type=str,
nargs='+',
required=False,
default=["x", "y", "z"],
help="magnetization components to plot and analyze, any of: x y z, defaults to all"
)
comm_spatialline.add_argument(
"--show",
action="store_true",
help="whether to show the plot in a new window"
)
comm_spatialline.add_argument(
"--save",
action="store_true",
help="whether to save the plot"
)
spatialline_axis_grp = comm_spatialline.add_mutually_exclusive_group(required=True)
spatialline_axis_grp.add_argument(
"-x",
dest="x_val",
type=int,
help="plots a vertical line of values with the given x-value"
)
spatialline_axis_grp.add_argument(
"-y",
dest="y_val",
type=int,
help="plots a horizontal line of values with the given y-value"
)
# preparse args
argobj_preparse_dates = comm_preparse.add_argument(
"date",
type=str,
nargs='*',
default=[anl.paths.top.latest_date()],
help="the list of dates to analyze (YYYY-MM-DD_hhmm), defaults to latest"
)
argobj_result_type = comm_preparse.add_argument(
"--type",
dest="result_type",
type=str,
required=True,
help="the type of simulaton to preparse, e.g. resonance, spatial"
)
# Parsing
args = parser.parse_args()
if args.command == "resonance":
__validate_date(args.date, argobj_resonance_dates)
__validate_arg_list(args.mag_vars, argobj_mag_vars, ("mx", "my", "mz"))
return (Commands.RESONANCE, (args.date, args.mag_vars, args.plot_depth), (args.cli_test,))
if args.command == "spatial":
__validate_date(args.date, argobj_spatial_dates)
__validate_arg_list(args.components, argobj_spatial_components, ("x", "y", "z"))
return (Commands.SPATIAL, (args.date, args.components, args.quantities), (args.cli_test,))
if args.command == "spatialline":
__validate_date(args.date, argobj_spatialline_dates)
__validate_arg_list(args.components, argobj_spatialline_components, ("x", "y", "z"))
comm_args = [args.date, args.quantity, args.components, args.show, args.save]
if args.x_val is not None:
comm_args.extend(("x", args.x_val))
elif args.y_val is not None:
comm_args.extend(("y", args.y_val))
return (Commands.SPATIALLINE, list(comm_args), (args.cli_test,))
if args.command == "preparse":
__validate_date(args.date, argobj_preparse_dates)
__validate_arg_option(args.result_type, argobj_result_type, ("resonance", "spatial"))
return (Commands.PREPARSE, (args.date, args.result_type), (args.cli_test,))
#endregion
#region Refs
def __fetch_raw():
print("Fetching raw data from Google Drive...")
anl.fetch.fetch_raw(DATE)
#endregion
#region Resonance
#region Splits and calculations
def __split_phi():
print("Splitting by phi...")
anl.split.split_phi(DATE)
def __split_phi_fRF():
print("Splitting by phi, f_RF...")
anl.split.split_phi_fRF(DATE)
def __calc_amp():
print("Calculating amplitude data...")
anl.amplitude.amp_phi_fRF(DATE)
anl.amplitude.max_amp_phi(DATE)
def __calc_mag_fit():
print("Calculating curve-fit for amplitudes...")
for mag in MAG_VARS:
anl.fit.fit_cauchy(mag_var=mag, xlim=[3.5e9, 6.0e9], date=DATE)
#endregion
#region Plots
def __plot_mag():
print("Plotting mx, my, mz against t from table data...")
anl.plot.plot_xy(
attr_data=anl.read.AttributedData(
data=anl.read.read_data(anl.paths.data.data_path(DATE)),
x_var="t",
y_vars=["mx", "my", "mz"]
),
xlabel="t (s)",
save_to=os.path.join(
anl.paths.plots.plot_dir(DATE, ["aggregate"]), "mx, my, mz against t.pdf")
)
def __plot_MaxAngle(): #pylint: disable=invalid-name
print("Plotting MaxAngle against t from table data...")
anl.plot.plot_xy(
attr_data=anl.read.AttributedData(
data=anl.read.read_data(anl.paths.data.data_path(DATE)),
x_var="t",
y_vars=["MaxAngle"]
),
xlabel="t (s)",
ylabel="MaxAngle (rad)",
save_to=os.path.join(
anl.paths.plots.plot_dir(DATE, ["aggregate"]), "MaxAngle against t.pdf")
)
def __plot_mag_phi():
print("Plotting mx, my, mz against t from data split by phi...")
anl.plot.plot_dataset_xy(
attr_data=anl.read.read_dataset(anl.paths.data.dataset_dir(DATE, {"phi": None})),
x_var="t",
y_vars=["mx", "my", "mz"],
xlabel="t (s)",
ylim=(-1.0, 1.0),
save_to_root=anl.paths.plots.plot_dir(DATE, ["phi"])
)
def __plot_mag_phi_fRF():
print("Plotting mx, my, mz against t from data split by phi, f_RF...")
if PLOT_DEPTH >= 2:
anl.plot.plot_dataset_xy(
attr_data=anl.read.read_dataset(
anl.paths.data.dataset_dir(DATE, {"phi": None, "f_RF": None})
),
x_var="t",
y_vars=["mx", "my", "mz"],
xlabel="t (s)",
save_to_root=anl.paths.plots.plot_dir(DATE, ["phi, f_RF"])
)
else:
print("Skipped.")
def __plot_amp():
print("Plotting amp against f_RF...")
for mag in MAG_VARS:
amp_data = anl.read.read_data(anl.paths.calcvals.amp_path(mag, DATE))
anl.plot.plot_xy(
attr_data=anl.read.AttributedData(
data=amp_data,
x_var="f_RF",
y_vars=amp_data.columns[1:]
),
xlabel="f_RF (Hz)",
ylabel=f"amp_{mag}",
save_to=os.path.join(
anl.paths.plots.plot_dir(DATE, ["aggregate"]), f"amp_{mag} against f_RF.pdf")
)
def __plot_MaxAmp(): #pylint: disable=invalid-name
print("Plotting MaxAmp against phi...")
anl.plot.plot_xy(
attr_data=anl.read.AttributedData(
data=anl.read.read_data(anl.paths.calcvals.maxamp_path(DATE)),
x_var="phi",
y_vars=["MaxAmp_mx", "MaxAmp_my", "MaxAmp_mz"]
),
xlabel="phi (deg)",
ylabel="MaxAmp",
xstep=45,
save_to=os.path.join(
anl.paths.plots.plot_dir(DATE, ["aggregate"]), "MaxAmp against phi.pdf")
)
def __plot_fitted_amp():
print("Plotting curve-fitted amp against f_RF...")
for mag in MAG_VARS:
anl.plot.plot_function(
data=anl.read.read_data(anl.paths.calcvals.fitted_amp_path(mag, DATE)),
func=anl.fit.cauchy,
params=["x_0", "gamma", "I"],
domain=[3.5e9, 6.0e9],
xlabel="f_RF (Hz)",
ylabel=f"fitted amp_{mag}",
save_to=os.path.join(
anl.paths.plots.plot_dir(DATE, ["aggregate"]),
f"fitted amp_{mag} against f_RF.pdf"
)
)
#endregion
#region Calibration checks
def __plotcheck_fitted_amp():
print("Checks: Plotting curve fit with data points for amp against f_RF...")
for var in MAG_VARS:
curve_data = anl.read.read_data(anl.paths.calcvals.fitted_amp_path(var, DATE))
amp_data = anl.read.read_data(anl.paths.calcvals.amp_path(var, DATE))
rows = curve_data["phi"][::(len(curve_data["phi"]) // 4)] # Extracts a few sample rows.
domain = (3.5e9, 6.0e9)
anl.plot.plot_function(
data=curve_data[curve_data["phi"].isin(rows)],
func=anl.fit.cauchy,
params=["x_0", "gamma", "I"],
domain=domain,
overlay=[anl.read.AttributedData(
amp_data[(amp_data["f_RF"] >= domain[0]) & (amp_data["f_RF"] <= domain[1])],
x_var="f_RF",
y_vars=rows,
fmt='x'
)],
xlabel="f_RF (Hz)",
ylabel=f"fitted amp_{var}",
title=f"Curve-fit check for amp_{var}",
save_to=os.path.join(
anl.paths.plots.plot_dir(DATE, ["checks"]),
f"check amp_{var} against f_RF.pdf"
)
)
#endregion
#endregion Resonance
#region Spatial
#region Conversions
def __convert_npy():
print("Converting all .npy files to .tsv files")
anl.geom.convert_npy(DATE)
#endregion
#region Plots
def __plot_spatial():
print("Plotting all spatial distribution data...")
plot_files = []
if QUANTITIES is None:
plot_files = [f for f in os.listdir(anl.paths.spatial.root(DATE))
if not f.endswith("json")]
else:
plot_files = [f for f in os.listdir(anl.paths.spatial.root(DATE))
if not f.endswith("json") and f.strip(".tsv") in QUANTITIES]
# Checks if plot_files is empty.
if not plot_files:
print("No data matches the requested patterns.")
for filename in plot_files:
for component in COMPONENTS:
try:
anl.plot.plot_image(
anl.read.read_data(
anl.paths.spatial.spatial_path(
filename, component, None, DATE)
),
xlabel="x (m)",
ylabel="y (m)",
title=filename + " (T)",
save_to=anl.paths.plots.spatial_dir(
filename, component, DATE
),
xindexes=[150, 362],
yindexes=[150, 362],
show_plot=False,
date=DATE
)
except FileNotFoundError:
print(f"{component} not found for {filename}. Component skipped.")
#TODO: Check that it's only the singular component that is not found.
# Otherwise the error should still be raised.
#endregion
#endregion Spatial
#region Spatial line
def __plot_spatial_line():
print("Plotting spatial line...")
for component in COMPONENTS:
anl.plot.plot_spatial_line(
date=DATE,
x_index=AXIS_VAL if AXIS == 'x' else None,
y_index=AXIS_VAL if AXIS == 'y' else None,
component=component,
filename=QUANTITY,
save_to = anl.paths.plots.spatial_line(
filename=QUANTITY,
component=component,
index=('y' if AXIS == 'x' else 'x'),
date=date
) if SAVE else None,
show_plot=SHOW,
)
#endregion
#region Preparse
def __convert_table_txt():
print("Converting table.txt to table.tsv...")
anl.read.convert_table_txt(DATE)
def __create_json():
print("Creating the json file...")
anl.write.write_json(DATE)
#endregion
#region Run
COMMAND, COMM_ARGS, TOP_ARGS = __parse_cli_input()
if COMMAND is Commands.RESONANCE:
date_arg, MAG_VARS, PLOT_DEPTH = COMM_ARGS #pylint: disable=W0632
elif COMMAND is Commands.SPATIAL:
date_arg, COMPONENTS, QUANTITIES = COMM_ARGS #pylint: disable=W0632
elif COMMAND is Commands.SPATIALLINE:
date_arg, QUANTITY, COMPONENTS, SHOW, SAVE, AXIS, AXIS_VAL = COMM_ARGS #pylint: disable=W0632
elif COMMAND is Commands.PREPARSE:
date_arg, RESULT_TYPE = COMM_ARGS #pylint: disable=W0632
DATES = __resolve_dates(date_arg)
CLI_TEST = TOP_ARGS[0]
if len(DATES) > 1:
print(f"Running analysis for {len(DATES)} results: {DATES}. \n")
if COMMAND is Commands.RESONANCE:
#TODO: Modifying DATE directly is very hacky.
for date in DATES:
DATE = date
print(
"Running analysis (resonance) with parameters:",
f"date: {DATE.__repr__()}",
f"mag-vars: {MAG_VARS.__repr__()}",
f"plot-depth: {PLOT_DEPTH.__repr__()}",
sep='\n ',
end='\n\n'
)
__timed_run([
__fetch_raw,
__split_phi,
__split_phi_fRF,
__calc_amp,
__calc_mag_fit,
__plot_mag,
__plot_MaxAngle,
__plot_mag_phi,
__plot_mag_phi_fRF,
__plot_amp,
__plot_MaxAmp,
__plot_fitted_amp,
__plotcheck_fitted_amp
])
elif COMMAND is Commands.SPATIAL:
for date in DATES:
DATE = date
print(
"Running analysis (spatial) with parameters:",
f"date: {DATE.__repr__()}",
f"components: {COMPONENTS.__repr__()}",
sep='\n ',
end='\n\n'
)
__timed_run([
__fetch_raw,
__convert_npy,
__plot_spatial
])
elif COMMAND is Commands.SPATIALLINE:
for date in DATES:
DATE = date
print(
"Running analysis (spatialline) with parameters:",
f"date: {DATE.__repr__()}",
f"quantity: {QUANTITY.__repr__()}",
f"components: {COMPONENTS.__repr__()}",
f"show: {SHOW.__repr__()}",
f"save: {SAVE.__repr__()}",
f"axis: {AXIS.__repr__()}",
f"axis_val: {AXIS_VAL.__repr__()}",
sep='\n ',
end='\n\n'
)
__timed_run([
__fetch_raw,
__plot_spatial_line
])
elif COMMAND is Commands.PREPARSE:
for date in DATES:
DATE = date
print(
"Running analysis (preparse) with parameters:",
f"date: {DATE.__repr__()}",
f"result_type: {RESULT_TYPE.__repr__()}",
sep='\n ',
end='\n\n'
)
if RESULT_TYPE == "resonance":
__timed_run([__convert_table_txt])
elif RESULT_TYPE == "spatial":
__timed_run([__create_json])
#endregion