2

I can successfully save & load small arrays using Numpy. Now I am saving the below array using np.save('array.npy')

[0, 100, 0, 5, 10, 15, 20, 25, 30, 25, 20, 15, 10, 5, 0]

when I try to load using np.load('array.npy'), it shows the below error:

    raise ValueError("Cannot load file containing pickled data "
ValueError: Cannot load file containing pickled data when allow_pickle=False

If I try to solve it by adding allow_pickle=True then it shows the below error:

    raise IOError(
OSError: Failed to interpret file 'array.npy' as a pickle

Its really a difficult situation. Please advise! :(

The code I am referring to is below:

def recv():
    import socket
    import time
    import numpy as np

    TCP_IP = "0.0.0.0"
    BUFFER_SIZE = 20  # Normally 1024, but we want fast response

    # receiving CAN frame payload
    TCP_PORT = 5003
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind((TCP_IP,TCP_PORT))
    s.listen(1)
    conn,addr = s.accept()
    while 1:
        data1 = conn.recv(BUFFER_SIZE)
        if not data1: break
        datalist = list(data1)
        print("CAN payload: %s" % datalist)
        conn.send(data1)  # echo
    conn.close()
    time.sleep(2)
    # ------------------------------------------------------------
    # assembling CAN frame
    from can import Message
    can_msg = Message(is_extended_id=bool(datalist[0]),arbitration_id=datalist[1],data=datalist[2:])

    # printing all received payloads
    print("CAN frame: ",can_msg)
    print("Vehicle speed: ",datalist[2:])

    # Saving all received payloads
    np.save('array.npy',datalist[2:])  # save


def EPS_process():
    # EPS process for Right turn, high speed
    import numpy as np

    print("Starting EPS process")
    speed_array = np.load('array.npy')  # load
2
  • Please post the non-working code. When running your example, it works fine, even without allow_pickle=True Commented Dec 31, 2020 at 12:04
  • it is a socket server. I have edited to add the code here Commented Jan 1, 2021 at 11:10

2 Answers 2

2

Okay, I want to share what resolves the situation. So basically @hpaulj was suggesting that for multiple processes the read might start before the save is finished. So I added a 3-sec delay before speed_array = np.load('array.npy')

This gives enough time for the NumPy array to save before I can read again. Otherwise, it will read the previously saved array and hence the pickle error.

Sign up to request clarification or add additional context in comments.

Comments

1

That array shouldn't give you any problems:

In [1]: arr = np.array([0, 100, 0, 5, 10, 15, 20, 25, 30, 25, 20, 15, 10, 5, 0])
In [2]: arr
Out[2]: 
array([  0, 100,   0,   5,  10,  15,  20,  25,  30,  25,  20,  15,  10,
         5,   0])
In [3]: np.save('test.npy',arr)
In [4]: np.load('test.npy')
Out[4]: 
array([  0, 100,   0,   5,  10,  15,  20,  25,  30,  25,  20,  15,  10,
         5,   0])

4 Comments

It is a socket server. I have edited to add the code here. Please check
That potentially makes a big difference. You have to, in some sense, verify that the file that's being read is the same as the one that's saved. For example with multiple processes the read might start before the save is finished.
actually, I am calling these def functions separately at separate times, so it should get ample time to save.
You were right. I added 3 sec time delay, now it is working fine :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.