Skip to content

Commit

Permalink
Make drop ticks method static and create related test
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraporta committed Oct 24, 2023
1 parent 1e61d01 commit fd60648
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
39 changes: 26 additions & 13 deletions derotation/analysis/derotation_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ def process_analog_signals(self):
self.direction,
)

self.drop_ticks_outside_of_rotation()
self.rotation_ticks_peaks = self.drop_ticks_outside_of_rotation(
self.rotation_ticks_peaks,
self.rot_blocks_idx["start"],
self.rot_blocks_idx["end"],
self.total_clock_time,
self.number_of_rotations,
)

self.check_number_of_rotations()
if not self.is_number_of_ticks_correct() and self.adjust_increment:
Expand Down Expand Up @@ -341,38 +347,45 @@ def create_signed_rotation_array(

return rotation_on

def drop_ticks_outside_of_rotation(self):
@staticmethod
def drop_ticks_outside_of_rotation(
rotation_ticks_peaks: np.ndarray,
starts: np.ndarray,
ends: np.ndarray,
full_length: int,
number_of_rotations: int,
):
"""Removes ticks that happen in between two rotations."""

logging.info("Dropping ticks outside of the rotation period...")

len_before = len(self.rotation_ticks_peaks)
len_before = len(rotation_ticks_peaks)

rolled_starts = np.roll(self.rot_blocks_idx["start"], -1)
rolled_starts[-1] = int(len(self.full_rotation))
rolled_starts = np.roll(starts, -1)
rolled_starts[-1] = full_length

inter_roatation_interval = [
idx
for i in range(self.number_of_rotations)
for i in range(number_of_rotations)
for idx in range(
self.rot_blocks_idx["end"][i],
ends[i],
rolled_starts[i],
)
]

self.rotation_ticks_peaks = np.delete(
self.rotation_ticks_peaks,
np.where(
np.isin(self.rotation_ticks_peaks, inter_roatation_interval)
),
rotation_ticks_peaks = np.delete(
rotation_ticks_peaks,
np.where(np.isin(rotation_ticks_peaks, inter_roatation_interval)),
)

len_after = len(self.rotation_ticks_peaks)
len_after = len(rotation_ticks_peaks)
logging.info(
f"Ticks dropped: {len_before - len_after}.\n"
+ f"Ticks remaining: {len_after}"
)

return rotation_ticks_peaks

def check_number_of_rotations(self):
"""Checks that the number of rotations is as expected.
Expand Down
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

np.random.seed(42)


@pytest.fixture
def number_of_rotations():
Expand Down Expand Up @@ -88,3 +90,25 @@ def direction():
@pytest.fixture
def k():
return 0


@pytest.fixture
def ticks_per_rotation():
return 100


@pytest.fixture
def rotation_ticks(
full_length,
ticks_per_rotation,
number_of_rotations,
):
correct_number_of_ticks = ticks_per_rotation * number_of_rotations
number_of_ticks = correct_number_of_ticks + np.random.randint(0, 10)

# distribute number_of_ticks in poisson indices
ticks = np.random.choice(
range(full_length), size=number_of_ticks, replace=False
)
ticks = np.sort(ticks)
return ticks
12 changes: 12 additions & 0 deletions tests/test_unit/test_drop_ticks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from derotation.analysis.derotation_pipeline import DerotationPipeline


def test_drop_ticks_generated_randomly(
rotation_ticks, start_end_times, full_length, number_of_rotations
):
start, end = start_end_times
cleaned_ticks = DerotationPipeline.drop_ticks_outside_of_rotation(
rotation_ticks, start, end, full_length, number_of_rotations
)

assert len(cleaned_ticks) == 362

0 comments on commit fd60648

Please sign in to comment.