upiterbarg / mpmath

Automatically exported from code.google.com/p/mpmath
Other
0 stars 0 forks source link

save and load data - ValueError: invalid digits #239

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I have stored some data using mpmath (backend = 'python'). 
I wanted to try mpmath with 'gmpy' but I cannot load my old data.

NOT WORKING - saving with 'python' backend and loading with 'gmpy' backend
WORKING - saving with 'gmpy' backend and loading with 'python' or 'gmpy' backend

What steps will reproduce the problem?

    ######## SAVE DATA - mpmath.libmp.BACKEND == 'python' #############################
    import numpy as np
    import pickle
    import os
    os.environ['MPMATH_NOGMPY'] = 'Y'
    import mpmath as mp
    mp.mp.dps = 1000

    a = mp.mpf('1') / mp.mpf('3')
    b = mp.mpf('1') / mp.mpf('10')
    arr = np.array([a, b], dtype=object)

    # save array as npy
    np.save('data', arr)

    # pickle variable a
    output = open('data.pkl', 'wb')
    pickle.dump(a, output)
    output.close()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    ######## LOAD DATA - mpmath.libmp.BACKEND == 'gmpy' #############################
    import numpy as np
    import pickle
    import os
    # os.environ['MPMATH_NOGMPY'] = 'Y'
    import mpmath as mp
    mp.mp.dps = 1000

    data = np.load(r'data.npy')
    print data

    pkl_file = open('data.pkl', 'rb')
    data = pickle.load(pkl_file)
    pkl_file.close()
    print data 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What is the expected output? What do you see instead?

    #### EXPECTED OUTPUT #############################################
    [ mpf('0.33333333333...')
    mpf('0.100000000000...')]
    0.333333333333333333...
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    #### REAL OUTPUT #############################################
    Traceback (most recent call last):
    File "/home/kelidas/workspace_git/scratch/scratch/recursion/test.py", line 21, in <module>
        data = np.load(r'data.npy')
    File "/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 378, in load
        return format.read_array(fid)
    File "/usr/local/lib/python2.7/dist-packages/numpy/lib/format.py", line 449, in read_array
        array = cPickle.load(fp)
    File "/usr/local/lib/python2.7/dist-packages/mpmath/ctx_mp_python.py", line 132, in __setstate__
        def __setstate__(self, val): self._mpf_ = from_pickable(val)
    File "/usr/local/lib/python2.7/dist-packages/mpmath/libmp/libmpf.py", line 43, in from_pickable
        return (sign, MPZ(man, 16), exp, bc)
    ValueError: invalid digits
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What version of the product are you using? On what operating system?
    mpmath 0.17, kubuntu 11.10 32bit, Python 2.7.2+, gmpy2 2.0.0
    mpmath 0.17, Windows 7 64bit, Enthought Canopy - Python 2.7.3, gmpy 1.15

Please provide any additional information below.

Original issue reported on code.google.com by gkeli...@gmail.com on 22 May 2013 at 9:01

GoogleCodeExporter commented 9 years ago
I solved this problem temporarily by changing "MPZ" to "long" in 
mpmath/libmp/libmpf.py.

def from_pickable(x):
    sign, man, exp, bc = x
    return (sign, long(man, 16), exp, bc)

Original comment by gkeli...@gmail.com on 12 Jun 2013 at 10:23