From 843396e1f746feca33cddc2498496e471ad898bd Mon Sep 17 00:00:00 2001 From: Jackie Date: Wed, 28 Aug 2024 17:24:45 -0700 Subject: [PATCH] Sort the data based on phase during initialization and add corresponding test. Also test for unique bins. --- eclipsebin/binning.py | 4 ++-- tests/test_eclipsing_binary_binner.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/eclipsebin/binning.py b/eclipsebin/binning.py index f6e99d5..5561f30 100644 --- a/eclipsebin/binning.py +++ b/eclipsebin/binning.py @@ -50,8 +50,8 @@ def __init__(self, phases, fluxes, flux_errors, nbins=200, fraction_in_eclipse=0 raise ValueError( "Number of data points must be greater than or equal to the number of bins." ) - - self.data = {"phases": phases, "fluxes": fluxes, "flux_errors": flux_errors} + sort_idx = np.argsort(phases) + self.data = {"phases": phases[sort_idx], "fluxes": fluxes[sort_idx], "flux_errors": flux_errors[sort_idx]} self.params = {"nbins": nbins, "fraction_in_eclipse": fraction_in_eclipse} # Identify primary and secondary eclipse minima diff --git a/tests/test_eclipsing_binary_binner.py b/tests/test_eclipsing_binary_binner.py index 9260b06..55a6de6 100644 --- a/tests/test_eclipsing_binary_binner.py +++ b/tests/test_eclipsing_binary_binner.py @@ -52,6 +52,8 @@ def test_initialization(wrapped_light_curve): assert len(binner.data["phases"]) == 100 assert len(binner.data["fluxes"]) == 100 assert len(binner.data["flux_errors"]) == 100 + # Check if the data is sorted + assert np.all(np.diff(binner.data["phases"]) >= 0) def test_initialization_invalid_data(unwrapped_light_curve): @@ -139,6 +141,10 @@ def test_calculate_eclipse_bins(wrapped_light_curve, unwrapped_light_curve): assert len(primary_bin_right_edges) == bins_in_primary assert len(secondary_bin_right_edges) == bins_in_secondary + # Check if the bin edges are unique + assert len(np.unique(primary_bin_right_edges)) == bins_in_primary + assert len(np.unique(secondary_bin_right_edges)) == bins_in_secondary + # Test the unwrapped light curve phases, fluxes, flux_errors = unwrapped_light_curve binner_unwrapped = EclipsingBinaryBinner( @@ -169,6 +175,10 @@ def test_calculate_eclipse_bins(wrapped_light_curve, unwrapped_light_curve): assert len(primary_bin_right_edges_unwrapped) == bins_in_primary_unwrapped assert len(secondary_bin_right_edges_unwrapped) == bins_in_secondary_unwrapped + # Check if the bin edges are unique + assert len(np.unique(primary_bin_right_edges_unwrapped)) == bins_in_primary_unwrapped + assert len(np.unique(secondary_bin_right_edges_unwrapped)) == bins_in_secondary_unwrapped + def test_calculate_out_of_eclipse_bins(wrapped_light_curve, unwrapped_light_curve): """ @@ -197,6 +207,10 @@ def test_calculate_out_of_eclipse_bins(wrapped_light_curve, unwrapped_light_curv assert len(ooe1_right_edges) == bins_in_ooe1 assert len(ooe2_right_edges) == bins_in_ooe2 + # Check if the bin edges are unique + assert len(np.unique(ooe1_right_edges)) == bins_in_ooe1 + assert len(np.unique(ooe2_right_edges)) == bins_in_ooe2 + # Test the unwrapped light curve phases, fluxes, flux_errors = unwrapped_light_curve binner = EclipsingBinaryBinner(phases, fluxes, flux_errors, nbins=50) @@ -220,6 +234,10 @@ def test_calculate_out_of_eclipse_bins(wrapped_light_curve, unwrapped_light_curv assert len(ooe1_right_edges) == bins_in_ooe1 assert len(ooe2_right_edges) == bins_in_ooe2 + # Check if the bin edges are unique + assert len(np.unique(ooe1_right_edges)) == bins_in_ooe1 + assert len(np.unique(ooe2_right_edges)) == bins_in_ooe2 + def test_find_bin_edges(wrapped_light_curve, unwrapped_light_curve): """