Skip to content

Commit ea8e10a

Browse files
authored
fix filter normalization (#411)
1 parent ce48168 commit ea8e10a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

backend/tests_e2e/test_spectrum_form_v2.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ def european_csv_file() -> Iterator[Path]:
6363
path.unlink(missing_ok=True)
6464

6565

66+
@pytest.fixture
67+
def percentage_filter_csv_file() -> Iterator[Path]:
68+
"""CSV with filter transmission in 0-100% range (not 0-1)."""
69+
content = """wavelength,Filter_BP630
70+
400,0.001
71+
500,0.002
72+
600,5.5
73+
610,34.0
74+
620,85.0
75+
630,99.2
76+
640,92.0
77+
650,50.0
78+
660,3.0
79+
700,0.001
80+
"""
81+
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as f:
82+
f.write(content)
83+
path = Path(f.name)
84+
yield path
85+
path.unlink(missing_ok=True)
86+
87+
6688
@pytest.fixture
6789
def spectrum_form_page(live_server: LiveServer, auth_page: Page) -> Iterator[Page]:
6890
"""Navigate to the spectrum form page."""
@@ -548,6 +570,29 @@ def test_submit_filter_spectrum_with_source(
548570
assert spectrum.reference is None # No reference was provided
549571

550572

573+
def test_submit_filter_percentage_data_normalized_to_fraction(
574+
spectrum_form_page: Page, percentage_filter_csv_file: Path
575+
) -> None:
576+
"""Filter data in 0-100% range is converted to 0-1 fraction on submission."""
577+
page = spectrum_form_page
578+
_upload_csv(page, percentage_filter_csv_file)
579+
_select_columns(page, wavelength_col=0, data_cols=[1])
580+
581+
_fill_spectrum_card(page, category="f", subtype="bp", owner="E2E Pct Filter")
582+
_fill_source(page, "E2E Pct Filter Source")
583+
584+
page.locator("#id_confirmation").check()
585+
page.locator("#submit-btn").click()
586+
587+
expect(page).to_have_url(re.compile(r".*/spectra/submitted/"))
588+
589+
spectrum = Spectrum.objects.all_objects().filter(source="E2E Pct Filter Source").first()
590+
assert spectrum is not None
591+
max_val = max(v for _, v in spectrum.data)
592+
assert max_val <= 1.0, f"Filter data should be 0-1 fraction, got max={max_val}"
593+
assert max_val > 0.9, f"Expected max near 0.99, got {max_val}"
594+
595+
551596
def test_submit_light_spectrum(spectrum_form_page: Page, sample_csv_file: Path) -> None:
552597
"""Submit a light source spectrum (exercises light category handling)."""
553598
page = spectrum_form_page

frontend/src/js/spectrum-form/form-controller.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,13 @@ function processSpectrum(spectrum, index, el, state) {
844844
spectrum.manualPeakWave = result.peakWave
845845
}
846846
} else {
847-
// Use absolute values (no normalization)
848-
processedData = spectrum.interpolated
847+
// Use absolute values (no normalization), but convert percentage (0-100) to fraction (0-1)
848+
const maxVal = Math.max(...spectrum.interpolated.map(([, v]) => v))
849+
if (maxVal > 1) {
850+
processedData = spectrum.interpolated.map(([w, v]) => [w, v / 100])
851+
} else {
852+
processedData = spectrum.interpolated
853+
}
849854
}
850855

851856
spectrum.processed = processedData

0 commit comments

Comments
 (0)