Ejemplo n.º 1
0
def test_preservation_of_specific_array_ordering_simple():
    arr_c = np.array([[1,2],[3,4], [5,6]], order='C')
    arr_f = np.array([[1,2],[3,4], [5,6]], order='F')

    assert_equal(arr_c, arr_f)

    assert arr_c.strides != arr_f.strides

    #   C array ordering
    arr_c_after = loads(dumps(arr_c))
    assert arr_c.strides == arr_c_after.strides
    assert not arr_c.flags.fortran
    assert not arr_c_after.flags.fortran
    assert_equal(arr_c, arr_c_after)

    _test_in_subprocess(arr_c)

    #   Fortran array order
    arr_f_after = loads(dumps(arr_f))
    assert arr_f.strides == arr_f_after.strides
    assert arr_f.flags.fortran
    assert arr_f_after.flags.fortran
    assert_equal(arr_f, arr_f_after)

    _test_in_subprocess(arr_f)
Ejemplo n.º 2
0
def test_preservation_of_specific_array_ordering():
    df_c = pd.DataFrame(np.array([[1,2],[3,4], [5,6]], order='C'))
    df_c_after = loads(dumps(df_c))
    assert_frame_equal_strict(df_c, df_c_after)
    assert_equal(df_c.values, df_c_after.values)
    assert not df_c.values.flags.fortran
    assert not df_c_after.values.flags.fortran

    df_f = pd.DataFrame(np.array([[1,2],[3,4], [5,6]], order='F'))
    df_f_after = loads(dumps(df_f))
    assert_frame_equal_strict(df_f, df_f_after)
    assert_equal(df_f.values, df_f_after.values)
    assert df_f.values.flags.fortran
    assert df_f_after.values.flags.fortran
Ejemplo n.º 3
0
def test_preservation_of_specific_array_ordering():
    df_c = pd.DataFrame(np.array([[1, 2], [3, 4], [5, 6]], order='C'))
    df_c_after = loads(dumps(df_c))
    assert_frame_equal_strict(df_c, df_c_after)
    assert_equal(df_c.values, df_c_after.values)
    assert not df_c.values.flags.fortran
    assert not df_c_after.values.flags.fortran

    df_f = pd.DataFrame(np.array([[1, 2], [3, 4], [5, 6]], order='F'))
    df_f_after = loads(dumps(df_f))
    assert_frame_equal_strict(df_f, df_f_after)
    assert_equal(df_f.values, df_f_after.values)
    assert df_f.values.flags.fortran
    assert df_f_after.values.flags.fortran
Ejemplo n.º 4
0
def test_number(val):
    dumped = dumps(val)
    loaded = loads(dumped)
    assert loaded == val
    assert type(loaded) == type(val)

    _test_in_subprocess(val)
Ejemplo n.º 5
0
def decode_in_subproc(q, encoded_object):
    obj = loads(encoded_object)
    # Multiprocessing is not capable of pickling NumPy arrays so a string representation is returned instead
    if isinstance(obj, dict):
        # Dictionary items are not ordered so we need to sort them to get a unique repr
        output_str = repr(sorted(obj.items()))
    else:
        output_str = repr(obj)
    q.put(output_str)
Ejemplo n.º 6
0
def test_mixed_python_and_pandas_types():
    data_before = TEST_DATA_FRAMES
    buf = dumps(data_before)
    data_after = loads(buf)

    assert isinstance(data_after, tuple)
    assert len(data_after) == 5
    assert len(data_before) == len(data_after)
    for df_before, df_after in zip(data_before, data_after):
        assert_frame_equal_strict(df_before, df_after)
Ejemplo n.º 7
0
def test_mixed_python_and_pandas_types():
    data_before = TEST_DATA_FRAMES
    buf = dumps(data_before)
    data_after = loads(buf)

    assert isinstance(data_after, tuple)
    assert len(data_after) == len(TEST_DATA_FRAMES)
    assert len(data_before) == len(data_after)
    for df_before, df_after in zip(data_before, data_after):
        assert_frame_equal_strict(df_before, df_after)
