def test_wedge_calculateDetectorAnalyserPositions_ManyToMany(): wedge = Wedge(concept='ManyToMany') Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) Det2 = Detector.TubeDetector1D(position=(1.5, 0.1, 1.0), direction=(1.0, 0, 0), length=0.5, pixels=5, split=[0, 2, 5]) Ana2 = Analyser.FlatAnalyser(position=(0.75, 0, 0), direction=(1, 0, 1)) wedge.append([Det2, Ana, Ana2]) detectorPixelPositions, analyserPixelPositions = wedge.calculateDetectorAnalyserPositions( ) #print(detectorPixelPositions) #print(analyserPixelPositions) DetPixelPos2 = np.array([[[1.3, 0.1, 1], [1.4, 0.1, 1], [1.5, 0.1, 1], [1.6, 0.1, 1], [1.7, 0.1, 1]]]) assert (np.all(DetPixelPos2 == detectorPixelPositions[0])) anapos = np.array([0.5, 0.5, 0.75, 0.75, 0.75]) assert (np.all( [analyserPixelPositions[0][i, 0] == anapos[i] for i in range(5)]))
def test_CalcualteA4Ef(): Instr = Instrument.Instrument() Det = Detector.TubeDetector1D(position=(1, 0, 1), direction=(1, 0, 0), pixels=10, split=[2, 5, 8], length=1.0) Det2 = Detector.TubeDetector1D(position=(1, 0.1, 1), direction=(1, 0, 0), pixels=10, split=[2, 5, 8], length=1.0) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) Ana2 = Analyser.FlatAnalyser(position=(1, 0, 0), direction=(1, 0, 1)) wedge = Wedge.Wedge(position=(0, 0, 0), detectors=[Det, Det2], analysers=[Ana, Ana2]) Instr.append(wedge) Instr.initialize() print(Instr.A4) print(Instr.Ef)
def Tester(): from MJOLNIR.Geometry import Instrument, Detector, Analyser, Wedge # Create instrument Instr = Instrument.Instrument() # Create two detectors and their corresponding analysers Det = Detector.TubeDetector1D(position=(1, 0, 1), direction=(1, 0, 0), pixels=10, split=[2, 5, 8], length=1.0) Det2 = Detector.TubeDetector1D(position=(1, 0.1, 1), direction=(1, 0, 0), pixels=10, split=[2, 5, 8], length=1.0) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) Ana2 = Analyser.FlatAnalyser(position=(1, 0, 0), direction=(1, 0, 1)) # Collect everything in the wedge object wedge = Wedge.Wedge(position=(0, 0, 0), detectors=[Det, Det2], analysers=[Ana, Ana2]) Instr.append(wedge) Instr.initialize() print(Instr.A4) print(Instr.Ef)
def test_parseXML(): # Improve this test! from MJOLNIR.Geometry import Wedge, Analyser, Detector, Instrument import os tempFileName = '__temp__' Instr = Instrument.Instrument() Instr.settings['Author'] = 'Jakob Lass' wedge = Wedge.Wedge(position=(0.5, 0, 0)) Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0)) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) wedge.append([Det, Ana]) Instr.append([wedge, wedge]) Instr.append(wedge) f = open(tempFileName, 'w') f.write(createXMLString(Instr)) f.close() InstrLoaded = parseXML(tempFileName) os.remove(tempFileName) assert (Instr == InstrLoaded)
def test_Wedge_error(): wedge = Wedge(position=(0,0,0)) Det = Detector.TubeDetector1D(position=(1.0,1,0),direction=(1,0,0)) Ana = Analyser.FlatAnalyser(position=(0.5,0,0),direction=(1,0,1)) try: wedge.detectors=Ana assert False except AttributeError: assert True try: wedge.analysers=Det assert False except AttributeError: assert True try: wedge.append("Wrong object type") assert False except AttributeError: assert True try: wedge.append(["List of",3.0,"wrong objects"]) assert False except AttributeError: assert True try: wedge.settings['concept']='OneToOne' wedge.calculateDetectorAnalyserPositions() assert False except ValueError: assert True try: wedge.settings['concept']='OneToOne' wedge.append([Det,Det,Ana]) wedge.calculateDetectorAnalyserPositions() assert False except RuntimeError: assert True try: wedge.settings['concept']='ManyToMany' wedge.detectors[0].split=[10,30,44] wedge.calculateDetectorAnalyserPositions() assert False except ValueError: assert True try: wedge.settings['concept']='Wrong' wedge.calculateDetectorAnalyserPositions() assert False except ValueError: assert True
def test_Build_a_simple_instrument(saveFig=False): import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D Instr = Instrument.Instrument() Det = Detector.TubeDetector1D(position=(1, 0, 1), direction=(1, 0, 0)) Ana = Analyser.FlatAnalyser(position=(1, 0, 0), direction=(1, 0, 1)) wedge = Wedge.Wedge(position=(0, 0, 0), detectors=Det, analysers=Ana) Instr.append(wedge) fig = plt.figure() ax = fig.gca(projection='3d') Instr.plot(ax) ax.set_xlim(-0.1, 1.1) ax.set_ylim(-0.1, 1.1) ax.set_zlim(-0.1, 1.1) if saveFig: plt.savefig('../docs/_templates/Build_a_simple_instrument.png', format='png', dpi=300) plt.show()
def Tester(): from MJOLNIR.Geometry import Instrument,Detector,Analyser,Wedge import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D Instr = Instrument.Instrument() Det = Detector.TubeDetector1D(position=(1,0,1),direction=(1,0,0)) Ana = Analyser.FlatAnalyser(position=(1,0,0),direction=(1,0,1)) wedge = Wedge.Wedge(position=(0,0,0),detectors=Det,analysers=Ana) Instr.append(wedge) fig = plt.figure() ax = fig.gca(projection='3d') Instr.plot(ax) ax.set_xlim(-0.1,1.1) ax.set_ylim(-0.1,1.1) ax.set_zlim(-0.1,1.1) fig.savefig('/home/lass/Dropbox/PhD/Software/MJOLNIR/docs/Tutorials/Instrument/SimpleInstrument.png',format='png')
def test_Instrument_Initialization(): Instr = Instrument() wedge = Wedge.Wedge(position=(0.5, 0, 0), concept='ManyToMany') pixels = 33 split = [12] Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0), pixels=pixels, split=split) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) wedge.append([Det, Det, Ana, Ana, Ana]) try: Instr.initialize() assert False except ValueError: assert True try: print(Instr.A4) assert False except RuntimeError: assert True try: print(Instr.Ef) assert False except RuntimeError: assert True Instr.append(wedge) try: Instr.initialize() assert False except ValueError: assert True Instr.wedges[0].detectors[0].split = [0, 12, 20, pixels] Instr.initialize() assert (len(Instr.A4) == 1) assert (len(Instr.A4[0]) == 2) assert (len(Instr.A4[0][0]) == pixels) assert (len(Instr.A4) == len(Instr.Ef)) assert (len(Instr.A4[0]) == len(Instr.Ef[0])) assert (len(Instr.A4[0][0]) == len(Instr.Ef[0][0])) assert (Instr.settings['Initialized'] == True) try: Instr.A4 = [] assert False except NotImplementedError: assert True try: Instr.Ef = [] assert False except NotImplementedError: assert True
def test_Wedge_plot(): wedge = Wedge() Det = Detector.TubeDetector1D(position=(1.0,1,0),direction=(1,0,0)) Ana = Analyser.FlatAnalyser(position=(0.5,0,0),direction=(1,0,1)) wedge.append([Det,Ana]) plt.ioff() fig = plt.figure() ax = fig.gca(projection='3d') wedge.plot(ax)
def test_Wedge_append(): wedge = Wedge() Det = Detector.TubeDetector1D(position=(1.0,1,0),direction=(1,0,0)) Ana = Analyser.FlatAnalyser(position=(0.5,0,0),direction=(1,0,1)) wedge.append([Det,Det,Ana]) wedge.append(Det) wedge.append(Ana) assert(len(wedge.detectors)==3) assert(len(wedge.analysers)==2)
def test_Instrument_init(): Instr = Instrument() assert (np.all(Instr.position == (0, 0, 0))) Det = Detector.Detector(position=(1.0, 1, 0), direction=(1, 0, 0)) Ana = Analyser.Analyser(position=(0.5, 0, 0), direction=(1, 0, 1)) wedge = Wedge.Wedge(detectors=[Det, Det], analysers=Ana) Instr.wedges = [wedge, wedge] assert (Instr.settings['Initialized'] == False)
def test_wedge_calculateDetectorAnalyserPositions_OneToOne(): wedge = Wedge(concept='OneToOne') Det = Detector.TubeDetector1D(position=(1.0, 0.0, 1.0), direction=(1.0, 0, 0), length=0.5, pixels=5) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) Det2 = Detector.TubeDetector1D(position=(1.5, 0.1, 1.0), direction=(1.0, 0, 0), length=0.5, pixels=5) Ana2 = Analyser.FlatAnalyser(position=(0.75, 0, 0), direction=(1, 0, 1)) wedge.append([Det, Det2, Ana, Ana2]) detectorPixelPositions, analyserPixelPositions = wedge.calculateDetectorAnalyserPositions( ) #print(detectorPixelPositions,analyserPixelPositions) DetPixelPos = np.array([[[0.8, 0, 1], [0.9, 0, 1], [1.0, 0, 1], [1.1, 0, 1], [1.2, 0, 1]]]) assert (np.all(DetPixelPos == detectorPixelPositions[0][0])) assert (np.all([x == Ana.position for x in analyserPixelPositions[0]])) DetPixelPos2 = np.array([[[1.3, 0.1, 1], [1.4, 0.1, 1], [1.5, 0.1, 1], [1.6, 0.1, 1], [1.7, 0.1, 1]]]) #print(analyserPixelPositions[1]) assert (np.all(DetPixelPos2 == detectorPixelPositions[1][0])) assert (np.all( [x[0] == Ana2.position[0] for x in analyserPixelPositions[1]])) offcenterpos = np.array( [0.02225497, 0.02166939, 0.02105171, 0.02041748, 0.01977911]) #print(np.sum([analyserPixelPositions[1][i][1]-offcenterpos[i] for i in range(5)])) assert (np.sum( [analyserPixelPositions[1][i][1] - offcenterpos[i] for i in range(5)]) < 1e-8)
def test_Instrument_plot(): Instr = Instrument() wedge = Wedge.Wedge(position=(0.5, 0, 0)) Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0)) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) wedge.append([Det, Ana]) Instr.append(wedge) plt.ioff() fig = plt.figure() ax = fig.gca(projection='3d') Instr.plot(ax)
def test_Wedge_init(): wedge = Wedge(position=(0,0,0)) Det = Detector.Detector(position=(1.0,1,0),direction=(1,0,0)) Ana = Analyser.Analyser(position=(0.5,0,0),direction=(1,0,1)) wedge.detectors=Det wedge.analysers=Ana wedge2 = Wedge(position=(0,0,0)) wedge2.detectors=[Det,Det] wedge2.analysers=[Ana,Ana] wedge3 = Wedge(detectors=[Det,Det],analysers=Ana) assert(len(wedge3.analysers)==1) assert(len(wedge3.detectors)==2)
def test_Instrument_saveload(): import os Instr = Instrument(position=(0, 1, 0)) Instr2 = Instrument() wedge = Wedge.Wedge(position=(0.5, 0, 0)) Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0)) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) wedge.append([Det, Ana]) Instr.append(wedge) tempFile = 'temp.bin' Instr.save(tempFile) Instr2.load(tempFile) os.remove(tempFile) assert (Instr == Instr2)
def test_Wedge_warnings(): wedge = Wedge() Det = Detector.TubeDetector1D(position=(1.0,1,0),direction=(1,0,0)) Ana = Analyser.FlatAnalyser(position=(0.5,0,0),direction=(1,0,1)) wedge.detectors = Det wedge.analysers = Ana with warnings.catch_warnings(record=True) as w: # From https://docs.python.org/3.1/library/warnings.html # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. wedge.detectors = Det wedge.analysers = Ana # Verify some things assert len(w) == 2 assert issubclass(w[0].category, UserWarning) assert issubclass(w[1].category, UserWarning) assert 'The list of detectors is not empty! Appending new detector(s)' in str(w[0].message) assert 'The list of analysers is not empty! Appending new analyser(s)' in str(w[1].message)
def test_Instrument_error(): try: Instr = Instrument(fileName='wrongDummyFile.bin') assert False except ValueError: assert True Instr = Instrument() Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) try: Instr.wedges = Ana assert False except AttributeError: assert True try: Instr.wedges = [Ana, Ana] assert False except AttributeError: assert True try: Instr.append("Wrong object type") assert False except AttributeError: assert True try: Instr.append(["List of", 3.0, "wrong objects"]) assert False except AttributeError: assert True try: Instr.settings = {'Name', 'New dictionary'} assert False except NotImplementedError: return True
def test_parseXML(): # Improve this test! tempFileName = '__temp__.xml' Instr = Instrument() Instr.settings['Author'] = 'Jakob Lass' wedge = Wedge.Wedge(position=(0.5, 0, 0)) Det = Detector.TubeDetector1D(position=(1.0, 1, 0), direction=(1, 0, 0)) Ana = Analyser.FlatAnalyser(position=(0.5, 0, 0), direction=(1, 0, 1)) wedge.append([Det, Ana]) Instr.append([wedge, wedge]) Instr.append(wedge) Instr.saveXML(tempFileName) InstrLoaded = Instrument(fileName=tempFileName) os.remove(tempFileName) assert (Instr == InstrLoaded)