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 May 30, 2024
1 parent 32b0f13 commit 4cc4699
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 7 deletions.
6 changes: 3 additions & 3 deletions crip/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .utils import *


def fovCropRadius(sid: float, sdd: float, detectorWidth: float, pixelSize: float, roundOff=True) -> float:
def fovCropRadius(sid: float, sdd: float, detectorWidth: float, pixelSize: float, roundOff=True) -> Or[int, float]:
''' Compute the radius [pixel] of the valid FOV of the reconstruction.
`sid` and `sdd` are Source-Isocenter-Distance and Source-Detector-Distance, respectively.
`detectorWidth` is the width of the detector, i.e., `#elements * elementWidth`.
Expand Down Expand Up @@ -75,7 +75,7 @@ def huNoRescale(image: TwoOrThreeD, b: float = -1000, k: float = 1) -> TwoOrThre


@ConvertListNDArray
def postlogsToProjections(postlogs: TwoOrThreeD, flat: Or[TwoD, float]) -> TwoOrThreeD:
''' Invert `postlog` images to the original projections according to `flat` field.
def postlogsToRaws(postlogs: TwoOrThreeD, flat: Or[TwoD, float]) -> TwoOrThreeD:
''' Invert `postlog` images to the original raw projections according to `flat` field.
'''
return np.exp(-postlogs) * flat
5 changes: 5 additions & 0 deletions test/_asset/spectrum.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
keV omega
30 10000
31 15000
32 20000
33 -1
1 change: 1 addition & 0 deletions test/_genasset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import numpy as np
6 changes: 2 additions & 4 deletions test/test_lowdose.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class Test_injectGaussianNoise:

def test_1(self):
noisy = injectGaussianNoise(self.clean, self.sigma, self.mu)

assert isOfSameShape(noisy, self.clean)

noise = noisy - self.clean
another = np.random.randn(*self.clean.shape) * self.sigma + self.mu

assert stats.ttest_ind(noise.flatten(), another.flatten(), equal_var=True).pvalue > 0.1
assert np.mean(noise) == pytest.approx(self.mu, abs=1)
assert np.std(noise) == pytest.approx(self.sigma, abs=1)
43 changes: 43 additions & 0 deletions test/test_physics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import pytest
import numpy as np
from crip.physics import *
from crip.utils import CripException


def test_getCommonDensity():
assert getCommonDensity('H2O') == 1.0
assert getCommonDensity('Water') == 1.0

with pytest.raises(CripException):
getCommonDensity('h2o')


class Test_Spectrum:

def test_init(self):
s = Spectrum(omega=[0, 10, 20, 0, *[0] * (151 - 4)])
assert s.omega[1] == 10
assert s.omega[2] == 20
assert s.omega[3] == 0
assert s.omega[4:].sum() == 0
assert s.sumOmega == 30

def test_isMonochromatic(self):
omega = [0.0] * 151
omega[50] = 1.0
is_mono, energy = Spectrum(omega).isMonochromatic()
assert is_mono == True
assert energy == 50

def test_fromText(self):
spec = Spectrum.fromText('50 1\n51 2\n52 3\n53 4\n54 5\n55 -1\n')
assert list(spec.omega[50:56]) == [1, 2, 3, 4, 5, 0]

def test_fromFile(self):
spec = Spectrum.fromFile(os.path.join(os.path.dirname(__file__), '_asset/spectrum.txt'))
assert list(spec.omega[30:34]) == [10000, 15000, 20000, 0]

def test_monochromatic(self):
spec = Spectrum.monochromatic(100)
assert [spec.omega[100], spec.omega[101]] == [10**5, 0]
43 changes: 43 additions & 0 deletions test/test_postprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
import numpy as np
from crip.postprocess import *


def test_fovCropRadius():
assert fovCropRadius(1000, 1000, 1000, 1) == 447


class test_fovCrop():
twoD = np.ones((16, 16))
threeD = np.ones((2, 16, 16))

def test_twoD(self):
cropped = fovCrop(self.twoD, radius=8)
assert cropped[0, 0] == 0
assert cropped[8, 8] == 1

