Commit 1c3cf82a authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

coverage reporting

parent 4c67795e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
*~
.*history
.coverage
*.pyc
*.log
*.h5
+12 −0
Original line number Diff line number Diff line
@@ -20,3 +20,15 @@ classifiers = [
]


[tool.coverage.run]
command_line = "-m pytest test"

[tool.coverage.paths]
source = [ "pysen", ]

[tool.coverage.report]
omit = [
	"test/*.py",
	"pysen/**/__init__.py",
	"pysen/ui/*",
]
+12 −11
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ ATTENUATOR_TABLE_08A = {
    'Pos_8' : 23.3,
    #'Pos_9' : 18.0,
    'Pos_9' : 40.0,  # set was to match Grafoil_Hellma*16385.dat
    'Pos10' : 26.5, }
    'Pos_10': 26.5, }

ATTENUATOR_TABLE_11A = {
    'ERROR' :  1.0,
@@ -99,18 +99,9 @@ ATTENUATOR_TABLE_11A = {
    'Pos_7' : 15.4,
    'Pos_8' : 53.8,
    'Pos_9' : 42.1,
    'Pos10' : 71.8, }
    'Pos_10': 71.8, }


def get_attenuator_pos(attenuator_angle):
    "for new DAQ (EPICS)"
    moatt = int(round(attenuator_angle))
    moatt = ((moatt + 330) % 360 )//30 -1
    if moatt==-1:
        return 'CLOSED'
    if moatt==0:
        return 'OPEN'
    return f"Pos_{moatt:d}"


#
@@ -253,6 +244,16 @@ def detector_pixel(theta, phi, theta0, **kwargs):
        iy=-1
    return ix, iy

def get_attenuator_pos(attenuator_angle):
    "for new DAQ (EPICS)"
    moatt = int(round(attenuator_angle))
    moatt = ((moatt + 330) % 360 )//30 -1
    if moatt==-1:
        return 'CLOSED'
    if moatt==0:
        return 'OPEN'
    return f"Pos_{moatt:d}"

def get_attenuation(att_pos, wavelength, att_table=None):
    "get attenuation factor, wavelength in A"

+7 −5
Original line number Diff line number Diff line
@@ -26,11 +26,11 @@ def polystr(coeff, variable='x', varsep=' '):
        res = res + ' ' + sign.get(s,'')
        # omit unity coeff. if power > 0
        if n==0 or n>0 and a!=1:
            res = res + (' %.4g' % a )
            res = res + (f" {a:.4g}")
        # finally the variable
        res = '%s%s%s' % (res,varsep,variable)
        res = f"{res}{varsep}{variable}"
        if n>1:
            res = '%s^%d' % (res,n)
            res = f"{res}^{n:d}"
    return res.strip(' +')


@@ -49,7 +49,8 @@ def average(val, weights=None):
    return val_av, np.sqrt(val_err)


def histogram1d(data, weights, bins):
# NOT USED ANYWHERE
def _histogram1d(data, weights, bins):
    """create histogram with bin widths """
    hist, bins = np.histogram(data, weights=weights, bins=bins)
    dbins =  bins[1:]-bins[:-1]
@@ -57,7 +58,8 @@ def histogram1d(data, weights, bins):
    return np.array((bins, dbins, hist))


def normalize_counts(counts, norm=1.0, fix_zeros=False):
# NOT USED ANYWHERE
def _normalize_counts(counts, norm=1.0, fix_zeros=False):
    """normalize counts, calculating errors"""
    if fix_zeros:
        cnt_err = np.where(counts>0, np.sqrt(counts*1.0), 1.0)
+90 −12
Original line number Diff line number Diff line
@@ -10,6 +10,12 @@ import pysen.config as cfg

class ConfigTestCase(unittest.TestCase):
    "test cases for pysen config file"

    def test_pcharge_per_second(self):
        "test proton charge per second"
        self.assertAlmostEqual(  13076.9, cfg.pcharge_per_second(), 1)
        self.assertAlmostEqual(  10000.0, cfg.pcharge_per_second(1.0,1000.0), 1)

    def test_tau_limits(self):
        "test tau limits"
        #
