Exemplo n.º 1
0
    def test_ignore_dim(self):

        a = assign(3,1,"m")
        q = assign(a**2, None, "s", ignore_dim=True)

        self.assertEqual(q.prefer_unit, si["s"])
        self.assertEqual(q.dim, Dimension(time=1))
Exemplo n.º 2
0
    def test_ignore_dim(self):

        a = assign(3, 1, "m")
        q = assign(a**2, None, "s", ignore_dim=True)

        self.assertEqual(q.prefer_unit, si["s"])
        self.assertEqual(q.dim, Dimension(time=1))
Exemplo n.º 3
0
    def test_simple_calculation(self):

        r = assign(4, 1, "m", "r")

        q = assign(2 * pi * r**3, None, None, "V", "Volumen")

        self.assertEqual(q.name, "V")
        self.assertEqual(q.longname, "Volumen")
        self.assertAlmostEqual(q.value, 2 * np.pi * 4**3)
        self.assertEqual(q.prefer_unit, None)
        self.assertEqual(q.value_formula, 2 * pi * r**3)
        self.assertEqual(q.dim, Dimension(length=3))
        self.assertAlmostEqual(q.error, 2 * np.pi * 3 * 4**2 * 1)
Exemplo n.º 4
0
    def test_simple_calculation(self):

        r = assign(4,1,"m","r")

        q = assign(2*pi*r**3, None, None, "V", "Volumen")

        self.assertEqual(q.name, "V")
        self.assertEqual(q.longname, "Volumen")
        self.assertAlmostEqual(q.value, 2*np.pi*4**3)
        self.assertEqual(q.prefer_unit, None)
        self.assertEqual(q.value_formula, 2*pi*r**3)
        self.assertEqual(q.dim, Dimension(length=3))
        self.assertAlmostEqual(q.error, 2*np.pi*3*4**2*1)
Exemplo n.º 5
0
def interpret (program, namespace):
	"""
	executes the program
	"""
	for command in program:
		if command.parseinfo.rule == "assignment":
			value = command.value
			error = command.error
			if value is not None:
			    value = parse_expr(value, namespace)
			if error is not None:
			    error = parse_expr(error, namespace)

			namespace[command.name] = assign (value, error, command.unit, command.name, command.longname)

		elif command.parseinfo.rule == "multi_assignment":
			# collect columns:
			columns = {}
			for columnIndex in range(len(command.header)):
				values = []
				for row in command.rows:
					value = parse_expr(row[columnIndex], namespace)
					values.append(value)
				columns[command.header[columnIndex].name] = {
				 "header": command.header[columnIndex],
				 "values": values
				}
			# pair value columns with err-columns:
			for name in columns:
				if name.endswith("_err"):
					continue
				header = columns[name]["header"]
				values = columns[name]["values"]
				if name + "_err" in columns:
					errorColumn = columns[name + "_err"]
					if errorColumn["header"].error is not None:
						raise RuntimeError("Variables with _err notation cannot use the <...> notation:  %s"%errorColumn["header"].name)
					if errorColumn["header"].longname is not None:
						raise RuntimeError("Variables with _err notation cannot have a long name: %s"%errorColumn["header"].longname)
					if header.error is not None:
						raise RuntimeError("Variables with a corresponding _err column cannot have a general error specified: %s"%header.name)
					namespace[name] = assign (values, errorColumn["values"], header.unit, name, header.longname, None, errorColumn["header"].unit)
				else:
					namespace[header.name] = assign (values, header.error, header.unit, header.name, header.longname)

		elif command.parseinfo.rule == "python_code":
			code = '\n'.join(command.code)
			exec (code, None, namespace)
		else:
			raise RuntimeError("Unknown syntactic command type")
	return namespace
Exemplo n.º 6
0
    def test_calculataion_two_variables(self):

        r = assign(4,1,"m","r")
        h = assign(3,2,"m","h")

        q = assign(2*pi*r**2*h, None, None, "V", "Volumen")

        self.assertEqual(q.name, "V")
        self.assertEqual(q.longname, "Volumen")
        self.assertAlmostEqual(q.value, 2*np.pi*4**2*3)
        self.assertEqual(q.prefer_unit, None)
        self.assertEqual(q.value_formula, 2*pi*r**2*h)
        self.assertEqual(q.dim, Dimension(length=3))
        self.assertAlmostEqual(q.error, 2*np.pi*np.sqrt((2*4*3*1)**2+(4**2*2)**2))
