toolsforexperiments / plottr

A flexible plotting and data analysis tool.
https://github.com/toolsforexperiments/plottr
MIT License
46 stars 55 forks source link

Bugfix: guessing the grid raises an exception when one of the dimensions is 1 #369

Closed yoshi74ls181 closed 1 year ago

yoshi74ls181 commented 1 year ago

Currently, plottr cannot plot a data with more than two axes if one of the dimensions is 1. This is because MeshGridDataDict.validate() trys to take the max of an empty array and raises an exception. This pull request fixes this bug.

yoshi74ls181 commented 1 year ago

Sorry about the bugs in the bugfix, I need to figure out how to run the tests locally...

marcosfrenkel commented 1 year ago

I am not able to recreate this error. When I have meshgrid with an outer axis with dimension 1, I can plot it fine. I also think that what you are trying to do is considered an invalid meshgrid, but I am not sure.

@wpfff can you take a look at this?

yoshi74ls181 commented 1 year ago

I might be doing something wrong when generating the data. I'll make an MWE and post it here.

yoshi74ls181 commented 1 year ago

You can see the problem if you plot the data generated as:

from plottr.data.datadict_storage import DataDict, DDH5Writer

data = DataDict(
    ax1=dict(),
    ax2=dict(),
    ax3=dict(),
    data=dict(axes=["ax1", "ax2", "ax3"])
)

with DDH5Writer(data, "./data") as writer:
    writer.add_data(
        ax1=[1, 1, 2, 2],
        ax2=[1, 2, 1, 2],
        ax3=[1, 1, 1, 1],
        data=[1, 2, 3, 4],
    )

Here is how autoplot looks without the fix in this pull request: image The options to plot the data in a grid are greyed out, and I can't use the "select element" feature. Also, the following error is printed:

Process 0: EXCEPTION RAISED: <class 'ValueError'>: zero-size array to reduction operation maximum which has no identity
 ->   File "C:\venvs\plottr-dev\lib\site-packages\pyqtgraph\flowchart\Node.py", line 296, in update
    out = self.process(**strDict(vals))
 ->   File "C:\Users\qipe\git\plottr\plottr\node\grid.py", line 485, in process
    dout = dd.datadict_to_meshgrid(data)
 ->   File "C:\Users\qipe\git\plottr\plottr\data\datadict.py", line 1188, in datadict_to_meshgrid
    newdata.validate()
 ->   File "C:\Users\qipe\git\plottr\plottr\data\datadict.py", line 1047, in validate
    max_step_along_axes = np.max(np.abs(np.diff(data_items[na]['values'],axis=axis_num)))
 ->   File "<__array_function__ internals>", line 200, in amax
 ->   File "C:\venvs\plottr-dev\lib\site-packages\numpy\core\fromnumeric.py", line 2820, in amax
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
 ->   File "C:\venvs\plottr-dev\lib\site-packages\numpy\core\fromnumeric.py", line 86, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

Here is how it looks with the fix in this pull request: image

wpfff commented 1 year ago

@yoshi74ls181 can you check if #391 fixes this as well?

yoshi74ls181 commented 1 year ago

@wpfff Yes it does, thanks!