Skip to content

Commit

Permalink
Format tests with black and update to work with current scipy
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed Nov 17, 2023
1 parent 3822409 commit eee60d9
Show file tree
Hide file tree
Showing 37 changed files with 2,835 additions and 2,375 deletions.
671 changes: 360 additions & 311 deletions tests/Stoner/Image/test_core.py

Large diffs are not rendered by default.

73 changes: 45 additions & 28 deletions tests/Stoner/Image/test_folder.py
Expand Up @@ -2,46 +2,63 @@
"""Stiner.Image.folders test
"""

from Stoner import Data
from Stoner.Image import ImageArray, ImageFolder, ImageStack
import numpy as np
from Stoner.Image import ImageArray, ImageFolder
import pytest
from os import path
import os

knownkeys = ['Averaging', 'Comment:', 'Contrast Shift', 'HorizontalFieldOfView',
'Lens', 'Loaded from', 'Magnification', 'MicronsPerPixel', 'field: units',
'field: units', 'filename', 'subtraction']
knownfieldvals = [-233.432852, -238.486666, -243.342465, -248.446173,
-253.297813, -258.332918, -263.340476, -268.20511]
knownkeys = [
"Averaging",
"Comment:",
"Contrast Shift",
"HorizontalFieldOfView",
"Lens",
"Loaded from",
"Magnification",
"MicronsPerPixel",
"field: units",
"field: units",
"filename",
"subtraction",
]
knownfieldvals = [
-233.432852,
-238.486666,
-243.342465,
-248.446173,
-253.297813,
-258.332918,
-263.340476,
-268.20511,
]

testdir=os.path.join(os.path.dirname(__file__),"coretestdata","testims")
fldr=ImageFolder(testdir, pattern="*.png")
testdir = os.path.join(os.path.dirname(__file__), "coretestdata", "testims")
fldr = ImageFolder(testdir, pattern="*.png")


def test_load():
fldr=ImageFolder(testdir, pattern="*.png")
assert len(fldr)==len(os.listdir(testdir)), "Didn't find all the images"
stack=fldr.as_stack()
assert len(stack)==len(fldr),"Conversion to ImageStack with as_stack() failed to preserve length"
fldr = ImageFolder(testdir, pattern="*.png")
assert len(fldr) == len(os.listdir(testdir)), "Didn't find all the images"
stack = fldr.as_stack()
assert len(stack) == len(fldr), "Conversion to ImageStack with as_stack() failed to preserve length"


def test_properties():
fldr=ImageFolder(testdir, pattern="*.png")
assert fldr.size==(512, 672),f"fldr.size incorrect at {fldr.size}"
empty=ImageFolder()
assert empty.size==tuple(),f"Empty folder.size wrong at {empty.size}"
fldr[1]=fldr[1].crop(4)
assert not fldr.size,f"fldr.size didn't return False for non-uniform images, got {fldr.size}"
fldr = ImageFolder(testdir, pattern="*.png")
assert fldr.size == (512, 672), f"fldr.size incorrect at {fldr.size}"
empty = ImageFolder()
assert empty.size == tuple(), f"Empty folder.size wrong at {empty.size}"
fldr[1] = fldr[1].crop(4)
assert not fldr.size, f"fldr.size didn't return False for non-uniform images, got {fldr.size}"
for im in fldr.images:
assert isinstance(im,ImageArray),"fldr.images returned something that wasn't an image array"
assert isinstance(im, ImageArray), "fldr.images returned something that wasn't an image array"


def test_methods():
fldr=ImageFolder(testdir, pattern="*.png")
assert len(list(fldr.loaded))==0,"ImageFolder got loaded unexpectedly!"
fldr = ImageFolder(testdir, pattern="*.png")
assert len(list(fldr.loaded)) == 0, "ImageFolder got loaded unexpectedly!"
fldr.loadgroup()
assert len(list(fldr.loaded))==8,"ImageFolder.looadgroup() failed to load!"

assert len(list(fldr.loaded)) == 8, "ImageFolder.looadgroup() failed to load!"


