-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdash-layout.py
More file actions
362 lines (305 loc) · 14.1 KB
/
dash-layout.py
File metadata and controls
362 lines (305 loc) · 14.1 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
import streamlit as st
import numpy as np
import os
import sys
import matplotlib.pyplot as plt
from astropy.io import ascii
import importlib
import pandas as pd
from scipy.signal import savgol_filter
# ----------------------------------------------------------------------------------------- #
# Execute DustPOL-py
# ----------------------------------------------------------------------------------------- #
@st.cache_data(persist="disk",max_entries=100,show_spinner="Executing DustPOL-py ...")
def execute_DustPOL(U_rad, ngas, fmax, grain_type, grain_shape, amax, amin, rat_theory, ratd, Smax, Bfield, Ncl, p_plot_option):
dir_dustpol = os.getcwd()+'/DustPOL-py/'
sys.path.insert(1, dir_dustpol)
input_file = dir_dustpol + 'input.dustpol'
q = np.genfromtxt(input_file, skip_header=1, dtype=None, names=['names', 'params'],
comments='!', usecols=(0, 1), encoding='utf-8')
# Update parameters
param_updates = {
1: ratd,
7: U_rad,
11: ngas,
15: amin,
16: amax,
14: grain_type,
19: grain_shape,
20: Smax,
24: rat_theory,
25: fmax,
26: Bfield,
28: Ncl
}
for idx, value in param_updates.items():
q['params'][idx]=value
ascii.write(q, input_file, comment=True, overwrite=True)
import DustPOL_class, align, DustPOL_io
importlib.reload(DustPOL_io)
args = DustPOL_class.DustPOL()
A_per_Ngas=args.extinction()
if p_plot_option == 'Starlight Polarization':
w, psil, ptot = args.cal_pol_abs()
return [w, psil, A_per_Ngas] if composition_plot_option in ['Silicate', 'Astrodust'] else [w, ptot, A_per_Ngas]
elif p_plot_option == 'Thermal dust Polarization':
w, I_list, p_list = args.cal_pol_emi()
return [w, p_list[0], A_per_Ngas] if composition_plot_option in ['Silicate', 'Astrodust'] else [w,p_list[1],A_per_Ngas]
else: #Both
w, psil, ptot = args.cal_pol_abs()
w, I_list, p_list = args.cal_pol_emi()
return [w, psil, p_list[0], A_per_Ngas] if composition_plot_option in ['Silicate', 'Astrodust'] else [w, ptot, p_list[1], A_per_Ngas]
# ----------------------------------------------------------------------------------------- #
# Setup layout
# ----------------------------------------------------------------------------------------- #
st.set_page_config(page_title='Visualization DustPOL-py', page_icon=":shark:", layout='wide', initial_sidebar_state='expanded')
with open('style.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
_,cen,_=st.sidebar.columns([1,3,1])
# cen.image("dustpol-logo.png", use_column_width=True)
cen.image("dustpol-logo.png", use_container_width=True)
# st.sidebar.title('Visualization `DustPOL-py`')
# Header - plot option
st.sidebar.header('Degree of dust polarization')
p_plot_option = st.sidebar.selectbox('Please choose (starlight/emission polarizations)', ['Starlight Polarization', 'Thermal dust Polarization', 'Both'])
st.sidebar.divider()
# Header - grain size
st.sidebar.header('Grain size')
col1, col2 = st.sidebar.columns(2)
amax = col1.selectbox('Maximum size ($\\mu$m):', [0.1, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.5, 2.0], index=9)
amin = col2.selectbox('Minimum size ($\\mu$m):', np.linspace(3.1e-4,amax+0.01*amax,20), index=0, format_func=lambda x: '{:.1e}'.format(x))
st.sidebar.divider()
# Header - grain composition
st.sidebar.header('Dust grains')
col1, col2 = st.sidebar.columns(2)
composition_plot_option = col1.selectbox('Dust composition', ('Silicate', 'Silicate+Carbon', 'Astrodust'), index=2)
grain_type_dict = {
'Silicate': 'sil',
'Silicate+Carbon': 'sil+car',
'Astrodust': 'astro'
}
grain_type = grain_type_dict[composition_plot_option]
grain_shape_options = [0.3333] if composition_plot_option in ['Silicate', 'Silicate+Carbon'] else [1.4, 2.0, 3.0]
grain_shape = col2.selectbox('Grain shape', grain_shape_options)
st.sidebar.divider()
# Header - RAT theory
st.sidebar.header('Alignment Theory')
rat_theory = st.sidebar.selectbox('Select theory of RAT alignment', ('RAT', 'MRAT'))
# Sub-header -- MRAT
Bfield, Ncl = (np.nan, np.nan)
if rat_theory == 'MRAT':
st.sidebar.subheader('B-field strength')
Bfield = st.sidebar.number_input('B[$\\mu G$]', value=600.0, step=10.0)
st.sidebar.subheader('Number of iron cluster (Ncl)')
Ncl = st.sidebar.slider('Specify Ncl', 10., 1.e5, 10., format='%.1e')
# Sub-header -- RAT-D
ratd,Smax=(False,-1.e-99)
c1,c2=st.sidebar.columns(2)
with c1:
ratd = c1.checkbox('RAT-D')
if (ratd):
Smax=c2.selectbox('Smax',[1e5,1e6,1e7,1e8,1e9,1e10],format_func=lambda x: '{:.1e}'.format(x),index=3)
c1,c2=st.sidebar.columns(2)
with c1.expander("Explanation"):
st.write('''Turn on/off the rotational disruption -->> contrainning the $a_{\\rm max}$
''')
if (ratd):
with c2.expander("Explanation"):
st.write('''Maximum tensile strength of grain ($\\rm erg\\,cm^{-3}$) -- characterizing grain's porosity
''')
st.sidebar.divider()
button = st.sidebar.button("Clear All Caches")
if button:
st.cache_data.clear()
with st.sidebar.expander("explanation"):
st.write('To clear all memory caches. Caches are on your disk and it is recommended to clear them all after a while!')
# Row A
st.markdown('### Parameters')
col1, col2, col3 = st.columns(3)
U_rads = col1.multiselect('Multiselect radiation field (U)', list(np.around(np.arange(0.1,1,0.1),2))+list(np.arange(1,10.,1))+list(np.arange(10.,500.,20))+list(np.arange(500.,1020.,20)))
with col1.expander("See explanation"):
st.write('''
$U=\\frac{\\int_{\\lambda}u_{\\lambda}d\\lambda}{8.64\\times 10^{-13}\\,\\rm erg\\,cm^{-3}}$
with $u_{\\lambda}$ the radiation spectrum. For a typical interstellar medium, $U=1$.
''')
ngass = np.array(col2.multiselect('Multiselect gas volume density (ngas)', [1e1,1e2,1e3,1e4,1e5,1e6,1e7], format_func=lambda x: '{:.1e}'.format(x)))
with col2.expander("See explanation"):
st.write('''
n$_{\\rm gas}=n(H) + 2n(H2)+...$ is in unit of $\\rm cm^{-3}$
''')
if rat_theory == 'RAT':
fmaxs = col3.multiselect('Multiselect max. alignment efficiency (fmax)', [0.25, 0.5, 1.0])
else:
fmaxs=[0.0]
st.session_state.disable_opt = True
col3.multiselect('Select maximum alignment efficiency (fmax)', [],disabled=st.session_state.disable_opt)
with col3.expander("See explanation"):
st.write('''
$f_{\\rm max}$ is the maximum alignment efficiency. For MRAT theory, $f_{\\rm max}$ is estimated by the input values of Bfield strength and Ncl
''')
st.divider()
##declearing output file
output_abs={}
output_emi={}
output_ext={}
def plot_figures():
col_count = 8
if p_plot_option == 'Both':
c1, _ = st.columns((col_count, 1))
c3, _ = st.columns((col_count, 1))
else:
c1, _ = st.columns((col_count, 1))
fig1, ax1, fig2, ax2 = None, None, None, None
if p_plot_option in ['Starlight Polarization', 'Both']:
fig1, ax1 = plt.subplots(figsize=(10, 3))
ax1.set_xlabel('$\\rm wavelength\\,(\\mu m)$')
ax1.set_ylabel('$\\rm p_{ext}/N_{H}\\,(\\%/cm^{-2})$')
ax1.set_title('$\\rm Starlight\\,Polarization$',pad=20)
ax11=ax1.twinx()
ax11.set_ylabel('$\\rm A_{\\lambda}/N_{\\rm H}$')
if p_plot_option in ['Thermal dust Polarization', 'Both']:
fig2, ax2 = plt.subplots(figsize=(10, 3))
ax2.set_xlabel('$\\rm wavelength\\,(\\mu m)$')
ax2.set_ylabel('$\\rm p_{em}\\,(\\%)$')
ax2.set_title('$\\rm Thermal\\,Polarization$',pad=20)
ax22=ax2.secondary_yaxis('right')
ax22.set_ylabel('$\\rm p_{em}\\,(\\%)$')
first=True
for U_rad in U_rads:
for n_gas in ngass:
for f_max in fmaxs:
results = execute_DustPOL(U_rad, n_gas, f_max, grain_type, grain_shape, amax, amin, rat_theory, ratd, Smax, Bfield, Ncl,p_plot_option)
if p_plot_option == 'Both':
w, pext, pem, A_per_Ngas = results
pext = savgol_filter(pext,20,2) # smooth pext (for visualization) -- not physically affected
output_abs['wavelength(micron)'] = w*1e4
output_abs['p/NH(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = pext/n_gas
output_emi['wavelength(micron)'] = w*1e4
output_emi['pem(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = pem
output_ext['wavelength(micron)'] = w*1e4
output_ext['A_per_Ngas(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = A_per_Ngas
ax1.semilogx(w * 1e4, pext / n_gas, label=f'U={U_rad:.1f} -- n$_{{\\rm H}}$={n_gas:.1e} -- f$_{{\\rm max}}$={f_max:.2f}')
ax11.loglog(w * 1e4, A_per_Ngas,color='k',ls='--')
if (ratd):
ax11.loglog(w * 1e4, A_per_Ngas,ls='--')
else:
ax11.loglog(w * 1e4, A_per_Ngas,color='k',ls='--')
if (first):
ax11.loglog(w*1e-4,np.ones(len(w)),color='k',ls='-',label='$\\rm pol.\\,spectrum$')
ax11.loglog(w*1e-4,np.ones(len(w)),color='k',ls='--',label='$\\rm Extinction\\, curve$')
ax2.semilogx(w * 1e4, pem, label=f'U={U_rad:.1f} -- n$_{{\\rm H}}$={n_gas:.1e} -- f$_{{\\rm max}}$={f_max:.2f}')
elif p_plot_option == 'Starlight Polarization':
w, pext,A_per_Ngas = results
pext = savgol_filter(pext,20,2) # smooth pext (for visualization) -- not physically affected
output_abs['wavelength(micron)'] = w*1e4
output_abs['p/NH(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = pext/n_gas
output_ext['wavelength(micron)'] = w*1e4
output_ext['A_per_Ngas(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = A_per_Ngas
ax1.semilogx(w * 1e4, pext / n_gas, label=f'U={U_rad:.1f} -- n$_{{\\rm H}}$={n_gas:.1e} -- f$_{{\\rm max}}$={f_max:.2f}')
if (ratd):
ax11.loglog(w * 1e4, A_per_Ngas,ls='--')
else:
ax11.loglog(w * 1e4, A_per_Ngas,color='k',ls='--')
if (first):
ax11.loglog(w*1e-4,np.ones(len(w)),color='k',ls='-',label='$\\rm pol.\\,spectrum$')
ax11.loglog(w*1e-4,np.ones(len(w)),color='k',ls='--',label='$\\rm Extinction\\, curve$')
elif p_plot_option == 'Thermal dust Polarization':
w, pem, A_per_Ngas = results
output_emi['wavelength(micron)'] = w*1e4
output_emi['pem(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = pem
output_ext['wavelength(micron)'] = w*1e4
output_ext['A_per_Ngas(U=%.1f,ngas=%.1e,fmax=%.1f)'%(U_rad,n_gas,f_max)] = A_per_Ngas
ax2.semilogx(w * 1e4, pem, label=f'U={U_rad:.1f} -- n$_{{\\rm H}}$={n_gas:.1e} -- f$_{{\\rm max}}$={f_max:.2f}')
first=False
if ax1:
ax1.legend(frameon=False)
ax11.legend(bbox_to_anchor=(0.95,1.35),frameon=False)
ax11.set_ylim([1e-23,1e-20])
ax1.set_xlim([0.05, 5e4])
st.pyplot(fig1)
if ax2:
ax2.legend(frameon=False)
ax2.set_xlim([0.05, 5e4])
st.pyplot(fig2)
# ----------------------------------------------------------------------------------------- #
# Plots polarization spectra
# ----------------------------------------------------------------------------------------- #
st.markdown('### Visualizations')
plot_figures()
# ----------------------------------------------------------------------------------------- #
# ASCII files for downloading
# ----------------------------------------------------------------------------------------- #
st.divider()
st.markdown('### ASCII files')
@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv(index=False).encode("utf-8")
my_large_df_abs = pd.DataFrame(data=output_abs)
csv_abs = convert_df(my_large_df_abs)
my_large_df_emi = pd.DataFrame(data=output_emi)
csv_emi = convert_df(my_large_df_emi)
my_large_df_ext = pd.DataFrame(data=output_ext)
csv_ext = convert_df(my_large_df_ext)
if p_plot_option=='Both':
col_save1, col_save2, col_save3 = st.columns(3)
col_save1.download_button(
label="Download pext/NH as CSV",
data=csv_abs,
file_name="pabs.csv",
mime="text/csv",
)
col_save2.download_button(
label="Download pem as CSV",
data=csv_emi,
file_name="pemi.csv",
mime="text/csv",
)
col_save3.download_button(
label="Download ext_curve as CSV",
data=csv_ext,
file_name="ext_curve.csv",
mime="text/csv",
)
elif p_plot_option=='Starlight Polarization':
col_save1, col_save2 = st.columns(2)
col_save1.download_button(
label="Download pext/NH as CSV",
data=csv_abs,
file_name="pabs.csv",
mime="text/csv",
)
col_save2.download_button(
label="Download ext_curve as CSV",
data=csv_ext,
file_name="ext_curve.csv",
mime="text/csv",
)
elif p_plot_option=='Thermal dust Polarization':
col_save1, col_save2 = st.columns(2)
col_save1.download_button(
label="Download pem as CSV",
data=csv_emi,
file_name="pem.csv",
mime="text/csv",
)
col_save2.download_button(
label="Download ext_curve as CSV",
data=csv_ext,
file_name="ext_curve.csv",
mime="text/csv",
)
st.sidebar.markdown('''
---
Model details: please refer to \\
https://ui.adsabs.harvard.edu/abs/2020ApJ...896...44L \\
https://ui.adsabs.harvard.edu/abs/2021ApJ...906..115T \\
https://www.aanda.org/articles/aa/pdf/2024/09/aa50127-24.pdf
''')
# st.sidebar.divider()
st.sidebar.markdown('''
---
Created with ❤️ by `Le N. Tram`
Model: https://github.com/lengoctram/DustPOL-py (version 1.6) \\
Contact: nle 'at' strw 'dot' leidenuniv 'dot' nl
''')