@@ -36,6 +42,26 @@ class ConfigTestCase(unittest.TestCase):
        tmin, tmax = cfg.tau_limits(11.0, mode='standard')
        self.assertAlmostEqual(  0.2480 , tmin, 4)
        self.assertAlmostEqual(138.9004 , tmax, 4)
        #
        tmin, tmax = cfg.tau_limits( 5.0, mode='mixed')
        self.assertAlmostEqual(  0.0023, tmin, 4)
        self.assertAlmostEqual( 13.0447 , tmax, 4)

    def test_phi_limits(self):
        "test phi limits"
        self.assertEqual((3.0,29.0), cfg.phi_limits('p1'))
        self.assertEqual((3.0,42.0), cfg.phi_limits('p2'))
        self.assertEqual((3.0,56.0), cfg.phi_limits('p3'))
        self.assertEqual((3.0,79.5), cfg.phi_limits('p4'))

    def test_wavelength_bandwidth(self):
        "test wavelength bandwitdh"
        self.assertAlmostEqual(3.6029, cfg.wavelength_bandwitdh('p1'), 3)
        self.assertAlmostEqual(3.0954, cfg.wavelength_bandwitdh('p2'), 3)
        self.assertAlmostEqual(2.7133, cfg.wavelength_bandwitdh('p3'), 3)
        self.assertAlmostEqual(2.4151, cfg.wavelength_bandwitdh('p4'), 3)
        # twice wavelength
        self.assertAlmostEqual(2*3.0954, cfg.wavelength_bandwitdh('p2', 30), 3)

    def test_pixel_angle_taco(self):
        "test old taco pixel mapping (angles)"
@@ -82,7 +108,7 @@ class ConfigTestCase(unittest.TestCase):
                ( -1, -1,  3.200,  -45.000 ), # outside the limits
               ]
        for ix_exp, iy_exp, theta, phi  in res0:
            ix, iy = cfg.detector_pixel(np.radians(theta), np.radians(phi), np.radians(theta0))
            ix, iy = cfg.detector_pixel(radians(theta), radians(phi), radians(theta0))
            self.assertEqual(ix_exp, ix, "ix: theta=%s phi=%s" % (theta, phi))
            self.assertEqual(iy_exp, iy, "iy: theta=%s phi=%s" % (theta, phi))

@@ -98,7 +124,7 @@ class ConfigTestCase(unittest.TestCase):
                ( -1, -1, 12.450,  -10.479 ), # outside the limits
               ]
        for ix_exp, iy_exp, theta, phi  in res0:
            ix, iy = cfg.detector_pixel(np.radians(theta), np.radians(phi), np.radians(theta0))
            ix, iy = cfg.detector_pixel(radians(theta), radians(phi), radians(theta0))
            self.assertEqual(ix_exp, ix, "ix: theta=%s phi=%s" % (theta, phi))
            self.assertEqual(iy_exp, iy, "iy: theta=%s phi=%s" % (theta, phi))

@@ -115,7 +141,7 @@ class ConfigTestCase(unittest.TestCase):
                ( 688, 304,  1.1569,  130.385 ),
               ]
        for ix, iy, theta_exp, phi_exp  in res0:
            theta, phi = cfg.pixel_angle(ix, iy, np.radians(theta0), nx=nx, ny=ny, dirx=-1, diry=-1)
            theta, phi = cfg.pixel_angle(ix, iy, radians(theta0), nx=nx, ny=ny, dirx=-1, diry=-1)
            self.assertAlmostEqual(theta_exp, degrees(theta), 3, "theta[%d,%d;%s]" % (ix,iy,theta0))
            self.assertAlmostEqual(phi_exp  , degrees(phi)  , 3, "phi[%d,%d;%s]"   % (ix,iy,theta0))

