AureumChaos / LEAP

A general purpose Library for Evolutionary Algorithms in Python.
Academic Free License v3.0
83 stars 19 forks source link

Some of the logging/probe functions will embed a carriage return in the middle of a row for CSV output #250

Closed markcoletti closed 1 year ago

markcoletti commented 1 year ago

E.g., this should have been one row, but a carriage return was embedded in the middle of the list:

1999,3,Combined Individual,,"[False False False  True  True False  True  True  True  True  True  True
  True  True  True  True False False  True  True]",14
lukepmccombs commented 1 year ago

This is an issue with numpy np.printoptions. When numpy converts an array to a string, explicitly or implicitly for writing to streams, it uses a predetermined line width to insert breaks. You can bypass this by setting an arbitrarily large linewidth parameter while using np.printoptions as a context manager, or np.set_printoptions to change it thereafter.

markcoletti commented 1 year ago

@lukepmccombs is working on this now. He's exploring native numpy solutions for emitting CSV rows.

lukepmccombs commented 1 year ago

Here is my proposed solution: In numpy you can override the printing function with another of your choosing via np.set_string_function. We can use this functionality to make a print function that doesn't insert newlines and is usable for loading the arrays later. A good option for this is:

def _to_quoted_list(arr):
    f'"{json.dumps(arr.tolist())}"'

which returns a quoted, single line json representation for any size array, and can be restored using json.loads on the contents of the cell.

lukepmccombs commented 1 year ago

This should be closable now. :+1: