Skip to content

Commit

Permalink
Merge pull request #899 from HERA-Team/phase-flip-fix
Browse files Browse the repository at this point in the history
Update logic for identifying phase flips
  • Loading branch information
tyler-a-cox committed May 30, 2023
2 parents 5d4337d + f842f63 commit fa684dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions hera_cal/smooth_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,20 @@ def detect_phase_flips(phases):
phase_flipped: boolean array the same shape as phases where True means pi radians flipped
relative to the first unflagged phase.
"""
complexes = np.exp(1.0j * phases)
first_nonnan = np.ravel(complexes)[np.isfinite(np.ravel(complexes))][0]
# check if each number is closer to first_nonnan or -first_nonnan
phase_flipped = np.abs(complexes - first_nonnan) > np.abs(complexes + first_nonnan)
return phase_flipped
original_shape = np.array(phases).shape
complexes = np.exp(1.0j * np.ravel(phases))
phase_flipped = np.zeros(len(complexes), dtype=bool)
currently_flipped = False
previous_non_nan = np.nan # ensures that first non-nan integration is not flipped
for i in range(len(complexes)):
if not np.isnan(complexes[i]):
flip_here = np.abs(complexes[i] - previous_non_nan) > np.abs(complexes[i] + previous_non_nan)
phase_flipped[i] = currently_flipped ^ flip_here
currently_flipped = phase_flipped[i]
previous_non_nan = complexes[i]
else:
phase_flipped[i] = currently_flipped
return phase_flipped.reshape(original_shape)


def dpss_filters(freqs, times, freq_scale=10, time_scale=1800, eigenval_cutoff=1e-9):
Expand Down
4 changes: 2 additions & 2 deletions hera_cal/tests/test_smooth_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def test_detect_phase_flips(self):
np.testing.assert_array_equal(phase_flipped, np.array([False, False, False, True, True, True]))
# test nan handling
phase_flipped = smooth_cal.detect_phase_flips(np.array([1, 1, 1, 4, np.nan, 4]))
np.testing.assert_array_equal(phase_flipped, np.array([False, False, False, True, False, True]))
np.testing.assert_array_equal(phase_flipped, np.array([False, False, False, True, True, True]))
phase_flipped = smooth_cal.detect_phase_flips(np.array([np.nan, 1, 1, 4, np.nan, 4]))
np.testing.assert_array_equal(phase_flipped, np.array([False, False, False, True, False, True]))
np.testing.assert_array_equal(phase_flipped, np.array([False, False, False, True, True, True]))

def test_dpss_filters(self):
times = np.linspace(0, 10 * 10 / 60. / 60. / 24., 40, endpoint=False)
Expand Down

0 comments on commit fa684dc

Please sign in to comment.