Ejemplo n.º 1
0
def test_facetreader():
    filename = examples.download_clown(load=False)
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.FacetReader)
    assert reader.filename == filename

    mesh = reader.read()
    assert all([mesh.n_points, mesh.n_cells])
Ejemplo n.º 2
0
def test_vtkreader():
    filename = examples.hexbeamfile
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.VTKDataSetReader)
    assert reader.filename == filename

    mesh = reader.read()
    assert all([mesh.n_points, mesh.n_cells])
Ejemplo n.º 3
0
def test_binarymarchingcubesreader():
    filename = examples.download_pine_roots(load=False)
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.BinaryMarchingCubesReader)
    assert reader.filename == filename

    mesh = reader.read()
    assert all([mesh.n_points, mesh.n_cells])
Ejemplo n.º 4
0
def test_byureader():
    filename = examples.download_teapot(load=False)
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.BYUReader)
    assert reader.filename == filename

    mesh = reader.read()
    assert all([mesh.n_points, mesh.n_cells])
Ejemplo n.º 5
0
def test_plyreader():
    filename = examples.spherefile
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.PLYReader)
    assert reader.filename == filename

    mesh = reader.read()
    assert all([mesh.n_points, mesh.n_cells])
Ejemplo n.º 6
0
def test_xmlpolydatareader(tmpdir):
    tmpfile = tmpdir.join("temp.vtp")
    mesh = pyvista.Sphere()
    mesh.save(tmpfile.strpath)

    reader = pyvista.get_reader(tmpfile.strpath)
    assert reader.filename == tmpfile.strpath
    new_mesh = reader.read()
    assert isinstance(new_mesh, pyvista.PolyData)
    assert new_mesh.n_points == mesh.n_points
    assert new_mesh.n_cells == mesh.n_cells
Ejemplo n.º 7
0
def test_xmlimagedatareader(tmpdir):
    tmpfile = tmpdir.join("temp.vti")
    mesh = pyvista.UniformGrid()
    mesh.save(tmpfile.strpath)

    reader = pyvista.get_reader(tmpfile.strpath)
    assert reader.filename == tmpfile.strpath
    new_mesh = reader.read()
    assert isinstance(new_mesh, pyvista.UniformGrid)
    assert new_mesh.n_points == mesh.n_points
    assert new_mesh.n_cells == mesh.n_cells
Ejemplo n.º 8
0
def test_xmlstructuredgridreader(tmpdir):
    tmpfile = tmpdir.join("temp.vts")
    mesh = pyvista.StructuredGrid()
    mesh.save(tmpfile.strpath)

    reader = pyvista.get_reader(tmpfile.strpath)
    assert reader.filename == tmpfile.strpath
    new_mesh = reader.read()
    assert isinstance(new_mesh, pyvista.StructuredGrid)
    assert new_mesh.n_points == mesh.n_points
    assert new_mesh.n_cells == mesh.n_cells
Ejemplo n.º 9
0
def test_plot3dmetareader():
    filename, _ = _download_file('multi.p3d')
    _download_file('multi-bin.xyz')
    _download_file('multi-bin.q')
    _download_file('multi-bin.f')
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.Plot3DMetaReader)
    assert reader.filename == filename

    mesh = reader.read()
    for m in mesh:
        assert all([m.n_points, m.n_cells])
Ejemplo n.º 10
0
def test_xmlmultiblockreader(tmpdir):
    tmpfile = tmpdir.join("temp.vtm")
    mesh = pyvista.MultiBlock([pyvista.Sphere() for i in range(5)])
    mesh.save(tmpfile.strpath)

    reader = pyvista.get_reader(tmpfile.strpath)
    assert reader.filename == tmpfile.strpath
    new_mesh = reader.read()
    assert isinstance(new_mesh, pyvista.MultiBlock)
    assert new_mesh.n_blocks == mesh.n_blocks
    for i in range(new_mesh.n_blocks):
        assert new_mesh[i].n_points == mesh[i].n_points
        assert new_mesh[i].n_cells == mesh[i].n_cells
Ejemplo n.º 11
0
def test_reader_cell_point_data(tmpdir):
    tmpfile = tmpdir.join("temp.vtp")
    mesh = pyvista.Sphere()
    mesh['height'] = mesh.points[:, 1]
    mesh['id'] = np.arange(mesh.n_cells)
    mesh.save(tmpfile.strpath)
    # mesh has an additional 'Normals' point data array

    reader = pyvista.get_reader(tmpfile.strpath)

    assert reader.number_cell_arrays == 1
    assert reader.number_point_arrays == 2

    assert reader.cell_array_names == ['id']
    assert reader.point_array_names == ['Normals', 'height']

    assert reader.all_cell_arrays_status == {'id': True}
    assert reader.all_point_arrays_status == {'Normals': True, 'height': True}

    assert reader.cell_array_status('id') is True
    assert reader.point_array_status('Normals') is True

    reader.disable_all_cell_arrays()
    assert reader.cell_array_status('id') is False

    reader.disable_all_point_arrays()
    assert reader.all_point_arrays_status == {
        'Normals': False,
        'height': False
    }

    reader.enable_all_cell_arrays()
    assert reader.cell_array_status('id') is True

    reader.enable_all_point_arrays()
    assert reader.all_point_arrays_status == {'Normals': True, 'height': True}

    reader.disable_cell_array('id')
    assert reader.cell_array_status('id') is False

    reader.disable_point_array('Normals')
    assert reader.point_array_status('Normals') is False

    reader.enable_cell_array('id')
    assert reader.cell_array_status('id') is True

    reader.enable_point_array('Normals')
    assert reader.point_array_status('Normals') is True
