Skip to content

Commit

Permalink
update MRC test to use writer and temp_file
Browse files Browse the repository at this point in the history
remove TODO in DM and SER which cant be accomplished.
  • Loading branch information
ercius committed Dec 12, 2020
1 parent 112b8d3 commit 8f4e176
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 41 deletions.
9 changes: 6 additions & 3 deletions tests/test_DM.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from pathlib import Path
import pytest
from xicam.NCEM.ingestors.DMPlugin import ingest_NCEM_DM


# TODO: add fixture that writes temp data file with ncempy for tests
from xicam.NCEM.ingestors.DMPlugin import ingest_NCEM_DM


@pytest.fixture
def DM_path():
return "/home/rp/data/NCEM/01_TimeSeriesImages_20images(1).dm3"
"""DM files must be written from DM."""
dPath = Path.home()
return str(dPath / Path('data') / Path('01_TimeSeriesImages_20images.dm3'))


def test_slicing(DM_path):
Expand Down
46 changes: 13 additions & 33 deletions tests/test_MRC.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,24 @@
from xicam.NCEM.ingestors.MRCPlugin import ingest_NCEM_MRC


# TODO: move file creation to fixture

@pytest.fixture
def mrc_path():
"""Write a small MRC file to a temporary directory and file location.
THIS DOES NOT WORK. NOT SURE WHY. -PAE
Returns
-------
: string
The path to the temporary file.
"""
dd, _, _ = np.mgrid[0:30, 0:40, 0:50]
dd = dd.astype('<u2')

tmp = tempfile.TemporaryDirectory()
fPath = Path(tmp.name) / Path('temp_mrc.mrc')

mrc.mrcWriter(str(fPath), dd, (0.1, 0.2, 0.3))

return str(fPath)
def temp_file():
tt = tempfile.NamedTemporaryFile(mode='wb')
tt.close() # need to close the file to use it later
return Path(tt.name)


def test_ingest():
def test_ingest(temp_file):
# Write out a temporary mrc file
mrc.mrcWriter(temp_file, np.ones((10, 11, 12), dtype=np.float32), (1, 2, 3))

# Write a small mrc file
dd, _, _ = np.mgrid[0:30, 0:40, 0:50]
dd = dd.astype('<u2')
tmp = tempfile.TemporaryDirectory()
fPath = str(Path(tmp.name) / Path('temp_mrc.mrc'))
mrc.mrcWriter(fPath, dd, (0.1, 0.2, 0.3))
assert temp_file.exists() is True

# Test
with mrc.fileMRC(fPath) as mrc_obj:
assert mrc_obj.getSlice(0).shape == (40, 50)
docs = list(ingest_NCEM_MRC([fPath]))
with mrc.fileMRC(temp_file) as mrc_obj:
assert mrc_obj.getSlice(0).shape == (11, 12)
docs = list(ingest_NCEM_MRC([temp_file]))
event_doc = docs[2][1]
data = event_doc['data']['raw']
assert data.shape == (30, 40, 50)
assert data[0].compute().shape == (40, 50)
assert data.shape == (10, 11, 12)
assert data[0].compute().shape == (11, 12)
10 changes: 5 additions & 5 deletions tests/test_SER.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from pathlib import Path

import pytest
from ncempy.io import ser
from xicam.NCEM.ingestors.SERPlugin import ingest_NCEM_SER, _get_slice


# TODO: add fixture that writes temp data file with ncempy for tests

@pytest.fixture
def SER_path():
return "/home/rp/data/NCEM/10_series_1.ser"
"""SER files cant be written. We can only use data from a microscope"""
dPath = Path.home()
return str(dPath / Path('data') / Path('10_series_1.ser'))


def test_slicing(SER_path):
# with ser.fileSER(SER_path) as ser_obj:
assert _get_slice(SER_path, 0).shape == (512, 512)
docs = list(ingest_NCEM_SER([SER_path]))
event_doc = docs[2][1]
Expand Down

0 comments on commit 8f4e176

Please sign in to comment.