Skip to content

Commit 1ffac51

Browse files
committed
fix tests and lint
1 parent 7ced77e commit 1ffac51

16 files changed

Lines changed: 139 additions & 139 deletions

File tree

pytissueoptics/examples/rayscattering/ex06.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ def exampleCode():
1414

1515
material = ScatteringMaterial(mu_s=10, mu_a=1, g=0.98, n=1.0)
1616

17-
cube = Cuboid(
18-
a=3, b=3, c=3, position=Vector(0, 0, 0), material=material, label="cube"
19-
)
17+
cube = Cuboid(a=3, b=3, c=3, position=Vector(0, 0, 0), material=material, label="cube")
2018
detector = Circle(
21-
radius=0.25, orientation=Vector(0, 0, -1), position=Vector(0, 0.5, 1.501), label="detector",
19+
radius=0.25,
20+
orientation=Vector(0, 0, -1),
21+
position=Vector(0, 0.5, 1.501),
22+
label="detector",
2223
).asDetector(halfAngle=np.pi / 4)
2324
scene = ScatteringScene([cube, detector])
2425

2526
logger = EnergyLogger(scene)
26-
source = DirectionalSource(
27-
position=Vector(0, -0.5, -2), direction=Vector(0, 0, 1), N=N, diameter=0.5
28-
)
27+
source = DirectionalSource(position=Vector(0, -0.5, -2), direction=Vector(0, 0, 1), N=N, diameter=0.5)
2928

3029
source.propagate(scene, logger)
3130

pytissueoptics/rayscattering/display/viewer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ def _addPointCloud(self, style: PointCloudStyle):
219219
logger = self._logger if style.detectedBy is None else self._logger.getFiltered(style.detectedBy)
220220

221221
pointCloud = PointCloudFactory(logger).getPointCloud(
222-
solidLabel=style.solidLabel, surfaceLabel=style.surfaceLabel, energyType=style.energyType,
222+
solidLabel=style.solidLabel,
223+
surfaceLabel=style.surfaceLabel,
224+
energyType=style.energyType,
223225
)
224226

225227
self._drawPointCloudOfSolids(pointCloud, style)

pytissueoptics/rayscattering/energyLogging/energyLogger.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import os
55
import pickle
6-
from typing import List, Optional, TextIO, Union, Dict
6+
from typing import Dict, List, Optional, TextIO, Union
77

88
import numpy as np
99

@@ -409,9 +409,7 @@ def export(self, exportName: str):
409409

410410
self._exportSceneInfo(f"{exportName}.json", solidLabels)
411411

412-
def _writeKeyData(
413-
self, file: TextIO, key: InteractionKey, solidIndex: int, surfaceIndex: int
414-
):
412+
def _writeKeyData(self, file: TextIO, key: InteractionKey, solidIndex: int, surfaceIndex: int):
415413
if key not in self._data or self._data[key].dataPoints is None:
416414
return
417415

pytissueoptics/rayscattering/photon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, position: Vector, direction: Vector, ID: int = 0):
3030
self._intersectionFinder: Optional[IntersectionFinder] = None
3131
self._logger: Optional[Logger] = None
3232

33-
self._lastIntersectedDetector: Optional[str] = None # Prevent intersecting with a detector when passing through it.
33+
self._lastIntersectedDetector: Optional[str] = None
3434

3535
@property
3636
def isAlive(self) -> bool:
@@ -99,7 +99,7 @@ def step(self, distance=0) -> float:
9999
# Prevent re-intersecting with the same detector when passing through it.
100100
self._lastIntersectedDetector = intersection.insideEnvironment.solid.getLabel()
101101

102-
# skipping vertex check for now. TODO? Unclear stepCorrection and implications.
102+
# skipping vertex check if detector
103103
return intersection.distanceLeft
104104
else:
105105
distanceLeft = self.reflectOrRefract(intersection)

pytissueoptics/rayscattering/tests/display/testViewer.py

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pytissueoptics.rayscattering.display.profiles import Profile1D, ProfileFactory
99
from pytissueoptics.rayscattering.display.viewer import PointCloudStyle, Viewer, Visibility
1010
from pytissueoptics.rayscattering.display.views import View2D
11-
from pytissueoptics.rayscattering.energyLogging import EnergyLogger, PointCloud, PointCloudFactory
11+
from pytissueoptics.rayscattering.energyLogging import EnergyLogger, PointCloud
1212
from pytissueoptics.rayscattering.scatteringScene import ScatteringScene
1313
from pytissueoptics.rayscattering.source import Source
1414
from pytissueoptics.scene.geometry import BoundingBox
@@ -112,14 +112,11 @@ def testWhenShow3DWithSource_shouldDisplaySource(self):
112112
self.mock3DViewer.show.assert_called_once()
113113