Ejemplo n.º 12
0
def test_ensightreader_arrays():
    filename = examples.download_backward_facing_step(load=False)

    reader = pyvista.get_reader(filename)
    assert reader.filename == filename
    assert reader.number_cell_arrays == 9
    assert reader.number_point_arrays == 0

    assert reader.cell_array_names == [
        'v2', 'nut', 'k', 'nuTilda', 'p', 'omega', 'f', 'epsilon', 'U'
    ]
    assert reader.point_array_names == []

    reader.disable_all_cell_arrays()
    reader.enable_cell_array('k')

    assert reader.all_cell_arrays_status == {
        'v2': False,
        'nut': False,
        'k': True,
        'nuTilda': False,
        'p': False,
        'omega': False,
        'f': False,
        'epsilon': False,
        'U': False
    }

    mesh = reader.read()
    assert isinstance(mesh, pyvista.MultiBlock)

    for i in range(mesh.n_blocks):
        assert all([mesh[i].n_points, mesh[i].n_cells])
        assert mesh[i].array_names == ['k']

    # re-enable all cell arrays and read again
    reader.enable_all_cell_arrays()
    all_mesh = reader.read()
    assert isinstance(all_mesh, pyvista.MultiBlock)

    for i in range(all_mesh.n_blocks):
        assert all([all_mesh[i].n_points, all_mesh[i].n_cells])
        assert all_mesh[i].array_names == [
            'v2', 'nut', 'k', 'nuTilda', 'p', 'omega', 'f', 'epsilon', 'U'
        ]
Ejemplo n.º 13
0
def test_pvdreader():
    filename = examples.download_wavy(load=False)
    reader = pyvista.get_reader(filename)
    assert isinstance(reader, pyvista.PVDReader)
    assert reader.reader == reader  # PVDReader refers to itself
    assert reader.filename == filename

    assert reader.number_time_points == 15
    assert reader.time_point_value(1) == 1.0
    assert np.array_equal(reader.time_values, np.arange(0, 15, dtype=np.float))

    assert reader.active_time_value == reader.time_values[0]

    active_datasets = reader.active_datasets
    assert len(active_datasets) == 1
    active_dataset0 = active_datasets[0]
    assert active_dataset0.time == 0.0
    assert active_dataset0.filename == "wavy/wavy00.vts"
    assert active_dataset0.group == ""
    assert active_dataset0.part == 0

    assert len(reader.datasets) == len(reader.time_values)

    active_readers = reader.active_readers
    assert len(active_readers) == 1
    active_reader = active_readers[0]
    assert isinstance(active_reader, pyvista.XMLStructuredGridReader)

    reader.set_active_time_value(1.0)
    assert reader.active_time_value == 1.0

    reader.set_active_time_point(2)
    assert reader.active_time_value == 2.0

    mesh = reader.read()
    assert isinstance(mesh, pyvista.MultiBlock)
    assert len(mesh) == 1
    assert isinstance(mesh[0], pyvista.StructuredGrid)
Ejemplo n.º 14
0
def test_ensightreader_timepoints():
    filename = examples.download_naca(load=False)

    reader = pyvista.get_reader(filename)
    assert reader.filename == filename

    assert reader.number_time_points == 2
    assert reader.time_values == [1.0, 3.0]
    assert reader.time_point_value(0) == 1.0
    assert reader.time_point_value(1) == 3.0

    assert reader.active_time_value == 1.0
    mesh_1 = reader.read()

    reader.set_active_time_value(3.0)
    assert reader.active_time_value == 3.0
    mesh_3 = reader.read()

    # assert all the data is different
    for m_1, m_3 in zip(mesh_1, mesh_3):
        assert not all(m_1['DENS'] == m_3['DENS'])

    reader.set_active_time_point(0)
    assert reader.active_time_value == 1.0
