-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsgtab.py
383 lines (346 loc) · 13 KB
/
sgtab.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
#import dash
import dash_table
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
from dash.dependencies import Input, Output
import numpy as np
from extinction import ccm89, apply, remove
import pandas as pd
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
from app import app
plotly.tools.set_credentials_file(username='ahowell', api_key='IV3kTKFQTTugQOjv5Yga')
#external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
##### Functions #########
#class File_Splitter:
# # class that splits filepaths into type, file, SN, and epoch
# def __init__(self, path):
# separated_path=path.split('/')
# self.type = separated_path[1]
# self.file = separated_path[2]
# separated_sn=self.file.split('.')
# self.sn = separated_sn[0]
# self.epoch = separated_sn[1]
def read_sfo(sfo_filename):
#initialize
sfo_dictionary = {}
newlist = []
df=pd.DataFrame(columns=['file','SN','Epoch','Type','S','z','Galaxy','Av','C','F','gfrac','sfrac'])
opened_file=open(sfo_filename,"r")
rows = opened_file.readlines()
for row in rows:
#read footer
if row[0] == ';':
pair = row.split('=',1)
#Get rid of comment lines and whitespace
pair[0]=pair[0].strip(';')
pair[0]=pair[0].strip()
pair[1]=pair[1].strip()
#convert numbers to floats but leave strings
try:
pair[1]=float(pair[1])
except:
pass
sfo_dictionary.update({pair[0]:pair[1]})
else:
#read main part of file
[file,S,z,Galaxy,Av,C,F,gfrac,sfrac] = row.split()
[category,Type,filename]=separated_file=file.split('/')
S=float(S)
z=float(z)
Av=float(Av)
C=float(C)
F=float(F)
gfrac=float(gfrac)
sfrac=float(sfrac)
[SN,Epoch,template_filetype]=filename.split('.')
if S < 999:
list=[file,SN,Epoch,Type,S,z,Galaxy,Av,C,F,gfrac,sfrac]
newlist.append(list)
#df2=pd.DataFrame([x for x in list],columns=['file','SN','Epoch','Type','S','z','Galaxy','Av','C','F','gfrac','sfrac'])
#df.append(df2)
#print(newlist)
df = pd.DataFrame(newlist,columns=['file','SN','Epoch','Type','S','z','Galaxy','Av','C','F','gfrac','sfrac'])
return [sfo_dictionary,df]
def read_spectrum(spectrum_path):
return pd.read_csv(spectrum_path, delim_whitespace=True, names=['wav','flux'],header=None)
def normalize_spectrum(spectrum):
#Normalize flux
#Need to zerostrip
median=np.median(spectrum['flux'])
spectrum['flux']=spectrum['flux']/median
return spectrum
def scale_spectrum(spectrum,scale_value):
#Scale spectrum to scale_value
spectrum['flux']=spectrum['flux']*scale_value
return spectrum
def binspec(spectrum,start_wavelength,end_wavelength,wavelength_bin):
#rebin spextrum given starting wavelength, ending wavelength, and bin
#returns a dataframe with columns "wav" and "flux"
#rightmost wavelength is not inclusive
#left and right are what gets filled in if begginning or ending wavelengths are out of bounds
binned_wavelength=np.arange(start_wavelength,end_wavelength,wavelength_bin)
binned_flux = np.interp(binned_wavelength,spectrum["wav"],spectrum["flux"],left=np.nan,right=np.nan)
zipped=zip(binned_wavelength,binned_flux)
binned_df=pd.DataFrame(zipped,columns=["wav","flux"])
return binned_df
def redden_scale_template(template,c,Av,z):
#need to keep reddening in unredshifted frame
#template is a pandas dataframe
# c is a constant read in from sfo file
#z is redshift
#Av is read in from sfo file
#uses extinction module, which requires numpy input
unredshifted_wav = template["wav"].to_numpy() / (1.0 + z)
np_template_flux = template["flux"].to_numpy()
output_flux=apply(ccm89(unredshifted_wav, Av, 3.1), np_template_flux)
output_df = template.copy()
output_df["wav"] = template["wav"]
output_df["flux"] = c * pd.Series(output_flux)
return output_df
###### Main Program ##########
'''
IDL code to scale spectra from sggui.pro
;; need to keep reddening in unredshifted frame
zp1= 1.0 + b.z[I]
unredshiftedw=obsw/zp1
redlawf=mkafromlam(unredshiftedw,b.rv)
obsminusgal=dblarr(N_ELEMENTS(obsf))
exttempf=dblarr(N_ELEMENTS(obsf))
FOR P=0,N_ELEMENTS(obsf)-1 DO $
obsminusgal[p]=(obsf[p]-b.ff[I]*galf[p])
FOR P=0,N_ELEMENTS(obsf)-1 DO $
exttempf[p]=b.cc[I]*tempf[p]*10^(-b.av[I]*redlawf[p]/2.5)
#redlaw is an array of A's given Rv
'''
sfo_filename = 'SN2019ein.p02.sfo'
[sfo_dictionary,df]=read_sfo(sfo_filename)
observation_filename=sfo_dictionary["o"].split('/')[-1]
observed_spectrum = read_spectrum(observation_filename)
normalized_observed_spectrum = normalize_spectrum(observed_spectrum)
#app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
#app = dash.Dash(__name__)
#app.layout = html.Div([
sgbody = html.Div([
#html.Div(id='datatable-interactivity-container',figure={"layout":go.Layout{width=500}}),
html.Div(id='datatable-interactivity-container'),
dbc.Row([
dbc.Col(
html.Label(
'Binning (A):',
style={
'fontWeight': 'bold',
},
),
width='auto'
),
dbc.Col(
dcc.Input(
id='bin_input',
size='30px',
type='number',
value=sfo_dictionary["disp"],
),
width='auto'
),
dbc.Col(
html.Label(
'Show graphs:',
style={
'fontWeight': 'bold',
},
),
width='auto'
),
dbc.Col(
dcc.Checklist(
id='plotting_checklist',
options=[
{'label': 'Observation - Galaxy', 'value': 'omg'},
{'label': 'Template', 'value': 'tem'},
{'label': 'Galaxy', 'value': 'gal'},
{'label': 'Observation', 'value': 'obs'},
{'label': 'Normalized Template', 'value': 'ute'}
],
value=['omg', 'tem'],
#style={'columnCount': 6},
#style={'padding': '10px'}
labelStyle={'display': 'inline-block'}
),
width='auto'
),
]),
dash_table.DataTable(
id='datatable-interactivity',
columns=[
{"name": i, "id": i, "deletable": False} for i in df.columns[1:]
],
style_header={
#'backgroundColor': 'lightgrey',
'fontWeight': 'bold',
'textAlign': 'center'
},
style_cell_conditional=[
{'if': {'column_id': 'SN'},
'textAlign': 'left',
'width': '100px'},
{'if': {'column_id': 'Epoch'},
'textAlign': 'left',
'width': '100px'},
{'if': {'column_id': 'Type'},
'textAlign': 'left',
'width': '100px'},
{'if': {'column_id': 'S'},
'width': '100px'},
{'if': {'column_id': 'z'},
'width': '100px'},
{'if': {'column_id': 'Galaxy'},
'textAlign': 'left',
'width': '100px'},
{'if': {'column_id': 'Av'},
'width': '100px'},
{'if': {'column_id': 'C'},
'width': '100px'},
{'if': {'column_id': 'F'},
'width': '100px'},
{'if': {'column_id': 'gfrac'},
'width': '100px'},
{'if': {'column_id': 'sfrac'},
'width': '100px'}
],
data=df.to_dict('records'),
row_selectable = 'single',
#editable=True,
filter_action='native',
sort_action='native',
sort_mode='single',
row_deletable=True,
#selected_rows=[],
#n_fixed_rows=2,
#style_as_list_view=True,
style_cell={'width': '30px'},
#style_data={
# 'width': '100px',
# 'maxWidth': '100px',
# 'minWidth': '100px',
#},
fixed_rows={'headers': True},
style_table={
'maxHeight': '500px',
'overflowY': 'auto',
'border': 'thin lightgrey solid'
},
page_action='native',
#pagination_mode="fe",
#pagination_settings={
# "current_page": 0,
# "page_size": 10,
#},
)
])
@app.callback(
Output('datatable-interactivity-container', "children"),
[Input('datatable-interactivity', "derived_virtual_data"),
Input('datatable-interactivity', "derived_virtual_selected_rows"),Input('bin_input', 'value'),Input('plotting_checklist', 'value')])
def update_graphs(rows, derived_virtual_selected_rows,bin_input,plotting_checklist):
# When the table is first rendered, `derived_virtual_data` and
# `derived_virtual_selected_rows` will be `None`. This is due to an
# idiosyncracy in Dash (unsupplied properties are always None and Dash
# calls the dependent callbacks when the component is first rendered).
# So, if `rows` is `None`, then the component was just rendered
# and its value will be the same as the component's dataframe.
# Instead of setting `None` in here, you could also set
# `derived_virtual_data=df.to_rows('dict')` when you initialize
# the component.
if derived_virtual_selected_rows is None:
derived_virtual_selected_rows = []
#prevent division by zero
if bin_input == 0:
bin_input = 1
if plotting_checklist is None:
plotting_checklist = []
dff = df if rows is None else pd.DataFrame(rows)
#derived_virtual_selected_rows is an immutable list
selection = 0
for i in derived_virtual_selected_rows:
selection = int(i)
#make a dictionary out of selected row
selected_row = dff.iloc[selection].to_dict()
template_path = selected_row["file"]
template_spectrum = read_spectrum(template_path)
normalized_template_spectrum = normalize_spectrum(template_spectrum)
reddened_scaled_template = redden_scale_template(normalized_template_spectrum,selected_row["C"],selected_row["Av"],selected_row["z"])
scaled_template_spectrum = normalized_template_spectrum.copy()
scaled_template_spectrum["flux"] = selected_row["C"] * scaled_template_spectrum["flux"]
#Get, normalized, scale Galaxy spectrum
galaxy_path = "gal/" + selected_row["Galaxy"]
galaxy_spectrum = read_spectrum(galaxy_path)
normalized_galaxy_spectrum = normalize_spectrum(galaxy_spectrum)
scaled_galaxy_spectrum = scale_spectrum(normalized_galaxy_spectrum,selected_row["F"])
#bin
beginw=4000
endw=8000
bin_wav=bin_input
binned_galaxy = binspec(scaled_galaxy_spectrum,beginw,endw,bin_wav)
binned_template = binspec(normalized_template_spectrum,beginw,endw,bin_wav)
binned_observed = binspec(normalized_observed_spectrum,beginw,endw,bin_wav)
#Calcluate Observation Minus Galaxy
ObsMinusGal = binned_observed.copy()
ObsMinusGal["flux"] = ObsMinusGal["flux"] - binned_galaxy["flux"]
plot_label={'obs': observation_filename,
'gal': selected_row["Galaxy"],
'omg': observation_filename + " - " + selected_row["Galaxy"],
'tem': selected_row["SN"]+" "+selected_row["Epoch"],
'ute': 'Scaled template'
}
traces = []
for i in plotting_checklist:
if i == 'obs':
spectrum_to_plot = normalized_observed_spectrum
elif i == 'omg':
spectrum_to_plot = ObsMinusGal
elif i == 'gal':
spectrum_to_plot = scaled_galaxy_spectrum
elif i == 'tem':
spectrum_to_plot = reddened_scaled_template
elif i == 'ute':
spectrum_to_plot = scaled_template_spectrum
traces.append(go.Scatter(
x=spectrum_to_plot["wav"],
y=spectrum_to_plot["flux"],
mode='lines',
name=plot_label[i]
),
)
#spectra_plots=[]
#for i in plotting_checklist:
# spectra.plots.append(plotfiller(i))
#print(spectrum_to_plot,traces)
return [
dcc.Graph(
id="SNgraph",
figure={
'data': traces,
'layout': go.Layout(
xaxis=dict(
title= "Wavelength",
tickformat=".0f"
),
yaxis=dict(
title= 'Normalized Flux'
),
#width=1000,
showlegend=True,
legend={'x': 0.9, 'y': 0.95},
)
}
)
# check if column exists - user may have deleted it
# If `column.deletable=False`, then you don't
# need to do this check.
#for column in ["pop"] if column in dff
]
#if __name__ == '__main__':
# app.run_server(debug=True)