@@ -129,7 +155,7 @@ class ConfigTestCase(unittest.TestCase):
                ( 688, 304,  9.2919,   +5.466 ),
               ]
        for ix, iy, theta_exp, phi_exp  in res0:
            theta, phi = cfg.pixel_angle(ix, iy, np.radians(theta0), nx=nx, ny=ny, dirx=-1, diry=-1)
            theta, phi = cfg.pixel_angle(ix, iy, radians(theta0), nx=nx, ny=ny, dirx=-1, diry=-1)
            self.assertAlmostEqual(theta_exp, degrees(theta), 3, "theta[%d,%d;%s]" % (ix,iy,theta0))
            self.assertAlmostEqual(phi_exp  , degrees(phi)  , 3, "phi[%d,%d;%s]"   % (ix,iy,theta0))

@@ -147,7 +173,7 @@ class ConfigTestCase(unittest.TestCase):
               ]

        for ix_exp, iy_exp, theta, phi  in res0:
            ix, iy = cfg.detector_pixel(np.radians(theta), np.radians(phi), np.radians(theta0),
            ix, iy = cfg.detector_pixel(radians(theta), radians(phi), radians(theta0),
                     nx=nx, ny=ny, dirx=-1, diry=-1)
            self.assertEqual(ix_exp, ix, "ix: theta=%s phi=%s" % (theta, phi))
            self.assertEqual(iy_exp, iy, "iy: theta=%s phi=%s" % (theta, phi))
@@ -162,11 +188,46 @@ class ConfigTestCase(unittest.TestCase):
                ( 688, 304,  9.2919,   +5.466 ),
               ]
        for ix_exp, iy_exp, theta, phi  in res0:
            ix, iy = cfg.detector_pixel(np.radians(theta), np.radians(phi), np.radians(theta0),
            ix, iy = cfg.detector_pixel(radians(theta), radians(phi), radians(theta0),
                     nx=nx, ny=ny, dirx=-1, diry=-1)
            self.assertEqual(ix_exp, ix, "ix: theta=%s phi=%s" % (theta, phi))
            self.assertEqual(iy_exp, iy, "iy: theta=%s phi=%s" % (theta, phi))

    def test_get_attenuation(self):
        "test attenuator mapping (EPICS)"
        self.assertEqual( 1.0, cfg.get_attenuation("OPEN",    wavelength=8.0), "OPEN")
        self.assertEqual( 1.0, cfg.get_attenuation("CLOSED",  wavelength=8.0), "CLOSED")
        #
        self.assertEqual( 2.2, cfg.get_attenuation("Pos_1",  wavelength= 8.0), "Pos_1")
        self.assertEqual( 3.4, cfg.get_attenuation("Pos_2",  wavelength= 8.0), "Pos_2")
        self.assertEqual( 5.0, cfg.get_attenuation("Pos_3",  wavelength= 8.0), "Pos_3")
        self.assertEqual(11.0, cfg.get_attenuation("Pos_4",  wavelength= 8.0), "Pos_4")
        self.assertEqual( 6.2, cfg.get_attenuation("Pos_5",  wavelength= 8.0), "Pos_5")
        self.assertEqual( 8.0, cfg.get_attenuation("Pos_6",  wavelength= 8.0), "Pos_6")
        self.assertEqual( 8.2, cfg.get_attenuation("Pos_7",  wavelength= 8.0), "Pos_7")
        self.assertEqual(23.3, cfg.get_attenuation("Pos_8",  wavelength= 8.0), "Pos_8")
        self.assertEqual(40.0, cfg.get_attenuation("Pos_9",  wavelength= 8.0), "Pos_9")
        self.assertEqual(26.5, cfg.get_attenuation("Pos_10", wavelength= 8.0), "Pos_10")
        #
        self.assertEqual( 2.2, cfg.get_attenuation("Pos_1",  wavelength=11.0), "Pos_1")
        self.assertEqual( 3.5, cfg.get_attenuation("Pos_2",  wavelength=11.0), "Pos_2")
        self.assertEqual( 6.7, cfg.get_attenuation("Pos_3",  wavelength=11.0), "Pos_3")
        self.assertEqual(13.4, cfg.get_attenuation("Pos_4",  wavelength=11.0), "Pos_4")
        self.assertEqual( 9.6, cfg.get_attenuation("Pos_5",  wavelength=11.0), "Pos_5")
        self.assertEqual(13.4, cfg.get_attenuation("Pos_6",  wavelength=11.0), "Pos_6")
        self.assertEqual(15.4, cfg.get_attenuation("Pos_7",  wavelength=11.0), "Pos_7")
        self.assertEqual(53.8, cfg.get_attenuation("Pos_8",  wavelength=11.0), "Pos_8")
        self.assertEqual(42.1, cfg.get_attenuation("Pos_9",  wavelength=11.0), "Pos_9")
        self.assertEqual(71.8, cfg.get_attenuation("Pos_10", wavelength=11.0), "Pos_10")
        # interpolation
        self.assertAlmostEqual( 2.20, cfg.get_attenuation("Pos_1",  wavelength= 9.5), 1, "Pos_1")
        self.assertAlmostEqual( 3.45, cfg.get_attenuation("Pos_2",  wavelength= 9.5), 1, "Pos_2")
        self.assertAlmostEqual(12.20, cfg.get_attenuation("Pos_4",  wavelength= 9.5), 1, "Pos_4")
        self.assertAlmostEqual(10.70, cfg.get_attenuation("Pos_6",  wavelength= 9.5), 1, "Pos_6")
        self.assertAlmostEqual(38.55, cfg.get_attenuation("Pos_8",  wavelength= 9.5), 1, "Pos_8")
        self.assertAlmostEqual(49.15, cfg.get_attenuation("Pos_10", wavelength= 9.5), 1, "Pos_10")


    def test_attenuator_position(self):
        "test attenuator mapping (EPICS)"
        self.assertEqual("CLOSED", cfg.get_attenuator_pos(30.0), "CLOSED")
@@ -175,13 +236,30 @@ class ConfigTestCase(unittest.TestCase):
            self.assertEqual(f"Pos_{pos}", cfg.get_attenuator_pos(pos*30.0+60), f"Pos_{pos}")
        self.assertEqual("Pos_10", cfg.get_attenuator_pos( 0.0), "Pos_10")

    def not_ready_coverage(self):
    def test_coverage(self):
        "test coverage"
        lmax, qmin, result, limits, _msg = cfg.coverage(8.0, 0.15)
        print(lmax, qmin)
        print(result.shape)
        for _ in limits:
            print(len(_))
        res= cfg.coverage(8.0, 0.15)
        #
        self.assertAlmostEqual(10.96, degrees(res['theta']), 2, "theta")
        #
        self.assertTrue(
            np.allclose([0.0954,0.4641,2.2578,10.9836,53.4312], res['taus'], atol=0.001),
            "tbins")
        self.assertTrue( np.allclose([0,7,14,21,28,35,42], res['tbins' ]), "tbins" )
        #
        self.assertAlmostEqual( 0.095, res['tau_min' ], 3, "tau_min" )
        self.assertAlmostEqual(53.431, res['tau_max' ], 3, "tau_max" )
        #
        self.assertAlmostEqual( 4.90,  res['lmin' ], 2, "lmin" )
        self.assertAlmostEqual( 6.45,  res['lave' ], 2, "lave" )
        self.assertAlmostEqual( 8.00,  res['lmax' ], 2, "lmax" )
        #
        self.assertAlmostEqual( 0.150, res['qmin' ], 3, "qmin" )
        self.assertAlmostEqual( 0.186, res['qave' ], 3, "qave" )
        self.assertAlmostEqual( 0.245, res['qmax' ], 3, "qmax" )
        #
        self.assertEqual("q=0.150/A, lambda=8.00A, phi=11.0deg", res['info' ], "qmax" )


if __name__ == "__main__":
    unittest.main(exit=False, verbosity=2)
Loading