Skip to content

Commit 95d05bc

Browse files
committed
normalize poscorr/centr and fixing pytest to work with new perturbation API
1 parent cc99962 commit 95d05bc

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/psfmachine/machine.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
import numpy as np
55
import pandas as pd
66
from scipy import sparse
7-
from scipy.ndimage import gaussian_filter1d
87
import astropy.units as u
98
from tqdm import tqdm
109
import matplotlib.pyplot as plt
1110
from astropy.stats import sigma_clip
1211

1312
from .utils import (
1413
_make_A_polar,
15-
_make_A_cartesian,
1614
solve_linear_model,
1715
sparse_lessthan,
18-
_combine_A,
1916
threshold_bin,
2017
get_breaks,
2118
smooth_vector,
@@ -638,14 +635,14 @@ def build_time_model(
638635
----------
639636
plot: boolean
640637
Plot a diagnostic figure.
641-
bin_method: boolean
642-
If True the `time` and `pos_corr` arrays will be downsampled instead of
643-
binned.
644-
split_time_segments : boolean, list/array of ints
638+
bin_method: string
639+
Type of bin method, options are "bin" and "downsample".
640+
split_time_segments : boolean
645641
If `True` will split the light curve into segments to fit different time
646-
models with a commong pixel normalization. If `False` will fit the full
647-
time series as one segment. If list or numpy array of ints, will use the
648-
index values as segment breaks.
642+
models with a common pixel normalization. If `False` will fit the full
643+
time series as one segment.
644+
focus_component: boolean
645+
Add a component that models th focus change at the begining of a segment.
649646
"""
650647
# create the time and space basis
651648
_whitened_time = (self.time - self.time.mean()) / (
@@ -677,11 +674,18 @@ def build_time_model(
677674
mpc1 = self.ra_centroid.to("arcsec").value / 4
678675
mpc2 = self.dec_centroid.to("arcsec").value / 4
679676

680-
# we smooth the vectors
677+
# smooth the vectors
681678
mpc1_smooth, mpc2_smooth = smooth_vector(
682679
[mpc1, mpc2], splits=splits, filter_size=self.poscorr_filter_size
683680
)
684-
# we combine them as the first order
681+
# normalize components
682+
mpc1_smooth = (mpc1_smooth - mpc1_smooth.mean()) / (
683+
mpc1_smooth.max() - mpc1_smooth.mean()
684+
)
685+
mpc2_smooth = (mpc2_smooth - mpc2_smooth.mean()) / (
686+
mpc2_smooth.max() - mpc2_smooth.mean()
687+
)
688+
# combine them as the first order
685689
other_vectors = [mpc1_smooth, mpc2_smooth, mpc1_smooth * mpc2_smooth]
686690

687691
# create a 3D perturbation matrix

tests/test_machine.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,13 @@ def test_compute_aperture_photometry():
120120
@pytest.mark.remote_data
121121
def test_poscorr_smooth():
122122
machine = TPFMachine.from_TPFs(tpfs, apply_focus_mask=False)
123-
machine._get_source_mask()
124-
machine.poscorr_filter_size = 0.4
125-
(
126-
time_original,
127-
time_binned,
128-
flux_binned_raw,
129-
flux_binned,
130-
flux_err_binned,
131-
poscorr1_smooth,
132-
poscorr2_smooth,
133-
poscorr1_binned,
134-
poscorr2_binned,
135-
) = machine._time_bin(npoints=3)
123+
machine.build_shape_model(plot=False)
124+
# no segments
125+
machine.time_corrector = "pos_corr"
126+
machine.poscorr_filter_size = 1
127+
machine.build_time_model(
128+
split_time_segments=False, bin_method="bin", focus_component=False
129+
)
136130

137131
median_pc1 = np.nanmedian(machine.pos_corr1, axis=0)
138132
median_pc2 = np.nanmedian(machine.pos_corr2, axis=0)
@@ -143,24 +137,29 @@ def test_poscorr_smooth():
143137
median_pc2.max() - median_pc2.mean()
144138
)
145139

146-
assert np.isclose(poscorr1_smooth, median_pc1, atol=1e-3).all()
147-
assert np.isclose(poscorr2_smooth, median_pc2, atol=1e-3).all()
140+
assert ((machine.P3.other_vectors[:, 0] - median_pc1) ** 2).sum() ** 0.5 < 1
141+
assert ((machine.P3.other_vectors[:, 1] - median_pc2) ** 2).sum() ** 0.5 < 1
148142

149143

150144
@pytest.mark.remote_data
151145
def test_segment_time_model():
152146
# testing segment with the current test dataset we have that only has 10 cadences
153147
# isn't the best, but we can still do some sanity checks.
154-
machine = TPFMachine.from_TPFs(tpfs, apply_focus_mask=False, n_time_points=3)
148+
machine = TPFMachine.from_TPFs(
149+
tpfs, apply_focus_mask=False, n_time_points=3, time_corrector="polynomial"
150+
)
155151
machine.build_shape_model(plot=False)
156152
# no segments
157-
machine.build_time_model(split_time_model=False, downsample=True)
158-
assert machine.time_model_w.shape[0] == machine.seg_splits.shape[0] - 1
159-
assert machine.time_model_w.shape[0] == machine._time_masked.shape[0]
153+
machine.build_time_model(
154+
split_time_segments=False, bin_method="bin", focus_component=False
155+
)
156+
assert machine.P3.vectors.shape == (10, 4)
160157

161-
# need tighter knot spacing
162-
machine.n_time_points = 2
158+
# fake 2 time breaks
159+
machine.time[4:] += 0.5
160+
machine.time[7:] += 0.41
163161
# user defined segments
164-
machine.build_time_model(split_time_model=[5], downsample=True)
165-
assert machine.time_model_w.shape[0] == machine.seg_splits.shape[0] - 1
166-
assert machine.time_model_w.shape[0] == machine._time_masked.shape[0]
162+
machine.build_time_model(
163+
split_time_segments=True, bin_method="bin", focus_component=False
164+
)
165+
assert machine.P3.vectors.shape == (10, 4 * 3)

0 commit comments

Comments
 (0)