def test_threeD(self):
cropped = fovCrop(self.threeD, radius=8, fill=10)
assert np.all(cropped[:, 0, 0] == 10)
assert np.all(cropped[:, 8, 8] == 1)


def test_muToHU():
image = np.array([[1, 2], [3, 4]])
assert np.allclose(muToHU(image, 1), [[0, 1000], [2000, 3000]])


def test_huToMu():
image = np.array([[0, 1000], [2000, 3000]])
assert np.allclose(huToMu(image, 1), [[1, 2], [3, 4]])


def test_huNoRescale():
image = np.array([[-1000, 0], [1000, 2000]])
assert np.allclose(huNoRescale(image), [[0, 1000], [2000, 3000]])


def test_postlogsToRaws():
postlogs = np.array([[0, 1]])
flat = 1000
assert np.allclose(postlogsToRaws(postlogs, flat), [[flat, flat * np.exp(-1)]])
96 changes: 96 additions & 0 deletions test/test_shared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pytest
import numpy as np
from crip.shared import *


class Test_rotate():
twoD = np.array([[1, 2], [3, 4]])
threeD = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

def test_twoD(self):
img = self.twoD
assert np.array_equal(rotate(img, 90), np.array([[3, 1], [4, 2]]))
assert np.array_equal(rotate(img, 180), np.array([[4, 3], [2, 1]]))
assert np.array_equal(rotate(img, 270), np.array([[2, 4], [1, 3]]))
assert np.array_equal(rotate(img, 360), img)
assert np.array_equal(rotate(img, 0), img)

with pytest.raises(CripException):
rotate(img, 45)

def test_threeD(self):
img = self.threeD
assert np.array_equal(rotate(img, 90), np.array([[[3, 1], [4, 2]], [[7, 5], [8, 6]]]))
assert np.array_equal(rotate(img, 180), np.array([[[4, 3], [2, 1]], [[8, 7], [6, 5]]]))
assert np.array_equal(rotate(img, 270), np.array([[[2, 4], [1, 3]], [[6, 8], [5, 7]]]))
assert np.array_equal(rotate(img, 360), img)
assert np.array_equal(rotate(img, 0), img)

with pytest.raises(CripException):
rotate(img, 45)


class Test_verticalFlip():
twoD = np.array([[1, 2], [3, 4]])
threeD = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

def test_twoD(self):
img = self.twoD
assert np.array_equal(verticalFlip(img), np.array([[3, 4], [1, 2]]))

def test_threeD(self):
img = self.threeD
assert np.array_equal(verticalFlip(img), np.array([[[3, 4], [1, 2]], [[7, 8], [5, 6]]]))


class Test_horizontalFlip():
twoD = np.array([[1, 2], [3, 4]])
threeD = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

def test_twoD(self):
img = self.twoD
assert np.array_equal(horizontalFlip(img), np.array([[2, 1], [4, 3]]))

def test_threeD(self):
img = self.threeD
assert np.array_equal(horizontalFlip(img), np.array([[[2, 1], [4, 3]], [[6, 5], [8, 7]]]))


class Test_stackFlip():
twoD = np.array([[1, 2], [3, 4]])
threeD = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

def test_twoD(self):
img = self.twoD
with pytest.raises(CripException):
stackFlip(img)

def test_threeD(self):
img = self.threeD
assert np.array_equal(stackFlip(img), np.array([[[5, 6], [7, 8]], [[1, 2], [3, 4]]]))


class Test_resizeTo():
pass


class Test_resizeBy():
pass


class Test_resize3D():
pass


def test_permute():
top = np.array([[1, 2], [3, 4]])
bottom = np.array([[5, 6], [7, 8]])
threeD = np.stack([top, bottom])


def test_fitPolyV2D2():
pass


def test_applyPolyV2D2():
pass
12 changes: 12 additions & 0 deletions test/test_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
import numpy as np

from crip.spec import *


# def test_decompMaterial():
# src = Atten([1, 2])
# base1 = Atten([3, 4])
# base2 = Atten([5, 6])
# res = decompMaterial(src, base1, base2, mode='coeff')
# assert np.allclose(res, [0.2, 0.4])

0 comments on commit 4cc4699

Please sign in to comment.