Ejemplo n.º 15
0
def cli(rbc, plt, verbose, output, axes, bounding_box, clip, stl):
    """Convert and visualise cell position files used in HemoCell [0].

    The script reads from any number of red blood cell position files (RBC.pos)
    and allows for direct visualisation with PyVista [1] or conversion to XDMF
    for inspection with Paraview through `-o, --output`.

    The RBC positions can be provided through `stdin` by specifying a dash
    (`-`) as argument and piping the input, e.g. `cat RBC.pos | pos_to_vtk -`.

    Optionally, any number of platelets position files (PLT.pos) can be
    supplied by the `--plt` argument, which might be repeated any number of
    times to specify multiple PLT.pos files.

    [0] https://hemocell.eu

    [1] https://dev.pyvista.org/index.html
    """
    if len(rbc) == 0 and len(plt) == 0:
        message = """No input files provided. When passing an RBC through
`stdin`, please provide a dash (`-`) as the argument."""
        raise click.UsageError(message)

    if (bounding_box is not None) and (stl is not None):
        message = """Options `--bounding-box` and `--stl` are exclusive and
cannot be combined. Please provide only one."""
        raise click.UsageError(message)

    rbcs = Cells(flatten([Cells.from_pos(rbc) for rbc in rbc]))
    plts = Cells(flatten([Cells.from_pos(plt) for plt in plt]))

    if len(rbcs) == 0:
        # The script terminates early when no RBCs are present. This
        # most-likely corresponds with users error, e.g. by providing the wrong
        # file path or an empty file.
        raise click.UsageError("No RBC cells to display." "")

    if bounding_box:
        bounds = flatten(zip((0, 0, 0), bounding_box))
        wireframe = pv.Box(bounds)

    if stl:
        wireframe = pv.get_reader(stl).read()
        wireframe.scale((1e3, 1e3, 1e3))

    if clip:
        rbcs = rbcs.clip(wireframe, bounding_box)
        plts = plts.clip(wireframe, bounding_box)

    if len(rbcs) == 0 and len(plts) == 0:
        raise click.UsageError("No cells remain after clipping.")

    if output:
        rbcs = create_glyphs(rbcs, CellType.RBC)
        merged_cells = rbcs.merge(plts) if plt else rbcs
        pv.save_meshio(output, merged_cells)

        if verbose:
            click.echo(f"Written to: '{click.format_filename(output)}'")
        return 0

    plotter = pv.Plotter()

    rbcs_glyph = create_glyphs(rbcs, CellType.RBC)
    rbcs_actor = plotter.add_mesh(rbcs_glyph, color="red")

    if plt:
        plts_glyph = create_glyphs(plts, CellType.PLT)
        plts_actor = plotter.add_mesh(plts_glyph, color="yellow")

    if bounding_box or stl:
        plotter.add_mesh(wireframe, style='wireframe')

    message = f'RBC: {len(rbcs)}\nPLT: {len(plts)}\n'

    if clip:
        # Only when the domain is clipped, a bounding domain is known either
        # through the given bounding box or by the given STL surface mesh.
        # These allow to determine an estimate of the domain's volume and
        # thereby estimate the expected hematocrit given the number of RBCs
        # left after clipping.
        volume = 1e-6 * wireframe.volume
        hc_percentage = 100 * rbcs.hematocrit(wireframe)
        message += f'Domain volume estimate (µm^3): {volume:.2f}\n'
        message += f'Hematocrit (vol%): {hc_percentage:.2f}\n'

    plotter.add_text(message, position='lower_right')

    # A simple widget illustrating the x-y-z axes orientation of the domain.
    if axes:
        plotter.add_axes(line_width=5, labels_off=False)

    # Two radio buttons are added that enable to show/hide the glyphs
    # corresponding to all RBCs or PLTs, where the lambda functions are given
    # to implement the callback function toggling the right set of glyphs.
    plotter.add_checkbox_button_widget(lambda x: rbcs_actor.SetVisibility(x),
                                       position=(5, 12),
                                       color_on="red",
                                       value=True)
    plotter.add_checkbox_button_widget(lambda x: plts_actor.SetVisibility(x),
                                       position=(5, 62),
                                       color_on="yellow",
                                       value=True)

    return plotter.show()
Ejemplo n.º 16
0
def test_get_reader_fail():
    with pytest.raises(ValueError):
        reader = pyvista.get_reader("not_a_supported_file.no_data")
Ejemplo n.º 17
0
mesh['height'] = mesh.points[:, 1]
mesh['id'] = np.arange(mesh.n_cells)
mesh.save(temp_file.name)

###############################################################################
# :func:`pyvista.read` function reads all the data in the file. This provides
# a quick and easy one-liner to read data from files.

new_mesh = pyvista.read(temp_file.name)
print(f"All arrays: {mesh.array_names}")

###############################################################################
# Using :func:`pyvista.get_reader` enables more fine-grained control of reading data
# files. Reading in a ``.vtp``` file uses the :class:`pyvista.XMLPolyDataReader`.

reader = pyvista.get_reader(temp_file.name)
reader
# Alternative method: reader = pyvista.XMLPolyDataReader(temp_file.name)

###############################################################################
# Some reader classes, including this one, offer the ability to inspect the
# data file before loading all the data. For example, we can access the number
# and names of point and cell arrays.

print(f"Number of point arrays: {reader.number_point_arrays}")
print(f"Available point data:   {reader.point_array_names}")
print(f"Number of cell arrays:  {reader.number_cell_arrays}")
print(f"Available cell data:    {reader.cell_array_names}")

###############################################################################
# We can select which data to read by selectively disabling or enabling