|
| 1 | +import pytest |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from spikewrap.structure._preprocess_run import PreprocessedRun |
| 5 | +import spikewrap.visualise |
| 6 | +import numpy as np |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import shutil |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def mock_preprocessed_run(tmp_path, monkeypatch): |
| 12 | + """ |
| 13 | + Fixture to create a temporary PreprocessedRun instance with mock data. |
| 14 | + """ |
| 15 | + |
| 16 | + from spikewrap.structure._preprocess_run import PreprocessedRun |
| 17 | + |
| 18 | + def mock_plot(*args, **kwargs): |
| 19 | + pass |
| 20 | + |
| 21 | + def mock_figure(*args, **kwargs): |
| 22 | + class MockFigure: |
| 23 | + def savefig(self, path): |
| 24 | + Path(path).parent.mkdir(parents=True, exist_ok=True) |
| 25 | + Path(path).touch() |
| 26 | + |
| 27 | + def clf(self): |
| 28 | + pass |
| 29 | + |
| 30 | + return MockFigure() |
| 31 | + |
| 32 | + def mock_visualise(*args, **kwargs): |
| 33 | + return mock_figure() |
| 34 | + |
| 35 | + monkeypatch.setattr(plt, "figure", mock_figure) |
| 36 | + monkeypatch.setattr(plt, "plot", mock_plot) |
| 37 | + monkeypatch.setattr(plt, "subplot", lambda *args, **kwargs: None) |
| 38 | + monkeypatch.setattr(plt, "title", lambda *args, **kwargs: None) |
| 39 | + |
| 40 | + import sys |
| 41 | + module_name = PreprocessedRun.__module__ |
| 42 | + module = sys.modules[module_name] |
| 43 | + monkeypatch.setattr(module, "visualise_run_preprocessed", mock_visualise) |
| 44 | + |
| 45 | + class MockRecording: |
| 46 | + def __init__(self): |
| 47 | + self.properties = {} |
| 48 | + self.data = np.random.random((10, 1000)) |
| 49 | + |
| 50 | + def save(self, folder, chunk_duration): |
| 51 | + Path(folder).mkdir(parents=True, exist_ok=True) |
| 52 | + (Path(folder) / "mock_recording_saved.txt").touch() |
| 53 | + return True |
| 54 | + |
| 55 | + def get_property(self, property_name): |
| 56 | + return self.properties.get(property_name, []) |
| 57 | + |
| 58 | + def get_traces(self, *args, **kwargs): |
| 59 | + return self.data |
| 60 | + |
| 61 | + def __array__(self): |
| 62 | + return self.data |
| 63 | + |
| 64 | + # Set up a mock recording with bad channels |
| 65 | + mock_recording = MockRecording() |
| 66 | + mock_recording.properties["bad_channels"] = [0, 1] |
| 67 | + |
| 68 | + raw_data_path = tmp_path / "raw_data" |
| 69 | + session_output_path = tmp_path / "output" |
| 70 | + run_name = "test_run" |
| 71 | + |
| 72 | + preprocessed_data = {"shank_0": {"0": mock_recording, "1": mock_recording}} |
| 73 | + |
| 74 | + raw_data_path.mkdir(parents=True, exist_ok=True) |
| 75 | + session_output_path.mkdir(parents=True, exist_ok=True) |
| 76 | + |
| 77 | + preprocessed_path = session_output_path / run_name / "preprocessed" |
| 78 | + preprocessed_path.mkdir(parents=True, exist_ok=True) |
| 79 | + |
| 80 | + diagnostic_path = session_output_path / "diagnostic_plots" |
| 81 | + diagnostic_path.mkdir(parents=True, exist_ok=True) |
| 82 | + |
| 83 | + preprocessed_run = PreprocessedRun( |
| 84 | + raw_data_path=raw_data_path, |
| 85 | + ses_name="test_session", |
| 86 | + run_name=run_name, |
| 87 | + file_format="mock_format", |
| 88 | + session_output_path=session_output_path, |
| 89 | + preprocessed_data=preprocessed_data, |
| 90 | + pp_steps={"step_1": "bad_channel_detection"}, |
| 91 | + ) |
| 92 | + |
| 93 | + def mock_save_diagnostic_plots(self): |
| 94 | + diagnostic_path = self._output_path / "diagnostic_plots" |
| 95 | + diagnostic_path.mkdir(parents=True, exist_ok=True) |
| 96 | + |
| 97 | + for shank_name in self._preprocessed: |
| 98 | + (diagnostic_path / f"{shank_name}_before_detection.png").touch() |
| 99 | + (diagnostic_path / f"{shank_name}_after_detection.png").touch() |
| 100 | + |
| 101 | + for ch in [0, 1]: |
| 102 | + (diagnostic_path / f"{shank_name}_bad_channel_{ch}.png").touch() |
| 103 | + |
| 104 | + # Monkeypatch the method to create placeholder files instead of real plots |
| 105 | + monkeypatch.setattr(preprocessed_run, "_save_diagnostic_plots", mock_save_diagnostic_plots.__get__(preprocessed_run)) |
| 106 | + |
| 107 | + yield preprocessed_run |
| 108 | + |
| 109 | + |
| 110 | +class TestDiagnosticPlots: |
| 111 | + """ |
| 112 | + Test class to validate diagnostic plots are saved correctly. |
| 113 | + """ |
| 114 | + |
| 115 | + def test_diagnostic_plots_saved(self, mock_preprocessed_run): |
| 116 | + """ |
| 117 | + Test if diagnostic plots are correctly saved after running save_preprocessed. |
| 118 | + """ |
| 119 | + output_dir = mock_preprocessed_run._output_path / "diagnostic_plots" |
| 120 | + |
| 121 | + if output_dir.exists(): |
| 122 | + shutil.rmtree(output_dir) |
| 123 | + assert not output_dir.exists(), "Diagnostic plots directory should not exist before running save_preprocessed" |
| 124 | + |
| 125 | + # Should trigger the diagnostic plot saving |
| 126 | + mock_preprocessed_run.save_preprocessed(overwrite=True, chunk_duration_s=1.0, n_jobs=1, slurm=False) |
| 127 | + |
| 128 | + assert output_dir.exists(), "Diagnostic plots directory was not created" |
| 129 | + shank_name = "shank_0" |
| 130 | + expected_files = [ |
| 131 | + f"{shank_name}_before_detection.png", |
| 132 | + f"{shank_name}_after_detection.png", |
| 133 | + f"{shank_name}_bad_channel_0.png", |
| 134 | + f"{shank_name}_bad_channel_1.png", |
| 135 | + ] |
| 136 | + |
| 137 | + for file_name in expected_files: |
| 138 | + assert (output_dir / file_name).exists(), f"Missing plot file: {file_name}" |
0 commit comments