Skip to content

Commit

Permalink
Update pyvista/core/utilities/reader.py
Browse files Browse the repository at this point in the history
  • Loading branch information
tkoyama010 committed May 9, 2024
1 parent 9d0c665 commit 3d98911
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 0 deletions.
153 changes: 153 additions & 0 deletions pyvista/core/utilities/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def get_reader(filename, force_ext=None):
+----------------+---------------------------------------------+
| ``.cgns`` | :class:`pyvista.CGNSReader` |
+----------------+---------------------------------------------+
| ``.cube`` | :class:`pyvista.GaussianCubeReader` |
+----------------+---------------------------------------------+
| ``.dat`` | :class:`pyvista.TecplotReader` |
+----------------+---------------------------------------------+
| ``.dcm`` | :class:`pyvista.DICOMReader` |
Expand Down Expand Up @@ -2579,6 +2581,153 @@ class GambitReader(BaseReader):
_vtk_class_name = "vtkGAMBITReader"


class GaussianCubeReader(BaseReader):
"""GaussianCubeReader for .cube files.
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> filename = examples.download_m4_total_density(load=False)
>>> filename.split("/")[-1] # omit the path
'm4_TotalDensity.cube'
"""

_vtk_module_name = "vtkIOChemistry"
_vtk_class_name = "vtkGaussianCubeReader"

def read(self, grid: bool = True):
"""Read the file and return the output.
Parameters
----------
grid : bool, default: False
Output as a grid if ``True``, otherwise return the polydata.
"""
from pyvista.core.filters import _update_alg # avoid circular import

_update_alg(self.reader, progress_bar=self._progress_bar, message=self._progress_msg)
data = (
wrap(self.reader.GetGridOutput()) if grid else wrap(self.reader.GetOutputDataObject(0))
)
if data is None: # pragma: no cover
raise RuntimeError("File reader failed to read and/or produced no output.")
data._post_file_load_processing() # type: ignore[union-attr]

# check for any pyvista metadata
data._restore_metadata() # type: ignore[union-attr]
return data

@property
def hb_scale(self) -> float:
"""Get the scaling factor to compute bonds with hydrogen atoms.
Returns
-------
float
The scaling factor to compute bonds with hydrogen atoms.
"""
return self.reader.GetHBScale()

@hb_scale.setter
def hb_scale(self, hb_scale: float):
"""Set the scaling factor to compute bonds with hydrogen atoms.
Parameters
----------
hb_scale : float
The scaling factor to compute bonds with hydrogen atoms.
"""
self.reader.SetHBScale(hb_scale)

@property
def b_scale(self) -> float:
"""Get the scaling factor to compute bonds between non-hydrogen atoms.
Returns
-------
float
The scaling factor to compute bonds between non-hydrogen atoms.
"""
return self.reader.GetBScale()

@b_scale.setter
def b_scale(self, b_scale: float):
"""Set the scaling factor to compute bonds between non-hydrogen atoms.
Parameters
----------
b_scale : float
The scaling factor to compute bonds between non-hydrogen atoms.
"""
self.reader.SetBScale(b_scale)


class MINCImageReader(BaseReader):
"""MINCImageReader for .mnc files.
.. versionadded:: 0.44.0
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> filename = examples.download_t3_grid_0(load=False)
>>> reader = pv.get_reader(filename)
>>> mesh = reader.read()
>>> mesh.plot()
"""

_vtk_module_name = "vtkIOMINC"
_vtk_class_name = "vtkMINCImageReader"


class PDBReader(BaseReader):
"""PDBReader for .pdb files.
.. versionadded:: 0.44.0
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> filename = examples.download_caffeine(load=False)
>>> filename.split("/")[-1] # omit the path
'caffeine.pdb'
"""

_vtk_module_name = "vtkIOChemistry"
_vtk_class_name = "vtkPDBReader"


class GESignaReader(BaseReader):
"""GESignaReader for .MR files.
.. versionadded:: 0.44.0
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> filename = examples.download_e07733s002i009(load=False)
>>> reader = pv.get_reader(filename)
>>> mesh = reader.read()
>>> mesh.plot()
"""

_vtk_module_name = "vtkIOImage"
_vtk_class_name = "vtkGESignaReader"


