-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot-histogram.py
104 lines (86 loc) · 3.36 KB
/
plot-histogram.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
import os
import json
import click
from click import echo
from core.site import histogram
from core.util import prompt, folder
from core.prompts.progress import prompt_folder_progress
from core.prompts.intro import prompt_plot_histograms_intro
def plot_histogram():
"""
Produce histogram sheets and single histograms
"""
prompt_plot_histograms_intro()
# 1. Customize the bin width if needed
echo("Would you like to customize the histogram width?")
is_custom_design = click.confirm("(Default: Y)", default=True)
if is_custom_design:
min_x = click.prompt(
"(1/3) Enter the minimum value for the x-axis",
type=float,
)
max_x = click.prompt(
"(2/3) Enter the maximum value for the x-axis",
type=float,
)
bin_width = click.prompt(
"(3/3) Enter the bin width",
type=float,
)
# 2. Choose folders contianing .json
script_path = os.path.dirname(os.path.abspath(__file__))
dir_names_with_json = folder.get_json_dir_names(script_path)
selected_dirs = prompt.get_user_input_folder_processing(
dir_names_with_json, ".json"
)
num_selected_dirs = len(selected_dirs)
if not dir_names_with_json:
click.echo("No folders containing .json files were found.")
return
# 3. Plot
for idx, dir_name in enumerate(selected_dirs.values(), start=1):
dir_path = os.path.join(script_path, dir_name, "output")
prompt_folder_progress(idx, dir_name, num_selected_dirs)
element_pair_dict = None
site_pair_dict = None
for file_name in os.listdir(dir_path):
if file_name.endswith("_element_pairs.json") or file_name.endswith(
"_site_pairs.json"
):
json_file_path = os.path.join(dir_path, file_name)
echo(f"Processing {os.path.basename(json_file_path)}")
with open(json_file_path, "r") as json_file:
data = json.load(json_file)
if "_element_pairs.json" in file_name:
element_pair_dict = data
elif "_site_pairs.json" in file_name:
site_pair_dict = data
# Ensure that both dictionaries are not None before proceeding
histogram_output_dir = os.path.join(script_path, dir_name)
if site_pair_dict is not None and element_pair_dict is not None:
if not is_custom_design:
histogram.draw_histograms(
site_pair_dict,
element_pair_dict,
histogram_output_dir,
)
if is_custom_design:
distances = [min_x, max_x]
bins = histogram.get_bins_from_distances(bin_width, distances)
histogram.plot_histograms(
site_pair_dict,
histogram_output_dir,
bins,
distances,
"histogram_site_pair",
)
histogram.plot_histograms(
element_pair_dict,
histogram_output_dir,
bins,
distances,
"histogram_element_pair",
)
echo("\nCongratulations! All histograms are re-generated.")
if __name__ == "__main__":
plot_histogram()