Exemplo n.º 7
0
    def test_calculataion_two_variables(self):

        r = assign(4, 1, "m", "r")
        h = assign(3, 2, "m", "h")

        q = assign(2 * pi * r**2 * h, None, None, "V", "Volumen")

        self.assertEqual(q.name, "V")
        self.assertEqual(q.longname, "Volumen")
        self.assertAlmostEqual(q.value, 2 * np.pi * 4**2 * 3)
        self.assertEqual(q.prefer_unit, None)
        self.assertEqual(q.value_formula, 2 * pi * r**2 * h)
        self.assertEqual(q.dim, Dimension(length=3))
        self.assertAlmostEqual(
            q.error, 2 * np.pi * np.sqrt((2 * 4 * 3 * 1)**2 + (4**2 * 2)**2))
Exemplo n.º 8
0
    def test_no_error(self):
        q = assign(3, None, "s")

        self.assertAlmostEqual(q.value, 3)
        self.assertEqual(q.prefer_unit, si["s"])
        self.assertEqual(q.value_formula, None)
        self.assertEqual(q.dim, Dimension(time=1))
        self.assertEqual(q.error, None)
        self.assertEqual(q.error_formula, None)
Exemplo n.º 9
0
    def test_no_error(self):
        q = assign(3, None, "s")

        self.assertAlmostEqual(q.value, 3)
        self.assertEqual(q.prefer_unit, si["s"])
        self.assertEqual(q.value_formula, None)
        self.assertEqual(q.dim, Dimension(time=1))
        self.assertEqual(q.error, None)
        self.assertEqual(q.error_formula, None)
Exemplo n.º 10
0
    def test_simple_assign(self):

        q = assign(12, 1 / 1000, "mm", "r", "Radius")

        self.assertEqual(q.name, "r")
        self.assertEqual(q.longname, "Radius")
        self.assertAlmostEqual(q.value, 0.012)
        self.assertEqual(q.prefer_unit, si["mm"])
        self.assertEqual(q.value_formula, None)
        self.assertEqual(q.dim, Dimension(length=1))
        self.assertAlmostEqual(q.error, 1e-6)
        self.assertEqual(q.error_formula, None)
Exemplo n.º 11
0
    def test_different_units(self):

        q = assign(14, 23, None, "r", "Radius", "dm", "cm")

        self.assertEqual(q.name, "r")
        self.assertEqual(q.longname, "Radius")
        self.assertAlmostEqual(q.value, 1.4)
        self.assertEqual(q.prefer_unit, si["dm"])
        self.assertEqual(q.value_formula, None)
        self.assertEqual(q.dim, Dimension(length=1))
        self.assertAlmostEqual(q.error, 0.23)
        self.assertEqual(q.error_formula, None)
Exemplo n.º 12
0
    def test_different_units(self):

        q = assign(14, 23, None, "r", "Radius", "dm", "cm")

        self.assertEqual(q.name, "r")
        self.assertEqual(q.longname, "Radius")
        self.assertAlmostEqual(q.value, 1.4)
        self.assertEqual(q.prefer_unit, si["dm"])
        self.assertEqual(q.value_formula, None)
        self.assertEqual(q.dim, Dimension(length=1))
        self.assertAlmostEqual(q.error, 0.23)
        self.assertEqual(q.error_formula, None)
Exemplo n.º 13
0
    def test_simple_assign(self):

        q = assign(12, 1/1000, "mm", "r", "Radius")

        self.assertEqual(q.name, "r")
        self.assertEqual(q.longname, "Radius")
        self.assertAlmostEqual(q.value, 0.012)
        self.assertEqual(q.prefer_unit, si["mm"])
        self.assertEqual(q.value_formula, None)
        self.assertEqual(q.dim, Dimension(length=1))
        self.assertAlmostEqual(q.error, 1e-6)
        self.assertEqual(q.error_formula, None)
