-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
123 lines (91 loc) · 2.89 KB
/
run.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
import matplotlib.pyplot as plt
import pandas as pd
from app.trs_isotopes_analyser import TRSIsotopesAnalyser
from app.config import load_config
from app.isotope_data import IsotopeData
from typing import Dict, Callable
import typer
ia : TRSIsotopesAnalyser
config: Dict
sort_by: Callable[[IsotopeData], int]
def main(config_path: str):
global config
global ia
global sort_by
config = load_config(config_path)
plt.rcParams['font.size'] = config['plot_font']['size']
plt.rcParams['font.family'] = config['plot_font']['family']
ia = TRSIsotopesAnalyser(
config['sites_path'],
config['isotopes_path'],
config['climate_path']
)
sort_by = lambda x: config['site_to_order'][x.site.code]
cli = typer.Typer(callback=main)
def savefig(name):
plt.savefig(
f'{config["save_path"]}/{name}.png',
facecolor='white',
transparent=False,
dpi=300,
bbox_inches='tight'
)
plt.close()
@cli.command()
def save_zscore_trends():
for isotope in config['isotopes']:
ia.plot_zscore_trends(
isotope,
config['isotope_to_name'][isotope],
config['sites_trends'],
**config['zscore_trends_kwargs']
)
savefig(f'trend_{isotope}')
r2_and_pvalues = ia.get_trends_r2(isotope, config['sites_trends'])
r2_and_pvalues = pd.DataFrame(r2_and_pvalues)
r2_and_pvalues.insert(0, 'Stat', ('R^2', 'p-value'))
r2_and_pvalues.to_csv(f'{config["save_path"]}/trend_r2_{isotope}.csv', index=False)
@cli.command()
def save_heatmaps():
for clim_index in config['clim_indexes']:
hm = ia.heatmap(
config['isotopes'],
clim_index,
range(9, 13),
range(1, 9),
config['isotope_to_color'],
sort_by=sort_by,
**config['heatmap_kwargs']
)
for yticklabel in hm.ax_heatmap.get_yticklabels():
yticklabel.set_color(config['site_to_color'][yticklabel.get_text().split('_')[1]])
savefig(f'heatmap_{clim_index}')
@cli.command()
def save_mannwhitneyu_heatmaps():
for isotope in config['isotopes']:
ia.mannwhitneyu_heatmap(
isotope,
config['isotope_to_name'][isotope],
sort_by,
config['site_to_color']
)
savefig(f'mannwhitneyu_heatmap_{isotope}')
@cli.command()
def save_boxplots():
for isotope in config['isotopes']:
ia.boxplot(
isotope,
sort_by,
config['isotope_to_title'][isotope],
subplots_kws={'figsize': (10, 5), 'dpi': 300},
site_to_color=config['site_to_color']
)
savefig(f'boxplot_{isotope}')
@cli.command()
def save_all():
save_zscore_trends()
save_heatmaps()
save_mannwhitneyu_heatmaps()
save_boxplots()
if __name__ == '__main__':
cli()