Exemple #1
0
    def testUnparse(self):
        s = Scalar(2)

        self.assertEqual(s.unparse(s.parse('')), '')
        self.assertEqual(s.unparse(s.parse('kg^2')), 'kg^2')
        self.assertEqual(s.unparse(s.parse('N*m')), 'N*m')
        self.assertEqual(s.unparse(s.parse('J*K/A*s')), 'J*K/A*s')
Exemple #2
0
    def testParse(self):
        s = Scalar(2)

        self.assertEqual(s.parse(''), [Unit('')])
        self.assertEqual(s.parse('kg^2'), [Unit('kg^2')])
        self.assertEqual(s.parse('kg^2*m^3/s^4'),
                         [Unit('kg^2'),
                          Unit('m^3'), Unit('s^-4')])
Exemple #3
0
def convert_to_scalar(m):
    if isinstance(m, Scalar):
        return m
    if isinstance(m, Matrix):
        return Matrix(m.rows, m.cols, lambda r, c: Scalar(m[r, c]))
    if isinstance(m, float) or isinstance(m, int):
        return Scalar(m)
    return m
Exemple #4
0
    def testIsTemperature(self):
        s = Scalar(2, 'K')

        self.assertTrue(s.isTemperature(s.parse('K')))
        self.assertTrue(s.isTemperature(s.parse('°C')))
        self.assertTrue(s.isTemperature(s.parse('°F')))

        self.assertFalse(s.isTemperature(s.parse('kg')))
        self.assertFalse(s.isTemperature(s.parse('K/s')))
Exemple #5
0
    def testPower(self):
        # Scalar with number
        s = Scalar(2, 'kg')

        self.assertEqual(s**3, Scalar(8, 'kg^3'))

        # Scalar with iterables
        s = Scalar([2, 3], 'cm/s')

        self.assertEqual(s**3, Scalar([8, 27], 'cm^3/s^3'))
Exemple #6
0
    def testAbsolute(self):
        # Scalar with number
        s = Scalar(2)
        t = Scalar(-3)

        self.assertEqual(abs(s), 2)
        self.assertEqual(abs(t), 3)

        # Scalar with iterable
        s = Scalar([1, -2])

        self.assertEqual(abs(s).tolist(), [1, 2])
Exemple #7
0
def get_encoder(type, **kargs):
    """
	Creates an encoder of the appropriate type and returns an instance of it.
	
	@param type: The type of encoder to create. The supported types are:
	"unity", "threshold", "scalar", "category", and "multi".
	
	@param kargs: Any keyword arguments to pass to the encoder.
	
	@return: An encoder instance.
	
	@raise UnsupportedEncoder: Raised if the requested encoder is not
	supported.
	"""

    t = type.lower()

    if t == 'unity':
        return Unity(**kargs)
    elif t == 'threshold':
        return Threshold(**kargs)
    elif t == 'scalar':
        return Scalar(**kargs)
    elif t == 'multi':
        return Multi(**kargs)
    elif t == 'category':
        return Category(**kargs)
    else:
        raise UnsupportedEncoder(t)
def data_transform(filename):

    df = pd.read_csv(filename)
    # df = df[100060:100100] # for test purpose only!
    df.reset_index(drop=True, inplace=True)

    target = df[['TARGET']]

    numeric_column = Numeric_Column
    categorical_column = Categorical_Column

    for column in numeric_column:
        df[column] = Scalar().transform(df[column], column)
    '''
    To support various length RNN, make sure all timesteps for masked are 0
    '''
    for column in categorical_column:
        df.loc[:, column] = df[column] + 1
    # avoid 0 for categorical variables

    # print df.head()

    for column in numeric_column + ['NUM_INSTALMENT_VERSION']:
        df.loc[df.MISSING_VALUE == 3, column] = 0
    df.loc[df.MISSING_VALUE == 3, 'MISSING_VALUE'] = 0

    numeric_ft = df[numeric_column]
    categorical_ft = df[categorical_column]

    # print len(df[df.MISSING_VALUE == 0])

    return numeric_ft, categorical_ft, target
