-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepopack-output.txt
More file actions
1658 lines (1370 loc) · 58 KB
/
repopack-output.txt
File metadata and controls
1658 lines (1370 loc) · 58 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
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
This file is a merged representation of the entire codebase, combining all repository files into a single document.
Generated by Repopack on: 2024-10-18T18:54:32.335Z
================================================================
File Summary
================================================================
Purpose:
--------
This file contains a packed representation of the entire repository's contents.
It is designed to be easily consumable by AI systems for analysis, code review,
or other automated processes.
File Format:
------------
The content is organized as follows:
1. This summary section
2. Repository information
3. Repository structure
4. Multiple file entries, each consisting of:
a. A separator line (================)
b. The file path (File: path/to/file)
c. Another separator line
d. The full contents of the file
e. A blank line
Usage Guidelines:
-----------------
- This file should be treated as read-only. Any changes should be made to the
original repository files, not this packed version.
- When processing this file, use the file path to distinguish
between different files in the repository.
- Be aware that this file may contain sensitive information. Handle it with
the same level of security as you would the original repository.
Notes:
------
- Some files may have been excluded based on .gitignore rules and Repopack's
configuration.
- Binary files are not included in this packed representation. Please refer to
the Repository Structure section for a complete list of file paths, including
binary files.
Additional Info:
----------------
For more information about Repopack, visit: https://github.com/yamadashy/repopack
================================================================
Repository Structure
================================================================
feda_tools/__init__.py
feda_tools/core/analysis.py
feda_tools/core/data/__init__.py
feda_tools/core/data/array_tools.py
feda_tools/core/data/df_tools.py
feda_tools/core/data/retreival.py
feda_tools/core/data/storage.py
feda_tools/core/model.py
feda_tools/core/process.py
feda_tools/core/twodim_hist.py
feda_tools/core/utilities.py
feda_tools/gui/__main__.py
feda_tools/gui/fit23_preview.py
feda_tools/gui/process_analysis.py
feda_tools/gui/threshold_adjustment.py
feda_tools/gui/widgets.py
feda_tools/pipeline.py
LICENSE
README.md
repopack.config.json
setup.py
================================================================
Repository Files
================================================================
================
File: feda_tools/__init__.py
================
__version__ = '1.0'
================
File: feda_tools/core/analysis.py
================
from typing import Dict, List, Tuple
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
from scipy.stats import halfnorm
from . import data as dat
def running_average(data, window_size):
window = np.ones(window_size) / window_size
return np.convolve(data, window, mode='valid')
def flour_aniso(g_factor, intensity_para, intensity_perp, l1_japan_corr, l2_japan_corr):
"""Fluorescence Anisotropy calculation.
See equation 7 in Kudryavtsev, V., Sikor, M., Kalinin, S., Mokranjac, D., Seidel, C.A.M. and Lamb, D.C. (2012),
Combining MFD and PIE for Accurate Single-Pair Förster Resonance Energy Transfer Measurements. ChemPhysChem, 13: 1060-1078.
https://doi.org/10.1002/cphc.201100822
"""
numerator = g_factor * intensity_para - intensity_perp
denominator = ((1 - 3 * l2_japan_corr) * g_factor * intensity_para +
(2 - 3 * l1_japan_corr) * intensity_perp)
return numerator / denominator
def interphoton_arrival_times(all_macro_times, all_micro_times, macro_res, micro_res):
# - Each detected photon has a time of detection encoded by the macro time + the micro time. **all_macro_times** and **all_micro_times** are arrays whose index is represents the detected photons in order of detection, while the value represents the associated macro or micro time for each photon.
# - **macro_res** and **micro_res** represent the resolution of the macro and micro times in seconds.
# - The **macro time** indicates the time in units of **macro_res** that the excitation laser was last fired directly before this photon was detected.
# - The **micro time** indicates the amount of time in units of **micro_res** that has elapsed since the excitation laser was last fired at which the photon was detected, i.e. it's the amount of time elapsed from the macro time at which the photon was detected.
# - The interphoton arrival time is calculated by iterating through **all_macro_times** and **all_micro_times** and calculating the time elapsed between each photon detection event.
arr_size = len(all_macro_times) - 1
photon_time_intervals = np.zeros(arr_size, dtype=np.float64)
for i in range(arr_size):
photon_1 = (all_macro_times[i]*macro_res) + (all_micro_times[i]*micro_res)
photon_2 = (all_macro_times[i+1]*macro_res) + (all_micro_times[i+1]*micro_res)
photon_time_intervals[i] = (photon_2 - photon_1)*1000 # Convert to ms
photon_ids = np.arange(1, arr_size + 1)
return photon_time_intervals, photon_ids
def estimate_background_noise(logrunavg, bins_y):
counts_logrunavg, bins_logrunavg, _ = plt.hist(logrunavg, bins=bins_y, alpha=0.6, color='r')
plt.close() # Close the plot to prevent it from displaying during function call
index_max = np.argmax(counts_logrunavg)
noise_mean = bins_logrunavg[index_max]*0.95
filtered_logrunavg = ma.masked_less(logrunavg, noise_mean).compressed()
mu, std = halfnorm.fit(filtered_logrunavg)
return mu, std, noise_mean, filtered_logrunavg, bins_logrunavg
================
File: feda_tools/core/data/__init__.py
================
from .retreival import *
from .storage import *
from .df_tools import *
from .array_tools import *
================
File: feda_tools/core/data/array_tools.py
================
import numpy.ma as ma
### define function for extracting the unmasked segments from the thresholded data.
def extract_greater(data, threshold_value):
filtered_values = ma.masked_greater(data, threshold_value)
burst_index = extract_unmasked_indices(filtered_values)
return burst_index, filtered_values
def extract_unmasked_indices(masked_array):
unmasked_indices_lists = []
current_indices = []
# iterate through masked array and collect unmasked index segments
for i, value in enumerate(masked_array):
if ma.is_masked(value):
if current_indices:
unmasked_indices_lists.append(current_indices)
current_indices = []
else:
current_indices.append(i)
# handle the last segment
if current_indices:
unmasked_indices_lists.append(current_indices)
return unmasked_indices_lists
================
File: feda_tools/core/data/df_tools.py
================
import pandas as pd
def filter_burstids(df, bid_path):
# get bid_df to use as a filter on the passed df.
bid_df = get_bid_df(bid_path)
# Filter df using bid_df.
result_df = df.merge(bid_df, on=['First Photon', 'Last Photon', 'First File'], how='inner')
return result_df
# read all of the burst id files in the selected directory and create a dataframe storing
# their First Photon, Last Photon, and First File values row-wise.
def get_bid_df(bid_path):
# create dfs list to append each bst file dataframe when reading the directory
dfs = []
# iterate through the bst files in the specified directory
for file in sorted((bid_path).glob('*.%s' % 'bst')):
# read each file and create a dataframe
file_df = pd.read_csv(file, sep ='\t', header=None)
# rename the unnammed columns
file_df.rename(columns={0: "First Photon", 1: "Last Photon"}, inplace=True)
# get the filename so we can assoc a file to the First File column
filename = os.path.basename(file).split('.')[0]
# process the filename so that it matches the format in the bur file
filename = filename.replace("_0", "")
filename = filename + ".ptu"
# assign this files First and Last Photon data to the associated file.
file_df['First File'] = filename
# append the burst file dataframe to a list, will concatenate them all after loop.
dfs.append(file_df)
# concatenate all the dfs into one
flphotons = pd.concat(dfs, axis = 0, ignore_index = True)
return flphotons
================
File: feda_tools/core/data/retreival.py
================
import os
import tttrlib
import fnmatch
def get_ptu_files(directory):
ptu_files = []
for file in os.listdir(directory):
if fnmatch.fnmatch(file, '*.ptu'):
ptu_files.append(file)
return ptu_files
def load_ptu_files(file_ptu, file_irf, file_bkg):
data_ptu = tttrlib.TTTR(file_ptu, 'PTU')
data_irf = tttrlib.TTTR(file_irf, 'PTU')
data_bkg = tttrlib.TTTR(file_bkg, 'PTU')
return data_ptu, data_irf, data_bkg
================
File: feda_tools/core/data/storage.py
================
import os
def save_results(output_directory, file_path, bi4_bur_df, bg4_df):
bur_filename = os.path.splitext(os.path.basename(str(file_path)))[0]
bur_filepath = os.path.join(output_directory, bur_filename) + ".bur"
bi4_bur_df.to_csv(bur_filepath, sep='\t', index=False, float_format='%.6f')
bg4_filepath = os.path.join(output_directory, bur_filename) + ".bg4"
bg4_df.to_csv(bg4_filepath, sep='\t', index=False, float_format='%.6f')
================
File: feda_tools/core/model.py
================
import tttrlib
import numpy as np
def setup_fit23(num_bins, macro_res, counts_irf_nb, g_factor, l1_japan_corr, l2_japan_corr):
dt = 25000/num_bins/1000
period = 1/(macro_res*np.power(10, 6))
fit23 = tttrlib.Fit23(
dt=dt,
irf=counts_irf_nb,
background=np.ones_like(counts_irf_nb)*0.002,
period=period,
g_factor=g_factor,
l1=l1_japan_corr,
l2=l2_japan_corr,
convolution_stop=10,
p2s_twoIstar_flag=True
)
return fit23
================
File: feda_tools/core/process.py
================
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
from feda_tools.core import analysis as an
# This function realistically should be broken up, its a spaghetti code mess that I'll or someone else will
# have to come back to and fix - Alex K 10-18-2024
def process_single_burst(
burst, all_macro_times, all_micro_times, routing_channels, macro_res, micro_res,
min_photon_count, bg4_micro_time_min, bg4_micro_time_max, g_factor, l1_japan_corr,
l2_japan_corr, bg4_bkg_para, bg4_bkg_perp, fit23, initial_fit_params
):
if len(burst) <= min_photon_count:
return None, None
first_photon = burst[0]
last_photon = burst[-1]
lp_time = all_macro_times[last_photon]*macro_res + all_micro_times[last_photon]*micro_res
fp_time = all_macro_times[first_photon]*macro_res + all_micro_times[first_photon]*micro_res
lp_time_ms = lp_time*1000
fp_time_ms = fp_time*1000
duration = lp_time_ms - fp_time_ms
macro_times = all_macro_times[burst]*macro_res*1000
mean_macro_time = np.mean(macro_times)
num_photons = len(burst)
count_rate = num_photons / duration if duration != 0 else np.nan
list_of_indexes = burst
mask_channel_0 = routing_channels[list_of_indexes] == 0
mask_channel_2 = routing_channels[list_of_indexes] == 2
indexes_channel_0 = np.array(list_of_indexes)[mask_channel_0]
indexes_channel_2 = np.array(list_of_indexes)[mask_channel_2]
if len(indexes_channel_0) > 0 and len(indexes_channel_2) > 0:
first_green_photon = min(np.min(indexes_channel_0), np.min(indexes_channel_2))
last_green_photon = max(np.max(indexes_channel_0), np.max(indexes_channel_2))
elif len(indexes_channel_0) >= 2:
first_green_photon = np.min(indexes_channel_0)
last_green_photon = np.max(indexes_channel_0)
elif len(indexes_channel_2) >= 2:
first_green_photon = np.min(indexes_channel_2)
last_green_photon = np.max(indexes_channel_2)
else:
first_green_photon = None
last_green_photon = None
if first_green_photon is not None and last_green_photon is not None:
lgp_time = all_macro_times[last_green_photon]*macro_res + all_micro_times[last_green_photon]*micro_res
fgp_time = all_macro_times[first_green_photon]*macro_res + all_micro_times[first_green_photon]*micro_res
lgp_time_ms = lgp_time*1000
fgp_time_ms = fgp_time*1000
duration_green = lgp_time_ms - fgp_time_ms
else:
duration_green = np.nan
macro_times_ch0 = all_macro_times[indexes_channel_0]*macro_res*1000
macro_times_ch2 = all_macro_times[indexes_channel_2]*macro_res*1000
combined_macro_times = np.concatenate([macro_times_ch0, macro_times_ch2], axis=0)
mean_macro_time_green = np.mean(combined_macro_times) if len(combined_macro_times) > 0 else np.nan
num_photons_gr = len(indexes_channel_0) + len(indexes_channel_2)
count_rate_gr = num_photons_gr / duration_green if duration_green != 0 else np.nan
bur_new_row = {
'First Photon': [first_photon],
'Last Photon': [last_photon],
'Duration (ms)': [duration],
'Mean Macro Time (ms)': [mean_macro_time],
'Number of Photons': [num_photons],
'Count Rate (kHz)': [count_rate],
'Duration (green) (ms)': [duration_green],
'Mean Macro Time (green) (ms)': [mean_macro_time_green],
'Number of Photons (green)': [num_photons_gr],
'Green Count Rate (kHz)': [count_rate_gr]
}
bi4_bur_df = pd.DataFrame.from_dict(bur_new_row)
bg4_channel_2_photons = [
index for index in burst if routing_channels[index] == 2 and
bg4_micro_time_min < all_micro_times[index] < bg4_micro_time_max
]
bg4_channel_2_count = len(bg4_channel_2_photons)
bg4_channel_0_photons = [
index for index in burst if routing_channels[index] == 0 and
bg4_micro_time_min < all_micro_times[index] < bg4_micro_time_max
]
bg4_channel_0_count = len(bg4_channel_0_photons)
bg4_total_count = bg4_channel_2_count + bg4_channel_0_count
bg4_rexp = an.flour_aniso(
g_factor, bg4_channel_2_count, bg4_channel_0_count,
l1_japan_corr, l2_japan_corr
)
bg4_rscat = an.flour_aniso(
g_factor,
bg4_channel_2_count - bg4_bkg_para,
bg4_channel_0_count - bg4_bkg_perp,
l1_japan_corr,
l2_japan_corr
)
counts = np.array([bg4_channel_0_count, bg4_channel_2_count])
# Use initial_fit_params as starting values
x0 = initial_fit_params
fixed = np.array([0, 0, 1, 0])
try:
r2 = fit23(data=counts, initial_values=x0, fixed=fixed, include_model=True)
fit_tau, fit_gamma, fit_r0, fit_rho = r2['x'][:4]
fit_softbifl = r2['x'][4] if len(r2['x']) > 4 else np.nan
fit_2istar = r2['x'][5] if len(r2['x']) > 5 else np.nan
fit_rs_scatter = r2['x'][6] if len(r2['x']) > 6 else np.nan
fit_rs_exp = r2['x'][7] if len(r2['x']) > 7 else np.nan
except Exception as e:
print(f"Fit23 error for burst {first_photon}-{last_photon}: {e}")
return None, None
bg4_new_row = {
'Ng-p-all': [bg4_channel_2_count],
'Ng-s-all': [bg4_channel_0_count],
'Number of Photons (fit window) (green)': [bg4_total_count],
'r Scatter (green)': [bg4_rscat],
'r Experimental (green)': [bg4_rexp],
'Fit tau': [fit_tau],
'Fit gamma': [fit_gamma],
'Fit r0': [fit_r0],
'Fit rho': [fit_rho],
'Fit softbifl': [fit_softbifl],
'Fit 2I*': [fit_2istar],
'Fit rs_scatter': [fit_rs_scatter],
'Fit rs_exp': [fit_rs_exp]
}
bg4_df = pd.DataFrame.from_dict(bg4_new_row)
return bi4_bur_df, bg4_df
================
File: feda_tools/core/twodim_hist.py
================
"""
Author - Frank Duffy
"""
import argparse
import os
import yaml
import pandas as pd
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
import matplotlib.pyplot as plt
import numpy as np
calc_list = [
"Mean Macro Time (sec)",
"Sg/Sr (prompt)",
"S(prompt)/S(total)"
]
def get_plot_dict(yaml_file):
"""
gets the plots specified by the user in the provided yaml file.
"""
# Conversts yaml doc to python object
plot_dict = yaml.safe_load(yaml_file)
return plot_dict
def arg_check(arg):
"""
checks if the arguments provided by the user correspond to paths that exist
"""
path = str(arg)
if os.path.exists(path):
return path
else:
raise argparse.ArgumentTypeError(path + ' could not be found. ' +
'Check for typos or for errors in your relative path string')
def parse_args(args):
"""
parse the arguments provided by the user and return them to the main program
"""
parser = argparse.ArgumentParser()
parser.add_argument('data_folder', type=arg_check)
parser.add_argument('plot_file', type=arg_check)
parsed_args = parser.parse_args(args)
print((parsed_args.data_folder, parsed_args.plot_file))
return parsed_args.data_folder, parsed_args.plot_file
def get_data(data_folder):
print("Getting data in " + data_folder)
# print(os.getcwd())
df_list = []
for file in os.listdir(data_folder):
df_list.append(pd.read_csv(data_folder + "\\" + file, sep = '\t'))
df = pd.concat(df_list)
# print(df)
return df
def get_calc(label, data_folder):
"""
perform the requisite calculation on the data frame and return it
"""
if label == "Mean Macro Time (sec)":
# data_folder = data_folder
df = get_data(data_folder + "\\bi4_bur" )
df["Mean Macro Time (ms)"] = df["Mean Macro Time (ms)"].div(1000)
df = df.rename(columns={"Mean Macro Time (ms)": "Mean Macro Time (sec)"})
return df
elif label == "Sg/Sr (prompt)":
df = get_data(data_folder + "\\bi4_bur")
df[label] = df["Green Count Rate (KHz)"].div(df["S prompt red (kHz) | 0-200"])
# df[label] = np.log(df[label])
return df
def clean_data(df):
if "Number of Photons" in df.columns:
df = df.loc[df["Number of Photons"] > 0]
elif "Number of Photons (fit window) (green)" in df.columns:
df = df.loc[df["Number of Photons (fit window) (green)"] > 0]
elif "Number of Photons (fit window) (red)" in df.columns:
df = df.loc[df["Number of Photons (fit window) (red)"] > 0]
elif "Number of Photons (fit window) (yellow)" in df.columns:
df = df.loc[df["Number of Photons (fit window) (yellow)"] > 0]
if "Unnamed: 14" in df.columns:
df.drop(labels="Unnamed: 14", axis = 1, inplace=True)
# df = df[df["Tau (yellow)"].between(1,6)]
# df = df[df["r Scatter (yellow)"].between(-0.2,1.5)]
# if "TGX_TRR" in df.columns:
# df = df[df["TGX_TRR"].between(-5,5)]
print(df)
df.replace([np.inf, -np.inf], np.nan, inplace =True)
print(df)
df.dropna(inplace = True)
print(df)
return df
def make_plot(x, y, xlabel, ylabel, xrange, yrange, bins):
# x is an array-like
# y is an array-like
# xlabel is a string
# ylabel is a string
# xrange is either the string "auto" or a dict {"min": <your float/int here>, "max": <your float/int here>}
# yrange is either the string "auto" or a dict {"min": <your float/int here>, "max": <your float/int here>}
# bins is a dict {"x": <your int here>, "y": <your int here>}
n_binsx = bins["x"]
n_binsy = bins["y"]
c_map = 'gist_ncar_r'
# Create a Figure, which doesn't have to be square.
fig = plt.figure(layout='constrained')
# Create the main axes, leaving 25% of the figure space at the top and on the
# right to position marginals.
ax = fig.add_gridspec(top=0.75, right=0.75).subplots()
# The main axes' aspect can be fixed.
ax.set(aspect="auto")
ax_histx = ax.inset_axes([0, 1.05, 1.0, 0.25], sharex=ax)
ax_histy = ax.inset_axes([1.05, 0, 0.25, 1], sharey=ax)
# Draw the scatter plot and marginals.
# no labels
ax_histx.tick_params(axis="x", labelbottom=False)
ax_histy.tick_params(axis="y", labelleft=False)
if xrange != "auto":
n_binsx = np.linspace(xrange["min"], xrange["max"], num=n_binsx)
if yrange != "auto":
n_binsy = np.linspace(yrange["min"], yrange["max"], num=n_binsy)
if ylabel == "Sg/Sr (prompt)":
n_binsy = np.geomspace(np.min(y), np.max(y), num=n_binsy)
plt.yscale("log")
# the 2d hist plot:
h = ax.hist2d(x, y, bins = [n_binsx, n_binsy], cmap = c_map)
hist_values_2d = h[0]
mappable = h[3]
fig.colorbar(mappable, ax=ax, location='left')
# now determine nice limits by hand:
binwidth = 0.25
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
lim = (int(xymax/binwidth) + 1) * binwidth
bins = np.arange(-lim, lim + binwidth, binwidth)
ax_histx.hist(x, bins=n_binsx)
ax_histy.hist(y, bins=n_binsy, orientation='horizontal')
ax.set_xlabel(xlabel, fontsize = 20)
ax.set_ylabel(ylabel, fontsize = 20)
return fig, ax, hist_values_2d, ax_histx, ax_histy
def make_2dhist(args=None):
data_folder, plot_file = parse_args(args)
with open(plot_file) as yaml_file:
plot_dict = get_plot_dict(yaml_file)
for plot in plot_dict:
xlabel = plot_dict[plot]['xlabel']
ylabel = plot_dict[plot]['ylabel']
xrange = plot_dict[plot]['xrange']
print("Plotting (" + xlabel + ", " + ylabel + ")")
# check if coordinate is a calculation
if xlabel in calc_list:
print("Calculating " + xlabel)
x_df = get_calc(xlabel, data_folder)
else:
xfolder = plot_dict[plot]['xfolder']
print("Getting " + xlabel + " from " + xfolder)
xdata_folder = data_folder + "\\" + xfolder
x_df = get_data(xdata_folder)
if ylabel in calc_list:
print("Calculating " + ylabel)
y_df = get_calc(ylabel, data_folder)
else:
yfolder = plot_dict[plot]['yfolder']
print("Getting " + ylabel + " from " + yfolder)
ydata_folder = data_folder + "\\" + yfolder
y_df = get_data(ydata_folder)
# clean the data i.e. remove photon counts == 0, ignore NaN and inf, etc.
# x_df = clean_data(x_df)
# y_df = clean_data(y_df)
print(x_df[xlabel])
print(y_df[ylabel])
print(x_df)
# dataset = pd.concat([x_df, y_df[ylabel]], axis = 1)
if np.array_equal(x_df, y_df):
# same dataset, just take x_df
print("same data set")
dataset = x_df
elif ylabel in x_df.columns:
# conflict, assume we'd rather have the ylabel column in y_df
print(ylabel + " in x_df")
x_df.drop(ylabel, axis = 1, inplace=True)
dataset = x_df.join(y_df[ylabel])
else:
# no conflicts, just join
print("***No conflicts***")
dataset = x_df.join(y_df[ylabel])
print(dataset)
dataset = clean_data(dataset)
print(dataset)
make_plot(dataset[xlabel].to_numpy(), dataset[ylabel].to_numpy(), xlabel, xrange, ylabel)
plt.show()
================
File: feda_tools/core/utilities.py
================
from typing import Dict, List, Tuple
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pathlib
import os
import tqdm
import tttrlib
import fnmatch
def update_tttr_dict(
df: pd.DataFrame,
data_path: pathlib.Path,
tttrs: Dict[str, tttrlib.TTTR] = dict(),
file_type: str = "PTU"
):
for ff, fl in zip(df['First File'], df['Last File']):
try:
tttr = tttrs[ff]
except KeyError:
fn = str(data_path / ff)
tttr = tttrlib.TTTR(fn, file_type)
tttrs[ff] = tttr
return tttrs
def read_analysis(
paris_path : pathlib.Path,
paths: List[str] = ['bg4', 'bi4_bur'], #
file_endings: List[str] = ['bg4', 'bur'], #
file_type: str = "PTU"
) -> (pd.DataFrame, Dict[str, tttrlib.TTTR]):
info_path = paris_path / 'Info'
data_path = paris_path.parent
dfs = list()
for path, ending in zip(paths, file_endings):
frames = list()
for fn in sorted((paris_path / path).glob('*.%s' % ending)):
df = pd.read_csv(fn, sep='\t')
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
frames.append(df)
path_df = pd.concat(frames)
dfs.append(path_df)
df = pd.concat(dfs, axis=1)
# df = df.dropna()
# Loop through each column and attempt to convert to numeric
for column in df.columns:
try:
df[column] = pd.to_numeric(df[column])
except ValueError:
# Handle exceptions, e.g., if the column contains non-numeric values
print(f"Could not convert {column} to numeric")
tttrs = dict()
update_tttr_dict(df, data_path, tttrs, file_type)
return df, tttrs
def extract_unmasked_indices(masked_array):
unmasked_indices_lists = []
current_indices = []
for i, value in enumerate(masked_array):
if np.ma.is_masked(value):
if current_indices:
unmasked_indices_lists.append(current_indices)
current_indices = []
else:
current_indices.append(i)
if current_indices:
unmasked_indices_lists.append(current_indices)
return unmasked_indices_lists
================
File: feda_tools/gui/__main__.py
================
import sys
from PyQt6 import QtWidgets
from feda_tools.gui.threshold_adjustment import ThresholdAdjustmentWindow
class MainApplication(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('FEDA Tools GUI')
self.init_ui()
def init_ui(self):
# Set ThresholdAdjustmentWindow as the central widget
self.threshold_window = ThresholdAdjustmentWindow()
self.setCentralWidget(self.threshold_window)
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
main_window = MainApplication()
sys.exit(app.exec())
if __name__ == '__main__':
main()
================
File: feda_tools/gui/fit23_preview.py
================
from PyQt6 import QtWidgets, QtCore
from .widgets import MatplotlibCanvas
from feda_tools.core import model
import numpy as np
class Fit23PreviewWindow(QtWidgets.QWidget):
analysis_started = QtCore.pyqtSignal()
def __init__(self, data_ptu, data_irf, burst_index, chunk_size, fit_params, output_directory):
super().__init__()
self.data_ptu = data_ptu
self.data_irf = data_irf
self.burst_index = burst_index
self.chunk_size = chunk_size
self.fit_params = fit_params
self.output_directory = output_directory # Save the output directory
self.init_ui()
self.thread = None
self.worker = None
def init_ui(self):
self.setWindowTitle('Fit23 Preview')
layout = QtWidgets.QVBoxLayout()
# Matplotlib canvas
self.canvas = MatplotlibCanvas(self, width=5, height=4, dpi=100)
layout.addWidget(self.canvas)
# Fit23 Parameters Input
params_layout = QtWidgets.QFormLayout()
self.tau_input = QtWidgets.QDoubleSpinBox()
self.tau_input.setRange(0.0, 100.0)
self.tau_input.setDecimals(4)
self.tau_input.setValue(3.03) # Default value
self.tau_input.valueChanged.connect(self.update_preview)
self.gamma_input = QtWidgets.QDoubleSpinBox()
self.gamma_input.setRange(0.0, 100.0)
self.gamma_input.setDecimals(4)
self.gamma_input.setValue(0.02) # Default value
self.gamma_input.valueChanged.connect(self.update_preview)
self.r0_input = QtWidgets.QDoubleSpinBox()
self.r0_input.setRange(0.0, 100.0)
self.r0_input.setDecimals(4)
self.r0_input.setValue(0.38) # Default value
self.r0_input.valueChanged.connect(self.update_preview)
self.rho_input = QtWidgets.QDoubleSpinBox()
self.rho_input.setRange(0.0, 100.0)
self.rho_input.setDecimals(4)
self.rho_input.setValue(1.64) # Default value
self.rho_input.valueChanged.connect(self.update_preview)
params_layout.addRow('Initial Tau:', self.tau_input)
params_layout.addRow('Initial Gamma:', self.gamma_input)
params_layout.addRow('Initial r0:', self.r0_input)
params_layout.addRow('Initial rho:', self.rho_input)
layout.addLayout(params_layout)
# Run Fit23 Button
run_fit_button = QtWidgets.QPushButton('Run Fit23 with Current Parameters')
run_fit_button.clicked.connect(self.run_fit23)
layout.addWidget(run_fit_button)
# Run Analysis button
self.run_button = QtWidgets.QPushButton('Run Full Analysis')
layout.addWidget(self.run_button)
self.run_button.clicked.connect(self.start_analysis)
self.setLayout(layout)
self.plot_fit23(initial_plot=True)
def plot_fit23(self, initial_plot=False):
# Prepare data for the entire chunk
num_bins = 128
# Get micro_times from data_ptu
all_micro_times = self.data_ptu.micro_times
# Create histogram counts for the chunk
counts, _ = np.histogram(all_micro_times, bins=num_bins)
# Prepare counts_irf from data_irf
counts_irf, _ = np.histogram(self.data_irf.micro_times, bins=num_bins)
counts_irf_nb = counts_irf.copy()
counts_irf_nb[0:3] = 0
counts_irf_nb[10:66] = 0
counts_irf_nb[74:128] = 0
# Setup fit23 with default or provided parameters
macro_res = self.data_ptu.get_header().macro_time_resolution
g_factor = 1.04
l1_japan_corr = 0.0308
l2_japan_corr = 0.0368
if initial_plot:
initial_fit_params = np.array([
self.tau_input.value(),
self.gamma_input.value(),
self.r0_input.value(),
self.rho_input.value()
])
self.fit_params = initial_fit_params.copy()
else:
self.fit_params = np.array([
self.tau_input.value(),
self.gamma_input.value(),
self.r0_input.value(),
self.rho_input.value()
])
self.fit23_model = model.setup_fit23(
num_bins, macro_res, counts_irf_nb, g_factor, l1_japan_corr, l2_japan_corr
)
# Perform fitting
x0 = self.fit_params # Use current fit_params
fixed = np.array([0, 0, 1, 0])
try:
r2 = self.fit23_model(data=counts, initial_values=x0, fixed=fixed, include_model=True)
self.fit_params = r2['x'][:4]
except Exception as e:
QtWidgets.QMessageBox.warning(self, 'Fit23 Error', f'An error occurred during Fit23:\n{e}')
return
# Plot data and model
self.canvas.axes.clear()
self.canvas.axes.semilogy(counts / np.max(counts), label='Data')
self.canvas.axes.semilogy(counts_irf / np.max(counts_irf), label='IRF')
self.canvas.axes.semilogy(self.fit23_model.model / np.max(self.fit23_model.model), label='Model')
# Set y-axis limits
self.canvas.axes.set_ylim(0.001, 1)
# Set labels and legend
self.canvas.axes.set_ylabel('log(Counts)')
self.canvas.axes.set_xlabel('Channel Number')
self.canvas.axes.legend()
# Draw the canvas
self.canvas.draw()
def update_preview(self):
self.plot_fit23()
def run_fit23(self):
# Re-plot Fit23 with user-defined initial parameters
self.plot_fit23()
def start_analysis(self):
# Proceed to full analysis using the latest fit_params
from .process_analysis import ProcessAnalysisWindow
self.process_window = ProcessAnalysisWindow(
self.data_ptu, self.data_irf, self.burst_index, self.chunk_size, self.fit_params, self.output_directory
)
self.process_window.show()
self.analysis_started.emit()
self.close()
================
File: feda_tools/gui/process_analysis.py
================
from PyQt6 import QtWidgets, QtCore
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from .widgets import MatplotlibCanvas
from feda_tools.core import process as proc
from feda_tools.core import data as dat
from feda_tools.core import model
from feda_tools.data import save_results
import numpy as np
import pandas as pd
class Worker(QtCore.QObject):
progress_update = pyqtSignal(int)
plot_update = pyqtSignal()
finished = pyqtSignal()
error_occurred = pyqtSignal(str)
def __init__(self, data_ptu, data_irf, burst_index, chunk_size, fit_params, update_interval):
super().__init__()
self.data_ptu = data_ptu
self.data_irf = data_irf
self.burst_index = burst_index
self.chunk_size = chunk_size
self.fit_params = fit_params
self.update_interval = update_interval
self.sg_sr = []
self.mean_macro_time = []
self.tau_values = []
self.r_s_values = []
self.is_running = True
@QtCore.pyqtSlot()
def run(self):
try:
# Example parameters, adjust as needed
min_photon_count = 60
bg4_micro_time_min = 0
bg4_micro_time_max = 12499
g_factor = 1.04
l1_japan_corr = 0.0308
l2_japan_corr = 0.0368
bg4_bkg_para = 0
bg4_bkg_perp = 0
num_bins = 128
# Prepare data
total_bursts = len(self.burst_index)
self.progress_update.emit(0)
# Initialize fit23 model
counts_irf, _ = np.histogram(self.data_irf.micro_times, bins=num_bins)
counts_irf_nb = counts_irf.copy()
counts_irf_nb[0:3] = 0
counts_irf_nb[10:66] = 0
counts_irf_nb[74:128] = 0
macro_res = self.data_ptu.get_header().macro_time_resolution
fit23_model = model.setup_fit23(
num_bins, macro_res, counts_irf_nb, g_factor, l1_japan_corr, l2_japan_corr
)
# Processing bursts
for i, burst in enumerate(self.burst_index, 1):
if not self.is_running:
break # Allow for future extension to stop processing
# Process each burst individually
bi4_bur_df, bg4_df = proc.process_single_burst(