Data analysis of a 3D form in python - python

My question must have been answered already somewhere but i couldn't find it.
I have a binary numpy 3D array (shape =(512, 512, 304) ) in which there is a random form (labelled as 1). Any other point is labelled as 0.
Lets take for a simple example a sphere.
I want to plot this form on a 3D plot where we can see the sphere.
I already tried 3D plot matplotlib but couldn't get the hand of it
I used the interactive function from (ipywidgets) to print it slice by slice but that's not effetive
I also want to calculate the volume of the form (it may be a completely random poly polyhedron)
I am looking for advices more than answer
Thanks in advance

You can use voxels, although it will be very slow if you try to run it with an array that big. You can for example plot only a 10% of the elements:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Make a sphere
x, y, z = np.ogrid[-1:1:512j, -1:1:512j, -1:1:304j]
sphere = np.sqrt(x * x + y * y + z * z) < 0.5
# Make 3D axis
ax = plt.figure().add_subplot(projection='3d')
# Make voxels figures at 10% resolution
ax.voxels(filled=sphere[::10, ::10, ::10])
ax.figure.show()
Output:

One way to put it is to plot it using widgets:
def show_axial(image_array, slice):
plt.imshow(img_array[:, :, mr_slice].T, cmap="gray")
interact(show_axial,img_array = fixed(im_arr), slice=widgets.IntSlider(max=im_arr.shape[2] - 1,min = 0))
That gaves a 2d plot with a slide bar that ranged between 0 to the image width and make it showable slide by slide.

Related

Converting matplotlib's streamplot coordiantes to numpy coordiantes

I'm currently working with matplotlib in order to create a module of a specific vector field using matplotlib.pyplot.streamplot
after the creation and coloring of the lines in the streamplot, i'm trying to color the whitespace around the divergence points around the plot, im trying to achieve a gradient of color that is dependent on the distance of the white pixels around it.
The streamplot in question is built according to:
xs=np.linspace(-10,10,2000)
ys=np.linspace(-10,10,2000)
Therefore, if the divergence is located (for demonstration purposes) at (0,0) it will be located exactly in the middle of the plot.
Now, the only method i can think of for coloring according to distance from it, is kind of clunky since it requires me to:
add a matplotlib.patches.Rectangle on top of the divergence point in a specific color that is not in the image yet.
convert the plot, with the streamlines and rectangles (one rectangle for each divergence point in streamplot) to a np.array
find the new coordinates of the colors of the rectangles (they represent the location of the divergence point in the new np.array created from streamplot).
calculate the pixels like i want from the colored pixels.
This whole method feels way to clunky and over-complicating, and obviously slower than i could do. im sure theres a way to convert the coordinates from the matplotlib plot to the ones in np.array somehow or perhaps handle the coloring in matplotlib which will be even easier.
sadly i couldn't find a solution that answers this specific need yet.
thanks in advance for any help given!
EDIT
I'm adding an example (not my code, but a representation of what I wish to achieve).
I want to clarify that the solution of adding a patches.circle on top of a circle patch is not my go to, since i'm looking to keep my painting options more dynamic.
If you can define the color intensity you want as a 2-dimensional function, you can plot that function with plt.imshow() and then put the streamplot on top of it. You just need to transform the coordinates linearly to match the image coordinates.
Here is an example:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 10]
# plot 2d function
grid = np.arange(-1, 1, 0.001)
x, y = np.meshgrid(grid, grid)
z = 1 - (x ** 2 + y ** 2) ** 0.5
plt.imshow(z, cmap='Blues')
# streamplot example from matplotlib docs (modified)
w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = Y ** 2
V = X ** 2
# transform according to previous plot
n = len(grid) / 2
scale = n / w
X = (X + w) * scale
Y = (Y + w) * scale
U = (U + w) * scale
V = (V + w) * scale
plt.xticks(ticks=[0, n, 2*n],
labels=[-w, 0, w])
plt.yticks(ticks=[0, n, 2*n],
labels=[-w, 0, w])
plt.streamplot(X, Y, U, V);

Mapping the color scale of 3D isosurface on a scalar field