class NetCDFCFReader(BaseReader):
"""NetCDFCFReader for .nc files.
Expand Down Expand Up @@ -2606,6 +2755,7 @@ class NetCDFCFReader(BaseReader):
'.cas': FluentReader,
'.case': EnSightReader,
'.cgns': CGNSReader,
'.cube': GaussianCubeReader,
'.dat': TecplotReader,
'.dcm': DICOMReader,
'.dem': DEMReader,
Expand All @@ -2624,6 +2774,8 @@ class NetCDFCFReader(BaseReader):
'.jpg': JPEGReader,
'.mha': MetaImageReader,
'.mhd': MetaImageReader,
'.mnc': MINCImageReader,
'.mr': GESignaReader,
'.nc': NetCDFCFReader,
'.neu': GambitReader,
'.nhdr': NRRDReader,
Expand All @@ -2632,6 +2784,7 @@ class NetCDFCFReader(BaseReader):
'.nrrd': NRRDReader,
'.obj': OBJReader,
'.p3d': Plot3DMetaReader,
'.pdb': PDBReader,
'.ply': PLYReader,
'.png': PNGReader,
'.pnm': PNMReader,
Expand Down
164 changes: 164 additions & 0 deletions pyvista/examples/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -7675,6 +7675,58 @@ def _dataset_room_cff_files_func():
_dataset_room_cff = _MultiFileDownloadableDatasetLoader(_dataset_room_cff_files_func)


def download_m4_total_density(load=True): # pragma: no cover
"""Download a total density dataset of the chemistry.
Parameters
----------
load : bool, default: True
Load the dataset after downloading it when ``True``. Set this
to ``False`` and only the filename will be returned.
Returns
-------
pyvista.ImageData | str
DataSet or filename depending on ``load``.
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> filename = examples.download_m4_total_density(load=False)
>>> reader = pv.get_reader(filename)
>>> reader.hb_scale = 1.1
>>> reader.b_scale = 10.0
>>> grid = reader.read()
>>> poly = reader.read(grid=False)
Add the outline and volume to the plotter.
>>> pl = pv.Plotter()
>>> outline = pl.add_mesh(grid.outline(), color="black")
>>> volume = pl.add_volume(grid)
Add atoms and bonds to the plotter.
>>> atoms = pl.add_mesh(poly.glyph(geom=pv.Sphere()), color="red")
>>> bonds = pl.add_mesh(poly.tube(), color="white")
>>> pl.show(cpos="zx")
.. seealso::
:ref:`M4 Total Density Dataset <m4_total_density_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _download_dataset(_dataset_m4_total_density, load=load)


_dataset_m4_total_density = _SingleFileDownloadableDatasetLoader('m4_TotalDensity.cube')


def download_headsq(load=True): # pragma: no cover
"""Download the headsq dataset.
Expand Down Expand Up @@ -7752,6 +7804,118 @@ def download_prism(load=True): # pragma: no cover
_dataset_prism = _SingleFileDownloadableDatasetLoader('prism.neu')


def download_t3_grid_0(load=True): # pragma: no cover
"""Download a T3 grid 0 image.
.. versionadded:: 0.44.0
Parameters
----------
load : bool, default: True
Load the dataset after downloading it when ``True``. Set this
to ``False`` and only the filename will be returned.
Returns
-------
pyvista.ImageData | str
DataSet or filename depending on ``load``.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.download_t3_grid_0()
>>> mesh.plot()
.. seealso::
:ref:`T3 Grid 0 Dataset <t3_grid_0_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _download_dataset(_dataset_t3_grid_0, load=load)


_dataset_t3_grid_0 = _SingleFileDownloadableDatasetLoader('t3_grid_0.mnc')


def download_caffeine(load=True): # pragma: no cover
"""Download the caffeine molecule.
.. versionadded:: 0.44.0
Parameters
----------
load : bool, default: True
Load the dataset after downloading it when ``True``. Set this
to ``False`` and only the filename will be returned.
Returns
-------
pyvista.PolyData | str
DataSet or filename depending on ``load``.
Examples
--------
>>> import pyvista as pv
>>> from pyvista import examples
>>> filename = examples.download_caffeine(load=False)
>>> reader = pv.get_reader(filename)
>>> poly = reader.read()
Add atoms and bonds to the plotter.
>>> pl = pv.Plotter()
>>> atoms = pl.add_mesh(
... poly.glyph(geom=pv.Sphere(radius=0.1)), color="red"
... )
>>> bonds = pl.add_mesh(poly.tube(radius=0.1), color="gray")
>>> pl.show(cpos="xy")
.. seealso::
:ref:`Caffeine Dataset <caffeine_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _download_dataset(_dataset_caffeine, load=load)


_dataset_caffeine = _SingleFileDownloadableDatasetLoader('caffeine.pdb')


def download_e07733s002i009(load=True): # paragma: no cover
"""Download a e07733s002i009 image.
.. versionadded:: 0.44.0
Parameters
----------
load : bool, default: True
Load the dataset after downloading it when ``True``. Set this
to ``False`` and only the filename will be returned.
Returns
-------
pyvista.ImageData | str
DataSet or filename depending on ``load``.
Examples
--------
>>> from pyvista import examples
>>> mesh = examples.download_e07733s002i009()
>>> mesh.plot()
.. seealso::
:ref:`E07733s002i009 Dataset <e07733s002i009_dataset>`
See this dataset in the Dataset Gallery for more info.
"""
return _download_dataset(_dataset_e07733s002i009, load=load)


_dataset_e07733s002i009 = _SingleFileDownloadableDatasetLoader('E07733S002I009.MR')


def download_tos_O1_2001_2002(load=True): # pragma: no cover
"""Download the TOS_O1_2001_2002 dataset.
Expand Down

0 comments on commit 3d98911

Please sign in to comment.