Ejemplo n.º 8
0
def test_preservation_of_specific_array_ordering_simple():
    arr_c = np.array([[1, 2], [3, 4], [5, 6]], order='C')
    arr_f = np.array([[1, 2], [3, 4], [5, 6]], order='F')

    assert_equal(arr_c, arr_f)

    assert arr_c.strides != arr_f.strides

    #   C array ordering
    arr_c_after = loads(dumps(arr_c))
    assert arr_c.strides == arr_c_after.strides
    assert not arr_c.flags.fortran
    assert not arr_c_after.flags.fortran
    assert_equal(arr_c, arr_c_after)

    #   Fortran array order
    arr_f_after = loads(dumps(arr_f))
    assert arr_f.strides == arr_f_after.strides
    assert arr_f.flags.fortran
    assert arr_f_after.flags.fortran
    assert_equal(arr_f, arr_f_after)
Ejemplo n.º 9
0
def test_datetime_identity():
    import datetime
    date = datetime.datetime(2013, 11, 1, 0, 0)
    val = {
        'start': date,
        'end': date,
        'd': {"ttf": pd.TimeSeries([1.],
                                pd.date_range("1970-1-1", periods=1, freq='S'))
        }
    }
    dumped = dumps(val)
    loaded = loads(dumped)
    assert loaded["start"] == val["start"], dumped
    assert loaded["end"] == val["end"]
    assert loaded["end"] == val["end"]
Ejemplo n.º 10
0
def test_datetime_identity():
    import datetime
    date = datetime.datetime(2013, 11, 1, 0, 0)
    val = {
        'start': date,
        'end': date,
        'd': {
            "ttf":
            pd.TimeSeries([1.], pd.date_range("1970-1-1", periods=1, freq='S'))
        }
    }
    dumped = dumps(val)
    loaded = loads(dumped)
    assert loaded["start"] == val["start"], dumped
    assert loaded["end"] == val["end"]
    assert loaded["end"] == val["end"]
Ejemplo n.º 11
0
def test_pandas_timeseries_handler(ts_before):
    buf = dumps(ts_before)
    ts_after = loads(buf)
    assert_series_equal_strict(ts_before, ts_after)
Ejemplo n.º 12
0
def test():
    # define some data for tutorials.rst
    testdata = ((1, 2, 3, 4),
                (2, 6, 7, 8),
                (9, 10, 11, 12))

    testdata2 = ((1, 1),
                 (2, 2),
                 (19, 3))

    # create a rockpydata object with named columns and filled with testdata
    d = RockPyData(column_names=('F', 'Mx', 'My', 'Mz'), row_names=('1.Zeile', '2.Zeile', '3.Zeile'),
                   units=('T', 'mT', 'fT', 'pT'), data=testdata)

    d_json = numpyson.dumps(d)
    print d_json
    dd = numpyson.loads(d_json)
    print repr(dd)

    print "dd:", dd

    #d = d.eliminate_duplicate_variable_rows(substfunc='last')
    #print d._find_unique_variable_rows()
    print('d:\n%s' % d)

    e = RockPyData(column_names=('F', 'Mx'), row_names=('1.Zeile', '2.Zeile', '3.Zeile'),
                   units=('T', 'mT', 'fT', 'pT'), data=testdata2)

    print('e:\n%s' % e)

    print('e+d:\n%s' % (e+d))
    print('e-d:\n%s' % (e-d))
    print('e/d:\n%s' % (e/d))
    print('e*d:\n%s' % (e*d))

    print('d/e:\n%s' % (d/e))
    print('d*e:\n%s' % (d*e))
    print('d+e:\n%s' % (d+e))
    print('d-e:\n%s' % (d-e))

    print d.units
    # define as many aliases as you want
    d.define_alias('M', ('Mx', 'My', 'Mz'))
    d.define_alias('Mzx', ('Mz', 'Mx'))

    # show some data
    # aliases 'all', 'variable' and 'dep_var' are predefined
    print('all:\n%s' % d['all'])
    print('Mzx:\n%s' % d['Mzx'])

    # lets alter some data
    d['Mx'] = np.array((13, 24, 35))

    # show M with modified Mx component
    print('M:\n%s' % d['M'])
    # show Mx
    print('Mx:\n%s' % d['Mx'])
    # we can also alter several columns at once
    d['M'] = ((2, 3, 4),
              (18, 88, 98),
              (39, 89, 99))
    print('M:\n%s' % d['M'])

    # some math fun
    # calculate magnitude of vector 'M' and save it as new column 'magM'
    d = d.append_columns('magM', d.magnitude('M'))

    # calculate values of 'magM' normalized to 100
    #d.append_columns('normM', d.normalize('magM', 100))

    # we can also add arbitrary data in a new column
    d = d.append_columns(("T",), np.array((1, 2, 3)))

    # we can also add an empty column
    d = d.append_columns(("empty",))

    # renaming a column
    d.rename_column('T', 'temp')

    # show all data again, now including magM and T as the last two columns
    print d

    # do a plot of F vs magM
    # plt.plot(d['F'], d['magM'])
    # plt.show()

    # fancy filtering of data
    tf_array = (d['Mx'].v > 10) & (d['Mx'].v < 20)
    print 'filtering:'
    filtered_d = d.filter(tf_array)
    print filtered_d['Mx']

    # arithmetic operations
    e = deepcopy(d)
    # mutlipy one column with value
    e['Mx'].v *= 2
    # calculate difference of two rockpydata objects
    #c = e - d
    #print c

    #c = e + d
    #print c

    #c = e / d
    #print c

    #c = e * d
    #print c

    #print repr(c)

    # test single line object
    l = RockPyData(column_names=('A', 'B', 'C', 'D'), row_names=('1.Zeile',),
                   units=('T', 'mT', 'fT', 'pT'), data=((1, 2, 3, 4),))
    l = l.append_columns('X', (5,))
    print l

    print l['X']

    #print d.mean()
    print d
    print d + (1, 2, 3, 4, 5, 6)

    print d.interpolate(np.arange(2, 10, .5), includesourcedata=True)
    #d.define_alias('variable', 'Mx')
    #print d.interpolate(np.arange(0, 10, .5))

    print "condense:"
    print condense([d, d*2, d*3], substfunc='median')
