Skip to content

Commit

Permalink
debug #341 changed the way step size is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Cathyhjj committed Jan 15, 2025
1 parent a879a9a commit 5b64c7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
27 changes: 16 additions & 11 deletions src/firefly/plans/line_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@


class LineScanRegion(regions_display.RegionBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.num_points = 2 # Default value for num_points

def setup_ui(self):
self.layout = QtWidgets.QHBoxLayout()
Expand Down Expand Up @@ -43,7 +46,11 @@ def setup_ui(self):
self.start_line_edit.textChanged.connect(self.update_step_size)
self.stop_line_edit.textChanged.connect(self.update_step_size)

def update_step_size(self, num_points=None):
def set_num_points(self, num_points):
self.num_points = max(2, int(num_points)) # Ensure num_points is >= 2
self.update_step_size()

def update_step_size(self):
try:
# Get Start and Stop values
start_text = self.start_line_edit.text().strip()
Expand All @@ -55,13 +62,10 @@ def update_step_size(self, num_points=None):
start = float(start_text)
stop = float(stop_text)

# Ensure num_points is an integer
num_points = int(num_points) if num_points is not None else 2

# Calculate step size
if num_points > 1:
step_size = (stop - start) / (num_points - 1)
self.step_size_line_edit.setText(f"{step_size}")
if self.num_points > 1:
step_size = (stop - start) / (self.num_points - 1)
self.step_size_line_edit.setText(f"{step_size:.5g}")
else:
self.step_size_line_edit.setText("N/A")
except ValueError:
Expand All @@ -83,20 +87,21 @@ def time_per_scan(self, detector_time):

def customize_ui(self):
super().customize_ui()
# When selections of detectors changed update_total_time

# Connect signals for total time updates
self.ui.scan_pts_spin_box.valueChanged.connect(self.update_total_time)
self.ui.detectors_list.selectionModel().selectionChanged.connect(
self.update_total_time
)
self.ui.spinBox_repeat_scan_num.valueChanged.connect(self.update_total_time)

# Connect scan_pts_spin_box value change to regions
self.ui.scan_pts_spin_box.valueChanged.connect(self.update_regions_step_size)
for region in self.regions:
self.ui.scan_pts_spin_box.valueChanged.connect(region.set_num_points)

def update_regions_step_size(self, num_points):
"""Update the step size for all regions."""
for region in self.regions:
region.update_step_size(num_points)
region.set_num_points(num_points)

def queue_plan(self, *args, **kwargs):
"""Execute this plan on the queueserver."""
Expand Down
36 changes: 24 additions & 12 deletions src/firefly/tests/test_line_scan_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,47 @@ async def test_time_calculator(display, sim_registry, ion_chamber, qtbot, qapp):
async def test_step_size_calculation(display, qtbot):
await display.update_regions(1)
region = display.regions[0]

# Test valid inputs
region.start_line_edit.setText("0")
region.stop_line_edit.setText("10")

# Set num_points and emit the signal
display.ui.scan_pts_spin_box.setValue(5)
region.update_step_size(5) # Emit the signal with the new num_points value
assert region.step_size_line_edit.text() == "2.5"
# Set num_points
display.ui.scan_pts_spin_box.setValue(7)
qtbot.wait(10)
assert (
region.step_size_line_edit.text() == "1.6667"
), "Step size should be 1.6666 for 7 points from 0 to 10."

# Change the number of points and verify step size updates
display.ui.scan_pts_spin_box.setValue(3)
region.update_step_size(3)
assert region.step_size_line_edit.text() == "5.0"
qtbot.wait(10)
assert (
region.step_size_line_edit.text() == "5"
), "Step size should be 5.0 for 3 points from 0 to 10."

# Test invalid input
region.start_line_edit.setText("Start..")
region.update_step_size(3)
assert region.step_size_line_edit.text() == "N/A"
qtbot.wait(10)
assert (
region.step_size_line_edit.text() == "N/A"
), "Step size should be 'N/A' for invalid start input."

# Test edge case: num_points = 1
display.ui.scan_pts_spin_box.setValue(1)
region.update_step_size(1)
assert region.step_size_line_edit.text() == "N/A"
qtbot.wait(10)
assert (
region.step_size_line_edit.text() == "N/A"
), "Step size should be 'N/A' for num_points = 1."

# Reset to a valid state and verify
region.start_line_edit.setText("0")
region.stop_line_edit.setText("10")
display.ui.scan_pts_spin_box.setValue(6)
region.update_step_size(6)
assert region.step_size_line_edit.text() == "2.0"
qtbot.wait(10)
assert (
region.step_size_line_edit.text() == "2"
), "Step size should be 2.0 for 6 points from 0 to 10."


@pytest.mark.asyncio
Expand Down

0 comments on commit 5b64c7f

Please sign in to comment.