Python Forum
Convert String of an int array to a Numpy array of ints
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert String of an int array to a Numpy array of ints
#4
In sending a message (Numpy int array) using RabbitMQ, this is how it is setup and sent

testImage = np.random.randint(0, 255, 10*1000)  # grey scale images
strImage = np.array_str(testImage)
message = <string of meta data> + strImage
channel.basic_publish(exchange='', routing_key='task_queue', body=message, properties=pika.BasicProperties(delivery_mode=2,  # make message persistent
    ))
This is the output of the message sent:
Output:
[x] Message params '0,1617801844,1617801844400951300,10000,[113 75 36 ... 167 157 131]'
In modifying the received message (from rabbitMQ) to get a Numpy int array I've tried:
    message = body.decode()
    parts = message.split(',')
    print("[x] message      %r" % message)
    print("[x] message type %s" % type(message))
    print("[x] parts        %s" % parts)
    print("[x] parts len    %s" % len(parts))
    
This is the output for the above code:
Output:
[x] message '0,1617801844,1617801844400951300,10000,[113 75 36 ... 167 157 131]' [x] message type <class 'str'> [x] parts ['0', '1617801844', '1617801844400951300', '10000', '[113 75 36 ... 167 157 131]'] [x] parts len 5
I then start working of the message part that is the Numpy array:
    imagePart = parts[4]
    print()
    print("[x] imagePart ", imagePart)
    print(repr(imagePart))
    print(re.sub("\s+", ",", imagePart.strip()))
    print(type(re.sub("\s+", ",", imagePart.strip())))
    print(np.array(re.sub("\s+", ",", imagePart.strip())))
    print(np.array(re.sub("\s+", ",", imagePart.strip())).size)
    print(type(np.array(re.sub("\s+", ",", imagePart.strip()))))
    print(repr(np.array(re.sub("\s+", ",", imagePart.strip()))))
    print()
    
The output for the above code block is (I'm not sure why it thinks it's of dtype 'U27'):
Output:
[x] imagePart [113 75 36 ... 167 157 131] '[113 75 36 ... 167 157 131]' [113,75,36,...,167,157,131] <class 'str'> [113,75,36,...,167,157,131] 1 <class 'numpy.ndarray'> array('[113,75,36,...,167,157,131]', dtype='<U27')
I then try a different approach:
    npPart = np.asarray(imagePart, dtype=np.str)
    npSplit = np.char.split(npPart)
    npSplitPart = np.array(npSplit, dtype=object)
    print(npSplitPart)
    print(npSplitPart.dtype)
    print("[x] npSplitPart type ", type(npSplitPart))
    print("[x] npSplitPart shape ", npSplitPart.shape)
    print("[x] npSplitPart size ", npSplitPart.size)
    print(repr(npSplitPart))
    print(np.array(npSplitPart))
This is the output for the above code block:
Output:
['[113', '75', '36', '...', '167', '157', '131]'] object [x] npSplitPart type <class 'numpy.ndarray'> [x] npSplitPart shape () [x] npSplitPart size 1 array(list(['[113', '75', '36', '...', '167', '157', '131]']), dtype=object) ['[113', '75', '36', '...', '167', '157', '131]']
What I would like is to get back to the original Numpy int array. Any idea what I am doing wrong or missing?

Thanks...
Reply


Messages In This Thread
RE: Convert String of an int array to a Numpy array of ints - by mdsousa - Apr-07-2021, 04:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy, array(2d,bool), flipping regions. MvGulik 2 1,015 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,084 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  ValueError: could not broadcast input array from shape makingwithheld 1 2,482 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  python code to calculate mean of an array of numbers using numpy viren 3 1,209 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 799 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 2,685 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 1,357 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 9,185 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 1,009 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 1,271 Nov-28-2023, 06:37 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020