Skip to content

Commit

Permalink
Merge branch 'elastic_format_data' of https://github.com/nkhadka21/slsim
Browse files Browse the repository at this point in the history
 into elastic_format_data

merged to remote
  • Loading branch information
nkhadka21 committed Dec 5, 2024
2 parents 07e860f + 464abff commit f30e29f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
28 changes: 15 additions & 13 deletions slsim/LsstSciencePipeline/util_lsst.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ def opsim_variable_lens_injection(
def optimized_transient_event_time_mjd(
mjd_times, lightcurve_range=(-50, 100), min_points=100, optimized_cadence=True
):
"""Finds a transient start time such that at least min_points fall within the light
curve range. If no points meet the requirement for the minimum number of observations,
a start point will be randomly selected from the given observation times. Additionally,
if optimized_cadence is set to False, the start point will also be chosen randomly.
"""Finds a transient start time such that at least min_points fall within
the light curve range. If no points meet the requirement for the minimum
number of observations, a start point will be randomly selected from the
given observation times. Additionally, if optimized_cadence is set to
False, the start point will also be chosen randomly.
:param mjd_times: List or array of MJD times.
:param lightcurve_range: Range of lightcurve in days (e.g., (-50,
Expand All @@ -249,11 +250,12 @@ def optimized_transient_event_time_mjd(
points_in_range = np.sum((mjd_times >= start) & (mjd_times <= end))

if points_in_range >= min_points:
start_point=zero_point_mjd # Return the first valid zero_point_mjd
start_point = zero_point_mjd # Return the first valid zero_point_mjd
else:
warning_msg = (
"In the given observations, there is no observation window which contains"
" %s observational points. So, random start point is choosen" % min_points
"In the given observations, there is no observation window which contains"
" %s observational points. So, random start point is choosen"
% min_points
)
warnings.warn(warning_msg, category=UserWarning, stacklevel=2)
start_point = transient_event_time_mjd(min_mjd=min_mjd, max_mjd=max_mjd)
Expand Down Expand Up @@ -296,8 +298,8 @@ def transient_data_with_cadence(
:param transform_pix2angle: Transformation matrix (2x2) of pixels
into coordinate displacements.
:param num_pix: number of pixel for the image.
:param optimized_cadence: Boolean. If True, search for the time interval where given
minimum number of observation points occur.
:param optimized_cadence: Boolean. If True, search for the time
interval where given minimum number of observation points occur.
:param min_points: minimum number of observations in a certain time
window.
:param noise: Boolean. If True, a gaussian noise is added to the
Expand All @@ -314,10 +316,10 @@ def transient_data_with_cadence(
min_lc_time = min(lens_class.source[0].lightcurve_time)
max_lc_time = max(lens_class.source[0].lightcurve_time)
start_point_mjd_time = optimized_transient_event_time_mjd(
mjd_times=copied_exposure_data["obs_time"],
lightcurve_range=(min_lc_time, max_lc_time),
min_points=min_points,
optimized_cadence=optimized_cadence
mjd_times=copied_exposure_data["obs_time"],
lightcurve_range=(min_lc_time, max_lc_time),
min_points=min_points,
optimized_cadence=optimized_cadence,
)
obs_time_in_days = convert_mjd_to_days(
copied_exposure_data["obs_time"], start_point_mjd_time
Expand Down
31 changes: 17 additions & 14 deletions tests/test_LsstSciencePipeline/test_opsim_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
opsim_variable_lens_injection,
optimized_transient_event_time_mjd,
transient_data_with_cadence,
extract_lightcurves_in_different_bands
extract_lightcurves_in_different_bands,
)
from slsim.Sources.source import Source
from slsim.Deflectors.deflector import Deflector
Expand Down Expand Up @@ -169,15 +169,15 @@ def test_transient_event_time_no_valid_start():

assert 58000 <= result <= 58100


def test_transient_event_time_no_optimized_cadence():
mjd_times = np.linspace(58000, 58100, 50)
lightcurve_range = (-50, 100)
result = optimized_transient_event_time_mjd(mjd_times, lightcurve_range,
optimized_cadence=False)
result = optimized_transient_event_time_mjd(
mjd_times, lightcurve_range, optimized_cadence=False
)
assert 58000 <= result <= 58100




@pytest.fixture
def lens_class_instance():
Expand Down Expand Up @@ -271,7 +271,7 @@ def test_transient_data_with_cadence(lens_class_instance, exposure_data):
num_pix=61,
min_points=5,
)
lightcurves=extract_lightcurves_in_different_bands(result)
lightcurves = extract_lightcurves_in_different_bands(result)
expected_keys = lightcurves.keys()
colname = result.colnames
assert isinstance(result, Table)
Expand Down Expand Up @@ -299,18 +299,21 @@ def test_transient_data_with_cadence(lens_class_instance, exposure_data):
assert "obs_time" in expected_keys
assert "image_lists" in expected_keys

results_i = result[result["band"]=="i"]
results_i = result[result["band"] == "i"]
mag_i = results_i["mag_image_1"]
final_lightcurve_i=lightcurves["magnitudes"]["mag_image_1"]["i"]
final_lightcurve_i = lightcurves["magnitudes"]["mag_image_1"]["i"]
assert np.all(np.array(list(mag_i))) == np.all(np.array(final_lightcurve_i))
error_i_low = results_i["mag_error_image_1_low"]
error_i_high = results_i["mag_error_image_1_high"]
lightcurve_error_i_low=lightcurves["errors_low"]["mag_error_image_1_low"]["i"]
lightcurve_error_i_high=lightcurves["errors_high"]["mag_error_image_1_high"]["i"]
assert np.all(np.array(list(error_i_low))) == np.all(np.array(
lightcurve_error_i_low))
assert np.all(np.array(list(error_i_high))) == np.all(np.array(
lightcurve_error_i_high))
lightcurve_error_i_low = lightcurves["errors_low"]["mag_error_image_1_low"]["i"]
lightcurve_error_i_high = lightcurves["errors_high"]["mag_error_image_1_high"]["i"]
assert np.all(np.array(list(error_i_low))) == np.all(
np.array(lightcurve_error_i_low)
)
assert np.all(np.array(list(error_i_high))) == np.all(
np.array(lightcurve_error_i_high)
)


if __name__ == "__main__":
pytest.main()
15 changes: 10 additions & 5 deletions tests/test_Plots/test_plot_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from slsim.Plots.plot_functions import (
create_image_montage_from_image_list,
plot_montage_of_random_injected_lens,
create_montage, plot_lightcurves
create_montage,
plot_lightcurves,
)
from slsim.image_simulation import sharp_image
from slsim.Sources.source import Source
Expand Down Expand Up @@ -123,6 +124,7 @@ def test_plot_montage_of_random_injected_lens(quasar_lens_pop_instance):
assert isinstance(fig, plt.Figure)
assert fig.get_size_inches()[0] == np.array([num_cols * 3, num_rows * 3])[0]


def test_create_montage_basics():
images = [
np.random.rand(5, 5),
Expand All @@ -131,14 +133,15 @@ def test_create_montage_basics():
np.random.rand(5, 5),
]
montage = create_montage(images)

# Check shape
assert montage.shape == (5, 15) # 1 row, 3 images wide

# Check normalization range
assert np.min(montage) >= 0
assert np.max(montage) <= 1


def test_create_montage_specified_grid():
images = [
np.random.rand(5, 5),
Expand All @@ -151,6 +154,7 @@ def test_create_montage_specified_grid():
# Check shape
assert montage.shape == (5, 15) # 1 row, 3 images wide


def test_plot_lightcurves():
data = {
"magnitudes": {
Expand All @@ -174,13 +178,14 @@ def test_plot_lightcurves():

fig = plot_lightcurves(data, images=True)
fig2 = plot_lightcurves(data, images=False)
ax1=fig.get_axes()
ax2=fig2.get_axes()
ax1 = fig.get_axes()
ax2 = fig2.get_axes()
assert fig is not None
assert isinstance(fig, plt.Figure)
assert fig2 is not None
assert isinstance(fig2, plt.Figure)
assert len(ax1) == len(ax2)+2
assert len(ax1) == len(ax2) + 2


if __name__ == "__main__":
pytest.main()

0 comments on commit f30e29f

Please sign in to comment.