Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Djones tasks #222

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/host/plotting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ def scale_image(image_data):

def plot_image(image_data, figure):
# sometimes low image mins mess up the plotting

perc01 = np.nanpercentile(image_data, 1)

image_data = np.nan_to_num(image_data, nan=perc01)
image_data = image_data + abs(np.amin(image_data)) + 0.1

scaled_image = scale_image(image_data)

figure.image(image=[scaled_image])
figure.image(
image=[scaled_image],
Expand Down Expand Up @@ -172,16 +174,16 @@ def plot_cutout_image(
plotting_kwargs=host_kwargs,
plotting_func=fig.scatter,
)

if global_aperture.exists():
filter_name = global_aperture[0].cutout.filter.name
plot_aperture(
fig,
global_aperture[0].sky_aperture,
wcs,
plotting_kwargs={
"fill_alpha": 0.1,
"line_color": "green",
"legend_label": f"Global Aperture ({filter_name})",
"legend_label": f"Global Aperture ({title})",
},
)

Expand Down Expand Up @@ -213,6 +215,7 @@ def plot_cutout_image(
fig.ygrid.visible = False

plot_image(image_data, fig)

script, div = components(fig)
return {"bokeh_cutout_script": script, "bokeh_cutout_div": div}

Expand Down Expand Up @@ -260,6 +263,7 @@ def asinh_to_maggies(x):
x_axis_label="Wavelength [Angstrom]",
y_axis_label="Flux",
)

if len(flux):
fig.y_range = Range1d(-0.05 * np.max(flux), 1.5 * np.max(flux))
fig.x_range = Range1d(np.min(wavelength) * 0.5, np.max(wavelength) * 1.5)
Expand Down Expand Up @@ -371,6 +375,7 @@ def asinh_to_maggies(x):
fig.scatter(pwave, maggies_to_mJy(model_data["phot"]), size=10)

fig.legend.location = "top_left"

script, div = components(fig)
return {f"bokeh_sed_{type}_script": script, f"bokeh_sed_{type}_div": div}

Expand Down
38 changes: 19 additions & 19 deletions app/host/prospector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
from .models import hdf5_file_path
from .photometric_calibration import mJy_to_maggies ##jansky_to_maggies

all_filters = [filt for filt in Filter.objects.all().select_related()]
trans_curves = [f.transmission_curve() for f in all_filters]


# add redshift scaling to agebins, such that
# t_max = t_univ
Expand Down Expand Up @@ -89,9 +92,14 @@ def build_obs(transient, aperture_type, use_mag_offset=True):

"""

photometry = AperturePhotometry.objects.filter(
transient=transient, aperture__type__exact=aperture_type
).filter(Q(is_validated="true") | Q(is_validated="contamination warning"))
photometry = (
AperturePhotometry.objects.filter(
transient=transient, aperture__type__exact=aperture_type
)
.filter(Q(is_validated="true") | Q(is_validated="contamination warning"))
.prefetch_related()
)
filter_names = photometry.values_list("filter__name", flat=True)

if not photometry.exists():
raise ValueError(f"No host photometry of type {aperture_type}")
Expand All @@ -108,13 +116,12 @@ def build_obs(transient, aperture_type, use_mag_offset=True):

filters, flux_maggies, flux_maggies_error = [], [], []

for filter in Filter.objects.all():
# if 'PanSTARRS' in filter.name or 'DES' in filter.name: continue
for filter, trans_curve in zip(all_filters, trans_curves):
try:
datapoint = photometry.get(filter=filter)
except AperturePhotometry.DoesNotExist:
# sometimes data just don't exist, we can ignore
continue
if filter.name in filter_names:
datapoint = photometry.get(filter=filter)
else:
continue
except AperturePhotometry.MultipleObjectsReturned:
raise

Expand All @@ -136,7 +143,8 @@ def build_obs(transient, aperture_type, use_mag_offset=True):
raise ValueError(
f"aperture_type must be 'global' or 'local', currently set to {aperture_type}"
)
wave_eff = filter.transmission_curve().wave_effective

wave_eff = trans_curve.wave_effective
ext_corr = extinction.fitzpatrick99(np.array([wave_eff]), mwebv * 3.1, r_v=3.1)[
0
]
Expand All @@ -158,7 +166,7 @@ def build_obs(transient, aperture_type, use_mag_offset=True):
if flux_mwcorr / fluxerr_mwcorr < 3:
continue

filters.append(filter.transmission_curve())
filters.append(trans_curve)
flux_maggies.append(mJy_to_maggies(flux_mwcorr * 10 ** (-0.4 * mag_offset)))
flux_maggies_error.append(
mJy_to_maggies(fluxerr_mwcorr * 10 ** (-0.4 * mag_offset))
Expand Down Expand Up @@ -716,17 +724,9 @@ def build_model(observations):
Construct all model components
"""

# model_params = TemplateLibrary["parametric_sfh"]
# model_params.update(TemplateLibrary["nebular"])
# model_params["zred"]["init"] = observations["redshift"]
# model = SpecModel(model_params)
model = build_model_nonparam(observations)

# new SPS model
# sps = CSPSpecBasis(zcontinuous=1)
sps = FastStepBasis(zcontinuous=2, compute_vega_mags=False)
noise_model = (None, None)

return {"model": model, "sps": sps, "noise_model": noise_model}


Expand Down
16 changes: 13 additions & 3 deletions app/host/transient_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def _prerequisites(self):
"""
Only prerequisite is that the host match task is not processed.
"""
return {"Host match": "not processed", "Cutout download": "processed"}
return {
"Host match": "not processed",
"Cutout download": "processed",
"Transient MWEBV": "processed",
}

@property
def task_name(self):
Expand Down Expand Up @@ -92,7 +96,10 @@ def _prerequisites(self):
"""
Only prerequisite is that the transient MWEBV task is not processed.
"""
return {"Transient MWEBV": "not processed"}
return {
"Transient MWEBV": "not processed",
"Transient information": "processed",
}

@property
def task_name(self):
Expand Down Expand Up @@ -612,7 +619,10 @@ class TransientInformation(TransientTaskRunner):
"""Task Runner to gather information about the Transient"""

def _prerequisites(self):
return {"Transient information": "not processed"}
return {
"Transient information": "not processed",
"Cutout download": "processed",
}

@property
def task_name(self):
Expand Down
45 changes: 26 additions & 19 deletions app/host/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import django_filters
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import user_passes_test
Expand Down Expand Up @@ -36,29 +38,31 @@
from silk.profiling.profiler import silk_profile


def filter_transient_categories(qs, value):
def filter_transient_categories(qs, value, task_register=None):
if task_register is None:
task_register = TaskRegister.objects.all()
if value == "Transients with Basic Information":
qs = qs.filter(
pk__in=TaskRegister.objects.filter(
pk__in=task_register.filter(
task__name="Transient information", status__message="processed"
).values("transient")
)
elif value == "Transients with Matched Hosts":
qs = qs.filter(
pk__in=TaskRegister.objects.filter(
pk__in=task_register.filter(
task__name="Host match", status__message="processed"
).values("transient")
)
elif value == "Transients with Photometry":
qs = qs.filter(
Q(
pk__in=TaskRegister.objects.filter(
pk__in=task_register.filter(
task__name="Local aperture photometry",
status__message="processed",
).values("transient")
)
| Q(
pk__in=TaskRegister.objects.filter(
pk__in=task_register.filter(
task__name="Global aperture photometry",
status__message="processed",
).values("transient")
Expand All @@ -67,13 +71,13 @@ def filter_transient_categories(qs, value):
elif value == "Transients with SED Fitting":
qs = qs.filter(
Q(
pk__in=TaskRegister.objects.filter(
pk__in=task_register.filter(
task__name="Local host SED inference",
status__message="processed",
).values("transient")
)
| Q(
pk__in=TaskRegister.objects.filter(
pk__in=task_register.filter(
task__name="Global host SED inference",
status__message="processed",
).values("transient")
Expand All @@ -82,9 +86,9 @@ def filter_transient_categories(qs, value):
elif value == "Finished Transients":
qs = qs.filter(
~Q(
pk__in=TaskRegister.objects.filter(
~Q(status__message="processed")
).values("transient")
pk__in=task_register.filter(~Q(status__message="processed")).values(
"transient"
)
)
)

Expand Down Expand Up @@ -231,7 +235,6 @@ def results(request, slug):
global_aperture_photometry = AperturePhotometry.objects.filter(
transient=transient, aperture__type__exact="global", flux__isnull=False
).filter(Q(is_validated="true") | Q(is_validated="contamination warning"))

contam_warning = (
True
if len(global_aperture_photometry.filter(is_validated="contamination warning"))
Expand Down Expand Up @@ -265,6 +268,7 @@ def results(request, slug):
global_sed_obj[0].__dict__[f"log_{param}_84"],
),
)

if local_sed_obj.exists():
local_sed_file = local_sed_obj[0].posterior.name
else:
Expand All @@ -282,7 +286,6 @@ def results(request, slug):
filter_.name: ("yes" if filter_.name in filters else "no")
for filter_ in all_filters
}

if request.method == "POST":
form = ImageGetForm(request.POST, filter_choices=filters)
if form.is_valid():
Expand Down Expand Up @@ -310,9 +313,10 @@ def results(request, slug):
bokeh_context = plot_cutout_image(
cutout=cutout,
transient=transient,
global_aperture=global_aperture,
local_aperture=local_aperture,
global_aperture=global_aperture.prefetch_related(),
local_aperture=local_aperture.prefetch_related(),
)

bokeh_sed_local_context = plot_sed(
transient=transient,
type="local",
Expand All @@ -338,13 +342,12 @@ def results(request, slug):
is_warning = False
for u in transient.taskregister_set.all().values_list("user_warning", flat=True):
is_warning |= u

context = {
**{
"transient": transient,
"form": form,
"local_aperture_photometry": local_aperture_photometry,
"global_aperture_photometry": global_aperture_photometry,
"local_aperture_photometry": local_aperture_photometry.prefetch_related(),
"global_aperture_photometry": global_aperture_photometry.prefetch_related(),
"filter_status": filter_status,
"local_aperture": local_aperture,
"global_aperture": global_aperture,
Expand All @@ -358,7 +361,6 @@ def results(request, slug):
**bokeh_sed_local_context,
**bokeh_sed_global_context,
}

return render(request, "results.html", context)


Expand Down Expand Up @@ -415,6 +417,9 @@ def acknowledgements(request):
def home(request):
analytics_results = {}

task_register_qs = TaskRegister.objects.filter(
status__message="processed"
).prefetch_related()
for aggregate, qs_value in zip(
[
"Basic Information",
Expand All @@ -430,7 +435,9 @@ def home(request):
],
):
analytics_results[aggregate] = len(
filter_transient_categories(Transient.objects.all(), qs_value)
filter_transient_categories(
Transient.objects.all(), qs_value, task_register=task_register_qs
)
)

# transients = TaskRegisterSnapshot.objects.filter(
Expand Down