Giter VIP home page Giter VIP logo

cube's Introduction

Cube

Simple visualizer of cube files. Jupyter notebook compatible. Blender compatible.

Installation

pip install -e git+https://github.com/yanzewu/cube

Prerequesties:

  • plotly
  • scikit-image (optional): For displaying smoother grids.
  • plyfile (optional): For exporting PLY files.

Usage

The simplist way is

import cube
cube.plot_cube('my_cube_file').show()

To make the grid smoother, first download scikit-image, and change the line to

cube.plot_cube('my_cube_file', surf_kwargs={'smooth_grid':True})

To clear the background, change the line to

fig = cube.plot_cube('my_cube_file')
cube.clear_axis(fig)
fig.show()

To plot multiple cube files in the same figure, do

cubefile1 = cube.read_cube('my_cube_file_1')
cubefile2 = cube.read_cube('my_cube_file_2')

fig = cube.plot_molecule(cubefile1.charges, cubefile1.coords, style='ballstick')
# assuming all cubefiles are describing the same geometry

X, Y, Z = cube.meshgrid()
fig = cube.plot_isosurface(X, Y, Z, cubefile1.data, style='mesh', figure=fig)
fig = cube.plot_isosurface(X, Y, Z, cubefile2.data, style='mesh', color='Gold', figure=fig)
# please change the style and color if necessary.

cube.set_aspect(fig)
fig.show()

Several useful functions are

  • read_cube(): Read a cube file into Cube instance. A Cube instance has the following attributes:
    • charges: A list of charges.
    • coords: An (N x 3) numpy array.
    • origin: The origin of the grids.
    • vecs: The unit vectors of the grid edges.
    • data: The (Nx x Ny x Nz) numpy array of voxel data.
    • meshgrid(): Generate an meshgrid representing the coordinate system.
  • plot_molecule(): Plot a molecule instance from charge and coordinates.
  • plot_isosurface(): Plot an isosurface from voxel data at certain value (specified by iso). If the data contains both positive and negative, surfaces will be drew at both iso and -iso.
  • plot_volume(): Plot density graph from voxel data. The range can be controled by vmin and vmax.
  • plot_cube(): A wrapper of plot_molecule() and plot_isosurface(). Plot the molecule and isosurface from a cube file.
  • util.clear_axis(): Remove the axis and labels.
  • util.set_background_color(): Set the background color of figure.
  • util.set_aspect(): Make the aspect of figure same as axis span.

The arguments are described in docstrings.

More examples can be found in example.ipynb.

Support for 3D Modeling

Plots generated from plot_molecule() (with style other than None) and plot_isosurface() (with smooth_grid=True) can be exported to PLY files, which then can be imported to 3D modeling softwares (e.g. Blender). Here is an example for exporting a molecule to Blender:

import cube
c = cube.read_cube('doc/example.cube')
fig = cube.plot_molecule(c.charges, c.coords, style='ballstick')
cube.util.mesh2ply(fig['data'][-1]).write('example.ply')

In Blender (version >= 3.3), select "File > Import > Stanford (.ply)" to import the file. To assign the same color as given by cube, first add a new material to the object, then in the material panel, find "Base Color", click the little dot beside the color panel, and select "Color Attribute". The color attribute is also available as an input option in the node view.

cube's People

Contributors

yanzewu avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.