seantronsen / pvt

GNU General Public License v3.0
0 stars 0 forks source link

Unclear how to use 2d scatter pane #35

Open ghoulapool opened 5 months ago

ghoulapool commented 5 months ago

Here's an image:

image

Here's the code that created it - shouldn't the x axis range from the smallest to largest value?

def callback(omega_m, omega_b, sigma_8, h, n_s, w_0, f, omega_nu, z, animation_tick, **_):
    modes_by_result = np.array(
        [[1.00000000e-03, 2.10598705e+04],
         [1.07700000e-03, 2.19387725e+04],
         [1.15900000e-03, 2.28445472e+04],
         [1.24800000e-03, 2.37779912e+04],
         [1.34300000e-03, 2.47396098e+04],
         [1.44600000e-03, 2.57294665e+04],
         [1.55700000e-03, 2.67470412e+04],
         [1.67600000e-03, 2.77915464e+04],
         [1.80400000e-03, 2.88616097e+04]]

    )
    return modes_by_result

. . .

pl = Plot2DScatterPane(callback, ncolors=3, cmap="plasma")
# pl = Plot2DLinePane(callback, ncolors=3, cmap="plasma")

pl.set_title("Cosmic Emu")
pl.set_xlabel("Mode")
pl.set_ylabel("Power")
pl = Animator(fps=60, contents=pl).animation_content
seantronsen commented 5 months ago

For good news, I was able to reproduce the issue which boiled down to a formatting quirk of the pyqtgraph library.

If you add an additional dimension to the ndarray, the plot works as expected: image

image

Part of this is detailed in the docstring for render_data in the abstract BasePlot2DPane class, but the documentation leaves much to be desired here. image

seantronsen commented 5 months ago

Still, the solution is not intuitive. I figured with the data being in the (N,2) shape, this would work as it should, but that's clearly not the case.

Looking into this a little further to see if this can be made more intuitive.

seantronsen commented 5 months ago

Found the issue: image

See line 265. We have assumed the user will specify a callback which returns multiple curves worth of data. In this case, the return shape was (N,2) which represents only one curve. As such, each point was plot as a unique element. I should have noticed that sooner from the different point colors in the original post.

seantronsen commented 5 months ago

Need to look into switching over to this interface in PyQtGraph: image

For now, I'm updating the documentation for the render method on BasePlot2DPane.

seantrons commented 5 months ago

Posting this here as a reference: image

For those who haven't seen the None syntax in the ndarray indexing before, it was a little odd to me as well the first time I saw it. Basically, it just means "insert a new dimension of size 1 here".

The result is the original ndarray with shape (N,2) is returned with shape (1,N,2) as a result of the None inside the index statement.

In other words, this is equivalent to

tuple destructuring

modes_by_result.reshape(1, *modes_by_result.shape)

and

tuple concatenation

modes_by_result.reshape( (1,) +  modes_by_result.shape )