Ejemplo n.º 13
0
def test_nested_array():
    data_before = {"1": np.array([1, 2])}
    buf = dumps(data_before)
    arr_after = loads(buf)
    assert_equal(data_before, arr_after)
    _test_in_subprocess(data_before)
Ejemplo n.º 14
0
def test_datetime_index_nested(data_before):
    buf = dumps(data_before)
    data_after = loads(buf)
    assert_index_equal(data_before["1"], data_after["1"])
Ejemplo n.º 15
0
def test_pandas_dataframe_handler(df_before):
    buf = dumps(df_before)
    df_after = loads(buf)
    assert_frame_equal_strict(df_before, df_after)
Ejemplo n.º 16
0
def test_numpy_array_handler(arr_before):
    buf = dumps(arr_before)
    arr_after = loads(buf)
    assert_equal(arr_before, arr_after)
Ejemplo n.º 17
0
def test_nested_array():
    data_before = {"1": np.array([1, 2])}
    buf = dumps(data_before)
    data_after = loads(buf)
    assert_equal(data_before["1"], data_after["1"])
Ejemplo n.º 18
0
def test_pandas_timeseries_handler(ts_before):
    buf = dumps(ts_before)
    ts_after = loads(buf)
    assert_series_equal_strict(ts_before, ts_after)
Ejemplo n.º 19
0
def test_numpy_array_handler(arr_before):
    buf = dumps(arr_before)
    arr_after = loads(buf)
    assert_equal(arr_before, arr_after)
Ejemplo n.º 20
0
def test_number(val):
    dumped = dumps(val)
    loaded = loads(dumped)
    assert loaded == val
    assert type(loaded) == type(val)
Ejemplo n.º 21
0
def test_pandas_datetime_index_handler(index_before):
    buf = dumps(index_before)
    index_after = loads(buf)
    assert_index_equal(index_before, index_after)
Ejemplo n.º 22
0
def test_datetime_index_nested(data_before):
    buf = dumps(data_before)
    data_after = loads(buf)
    assert_index_equal(data_before["1"], data_after["1"])
Ejemplo n.º 23
0
def test_nested_array():
    data_before = {"1": np.array([1, 2])}
    buf = dumps(data_before)
    data_after = loads(buf)
    assert_equal(data_before["1"], data_after["1"])
Ejemplo n.º 24
0
def test_pandas_dataframe_handler(df_before):
    buf = dumps(df_before)
    df_after = loads(buf)
    assert_frame_equal_strict(df_before, df_after)
Ejemplo n.º 25
0
def test_pandas_datetime_index_handler(index_before):
    buf = dumps(index_before)
    index_after = loads(buf)
    assert_index_equal(index_before, index_after)
Ejemplo n.º 26
0
def test_pandas_index_handler(index_before):
    buf = dumps(index_before)
    index_after = loads(buf)
    assert_index_equal(index_before, index_after)

    _test_in_subprocess(index_before)