Skip to content

Commit

Permalink
add more test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
z0gSh1u committed Jun 19, 2024
1 parent f193365 commit b54b543
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: InstallPython39
- name: InstallPython310
uses: actions/setup-python@v1
with:
python-version: '3.10'
Expand Down
3 changes: 2 additions & 1 deletion crip/physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,9 @@ def atomsFromMolecule(molecule: str) -> Dict[str, int]:


def zeffTheoretical(molecule: str, m=2.94) -> float:
''' Compute the eheoretical effective atomic number (Zeff) of a molecule using the power law with parameter `m`.
''' Compute the theoretical effective atomic number (Zeff) of a molecule using the power law with parameter `m`.
`molecule` is parsed by `atomsFromMolecule`.
[1] https://en.wikipedia.org/wiki/Effective_atomic_number_(compounds_and_mixtures)
'''
atoms = atomsFromMolecule(molecule)
totalElectrons = 0
Expand Down
16 changes: 11 additions & 5 deletions crip/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import matplotlib.patches
import matplotlib.axes
from mpl_toolkits.axes_grid1 import ImageGrid as MplImageGrid
from scipy.ndimage import uniform_filter

from ._typing import *
from .utils import *
Expand All @@ -19,7 +20,7 @@


def smooth1D(data: NDArray, winSize: int = 5) -> NDArray:
''' Smooth an 1D array using moving average window.
''' Smooth an 1D array using moving average window with length `winSize`.
The implementation is from https://stackoverflow.com/questions/40443020
'''
cripAssert(is1D(data), '`data` should be 1D array.')
Expand All @@ -33,10 +34,15 @@ def smooth1D(data: NDArray, winSize: int = 5) -> NDArray:
return np.concatenate((start, out0, stop))


# def zsmooth(imgs: ThreeD, r: int):
# ''' Average along `C` dimension [i - r, i + r].
# '''
# return np.mean(imgs[i - r:i + r], axis=0)
def smoothZ(img: ThreeD, ksize=3) -> ThreeD:
''' Smooth a 3D image using a uniform filter with `ksize` along Z dimension.
'''
cripAssert(is3D(img), '`img` should be 3D array.')

kernel = (ksize, 1, 1)
img = uniform_filter(img, kernel, mode='reflect')

return img


def window(img: TwoOrThreeD,
Expand Down
34 changes: 34 additions & 0 deletions test/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ def test_diff_shape(self):
computeSSIM(x, y)


def test_computeRMSE():
x = np.array([[1, 2], [3, 4]])
y = x + 1

assert computeRMSE(x, y) == 1.0
assert np.allclose(computeRMSE(x, y, pixelwise=True), np.ones((2, 2)))


def test_computeMAE():
x = np.array([[1, 2], [3, 4]])
y1 = x + 1
y2 = x - 1

assert computeMAE(x, y1) == 1.0
assert computeMAE(x, y2) == 1.0
assert np.allclose(computeMAE(x, y2, pixelwise=True) == 1.0, np.ones((2, 2)))


def test_pvalueInd():
samples = np.random.randn(100)
s1 = samples[:50]
s2 = samples[50:]

assert pvalueInd(s1, s2, True) > 0.05


def test_pvalueRel():
samples = np.random.randn(100)
s1 = samples[:50]
s2 = samples[50:]

assert pvalueInd(s1, s2, True) > 0.05


class Test_AverageMeter:
meter = AverageMeter()

Expand Down
19 changes: 19 additions & 0 deletions test/test_physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ def test_fromFile(self):
def test_monochromatic(self):
spec = Spectrum.monochromatic(100)
assert [spec.omega[100], spec.omega[101]] == [10**5, 0]


class Test_Atten():

def test_init(self):
pass


def test_atomsFromMolecule():
assert atomsFromMolecule('H2 O1') == {'H': 2, 'O': 1}
assert atomsFromMolecule('H2 O2') == {'H': 2, 'O': 2}

def test_zeffTheoretical():
assert zeffTheoretical('H1') == 1
assert zeffTheoretical('He1') == 2
assert zeffTheoretical('H2 O1') == pytest.approx(7.42, abs=0.05)

def test_zeffExperimental():
pass
25 changes: 23 additions & 2 deletions test/test_plot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import pytest
import numpy as np
from crip.plot import window
from crip.plot import *


def test_smooth1D():
data = np.array([1, 2, 3, 4, 5])
res = smooth1D(data, 3)
assert res.ndim == 1
assert len(res) == 5


def test_smoothZ():
s1 = np.ones((2, 2)) * 1.5
s2 = np.ones((2, 2)) * 2
s3 = np.ones((2, 2)) * 3.5
img = np.array([s1, s2, s3])
res = smoothZ(img, 3)
assert res.shape == (3, 2, 2)
assert res[1, 0, 0] == (1.5 + 2 + 3.5) / 3

def test_window():
pass
img = np.array([[0, 1], [2, 3]])
res = window(img, (1, 2), 'wwwl', '01')
assert res.ndim == 2
assert res.max() == 1
assert res.min() == 0

0 comments on commit b54b543

Please sign in to comment.