Exemple #9
0
    def calculate_Ricci_scalar(self,
                               ricci_tensor=None,
                               g=None,
                               g_inv=None,
                               simplify=None):
        """Calculate the Ricci scalar for a given Ricci tensor. 

        Parameters: 
        -----------

        ricci_tensor : dict, sympy.core.symbols.Symbol 
            Pass this function the output of ``self.calculate_Ricci_tensor()``. 

        g : dict, sympy.core.symbols.Symbol 
            Pass this function the metric to be studied. 

        index_dict : dict, string 
            A dictionary with the names of the indices as strings. 

        Returns: 
        --------
        ricci_scalar : sympy.core.symbols.Symbol 
        """
        if g_inv == None:
            print('Trying to invert the metric...')
            g_inv = self.invert_metric(g=g)
            print('Succesfully inverted the metric.')

        ricci_scalar = Scalar(index_dict=ricci_tensor.index_dict)

        dim = len(ricci_tensor.index_dict)
        for i in range(dim):
            for j in range(dim):
                m = ricci_tensor.index_dict[i]  # mu
                n = ricci_tensor.index_dict[j]  # nu
                mn = m + n
                try:
                    ricci_scalar.elements[
                        ''] += g_inv.elements[mn] * ricci_tensor.elements[mn]
                except ZeroDivisionError:
                    ppp = None
            if simplify is True:
                ricci_scalar.elements[''] = sp.simplify(
                    ricci_scalar.elements[''])
        return ricci_scalar
def test_sub_scalars():
    m1 = Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
    m1sub2_correct = Matrix(3, 2, [[-1, 0], [1, 2], [3, 4]])
    m1sub2 = m1 - 2
    test_assert(m1sub2.compare(m1sub2_correct))
    m1sub2 = m1 - 2.0
    test_assert(m1sub2.compare(m1sub2_correct))
    m1sub2 = m1 - Scalar(2)
    test_assert(m1sub2.compare(m1sub2_correct))
def test_add_scalars():
    m1 = Matrix(3, 2, [[1, 2], [3, 4], [5, 6]])
    m1plus2_correct = Matrix(3, 2, [[3, 4], [5, 6], [7, 8]])
    m1plus2 = m1 + 2
    test_assert(m1plus2.compare(m1plus2_correct))
    m1plus2 = m1 + 2.0
    test_assert(m1plus2.compare(m1plus2_correct))
    m1plus2 = m1 + Scalar(2)
    test_assert(m1plus2.compare(m1plus2_correct))
Exemple #12
0
    def testConversionFactor(self):
        s = Scalar(2)

        self.assertEqual(s.conversionFactor(Unit('mm'), Unit('m')), 1e-3)
        self.assertEqual(s.conversionFactor(Unit('kg^2'), Unit('mg^2')), 1e12)
        self.assertEqual(s.conversionFactor(Unit('s^-1'), Unit('ms^-1')), 1e-3)

        with self.assertRaises(ScalarError):
            s.conversionFactor(Unit('mm'), Unit('m^2'))
Exemple #13
0
    def testInequality(self):
        # Scalars with numbers
        s = Scalar(2, 'kg')
        t = Scalar(2, 'kg')
        u = Scalar(2000, 'g')
        v = Scalar(2, 'kg*kg/kg')
        w = Scalar(2, 'kg^2')

        self.assertFalse(s != t)
        self.assertFalse(s != u)
        self.assertFalse(s != v)
        self.assertTrue(s != w)

        # Scalars with iterables
        s = Scalar([1, 2], 'kg')
        t = Scalar([1, 2], 'kg')
        u = Scalar([1000, 2000], 'g')
        v = Scalar([1, 2], 'kg*kg/kg')
        w = Scalar([1, 2], 'kg^2')

        self.assertFalse(s != t)
        self.assertFalse(s != u)
        self.assertFalse(s != v)
        self.assertTrue(s != w)
Exemple #14
0
 def getScalar(self,name,unit=None):
   node = self.file.getNode("/estimators",name)
   try:
     unitName = node.getAttr("unit")
     dataUnit = Unit.getByName(unitName[0])
   except:
     if unit == None:
       dataUnit = None
     else:
       dataUnit = unit.native
   nstep = self.file.getNodeAttr("/estimators","nstep")[0]
   if unit == None:
     data = node.read()[:nstep]
   else:
     data = unit.convert(node.read()[:nstep],dataUnit)
   scalar = Scalar(name,data,unit)
   return scalar