Let's say we have some 3D complex valued function f(x,y,z). Using Plotly, I'm trying to plot isosurfaces of the magnitude |f(x,y,z)| of such function. So far, everything is OK and my code seems to do well, please find below a working example on atomic orbitals functions :
import chart_studio.plotly as py
import plotly.graph_objs as go
import scipy.special as scispe
import numpy as np
import math
a=5.29e-11 # Bohr radius (m)
def orbital(n,l,m,r,theta,phi): # Complex function I want to plot
L=scispe.genlaguerre(n-l-1,2*l+1) # Laguerre polynomial
radial= (2/(n*a))**(3/2) * np.sqrt(math.factorial(n-l-1)/(2*n*math.factorial(n+l))) * np.exp(-2*r/n) * (2*r/n)**l * L(2*r/n)
wavefunction = radial * scispe.sph_harm(m,l, phi, theta)
return wavefunction
#Quantum numbers
n=2
l=1
m=0
goodspan = (3 * n**2 - l * (l+1))/2 #Plot span adpated to the mean electron position
x, y, z = np.mgrid[-goodspan:goodspan:40j, -goodspan:goodspan:40j, -goodspan:goodspan:40j] #in units of a
r = np.sqrt(x**2 + y**2 + z**2) #Function has to be evaluated in spherical coordinates
theta = np.arccos(z/r)
phi = np.arctan(y/x)
AO=orbital(n,l,m,r,theta,phi)
magnitude = abs(AO) # Compute the magnitude of the function
phase = np.angle(AO) # Compute the phase of the function
isoprob = np.amax(magnitude)/2 # Set value the isosurface
fig = go.Figure(data=go.Isosurface(
x=x.flatten(),
y=y.flatten(),
z=z.flatten(),
value=magnitude.flatten(),
opacity=0.5,
isomin=isoprob,
isomax=isoprob,
surface_count=1,
caps=dict(x_show=True, y_show=True)
))
fig.show()
which gives me this :
At this point, the color scale of the graph is attributed depending on the value of the magnitude |f(x,y,z)|, so that a single isosurface is always uniform in color.
Now, instead to have a color scale mapped on the magnitude |f(x,y,z)|, I would like it to be mapped on the value of the phase Ф(x,y,z) = arg(f(x,y,z)), so that the color of each point of a ploted isosurface tells us about the value of the field Ф(x,y,z) (which would be distributed on [-π,π] ideally) instead of |f(x,y,z)| in thsi point.
Basically, I would like to do this with Plotly instead of Mayavi if it's possible.
It seems to me that all of that has something to do with a special way to set the cmin and cmax parameters of the function Isosurface, but I can't figure out how to do this.
As #gnodab mentioned in his comment, plotly isosurfaces do not really support colouring the surfaces by a fifth dimension (at least there is no obvious way to do it). I am also not sure if it might be possible to extract the data describing the isosurface somehow to be re-plotted as a regular surface.
In this post, however, they describe how to generate an isosurface with skimage.measure.marching_cubes_lewiner which is then plotted and coloured by a custom colorscale with plotly as 'mesh3d' trace. This might be what you want. If I find the time, I'll give that a try and edit my answer later.
Given #Jan Joswig's answer and the link they provided, the quick/compact way of doing it will be:
import plotly.graph_objects as go
from skimage import measure
import numpy as np
xyz_shape = vol.shape
verts, faces = measure.marching_cubes(vol, .5)[:2] # iso-surface at .5 level
x, y, z = verts.T
I, J, K = faces.T
fig = go.Figure(
data=[go.Mesh3d(
x=x,
y=y,
z=z,
color='lightpink',
opacity=0.50,
i=I,
j=J,
k=K, )])
fig.show()

How to generate a paraboloid surface and make it respond to event