114114
def testWhenShow3DWithDefaultPointCloud_shouldDisplayPointCloudOfSolidsAndSurfaceLeaving(self):
115-
mockPointCloudFactory = mock(PointCloudFactory)
116115
aPointCloud = PointCloud(
117-
solidPoints=np.array([[0.5, 0, 0, 0]]), surfacePoints=np.array([[1, 0, 0, 0], [-1, 0, 0, 0]])
116+
solidPoints=np.array([[0.5, 0, 0, 0, 0]]), surfacePoints=np.array([[1, 0, 0, 0, 0], [-1, 0, 0, 0, 0]])
118117
)
119-
when(mockPointCloudFactory).getPointCloud(...).thenReturn(aPointCloud)
120-
self.viewer._pointCloudFactory = mockPointCloudFactory
121-
122-
self.viewer.show3D(visibility=Visibility.POINT_CLOUD)
118+
with self._mockPointCloud(aPointCloud):
119+
self.viewer.show3D(visibility=Visibility.POINT_CLOUD)
123120

124121
self.mock3DViewer.addDataPoints.assert_called()
125122
addedSolidPoints = self.mock3DViewer.addDataPoints.call_args_list[0][0][0]
@@ -130,25 +127,21 @@ def testWhenShow3DWithDefaultPointCloud_shouldDisplayPointCloudOfSolidsAndSurfac
130127
self.mock3DViewer.show.assert_called_once()
131128

132129
def testGivenNoData_whenShow3DWithPointCloud_shouldNotDisplayPointCloud(self):
133-
mockPointCloudFactory = mock(PointCloudFactory)
134130
aPointCloud = PointCloud()
135-
when(mockPointCloudFactory).getPointCloud(...).thenReturn(aPointCloud)
136-
self.viewer._pointCloudFactory = mockPointCloudFactory
137-
138-
self.viewer.show3D(visibility=Visibility.POINT_CLOUD)
131+
with self._mockPointCloud(aPointCloud):
132+
self.viewer.show3D(visibility=Visibility.POINT_CLOUD)
139133

140134
self.mock3DViewer.addDataPoints.assert_not_called()
141135
self.mock3DViewer.show.assert_called_once()
142136

143137
def testWhenShow3DWithSurfacePointCloud_shouldOnlyDisplaySurfacePoints(self):
144-
mockPointCloudFactory = mock(PointCloudFactory)
145138
aPointCloud = PointCloud(
146-
solidPoints=np.array([[0.5, 0, 0, 0]]), surfacePoints=np.array([[1, 0, 0, 0], [-1, 0, 0, 0]])
139+
solidPoints=np.array([[0.5, 0, 0, 0, 0]]), surfacePoints=np.array([[1, 0, 0, 0, 0], [-1, 0, 0, 0, 0]])
147140
)
148-
when(mockPointCloudFactory).getPointCloud(...).thenReturn(aPointCloud)
149-
self.viewer._pointCloudFactory = mockPointCloudFactory
150-
151-
self.viewer.show3D(visibility=Visibility.POINT_CLOUD, pointCloudStyle=PointCloudStyle(showSolidPoints=False))
141+
with self._mockPointCloud(aPointCloud):
142+
self.viewer.show3D(
143+
visibility=Visibility.POINT_CLOUD, pointCloudStyle=PointCloudStyle(showSolidPoints=False)
144+
)
152145