Exemple #15
0
    def testSubtraction(self):
        # Subtraction with numbers
        s = Scalar(2, 'cm^2')
        t = Scalar(3, 'cm^2')
        u = Scalar(4, 'm^2')
        v = Scalar(5, 'm')

        # Subtraction with like units
        self.assertEqual(s - t, Scalar(-1, 'cm^2'))
        self.assertEqual(t - s, Scalar(1, 'cm^2'))

        # Subtraction with unit conversion
        self.assertEqual(s - u, Scalar(-39998, 'cm^2'))
        self.assertEqual(u - s, Scalar(3.9998, 'm^2'))

        # Subtraction with unlike units
        with self.assertRaises(ScalarError):
            s - v

        with self.assertRaises(ScalarError):
            v - s

        # Subtraction with numbers
        with self.assertRaises(TypeError):
            s - 2

        with self.assertRaises(TypeError):
            2 - s

        # Scalars with iterables
        s = Scalar([1, 2], 'cm^2')
        t = Scalar([3, 4], 'cm^2')
        u = Scalar([5, 6], 'm^2')
        v = Scalar([7, 8], 'm')

        # Addition with like units
        self.assertEqual(s - t, Scalar([-2, -2], 'cm^2'))
        self.assertEqual(t - s, Scalar([2, 2], 'cm^2'))

        # Addition with unit conversion
        self.assertEqual(s - u, Scalar([-49999, -59998], 'cm^2'))
        self.assertEqual(u - s, Scalar([49999, 59998], 'cm^2'))

        # Addition with unlike units
        with self.assertRaises(ScalarError):
            s - v

        with self.assertRaises(ScalarError):
            v - s

        # Addition with numbers
        with self.assertRaises(TypeError):
            s - np.array([1, 2])

        with self.assertRaises(TypeError):
            np.array([1, 2]) - s
Exemple #16
0
    def testInit(self):
        # Integer value
        s = Scalar(2)

        self.assertEqual(s.values, 2)
        self.assertEqual(s.units, '')

        self.assertEqual(s.latex, '')
        self.assertEqual(s.parsed, [Unit('')])
        self.assertEqual(s.unparsed, '')

        # Float value
        s = Scalar(2.0)

        self.assertEqual(s.values, 2.0)
        self.assertEqual(s.units, '')

        self.assertEqual(s.latex, '')
        self.assertEqual(s.parsed, [Unit('')])
        self.assertEqual(s.unparsed, '')

        # Iterable value
        s = Scalar([1, 2, 3])

        self.assertListEqual(s.values.tolist(), [1, 2, 3])
        self.assertEqual(s.units, '')

        self.assertEqual(s.latex, '')
        self.assertEqual(s.parsed, [Unit('')])
        self.assertEqual(s.unparsed, '')

        # Single unit
        s = Scalar(2, 'kg^2')

        self.assertEqual(s.values, 2)
        self.assertEqual(s.units, 'kg^2')

        self.assertEqual(s.latex, 'kg^{2}')
        self.assertEqual(s.parsed, [Unit('kg^2')])
        self.assertEqual(s.unparsed, 'kg^2')

        # Multiple units
        s = Scalar(2, 'kg^2*m^3/A*s^4')

        self.assertEqual(s.values, 2)
        self.assertEqual(s.units, 'kg^2*m^3/A*s^4')

        self.assertEqual(s.latex, 'kg^{2}.m^{3}.A^{-1}.s^{-4}')
        self.assertEqual(
            s.parsed, [Unit('kg^2'),
                       Unit('m^3'),
                       Unit('A^-1'),
                       Unit('s^-4')])
        self.assertEqual(s.unparsed, 'kg^2*m^3/A*s^4')

        # Incorrect inputs
        with self.assertRaises(ScalarError):
            Scalar('a')

        with self.assertRaises(ScalarError):
            Scalar(2, 3)