I am new to vispy and computer graphics. I have to generate a paraboloid according to a certain equation whose center and parameters vary according to the user input. I have gone through vispy documentation and examples and got some idea regarding the package.
The paraboloid that I need to generate should have rotational symmetry such as shown in the figure below:
Whereas what I got is here
My code is given below. I have modified the isosurface.py example in the vispy examples.
import sys
import numpy as np
from vispy import app, scene
from matplotlib import pyplot as plt
# Create a canvas with a 3D viewport
canvas = scene.SceneCanvas(keys='interactive')
view = canvas.central_widget.add_view()
## Define a scalar field from which we will generate an isosurface
def psi3(i, j, k, offset=(25, 25, 25)):
x = i-offset[0]
y = j-offset[1]
z = k-offset[2]
r = (0.2*x**2 + 0.2*y**2 - 4*z)
return r
# Create isosurface visual
data = np.fromfunction(psi3, (50, 50, 50))
surface = scene.visuals.Isosurface(data, level=data.max() / 4., color=(0.5, 0.6, 1, 1), shading='smooth', parent=view.scene)
surface.transform = scene.transforms.STTransform(translate=(-25, -25, -25))
# Add a 3D axis to keep us oriented
axis = scene.visuals.XYZAxis(parent=view.scene)
# Use a 3D camera
# Manual bounds; Mesh visual does not provide bounds yet
# Note how you can set bounds before assigning the camera to the viewbox
cam = scene.TurntableCamera(elevation=30, azimuth=30)
cam.set_range((-10, 10), (-10, 10), (-10, 10))
view.camera = cam
if __name__ == '__main__':
canvas.show()
if sys.flags.interactive == 0:
app.run()
My queries are the following:
How do I make the paraboloid look like in the first image (without the edges getting clipped off)
Is there a better way to draw the paraboloid other than using isosurfaces. The coefficients of the paraboloid should be varied by the user.
How to make the paraboloid respond to mouse events: hover, drag-drop etc. I understand from the documentation that I have to couple it to the Node class. I am unable to figure out the exact way to do this as I am a newbie.
Edit:
Here is the corresponding code using matplotlib for generating the required paraboloid. Also I am able to create a paraboloidal strip in matplotlib.
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
# Create the surface
radius = 5
hole_radius = 4
# Generate the grid in cylindrical coordinates
r = np.linspace(0, radius, 100)
theta = np.linspace(0, 2 * np.pi, 100)
R, THETA = np.meshgrid(r, theta)
X, Y = R * np.cos(THETA), R * np.sin(THETA)
a=0.6;b=0.6;c=0.6
Z1 = (X/a)**2+(Y/b)**2 # Elliptic paraboloid
# Do not plot the inner region
x = np.where(X**2+Y**2<=hole_radius**2,np.NAN,X)
y = np.where(X**2+Y**2<=hole_radius**2,np.NAN,Y)
# Plot the surface
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(x, y, Z1, cmap=cm.coolwarm, linewidth=0, antialiased=True, cstride=2, rstride=2)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
plt.show()
This results in the following:
The difference between the surface plot of vispy and matplotlib is that the latter work by accepting 2D arrays for x and y, whereas vispy's SurfacePlot() accepts only 1D vectors in both x and y.
Since the grid in cylindrical coordinates and converting them to cartesian coordinates for plotting, the grid cannot be generated by replicating the 1D x and y vectors.
Update:
As pointed by #djhoesem, isosurface is not the correct method to do this.
I'm the maintainer of vispy, but have very little isosurface experience. Let's see what I can answer:
The easiest thing I saw to do this is to make levels even smaller (I made it zero for testing). I'm not sure how this effects performance or the output exactly but the isosurface function mentions the paper it is based on. Maybe that can tell you more.
See Paul Bourke, "Polygonising a Scalar Field"
(http://paulbourke.net/geometry/polygonise/)
To make the parameters controllable by the user you could subclass the existing Isosurface class and add properties to control these. However, this will probably perform poorly if you want immediate feedback since you'd have to regenerate the numpy array and rerun all the other calculations on the CPU. The IsosurfaceVisual class expects volumetric data and then converts it to an isosurface. It generates a mesh that the MeshVisual understands (IsosurfaceVisual is a subclass of MeshVisual). If you want anything better you'd probably have to write your own shader code to do it. Depends what your exact requirements are (do you have to accept any formula with controllable coefficients?).
The scene.visuals Visual classes are already subclasses of the Node class so you don't have to do anything extra there. The other SceneCanvas-based examples should give you some ideas of how you can handle mouse events. That said, you mentioned "drag-drop", I'm not sure that would be something you'd handle in VisPy land but more likely in PyQt5 land (if that's the backend you are using).
Added the feature via pull request: https://github.com/vispy/vispy/pull/1863#event-3344873987
Made a small modification to the code and resolved the issue.
The corresponding example can be found here:
https://github.com/vispy/vispy/blob/master/examples/basics/visuals/axially_symmetric_surfaces.py

Running out of memory: np.meshgrid

