예제 #1
0
파일: Filter.py 프로젝트: morgenst/pyfluka
    def __init__(self, **kwargs):
        """
        Constructor

        Required parameters:
        - quantity: quantity to be filtered on
        - threshold: either absolute or relative filter
            - absolute: requires unit compatible with quantity
            - relative: applies filter on relative contribute to sum
        :param dict kwargs: configuration parameter
        :raises InvalidInputError: if required arguments are not provided
        """
        try:
            self.quantity = kwargs["quantity"]
        except KeyError:
            raise InvalidInputError("Try to initialise Filter without specifying quantity.")
        try:
            try:
                self.threshold = float(kwargs["threshold"])
                self.type = "rel"
            except ValueError:
                self.threshold = PQ.create_generic(self.quantity + "Threshold", kwargs["threshold"])
                self.type = "abs"
        except KeyError:
            raise InvalidInputError("Try to initialise Filter without specifying threshold.")
예제 #2
0
 def __init__(self, *args, **kwargs):
     try:
         setattr(self, kwargs['type'], True)
         self.multiplier = kwargs['multiplier']
         tmp_multiplicand = kwargs['multiplicand']
         if tmp_multiplicand.count("const"):
             tmp_multiplicand = tmp_multiplicand.replace("const:", "")
             try:
                 self.multiplicand = float(tmp_multiplicand)
             except ValueError:
                 self.multiplicand = PQ.create_generic("multiplicand", tmp_multiplicand)
         elif tmp_multiplicand.count("builtin"):
             self.multiplicand = self.find_builtin(tmp_multiplicand.replace("builtin:", ""))
         elif tmp_multiplicand.count("global:"):
             if tmp_multiplicand.count("mass"):
                 self.multiplicand = "mass"
                 self.dict = True
             else:
                 self.multiplicand = _global_data[tmp_multiplicand.replace("global:", "")]
                 self.scalar = True
         else:
             self.multiplicand = tmp_multiplicand
         self.product = kwargs['product'].split(":")[0]
         m = importlib.import_module("pyfluka.utils.PhysicsQuantities")
         try:
             self.quantity = getattr(m, kwargs['product'].split(":")[-1])
         except AttributeError:
             self.quantity = partial(getattr(m, 'create_generic'), kwargs['product'].split(":")[-1])
         self.current_detector = None
     except KeyError:
         raise InvalidInputError("Unable to initialise multiplication operator")
예제 #3
0
 def test_generic_pq(self):
     generic_quantity = PQ.create_generic("foo", 10., 2., ureg.Bq)
     self.assertEqual(generic_quantity.__class__.__name__, "foo")
     self.assertEqual(generic_quantity.val, ureg.Quantity(10., ureg.Bq))
     self.assertEqual(generic_quantity.unc, ureg.Quantity(2., ureg.Bq))
예제 #4
0
 def test_division_scalar_int_lhs(self):
     activity = PQ.Activity(10., 2.)
     result = 5 / activity
     self.assertEqual(result, PQ.create_generic("InvertedActivity", 0.5, 0., 1. / ureg.Bq))
예제 #5
0
 def test_float_dimensionless_gt_true(self):
     q1 = PQ.create_generic("dimless", 30.)
     q2 = 20.
     self.assertTrue(q1 > q2)
예제 #6
0
 def test_get_header_generic(self):
     q = PQ.create_generic("gen", 100., unit=ureg.Bq)
     self.assertEqual('{:Lsu}'.format(q), " [$Bq$]")
예제 #7
0
 def test_generic_formatting_latex(self):
     q = PQ.create_generic("generic", 100., 0., ureg.Bq)
     self.assertEqual("$100.0 Bq \pm 0.0 Bq$", '{:L}'.format(q))
예제 #8
0
 def test_generic_comparison_2(self):
     generic = PQ.create_generic("foo", 10000., 2000., ureg.Bq / ureg.kg)
     res = PQ.SpecificActivity(10., 2., ureg.Bq / ureg.g)
     self.assertEqual(generic, res)
예제 #9
0
 def testActivityUnEqualsDiffUnc(self):
     val1 = PQ.Activity(20., 2.1)
     val2 = PQ.Activity(20., 3.1)
     self.assertNotEqual(val1, val2)
