|
| 1 | +"""Integration tests for `get_default_backend_configuration`.""" |
| 2 | +from io import StringIO |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | +from hdmf_zarr import NWBZarrIO |
| 9 | +from pynwb import NWBHDF5IO, NWBFile |
| 10 | +from pynwb.testing.mock.base import mock_TimeSeries |
| 11 | +from pynwb.testing.mock.file import mock_NWBFile |
| 12 | + |
| 13 | +from neuroconv.tools.nwb_helpers import ( |
| 14 | + HDF5BackendConfiguration, |
| 15 | + ZarrBackendConfiguration, |
| 16 | + get_default_backend_configuration, |
| 17 | + get_module, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def generate_complex_nwbfile() -> NWBFile: |
| 22 | + nwbfile = mock_NWBFile() |
| 23 | + |
| 24 | + raw_array = np.array([[1, 2, 3], [4, 5, 6]]) |
| 25 | + raw_time_series = mock_TimeSeries(name="RawTimeSeries", data=raw_array) |
| 26 | + nwbfile.add_acquisition(raw_time_series) |
| 27 | + |
| 28 | + number_of_trials = 10 |
| 29 | + for start_time, stop_time in zip( |
| 30 | + np.linspace(start=0.0, stop=10.0, num=number_of_trials), np.linspace(start=1.0, stop=11.0, num=number_of_trials) |
| 31 | + ): |
| 32 | + nwbfile.add_trial(start_time=start_time, stop_time=stop_time) |
| 33 | + |
| 34 | + ecephys_module = get_module(nwbfile=nwbfile, name="ecephys") |
| 35 | + processed_array = np.array([[7.0, 8.0], [9.0, 10.0], [11.0, 12.0], [13.0, 14.0]]) |
| 36 | + processed_time_series = mock_TimeSeries(name="ProcessedTimeSeries", data=processed_array) |
| 37 | + ecephys_module.add(processed_time_series) |
| 38 | + |
| 39 | + return nwbfile |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture(scope="session") |
| 43 | +def hdf5_nwbfile_path(tmpdir_factory): |
| 44 | + nwbfile_path = tmpdir_factory.mktemp("data").join("test_default_backend_configuration_hdf5_nwbfile.nwb.h5") |
| 45 | + if not Path(nwbfile_path).exists(): |
| 46 | + nwbfile = generate_complex_nwbfile() |
| 47 | + with NWBHDF5IO(path=str(nwbfile_path), mode="w") as io: |
| 48 | + io.write(nwbfile) |
| 49 | + return str(nwbfile_path) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture(scope="session") |
| 53 | +def zarr_nwbfile_path(tmpdir_factory): |
| 54 | + nwbfile_path = tmpdir_factory.mktemp("data").join("test_default_backend_configuration_hdf5_nwbfile.nwb.zarr") |
| 55 | + if not Path(nwbfile_path).exists(): |
| 56 | + nwbfile = generate_complex_nwbfile() |
| 57 | + with NWBZarrIO(path=str(nwbfile_path), mode="w") as io: |
| 58 | + io.write(nwbfile) |
| 59 | + return str(nwbfile_path) |
| 60 | + |
| 61 | + |
| 62 | +def test_complex_hdf5(hdf5_nwbfile_path): |
| 63 | + with NWBHDF5IO(path=hdf5_nwbfile_path, mode="a") as io: |
| 64 | + nwbfile = io.read() |
| 65 | + |
| 66 | + raw_array = np.array([[11, 21, 31], [41, 51, 61]], dtype="int32") |
| 67 | + raw_time_series = mock_TimeSeries(name="NewRawTimeSeries", data=raw_array) |
| 68 | + nwbfile.add_acquisition(raw_time_series) |
| 69 | + |
| 70 | + number_of_epochs = 5 |
| 71 | + for start_time, stop_time in zip( |
| 72 | + np.linspace(start=0.0, stop=10.0, num=number_of_epochs), |
| 73 | + np.linspace(start=1.0, stop=11.0, num=number_of_epochs), |
| 74 | + ): |
| 75 | + nwbfile.add_epoch(start_time=start_time, stop_time=stop_time) |
| 76 | + |
| 77 | + ecephys_module = get_module(nwbfile=nwbfile, name="ecephys") |
| 78 | + processed_array = np.array([[7.1, 8.1], [9.1, 10.1], [11.1, 12.1], [13.1, 14.1]]) |
| 79 | + processed_time_series = mock_TimeSeries(name="NewProcessedTimeSeries", data=processed_array) |
| 80 | + ecephys_module.add(processed_time_series) |
| 81 | + |
| 82 | + backend_configuration = get_default_backend_configuration(nwbfile=nwbfile, backend="hdf5") |
| 83 | + |
| 84 | + assert isinstance(backend_configuration, HDF5BackendConfiguration) |
| 85 | + |
| 86 | + dataset_configurations = backend_configuration.dataset_configurations |
| 87 | + assert len(dataset_configurations) == 4 |
| 88 | + assert "acquisition/NewRawTimeSeries/data" in dataset_configurations |
| 89 | + assert "epochs/start_time/data" in dataset_configurations |
| 90 | + assert "epochs/stop_time/data" in dataset_configurations |
| 91 | + assert "processing/ecephys/NewProcessedTimeSeries/data" in dataset_configurations |
| 92 | + |
| 93 | + # Best summary test of expected output is the printout |
| 94 | + with patch("sys.stdout", new=StringIO()) as stdout: |
| 95 | + print(backend_configuration) |
| 96 | + |
| 97 | + expected_print = """ |
| 98 | +Configurable datasets identified using the hdf5 backend |
| 99 | +------------------------------------------------------- |
| 100 | +
|
| 101 | +epochs/start_time/data |
| 102 | +---------------------- |
| 103 | + dtype : float64 |
| 104 | + full shape of source array : (5,) |
| 105 | + full size of source array : 0.00 GB |
| 106 | +
|
| 107 | + buffer shape : (5,) |
| 108 | + expected RAM usage : 0.00 GB |
| 109 | +
|
| 110 | + chunk shape : (5,) |
| 111 | + disk space usage per chunk : 0.00 MB |
| 112 | +
|
| 113 | + compression method : gzip |
| 114 | +
|
| 115 | +
|
| 116 | +epochs/stop_time/data |
| 117 | +--------------------- |
| 118 | + dtype : float64 |
| 119 | + full shape of source array : (5,) |
| 120 | + full size of source array : 0.00 GB |
| 121 | +
|
| 122 | + buffer shape : (5,) |
| 123 | + expected RAM usage : 0.00 GB |
| 124 | +
|
| 125 | + chunk shape : (5,) |
| 126 | + disk space usage per chunk : 0.00 MB |
| 127 | +
|
| 128 | + compression method : gzip |
| 129 | +
|
| 130 | +
|
| 131 | +acquisition/NewRawTimeSeries/data |
| 132 | +--------------------------------- |
| 133 | + dtype : int32 |
| 134 | + full shape of source array : (2, 3) |
| 135 | + full size of source array : 0.00 GB |
| 136 | +
|
| 137 | + buffer shape : (2, 3) |
| 138 | + expected RAM usage : 0.00 GB |
| 139 | +
|
| 140 | + chunk shape : (2, 3) |
| 141 | + disk space usage per chunk : 0.00 MB |
| 142 | +
|
| 143 | + compression method : gzip |
| 144 | +
|
| 145 | +
|
| 146 | +processing/ecephys/NewProcessedTimeSeries/data |
| 147 | +---------------------------------------------- |
| 148 | + dtype : float64 |
| 149 | + full shape of source array : (4, 2) |
| 150 | + full size of source array : 0.00 GB |
| 151 | +
|
| 152 | + buffer shape : (4, 2) |
| 153 | + expected RAM usage : 0.00 GB |
| 154 | +
|
| 155 | + chunk shape : (4, 2) |
| 156 | + disk space usage per chunk : 0.00 MB |
| 157 | +
|
| 158 | + compression method : gzip |
| 159 | +
|
| 160 | +""" |
| 161 | + assert stdout.getvalue() == expected_print |
| 162 | + |
| 163 | + |
| 164 | +def test_complex_zarr(zarr_nwbfile_path): |
| 165 | + with NWBZarrIO(path=zarr_nwbfile_path, mode="a") as io: |
| 166 | + nwbfile = io.read() |
| 167 | + |
| 168 | + raw_array = np.array([[11, 21, 31], [41, 51, 61]], dtype="int32") |
| 169 | + raw_time_series = mock_TimeSeries(name="NewRawTimeSeries", data=raw_array) |
| 170 | + nwbfile.add_acquisition(raw_time_series) |
| 171 | + |
| 172 | + number_of_epochs = 5 |
| 173 | + for start_time, stop_time in zip( |
| 174 | + np.linspace(start=0.0, stop=10.0, num=number_of_epochs), |
| 175 | + np.linspace(start=1.0, stop=11.0, num=number_of_epochs), |
| 176 | + ): |
| 177 | + nwbfile.add_epoch(start_time=start_time, stop_time=stop_time) |
| 178 | + |
| 179 | + ecephys_module = get_module(nwbfile=nwbfile, name="ecephys") |
| 180 | + processed_array = np.array([[7.1, 8.1], [9.1, 10.1], [11.1, 12.1], [13.1, 14.1]]) |
| 181 | + processed_time_series = mock_TimeSeries(name="NewProcessedTimeSeries", data=processed_array) |
| 182 | + ecephys_module.add(processed_time_series) |
| 183 | + |
| 184 | + backend_configuration = get_default_backend_configuration(nwbfile=nwbfile, backend="zarr") |
| 185 | + |
| 186 | + assert isinstance(backend_configuration, ZarrBackendConfiguration) |
| 187 | + |
| 188 | + dataset_configurations = backend_configuration.dataset_configurations |
| 189 | + assert len(dataset_configurations) == 4 |
| 190 | + assert "acquisition/NewRawTimeSeries/data" in dataset_configurations |
| 191 | + assert "epochs/start_time/data" in dataset_configurations |
| 192 | + assert "epochs/stop_time/data" in dataset_configurations |
| 193 | + assert "processing/ecephys/NewProcessedTimeSeries/data" in dataset_configurations |
| 194 | + |
| 195 | + # Best summary test of expected output is the printout |
| 196 | + with patch("sys.stdout", new=StringIO()) as stdout: |
| 197 | + print(backend_configuration) |
| 198 | + |
| 199 | + expected_print = """ |
| 200 | +Configurable datasets identified using the zarr backend |
| 201 | +------------------------------------------------------- |
| 202 | +
|
| 203 | +epochs/start_time/data |
| 204 | +---------------------- |
| 205 | + dtype : float64 |
| 206 | + full shape of source array : (5,) |
| 207 | + full size of source array : 0.00 GB |
| 208 | +
|
| 209 | + buffer shape : (5,) |
| 210 | + expected RAM usage : 0.00 GB |
| 211 | +
|
| 212 | + chunk shape : (5,) |
| 213 | + disk space usage per chunk : 0.00 MB |
| 214 | +
|
| 215 | + compression method : gzip |
| 216 | +
|
| 217 | +
|
| 218 | +epochs/stop_time/data |
| 219 | +--------------------- |
| 220 | + dtype : float64 |
| 221 | + full shape of source array : (5,) |
| 222 | + full size of source array : 0.00 GB |
| 223 | +
|
| 224 | + buffer shape : (5,) |
| 225 | + expected RAM usage : 0.00 GB |
| 226 | +
|
| 227 | + chunk shape : (5,) |
| 228 | + disk space usage per chunk : 0.00 MB |
| 229 | +
|
| 230 | + compression method : gzip |
| 231 | +
|
| 232 | +
|
| 233 | +acquisition/NewRawTimeSeries/data |
| 234 | +--------------------------------- |
| 235 | + dtype : int32 |
| 236 | + full shape of source array : (2, 3) |
| 237 | + full size of source array : 0.00 GB |
| 238 | +
|
| 239 | + buffer shape : (2, 3) |
| 240 | + expected RAM usage : 0.00 GB |
| 241 | +
|
| 242 | + chunk shape : (2, 3) |
| 243 | + disk space usage per chunk : 0.00 MB |
| 244 | +
|
| 245 | + compression method : gzip |
| 246 | +
|
| 247 | +
|
| 248 | +processing/ecephys/NewProcessedTimeSeries/data |
| 249 | +---------------------------------------------- |
| 250 | + dtype : float64 |
| 251 | + full shape of source array : (4, 2) |
| 252 | + full size of source array : 0.00 GB |
| 253 | +
|
| 254 | + buffer shape : (4, 2) |
| 255 | + expected RAM usage : 0.00 GB |
| 256 | +
|
| 257 | + chunk shape : (4, 2) |
| 258 | + disk space usage per chunk : 0.00 MB |
| 259 | +
|
| 260 | + compression method : gzip |
| 261 | +
|
| 262 | +""" |
| 263 | + assert stdout.getvalue() == expected_print |
0 commit comments