I'm struggling with an issue relating to Matplotlib and Numpy.
I am trying to create hillshading on my surface plots.
My input data is an irregular spacing of XYZ points derived from LiDAR.
I can generate a trisurf3D plot or 3Dscatter no problem. Save it, change the camera angles, colour it based on Z and animate it but for the life of me I can't get any sort of shading in there at all.
I'm getting stuck at Matplotlib requiring 2D arrays for X and Y and Z. My input data is honestly tiny: 376704 points, each with an XYZ value. I have converted the points to a euclidean coordinate system starting at 0:
from laspy.file import File as LAS
import numpy as np
def lasToNumpy(lasFile):
f = LAS(lasFile,mode='r')
## Establish min values
xmin = min(f.x)
ymin = min(f.y)
zmin = min(f.z)
## Arrays now in meters from 0 to max
x = np.array(f.x-xmin)
y = np.array(f.y-ymin)
z = np.array(f.z-zmin)
## Assign a max of each x and y
xmax = max(x)
ymax = max(y)
The issue is my next step is to create a meshgrid (as is seemingly required to generate a 2D array).
This eats about 50GB of RAM:
X, Y = np.meshgrid(x,y)
And rightfully so.
All I want to do is add hillshading to my surface but the whole 2D array seems so illogically unnecessary! What are my options here? Is this just not going to happen? For reference my my trisurf3D works fine:
fig = plt.figure(figsize=(60.0,60.0))
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(x,y,z, cmap='plasma', edgecolor='black', alpha=0.5)
Really want to throw some hill shading in there as well.
This question may be obsolete now, but for other users, the problem here is that you are trying to make a mesh of 376704 points in each direction using np.meshgrid. The purpose of np.meshgrid is to take the x and y ranges and create a grid. For example:
x=np.arange(0,100) #1D array
y=np.linspace(-50,50,1111) # 1D array
xgrid,ygrid=np.meshgrid(x,y) #Outputs 2D arrays
Only use np.meshgrid if you want to grid your data. You can grid your data to lower resolution using a 3D interpolator such as RegularGridInterpolator and is one way to solve your problem and create your hill.
A quicker and better option in my opinion is using tricontourf. The function takes in the 1D arrays that you have to create the hill shading figure you desire. If you can't get this to work, update your question with a some data.

Convert XYZ point cloud to grayscale image

Everyone
I'm trying to convert point cloud (X, Y, Z) to the grayscale image using python. I learned that the grayscale image could be generated by a Numpy array. But what I have now is a set of points which contains X, Y and height. I wanna generate a grayscale image based on X, Y and grayscale value which is Height.
Can someone give me an idea about this?
Thanks beforehand.
Rowen
Thanks, guys. I just finished writing my own codes to do interpolation. But my idea is from yours. Thank you to #asaflotz and #Paul Panzer.
The thing is in my scenario, points in point cloud are not arranged well. The intervals between two nearby points are not uniform. It's impossible to use grid directly. So I picked up an unstructured method in Scipy.Interpolate which has so many practical methods can be used depending on different use case. My code below is a modified version of the example from Scipy.Interpolate.griddata.
x_range=((df.X.max()-df.X.min()))
y_range=((df.Y.max()-df.Y.min()))
grid_x, grid_y = np.mgrid[df.X.min():df.X.max():(x_range*1j), df.Y.min():df.Y.max():(y_range*1j)]
points = df[['X','Y']].values
values = df['new'].values
grid_z0 = griddata(points, values, (grid_x, grid_y), method='linear').astype(np.uint8)
im=Image.fromarray(grid_z0,'L')
im.show()
Noticed that in griddata, methods like 'linear', 'nearest', 'cubic' can be applied depending on your scenarios.
Here is the grayscale elevation image generated.
Lastly, my question has been solved basically. Please comment on this post if you have any good ideas or confusion. Thanks all!
Rowen
let's assume that the X,Y are arranged so they will form a grid (which is mandatory in order to build a rectangular image). from there this is easy:
import numpy as np
import matplotlib.pyplot as plt
# generate some data
ax = np.arange(-9, 10)
X, Y = np.meshgrid(ax, ax)
Z = X ** 2 + Y ** 2
# normalize the data and convert to uint8 (grayscale conventions)
zNorm = (Z - Z.min()) / (Z.max() - Z.min()) * 255
zNormUint8 = zNorm.astype(np.uint8)
# plot result
plt.figure()
plt.imshow(zNormUint8)

Categories

Resources