Exemple #17
0
    def testBase(self):
        # Standard SI unit
        s = Scalar(2, 'mm^2')

        self.assertTupleEqual(s.base(), ([Unit('m^2')], 1e-6))

        # Special case for "kg"
        s = Scalar(2, 'g^2')

        self.assertTupleEqual(s.base(), ([Unit('kg^2')], 1e-6))

        # Derived unit
        s = Scalar(2, 'kN^2')

        self.assertTupleEqual(
            s.base(), ([Unit('kg^2'), Unit('m^2'),
                        Unit('s^-4')], 1e6))

        # Multiple units
        s = Scalar(2, 'kg*N^2/s^2')

        self.assertTupleEqual(s.base(), ([
            Unit('kg'),
            Unit('kg^2'),
            Unit('m^2'),
            Unit('s^-4'),
            Unit('s^-2')
        ], 1e0))
Exemple #18
0
    def testString(self):
        s = Scalar(2, 'kg^2')

        self.assertEqual(str(s), '2.0 kg^2')
Exemple #19
0
 def createChain(str):
     links=list(map(lambda c: chains[c] if c.isupper() else Scalar(c),str))
     return Chain(links)
Exemple #20
0
    def testDivision(self):
        # Scalars with numbers
        s = Scalar(100, 'cm^2')
        t = Scalar(2, 'cm^2')
        u = Scalar(4, 'm')
        v = Scalar(5, 'kg/s')

        # Division with like units
        self.assertEqual(s / t, Scalar(50, ''))
        self.assertEqual(t / s, Scalar(0.02, ''))

        # Division with unit conversion
        self.assertEqual(s / u, Scalar(0.25, 'cm'))
        self.assertEqual(u / s, Scalar(400, '/m'))

        # Division with unlike units
        self.assertEqual(s / v, Scalar(20, 'cm^2*s/kg'))
        self.assertEqual(v / s, Scalar(0.05, 'kg/s*cm^2'))

        # Division with numbers
        self.assertEqual(2 / s, Scalar(0.02, '/cm^2'))
        self.assertEqual(s / 2, Scalar(50, 'cm^2'))

        # Scalars with iterables
        s = Scalar([100, 100], 'cm^2')
        t = Scalar([2, 4], 'cm^2')
        u = Scalar([5, 10], 'm')
        v = Scalar([20, 25], 'kg/s')

        # Division with like units
        self.assertEqual(s / t, Scalar([50, 25], ''))
        self.assertEqual(t / s, Scalar([0.02, 0.04], ''))

        # Division with unit conversion
        self.assertEqual(s / u, Scalar([20, 10], 'cm^2/m'))
        self.assertEqual(u / s, Scalar([500, 1000], '/m'))

        # Division with unlike units
        self.assertEqual(s / v, Scalar([5, 4], 'cm^2*s/kg'))
        self.assertEqual(v / s, Scalar([0.2, 0.25], 'kg/s*cm^2'))

        # Division with numbers
        self.assertEqual(2 / s, Scalar([0.02, 0.02], '/cm^2'))
        self.assertEqual(s / 2, Scalar([50, 50], 'cm^2'))
        self.assertEqual(s / np.array([2, 4]), Scalar([50, 25], 'cm^2'))
Exemple #21
0
    def testConvertTemperature(self):
        # Conversion from kelvin
        s = Scalar(2, 'K')

        self.assertEqual(s.convertTemperature(s.parse('K'), s.parse('°C')),
                         -271.15)
        self.assertEqual(s.convertTemperature(s.parse('K'), s.parse('°F')),
                         -456.07)

        # Conversion from celsius
        s = Scalar(2, '°C')

        self.assertEqual(s.convertTemperature(s.parse('°C'), s.parse('K')),
                         275.15)
        self.assertEqual(s.convertTemperature(s.parse('°C'), s.parse('°F')),
                         35.6)

        # Conversion from fahrenheit
        s = Scalar(2, '°F')

        self.assertAlmostEqual(
            s.convertTemperature(s.parse('°F'), s.parse('K')), 256.4833333)
        self.assertAlmostEqual(
            s.convertTemperature(s.parse('°F'), s.parse('°C')), -16.6666667)