if __name__=="__main__":
pytest.main(["--pdb",__file__])
if __name__ == "__main__":
pytest.main(["--pdb", __file__])
101 changes: 53 additions & 48 deletions tests/Stoner/Image/test_kerr.py
Expand Up @@ -5,95 +5,100 @@
@author: phyrct
"""

from Stoner.Image import ImageArray, ImageFile
from Stoner.Image.kerr import KerrArray, KerrImageFile,KerrStack
from Stoner.Core import typeHintedDict
from Stoner import Data,__home__
from Stoner.Image.kerr import KerrArray, KerrImageFile, KerrStack
from Stoner import Data, __home__
import numpy as np
import pytest
import sys
from os import path
import os
import matplotlib.pyplot as plt

import warnings

import Stoner

Stoner.Options.multiprocessing = False

#data arrays for testing - some useful small images for tests
# data arrays for testing - some useful small images for tests

testdir=os.path.join(os.path.dirname(__file__),"kerr_testdata")
testdir2=os.path.join(os.path.dirname(__file__),"coretestdata","testims")
sample_data_dir=os.path.join(__home__,"../sample-data")
testdir = os.path.join(os.path.dirname(__file__), "kerr_testdata")
testdir2 = os.path.join(os.path.dirname(__file__), "coretestdata", "testims")
sample_data_dir = os.path.join(__home__, "../sample-data")


def shares_memory(arr1, arr2):
"""Check if two numpy arrays share memory"""
#ret = arr1.base is arr2 or arr2.base is arr1
# ret = arr1.base is arr2 or arr2.base is arr1
ret = np.may_share_memory(arr1, arr2)
return ret

selfimage=KerrArray(os.path.join(testdir,"kermit3.png"),ocr_metadata=True)
selfimage2=KerrArray(os.path.join(testdir,"kermit3.png"))
selfimage3=KerrImageFile(os.path.join(sample_data_dir,"testnormalsave.png"))

selfimage = KerrArray(os.path.join(testdir, "kermit3.png"), ocr_metadata=True)
selfimage2 = KerrArray(os.path.join(testdir, "kermit3.png"))
selfimage3 = KerrImageFile(os.path.join(sample_data_dir, "testnormalsave.png"))
selfks = KerrStack(testdir2)


def test_kerr_ops():
im = selfimage3.clone
assert isinstance(im.image,KerrArray),"KerrImageFile not blessing the image property correctly"
im1=im.float_and_croptext()
assert isinstance(im1.image,KerrArray),"Calling a crop routine without the _ argument returns a new KerrArray"
im2=im.float_and_croptext(_=True)
assert im2==im,"Calling crop method with _ argument changed the KerrImageFile"
im=KerrImageFile(selfimage2.clone)
assert isinstance(im.image, KerrArray), "KerrImageFile not blessing the image property correctly"
im1 = im.float_and_croptext()
assert isinstance(im1.image, KerrArray), "Calling a crop routine without the _ argument returns a new KerrArray"
im2 = im.float_and_croptext(_=True)
assert im2 == im, "Calling crop method with _ argument changed the KerrImageFile"
im = KerrImageFile(selfimage2.clone)
im.float_and_croptext(_=True)
mask=im.image.defect_mask_subtract_image()
im.image[~mask]=np.mean(im.image[mask])
selim=im
assert mask.sum()==343228,"Mask didn't work out right"
im=KerrImageFile(selfimage2.clone)
mask = im.image.defect_mask_subtract_image()
im.image[~mask] = np.mean(im.image[mask])
_ = im
assert mask.sum() == 343228, "Mask didn't work out right"
im = KerrImageFile(selfimage2.clone)
im.float_and_croptext(_=True)
mask=im.image.defect_mask(radius=4)
im.image[~mask]=np.mean(im.image[mask])
selim2=im
assert mask.sum()==342540,"Mask didn't work out right"
mask = im.image.defect_mask(radius=4)
im.image[~mask] = np.mean(im.image[mask])
selim2 = im
assert mask.sum() == 342540, "Mask didn't work out right"
selim2.level_image()
selim2.remove_outliers()
selim2.normalise()
selim2.plot_histogram()
selim2.imshow()
assert len(plt.get_fignums())==2,"Didn't open the correct number of figures"
assert len(plt.get_fignums()) == 2, "Didn't open the correct number of figures"
plt.close("all")




def test_tesseract_ocr():
#this incidentally tests get_metadata too
# this incidentally tests get_metadata too
if not selfimage.tesseractable:
print("#"*80)
print("#" * 80)
print("Skipping test that uses tesseract.")
return None
m=selfimage.metadata
_ = selfimage.metadata

# assert all((m['ocr_scalebar_length_microns']==50.0,
# m['ocr_date']=='11/30/15',
# m['ocr_field'] == -0.13)), 'Misread metadata {}'.format(m))
keys=('ocr_scalebar_length_pixels', 'ocr_field_of_view_microns',
'Loaded from', 'ocr_microns_per_pixel', 'ocr_pixels_per_micron')
_ = (
"ocr_scalebar_length_pixels",
"ocr_field_of_view_microns",
"Loaded from",
"ocr_microns_per_pixel",
"ocr_pixels_per_micron",
)
# assert all([k in m.keys() for k in keys]), 'some part of the metadata didn\'t load {}'.format(m))
m_un=selfimage2.metadata
assert 'ocr_field' not in m_un.keys(), 'Unannotated image has wrong metadata'
m_un = selfimage2.metadata
assert "ocr_field" not in m_un.keys(), "Unannotated image has wrong metadata"


def test_kerrstack():
print("X"*80+"\n"+"Test Kerrstack")
ks=selfks.clone
ks.each.normalise(scale=(0,1.0))
assert np.min(ks.imarray)==0.0 and np.max(ks.imarray)==1.0, 'KerrStack subtract failed min,max: {},{}'.format(np.min(ks.imarray),np.max(ks.imarray))
d=ks.hysteresis()
print("X" * 80 + "\n" + "Test Kerrstack")
ks = selfks.clone
ks.each.normalise(scale=(0, 1.0))
assert np.min(ks.imarray) == 0.0 and np.max(ks.imarray) == 1.0, "KerrStack subtract failed min,max: {},{}".format(
np.min(ks.imarray), np.max(ks.imarray)
)
d = ks.hysteresis()
assert isinstance(d, Data), "hysteresis didn't return Data"
assert d.data.shape==(len(ks),2), "hysteresis didn't return correct shape"
assert d.data.shape == (len(ks), 2), "hysteresis didn't return correct shape"


if __name__=="__main__": # Run some tests manually to allow debugging
pytest.main(["--pdb",__file__])
if __name__ == "__main__": # Run some tests manually to allow debugging
pytest.main(["--pdb", __file__])

0 comments on commit eee60d9

Please sign in to comment.