예제 #10
0
 def testActivityEqualsUnc(self):
     val1 = PQ.Activity(20., 2.1)
     val2 = PQ.Activity(20., 2.1)
     self.assertEqual(val1, val2)
예제 #11
0
 def testActivityUnEqualsDiffUnit(self):
     val1 = PQ.Activity(20.)
     val2 = PQ.Activity(20., unit=ureg.kBq)
     self.assertNotEqual(val1, val2)
예제 #12
0
 def testActivityUnEqualsSameUnit(self):
     val1 = PQ.Activity(20.)
     val2 = PQ.Activity(10.)
     self.assertNotEqual(val1, val2)
예제 #13
0
 def setUp(self):
     self.config = {"quantity": "Activity", "threshold": 0.01}
     self.filter = Filter(**self.config)
     self.data = {"det1": {PQ.Isotope(3, 1, 0): StoredData(PQ.Activity(10.)),
                           PQ.Isotope(4, 2, 0): StoredData(PQ.Activity(2.)),
                           PQ.Isotope(22, 11, 0): StoredData(PQ.Activity(5.))}}
예제 #14
0
 def test_relative_filter_resnuc_data_single_pass(self):
     config = {"quantity": "Activity", "threshold": "0.5"}
     fil = Filter(**config)
     fil.invoke(self.data)
     self.assertEqual(self.data, {"det1": {PQ.Isotope(3, 1, 0): StoredData(PQ.Activity(10.))}})
예제 #15
0
 def test_config_absolute_threshold(self):
     config = {"quantity": "Activity", "threshold": "2.0 Bq"}
     fil = Filter(**config)
     self.assertEqual(fil.threshold, PQ.create_generic("ActivityThreshold", 2.0, 0., ureg.Bq))
예제 #16
0
 def test_generic_multiplication(self):
     generic_quantity = PQ.create_generic("foo", 10., 2., ureg.Bq)
     new_value = generic_quantity * 5.
     self.assertTrue(new_value.val, ureg.Quantity(50., ureg.Bq))
예제 #17
0
 def test_generic_comparison(self):
     generic = PQ.create_generic("foo", 10., 2., ureg.Bq)
     activity = PQ.Activity(10., 2., ureg.Bq)
     self.assertEqual(generic, activity)
예제 #18
0
 def testIsotopeCreationFromString(self):
     isotope = PQ.Isotope(3, "H")
     self.assertEqual([isotope.A, isotope.Z], [3, 1])
예제 #19
0
 def test_copy_ctor_generic(self):
     generic = PQ.create_generic("foo", 10000., 2000., ureg.Bq / ureg.kg)
     res = PQ.SpecificActivity(10., 2., ureg.Bq / ureg.g)
     val = PQ.SpecificActivity(generic)
     self.assertEqual(val, res)
예제 #20
0
 def testSummationActivityDifferentUnitsEquals(self):
     val1 = PQ.Activity(20., 2.)
     val2 = PQ.Activity(10., 3.)
     unc = sqrt(pow(2., 2) + pow(3., 2))
     res = PQ.Activity(30., unc)
     self.assertEqual(val1 + val2, res)
예제 #21
0
 def test_generic_formatting_latex_nounit(self):
     q = PQ.create_generic("generic", 100., 0., ureg.Bq)
     self.assertEqual("$100.0 \pm 0.0 $", '{:Lnu}'.format(q))
예제 #22
0
 def testLatexString(self):
     val = PQ.Activity(20., 2.)
     self.assertEqual('{:L}'.format(val), "$20.0 Bq \pm 2.0 Bq$")
예제 #23
0
 def test_comparison_dimension_check_pass_float(self):
     q1 = PQ.create_generic("dimless", 20.)
     q2 = 10.
     self.assertFalse(q1._validate_comparison_input(q2))
예제 #24
0
 def test_string(self):
     val = PQ.Activity(20., 2.)
     self.assertEqual('{!s}'.format(val), "Activity: 20.0 Bq +- 2.0 Bq")
예제 #25
0
 def test_float_dimensionless_gt_false(self):
     q1 = PQ.create_generic("dimless", 1.)
     q2 = 5.
     self.assertFalse(q1 > q2)
예제 #26
0
 def test_latex_no_units(self):
     val = PQ.Activity(20., 2.)
     self.assertEqual('{:Lnu}'.format(val), "$20.0 \pm 2.0 $")