Exemple #22
0
    def testSimplify(self):
        s = Scalar(2)

        # Without converting to base units
        self.assertTupleEqual(s.simplify(s.parse('mm^2')),
                              ([Unit('mm^2')], 1e0))
        self.assertTupleEqual(s.simplify(s.parse('kg*m/m')),
                              ([Unit('kg')], 1e0))
        self.assertTupleEqual(s.simplify(s.parse('kg*m^3/m')),
                              ([Unit('kg'), Unit('m^2')], 1e0))
        self.assertTupleEqual(s.simplify(s.parse('kg*s/ms^2')),
                              ([Unit('kg'), Unit('s^-1')], 1e6))

        # With converting to base units
        self.assertTupleEqual(s.simplify(s.parse('mm^2'), base=True),
                              ([Unit('m^2')], 1e-6))
        self.assertTupleEqual(s.simplify(s.parse('N*s^2'), base=True),
                              ([Unit('kg'), Unit('m')], 1e0))
        self.assertTupleEqual(
            s.simplify(s.parse('N*m'), base=True),
            ([Unit('kg'), Unit('m^2'), Unit('s^-2')], 1e0))
        self.assertTupleEqual(s.simplify(s.parse('N/mm'), base=True),
                              ([Unit('kg'), Unit('s^-2')], 1e3))
Exemple #23
0
def test_simple():
  test_assert(3.0 == (Scalar(1) + Scalar(2)).value, "1 + 2 == 3")
  test_assert(3.0 == (1 + Scalar(2)).value, "1 + 2 == 3")
  test_assert(3.0 == (Scalar(1) + 2).value, "1 + 2 == 3")

  test_assert(3.5 == (Scalar(.5) * Scalar(7)).value, ".5 * 7 = 3.5")
  test_assert(3.5 == (.5 * Scalar(7)).value, ".5 * 7 = 3.5")
  test_assert(3.5 == (Scalar(.5) * 7).value, ".5 * 7 = 3.5")

  test_assert(-1 == (Scalar(1) - Scalar(2)).value, "1 - 2 == -1")
  test_assert(-1 == (1 - Scalar(2)).value, "1 - 2 == -1")
  test_assert(-1 == (Scalar(1) - 2).value, "1 - 2 == -1")

  test_assert(3.4 == (Scalar(17) / Scalar(5)).value, "17 / 5 == 3.4")
  test_assert(3.4 == (17 / Scalar(5)).value, "17 / 5 == 3.4")
  test_assert(3.4 == (Scalar(17) / 5).value, "17 / 5 == 3.4")

  test_assert(81 == (Scalar(3) ** Scalar(4)).value, "3^4 == 81")
  test_assert(81 == (3 ** Scalar(4)).value, "3^4 == 81")
  test_assert(81 == (Scalar(3) ** 4).value, "3^4 == 81")

  test_assert(-17 == (-Scalar(17)).value, "-17")
Exemple #24
0
    def testMultiplication(self):
        # Scalars with numbers
        s = Scalar(2, 'cm^2')
        t = Scalar(3, 'cm^2')
        u = Scalar(4, 'm')
        v = Scalar(5, 'kg/s')

        # Multiplication with like units
        self.assertEqual(s * t, Scalar(6, 'cm^4'))
        self.assertEqual(t * s, Scalar(6, 'cm^4'))

        # Multiplication with unit conversion
        self.assertAlmostEqual(s * u, Scalar(8, 'cm^2*m'))
        self.assertEqual(u * s, Scalar(8, 'm*cm^2'))

        # Multiplication with unlike units
        self.assertEqual(s * v, Scalar(10, 'cm^2*kg/s'))
        self.assertEqual(v * s, Scalar(10, 'kg*cm^2/s'))

        # Multiplication with numbers
        self.assertEqual(2 * s, Scalar(4, 'cm^2'))
        self.assertEqual(s * 2, Scalar(4, 'cm^2'))

        # Scalars with iterables
        s = Scalar([2, 3], 'cm^2')
        t = Scalar([4, 5], 'cm^2')
        u = Scalar([6, 7], 'm')
        v = Scalar([8, 9], 'kg/s')

        # Multiplication with like units
        self.assertEqual(s * t, Scalar([8, 15], 'cm^4'))
        self.assertEqual(t * s, Scalar([8, 15], 'cm^4'))

        # Multiplication with unit conversion
        self.assertEqual(s * u, Scalar([1200, 2100], 'cm^3'))
        self.assertEqual(u * s, Scalar([12, 21], 'm*cm^2'))

        # Multiplication with unlike units
        self.assertEqual(s * v, Scalar([16, 27], 'cm^2*kg/s'))
        self.assertEqual(v * s, Scalar([16, 27], 'kg*cm^2/s'))

        # Multiplication with numbers
        self.assertEqual(2 * s, Scalar([4, 6], 'cm^2'))
        self.assertEqual(s * 2, Scalar([4, 6], 'cm^2'))
        self.assertEqual(s * np.array([2, 3]), Scalar([4, 9], 'cm^2'))