Exemplo n.º 14
0
def interpret(program, namespace):
    """
	executes the program
	"""
    for command in program:
        if command.parseinfo.rule == "assignment":
            value = command.value
            error = command.error
            if value is not None:
                value = parse_expr(value, namespace)
            if error is not None:
                error = parse_expr(error, namespace)

            namespace[command.name] = assign(value, error, command.unit,
                                             command.name, command.longname)

        elif command.parseinfo.rule == "multi_assignment":
            # collect columns:
            columns = {}
            for columnIndex in range(len(command.header)):
                values = []
                for row in command.rows:
                    value = parse_expr(row[columnIndex], namespace)
                    values.append(value)
                columns[command.header[columnIndex].name] = {
                    "header": command.header[columnIndex],
                    "values": values
                }
            # pair value columns with err-columns:
            for name in columns:
                if name.endswith("_err"):
                    continue
                header = columns[name]["header"]
                values = columns[name]["values"]
                if name + "_err" in columns:
                    errorColumn = columns[name + "_err"]
                    if errorColumn["header"].error is not None:
                        raise RuntimeError(
                            "Variables with _err notation cannot use the <...> notation:  %s"
                            % errorColumn["header"].name)
                    if errorColumn["header"].longname is not None:
                        raise RuntimeError(
                            "Variables with _err notation cannot have a long name: %s"
                            % errorColumn["header"].longname)
                    if header.error is not None:
                        raise RuntimeError(
                            "Variables with a corresponding _err column cannot have a general error specified: %s"
                            % header.name)
                    namespace[name] = assign(values, errorColumn["values"],
                                             header.unit, name,
                                             header.longname, None,
                                             errorColumn["header"].unit)
                else:
                    namespace[header.name] = assign(values, header.error,
                                                    header.unit, header.name,
                                                    header.longname)

        elif command.parseinfo.rule == "python_code":
            code = '\n'.join(command.code)
            exec(code, None, namespace)
        else:
            raise RuntimeError("Unknown syntactic command type")
    return namespace
Exemplo n.º 15
0
 def test_multi_dimensional_error_duplication(self):
     q = assign([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [5, 4])
     self.assertTrue(
         np.allclose(q.error, [[[5, 4], [5, 4]], [[5, 4], [5, 4]]]))
Exemplo n.º 16
0
    def test_duplicating_error(self):

        q = assign([4, 5, 6], 0.5)

        self.assertEqual(len(q.error), 3)
        self.assertTrue(np.allclose(q.error, [0.5, 0.5, 0.5]))
Exemplo n.º 17
0
    def test_dimension_mismatch_to_calculated_dimension(self):

        a = assign(3, 1, "m")
        self.assertRaises(RuntimeError, assign, a**2, None, unit="s")
Exemplo n.º 18
0
    def test_duplicating_error(self):

        q = assign([4,5,6], 0.5)

        self.assertEqual(len(q.error), 3)
        self.assertTrue(np.allclose(q.error,[0.5,0.5,0.5]))
Exemplo n.º 19
0
    def test_dimension_mismatch_to_calculated_dimension(self):

        a = assign(3,1,"m")
        self.assertRaises(RuntimeError, assign, a**2, None, unit="s")
Exemplo n.º 20
0
 def test_ignore_dim_factor(self):
     a = assign(3, 1, None, "m")
     q = assign(a**2, None, "ms", ignore_dim=True)
     self.assertAlmostEqual(q.value, 0.009)
     self.assertAlmostEqual(q.error, 0.006)
Exemplo n.º 21
0
 def test_ignore_dim_factor(self):
     a = assign(3,1,None,"m")
     q = assign(a**2, None, "ms", ignore_dim=True)
     self.assertAlmostEqual(q.value, 0.009)
     self.assertAlmostEqual(q.error, 0.006)
Exemplo n.º 22
0
 def test_multi_dimensional_error_duplication(self):
     q = assign([[[1,2],[3,4]],[[5,6],[7,8]]], [5,4])
     self.assertTrue(np.allclose(q.error, [[[5,4],[5,4]],[[5,4],[5,4]]]))