holoviz / datashader

Quickly and accurately render even the largest data.
http://datashader.org
BSD 3-Clause "New" or "Revised" License
3.31k stars 366 forks source link

Mixed float32/float64 TriMesh data issues #674

Open noppelmax opened 5 years ago

noppelmax commented 5 years ago

Hey,

first i want to thank you for your work!

Description

I currently trying to get a TriMesh project ready. My data is described in a different way, the mesh should look like but i get this working for now. My problem is, that for my data, the actual value of a cell (triangle) is the value of the cell and not of one node. I found, i could set the values in the array of the vertices but for me it depends on the triangle. Is this possible?

Data

My data is structured like follows:

<xarray.Dataset>
Dimensions:      (bnds: 2, height: 90, ncells: 327680, time: 1, vertices: 3)
Coordinates:
  * height       (height) float64 1.0 2.0 3.0 4.0 5.0 ... 87.0 88.0 89.0 90.0
  * time         (time) float64 2.016e+07
Dimensions without coordinates: bnds, ncells, vertices
Data variables:
    clon         (ncells) float64 1.257 0.877 1.257 ... 0.005273 0.003057
    clon_bnds    (ncells, vertices) float64 -2.708 0.6283 ... 0.007998 8.474e-15
    clat         (ncells) float64 1.566 1.559 1.563 ... -0.4698 -0.4701 -0.4674
    clat_bnds    (ncells, vertices) float64 1.571 1.563 1.563 ... -0.466 -0.4636
    height_bnds  (height, bnds) float64 ...
    TR_stn       (time, height, ncells) float32 ...
    TR_stt       (time, height, ncells) float32 ...
    TR_sts       (time, height, ncells) float32 ...
    TR_trn       (time, height, ncells) float32 ...
    TR_trt       (time, height, ncells) float32 ...
    pv           (time, height, ncells) float32 ...
Attributes:
    CDI:                  Climate Data Interface version 1.7.2 (http://mpimet...
    Conventions:          CF-1.4

Where 327680 is the number of cells.

Code

xrData = xr.open_dataset("/home/max/Downloads/2016033000-ART-passive_grid_pmn_DOM01_ML_0002.nc",decode_cf=False)

pts = np.column_stack((xrData.clon,xrData.clat,xrData.isel(height=0,time=0).TR_stn))
print(pts)

verts = np.column_stack((xrData.clon_bnds.stack(z=('vertices','ncells')),xrData.clat_bnds.stack(z=('vertices','ncells'))))
#verts = np.append(verts,[0,0])
print(verts.shape)
n1 = []
n2 = []
n3 = []

l = len(xrData.clon_bnds)
for i in range(0,l):
    n1.append([i])
    n2.append([i+l])
    n3.append([i+l+l])

n = np.column_stack((n1,n2,n3,xrData.isel(height=0,time=0).TR_stn))

verts = pd.DataFrame(verts,  columns=['x', 'y'])
tris  = pd.DataFrame(n, columns=['v0', 'v1', 'v2','TR_stn'])

print('vertices:', len(verts), 'triangles:', len(tris))

mesh = du.mesh(verts,tris)
cvs = ds.Canvas(plot_height=900, plot_width=900)
%time agg = cvs.trimesh(verts, tris, mesh=mesh)
#tf.Image(tf.shade(agg))
datashade(hv.TriMesh((tris,verts), vdims=["TR_stn"], label="Wireframe").options(filled=True))

Errors and warnings

I get the following output:

 [ 0.87695428  1.55913341  0.04311323]
 [ 1.25663706  1.5627978   0.04319941]
 ...
 [ 0.01106288 -0.46984588  0.00162048]
 [ 0.00527347 -0.47011298  0.00165073]
 [ 0.00305715 -0.46740222  0.00163877]]
(983040, 2)
vertices: 983040 triangles: 327680

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-61-90e80b7ef202> in <module>()
     22 print('vertices:', len(verts), 'triangles:', len(tris))
     23 
---> 24 mesh = du.mesh(verts,tris)
     25 cvs = ds.Canvas(plot_height=900, plot_width=900)
     26 get_ipython().run_line_magic('time', 'agg = cvs.trimesh(verts, tris, mesh=mesh)')

/usr/local/lib/python3.5/dist-packages/datashader/utils.py in mesh(vertices, simplices)
    462         lambda dt: np.issubdtype(dt, np.integer)
    463     ).all()
--> 464     assert simplices_all_ints, ('Simplices must be integral. You may '
    465                                 'consider casting simplices to integers '
    466                                 'with ".astype(int)"')

AssertionError: Simplices must be integral. You may consider casting simplices to integers with ".astype(int)"

Version Environment

max@sam ~/g/praktClimaAnalyse (master)> python3 --version
Python 3.5.2
max@sam ~/g/praktClimaAnalyse (master)> jupyter --version
4.4.0
max@sam ~/g/praktClimaAnalyse (master)> datashader --version
datashader 0.6.8
max@sam ~/g/praktClimaAnalyse (master)> firefox --version
Mozilla Firefox 64.0
max@sam ~/g/praktClimaAnalyse (master)> uname -a
Linux sam 4.15.0-42-generic #45~16.04.1-Ubuntu SMP Mon Nov 19 13:02:27 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
max@sam ~/g/praktClimaAnalyse (master)> 

Thanks! And best regards, Max

noppelmax commented 5 years ago

Maybe #526 is also relevant.

noppelmax commented 5 years ago

OK. i found out, the panda DataFrame is of dtype float64 for all columns after tris = pd.DataFrame(n, columns=['v0', 'v1', 'v2','TR_stn']). This is why assert failed. As i see at the pandadoc all columns in a panda dataframe must have the same dtype. How could i display float64 data belonging to the triangle?

d70-t commented 3 years ago

Just came across this issue while searching how to display data defined on cells. It would be great to see this implemented :+1:

jbednar commented 3 years ago

PRs welcome! :-) Just to be clear, here I think the issue is to deal with mixed float32/float64 datatypes properly, and in the meantime converting them all to float32 would presumably work.

d70-t commented 3 years ago

Thank you very much for the feedback! I just didn't try the obvious. It is indeed possible to add the cell-values to the dataframe containing node indices and it seems to just do the right thing.