Converting string to int

340 views
Skip to first unread message

Richard Sun

unread,
Sep 19, 2019, 9:37:32 AM9/19/19
to Bonsai Users
Dear Goncalo,

I apologize in advance if this is a topic that has been previously covered but unfortunately I couldn't find a direct answer through a fairly thorough search within this group.

Here's my problem:
I'm sending an analog signal from a Teensy via USB to be read by Bonsai using the SerialStringRead tool. Currently, this is a 6-element uint8 array being sent as a string with space being the delimiter. For example, the array at any given time point may look something like this: [27 199 50 39 0 192]. Ultimately, I'm incorporating this information with timestamps and UDP inputs into a single csv file using the CombineLatest or Zip tools, which is currently not a problem if I include the serial information into my csv as a string. My question is, is there a way in Bonsai to convert each element of the string into an integer? This way, I can perform real-time processing (i.e. plotting a live trace of each channel) as opposed to being limited to post-hoc analysis.

I've experimented with the ConvertToArray tool but cannot seem to get integer outputs (see attachment). And when I do change the depth of the ConvertToArray tool, an error results (see attachment).

If you have any inputs or alternative suggestions, it would be greatly appreciated.

Thank you so much for you help.

Regards,

Richard

image_preview (1).png
image_preview (2).png

Gonçalo Lopes

unread,
Sep 19, 2019, 7:14:33 PM9/19/19
to Richard Sun, Bonsai Users
Hi Richard,

This can probably be solved using the Parse node. Is the array fixed length or variable length? If it is fixed length, you can use Parse directly with a pattern like "%i %i %i %i", etc. If you exceed the maximum length, you can leave the last element as a string (%s) and then Parse the remaining item again for the rest.

If the array is variable length, you will have to split the string using a separator. Unfortunately, it is currently surprisingly annoying to call the existing Split method on the string type, so you will have to resort to a PythonTransform:

from System import Array

@returns(Array[str])
def process(value):
  if value is None:
    return Array[str]([])
  return value.Split(" ")

I will consider adding an easier way to split strings and do other similar operations in the next release.
Hope this helps.

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/ed5450a1-1505-44a0-93b5-76a9dc030830%40googlegroups.com.

Richard Sun

unread,
Sep 26, 2019, 8:35:48 PM9/26/19
to Bonsai Users
Dear Goncalo,

Thank you so much for the response. My array is variable length. I tried incorporating your code and I'm able to save the string arrays using CsvWriter without issues. The second goal I'm trying to achieve is to visualize the values of each channel. This will require the conversion of each element of the string array into an integer. Since there are nine elements per array, there will be nine channels. I tried using the ConvertFromArray method but encounted an error I don't quite understand. If you have any suggestions in terms of other methods to try, I'd greatly appreciate it. Thank you very much!

Best,

Richard


To unsubscribe from this group and stop receiving emails from it, send an email to bonsai...@googlegroups.com.
Bonsai01.png
Reply all
Reply to author
Forward
0 new messages