Exemple #25
0
    def testConvert(self):
        # Single unit conversion
        s = Scalar(2, 'm^2')

        self.assertEqual(s.convert('cm^2'), Scalar(20000, 'cm^2'))

        with self.assertRaises(ScalarError):
            s.convert('cm')

        with self.assertRaises(ScalarError):
            s.convert('kg')

        # Iterable unit conversion
        s = Scalar([1, 2], 'm^2')

        self.assertEqual(s.convert('cm^2'), Scalar([10000, 20000], 'cm^2'))

        with self.assertRaises(ScalarError):
            s.convert('cm')

        with self.assertRaises(ScalarError):
            s.convert('kg')

        # Temperature conversion
        s = Scalar(2, 'K')

        self.assertEqual(s.convert('°C'), Scalar(-271.15, '°C'))
        self.assertEqual(s.convert('°F'), Scalar(-456.07, '°F'))

        # Complex unit conversion
        s = Scalar(2, 'N')
        t = Scalar(3, 'J/s')

        self.assertEqual(s.convert('kg*cm/s^2'), Scalar(200, 'kg*cm/s^2'))
        self.assertEqual(t.convert('g*m^3/s^3*m'), Scalar(3000, 'g*m^3/s^3*m'))
Exemple #26
0
def func_constant(x):
  return Scalar(3) if isinstance(x, Scalar) else 3
Exemple #27
0
    def testAddition(self):
        # Scalars with numbers
        s = Scalar(2, 'cm^2')
        t = Scalar(3, 'cm^2')
        u = Scalar(4, 'm^2')
        v = Scalar(5, 'm')

        # Addition with like units
        self.assertEqual(s + t, Scalar(5, 'cm^2'))
        self.assertEqual(t + s, Scalar(5, 'cm^2'))

        # Addition with unit conversion
        self.assertEqual(s + u, Scalar(40002, 'cm^2'))
        self.assertAlmostEqual(u + s, Scalar(4.0002, 'm^2'))

        # Addition with unlike units
        with self.assertRaises(ScalarError):
            s + v

        with self.assertRaises(ScalarError):
            v + s

        # Addition with numbers
        with self.assertRaises(TypeError):
            s + 2

        with self.assertRaises(TypeError):
            2 + s

        # Scalars with iterables
        s = Scalar([1, 2], 'cm^2')
        t = Scalar([3, 4], 'cm^2')
        u = Scalar([5, 6], 'm^2')
        v = Scalar([7, 8], 'm')

        # Addition with like units
        self.assertEqual(s + t, Scalar([4, 6], 'cm^2'))
        self.assertEqual(t + s, Scalar([4, 6], 'cm^2'))

        # Addition with unit conversion
        self.assertEqual(s + u, Scalar([50001, 60002], 'cm^2'))
        self.assertEqual(u + s, Scalar([50001, 60002], 'cm^2'))

        # Addition with unlike units
        with self.assertRaises(ScalarError):
            s + v

        with self.assertRaises(ScalarError):
            v + s

        # Addition with numbers
        with self.assertRaises(TypeError):
            s + np.array([1, 2])

        with self.assertRaises(TypeError):
            np.array([1, 2]) + s