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_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_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()
Beispiel #4
0
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
    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')
Beispiel #6
0
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_Instrument_append():
    Instr = Instrument()

    wedge = Wedge.Wedge(position=(0.5, 0, 0))

    Instr.append([wedge, wedge])
    Instr.append(wedge)

    assert (len(Instr.wedges) == 3)
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_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_Instrument_warnings():
    Instr = Instrument()

    wedge = Wedge.Wedge(position=(0.5, 0, 0))

    Instr.wedges = wedge

    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.
        Instr.wedges = wedge
        # Verify some things
        assert len(w) == 1
        assert issubclass(w[0].category, UserWarning)
        assert 'The list of wedges is not empty! Appending new wedges(s)' in str(
            w[0].message)
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_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)