153146
self.mock3DViewer.addDataPoints.assert_called_once()
154147
self.assertTrue(
@@ -157,19 +150,16 @@ def testWhenShow3DWithSurfacePointCloud_shouldOnlyDisplaySurfacePoints(self):
157150
self.mock3DViewer.show.assert_called_once()
158151

159152
def testWhenShow3DWithEnteringSurfacePointCloud_shouldOnlyDisplayEnteringSurfacePoints(self):
160-
mockPointCloudFactory = mock(PointCloudFactory)
161153
aPointCloud = PointCloud(
162-
solidPoints=np.array([[0.5, 0, 0, 0]]), surfacePoints=np.array([[1, 0, 0, 0], [-1, 1, 1, 1]])
163-
)
164-
when(mockPointCloudFactory).getPointCloud(...).thenReturn(aPointCloud)
165-
self.viewer._pointCloudFactory = mockPointCloudFactory
166-
167-
self.viewer.show3D(
168-
visibility=Visibility.POINT_CLOUD,
169-
pointCloudStyle=PointCloudStyle(
170-
showSolidPoints=False, showSurfacePointsLeaving=False, showSurfacePointsEntering=True
171-
),
154+
solidPoints=np.array([[0.5, 0, 0, 0, 0]]), surfacePoints=np.array([[1, 0, 0, 0, 0], [-1, 1, 1, 0, 1]])
172155
)
156+
with self._mockPointCloud(aPointCloud):
157+
self.viewer.show3D(
158+
visibility=Visibility.POINT_CLOUD,
159+
pointCloudStyle=PointCloudStyle(
160+
showSolidPoints=False, showSurfacePointsLeaving=False, showSurfacePointsEntering=True
161+
),
162+
)
173163

174164
self.mock3DViewer.addDataPoints.assert_called_once()
175165
self.assertTrue(
@@ -206,49 +196,38 @@ def testWhenShow3DWithViewsIndexList_shouldAdd2DImageOfTheseViewsInThe3DDisplay(
206196
self.mock3DViewer.show.assert_called_once()
207197

208198
def testGiven3DLogger_whenShow3DDefault_shouldDisplayEverythingExceptViews(self):
209-
mockPointCloudFactory = mock(PointCloudFactory)
210199
aPointCloud = PointCloud()
211-
when(mockPointCloudFactory).getPointCloud(...).thenReturn(aPointCloud)
212-
self.viewer._pointCloudFactory = mockPointCloudFactory
213200

214-
self.viewer.show3D()
201+
with self._mockPointCloud(aPointCloud):
202+
self.viewer.show3D()
215203

216204
verify(self.source, times=1).addToViewer(...)
217205
verify(self.scene, times=1).addToViewer(...)
218-
verify(mockPointCloudFactory, times=1).getPointCloud(...)
219206
self.mock3DViewer.addImage.assert_not_called()
220207
self.mock3DViewer.show.assert_called_once()
221208

222209
def testGiven2DLogger_whenShow3DDefault_shouldDisplayEverythingExceptPointCloud(self):
223210
self._givenLoggerWithXSceneView()
224211
self.logger.has3D = False
225212

226-
mockPointCloudFactory = mock(PointCloudFactory)
227-
when(mockPointCloudFactory).getPointCloud(...).thenReturn()
228-
self.viewer._pointCloudFactory = mockPointCloudFactory
229-
230-
self.viewer.show3D()
213+
with self._mockPointCloud(PointCloud()):
214+
self.viewer.show3D()
231215

232216
verify(self.source, times=1).addToViewer(...)
233217
verify(self.scene, times=1).addToViewer(...)
234-
verify(mockPointCloudFactory, times=0).getPointCloud(...)
235218
self.mock3DViewer.addImage.assert_called()
236219
self.mock3DViewer.show.assert_called_once()
237220

238221
def testGiven2DLogger_whenShow3DWithDefault3DVisibility_shouldWarnAndDisplayDefault2D(self):
239222
self._givenLoggerWithXSceneView()
240223
self.logger.has3D = False
241224

242-
mockPointCloudFactory = mock(PointCloudFactory)
243-
when(mockPointCloudFactory).getPointCloud(...).thenReturn()
244-
self.viewer._pointCloudFactory = mockPointCloudFactory
245-
246-
with self.assertWarns(UserWarning):
247-
self.viewer.show3D(visibility=Visibility.DEFAULT_3D)
225+
with self._mockPointCloud(PointCloud()):
226+
with self.assertWarns(UserWarning):
227+
self.viewer.show3D(visibility=Visibility.DEFAULT_3D)
248228

249229
verify(self.source, times=1).addToViewer(...)
250230
verify(self.scene, times=1).addToViewer(...)
251-
verify(mockPointCloudFactory, times=0).getPointCloud(...)
252231
self.mock3DViewer.addImage.assert_called()
253232
self.mock3DViewer.show.assert_called_once()
254233

@@ -260,3 +239,10 @@ def _givenLoggerWithXSceneView(self):
260239
self.logger.views = [sceneView]
261240
when(self.logger).updateView(sceneView).thenReturn()
262241
when(self.scene).getBoundingBox().thenReturn(BoundingBox([-2, 2], [-2, 2], [0, 5]))
242+
243+
@staticmethod
244+
def _mockPointCloud(pointCloud: PointCloud):
245+
return patch(
246+
"pytissueoptics.rayscattering.display.viewer.PointCloudFactory.getPointCloud",
247+
return_value=pointCloud,
248+
)

pytissueoptics/rayscattering/tests/energyLogging/testEnergyLogger.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ def testWhenExport_shouldExport3DDataPointsToFile(self):
296296
self.logger = EnergyLogger(scene)
297297

298298
# Log entering surface event, world scattering event and scattering event in both solids.
299-
self.logger.logDataPoint(0.1, Vector(0.7, 0.8, 0.8), InteractionKey("middleLayer"))
300-
self.logger.logDataPoint(-0.9, Vector(0.5, 1.0, 0.75), InteractionKey("frontLayer", "interface1"))
301-
self.logger.logDataPoint(0.4, Vector(0, 5, 0), InteractionKey("sphere"))
302-
self.logger.logDataPoint(0.2, Vector(0, 0, 0), InteractionKey(WORLD_SOLID_LABEL))
299+
self.logger.logDataPoint(0.1, Vector(0.7, 0.8, 0.8), InteractionKey("middleLayer"), ID=1)
300+
self.logger.logDataPoint(-0.9, Vector(0.5, 1.0, 0.75), InteractionKey("frontLayer", "interface1"), ID=0)
301+
self.logger.logDataPoint(0.4, Vector(0, 5, 0), InteractionKey("sphere"), ID=0)
302+
self.logger.logDataPoint(0.2, Vector(0, 0, 0), InteractionKey(WORLD_SOLID_LABEL), ID=0)
303303

304304
with tempfile.TemporaryDirectory() as tempDir:
305305
filePath = os.path.join(tempDir, "test_sim")
@@ -309,12 +309,15 @@ def testWhenExport_shouldExport3DDataPointsToFile(self):
309309
with open(filePath + ".csv", "r") as f:
310310
lines = f.readlines()
311311

312+
def parse_line(line):
313+
return list(map(float, line.strip().split(",")))
314+
312315
self.assertEqual(5, len(lines))
313-
self.assertEqual("energy,x,y,z,solid_index,surface_index\n", lines[0])
314-
self.assertEqual("0.2,0.0,0.0,0.0,-1,-1\n", lines[1])
315-
self.assertEqual("-0.9,0.5,1.0,0.75,1,5\n", lines[2])
316-
self.assertEqual("0.1,0.7,0.8,0.8,2,-1\n", lines[3])
317-
self.assertEqual("0.4,0.0,5.0,0.0,3,-1\n", lines[4])
316+
self.assertEqual("energy,x,y,z,photon_index,solid_index,surface_index\n", lines[0])
317+
self.assertEqual(parse_line(lines[1]), [0.2, 0.0, 0.0, 0.0, 0.0, -1.0, -1.0])
318+
self.assertEqual(parse_line(lines[2]), [-0.9, 0.5, 1.0, 0.75, 0.0, 1.0, 5.0])
319+
self.assertEqual(parse_line(lines[3]), [0.1, 0.7, 0.8, 0.8, 1.0, 2.0, -1.0])
320+
self.assertEqual(parse_line(lines[4]), [0.4, 0.0, 5.0, 0.0, 0.0, 3.0, -1.0])
318321

319322
def testWhenExport_shouldExportMetadataToFile(self):
320323
scene = PhantomTissue(worldMaterial=ScatteringMaterial(0.1, 0.1, 0.99))

pytissueoptics/rayscattering/tests/opencl/src/testCLFresnel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def _setUpWith(self, n1=1.0, n2=1.5, normal=Vector(0, 0, 1)):
5555
self.INSIDE_SOLID_ID,
5656
self.OUTSIDE_SOLID_ID,
5757
False,
58+
False,
59+
0,
5860
)
5961
]
6062
)

0 commit comments

Comments
 (0)