Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XML based vtk datatype #19104

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
59 changes: 59 additions & 0 deletions lib/galaxy/datatypes/constructive_solid_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
FilePrefix,
)
from galaxy.datatypes.tabular import Tabular
from galaxy.datatypes.xml import GenericXml
from xml.etree import ElementTree as ET
bernt-matthias marked this conversation as resolved.
Show resolved Hide resolved

if TYPE_CHECKING:
from io import TextIOBase
Expand Down Expand Up @@ -813,3 +815,60 @@ def get_next_line(fh):
# Discard the rest of the line
fh.readline()
return line.strip()

class Vtu(GenericXml):
tStehling marked this conversation as resolved.
Show resolved Hide resolved
"""Format for defining VTU (VTK Unstructured Grid) data https://docs.vtk.org/"""
tStehling marked this conversation as resolved.
Show resolved Hide resolved

edam_data = "edam:data_3671"
tStehling marked this conversation as resolved.
Show resolved Hide resolved
edam_format = "edam:format_3621"
file_ext = "vtu"
tStehling marked this conversation as resolved.
Show resolved Hide resolved

MetadataElement(name="file_format", default=None, desc="File format", readonly=True, optional=True, visible=True)
MetadataElement(name="num_cells", desc="Number of cells in the mesh", param_type="int", optional=True)
MetadataElement(name="num_points", desc="Number of points in the mesh", param_type="int", optional=True)

def set_meta(self, dataset: DatasetProtocol, **kwd) -> None:
"""Set metadata for VTU files, including number of cells and points."""
try:
with open(dataset.get_file_name(), "r") as file:
tree = ET.parse(file)
tStehling marked this conversation as resolved.
Show resolved Hide resolved
root = tree.getroot()

unstructured_grid = root.find(".//UnstructuredGrid")
if unstructured_grid is not None:

piece = unstructured_grid.find(".//Piece")
if piece is not None:
num_cells = piece.get("NumberOfCells")
num_points = piece.get("NumberOfPoints")


dataset.metadata.file_format = "VTK Unstructured Grid"
dataset.metadata.num_cells = int(num_cells) if num_cells else None
dataset.metadata.num_points = int(num_points) if num_points else None
except Exception as e:
dataset.metadata.file_format = None
dataset.metadata.num_cells = None
dataset.metadata.num_points = None

def set_peek(self, dataset: DatasetProtocol, **kwd) -> None:
"""Set the peek and blurb text"""
if not dataset.dataset.purged:
dataset.peek = data.get_file_peek(dataset.get_file_name())
"VTK Unstructured Grid file (VTU)"
else:
dataset.peek = "file does not exist"
dataset.blurb = "file purged from disk"

def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
""" "Checking for keystring - 'VTKFile type="UnstructuredGrid"'.

>>> from galaxy.datatypes.sniff import get_test_fname
>>> fname = get_test_fname( 'data.vtu' )
>>> Vtu().sniff( fname )
True
>>> fname = get_test_fname( 'solid.xml' )
>>> Vtu().sniff( fname )
False
"""
return self._has_root_element_in_prefix(file_prefix, 'VTKFile type="UnstructuredGrid"')
41 changes: 41 additions & 0 deletions lib/galaxy/datatypes/test/data.vtu
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
<UnstructuredGrid>
<Piece NumberOfPoints="21" NumberOfCells="21">
<PointData>
<DataArray type="Float64" Name="P_BC_FLOW" format="binary" RangeMin="-2" RangeMax="0">
AQAAAACAAACoAAAAFgAAAA==eJxjYCAKHCBAE1JPqvno4gwABYYGwQ==
</DataArray>
<DataArray type="UInt64" Name="bulk_node_ids" format="binary" RangeMin="0" RangeMax="70">
AQAAAACAAACoAAAAOwAAAA==eJwtxRECgDAAAMBJkDQomQySBk2SYNKg/v+kQXdyIfySJ2fP3r34cHTx6tObqy/fbn7c/frzAL7YAwQ=
</DataArray>
</PointData>
<CellData>
</CellData>
<Points>
<DataArray type="Float64" Name="Points" NumberOfComponents="3" format="binary" RangeMin="42.42640687119285" RangeMax="94.33981132056604">
AQAAAACAAAD4AQAAYwAAAA==eJxjYAABOwcw1RAIoeEAXVzlAIo4gx929XBxqHq4fjT1GOLo6j1xqPdEVQ+31wWHe2Di6OajqccQRzffDlU9hjiaerh5Jmjm+KGKY4QDurgnqjiGv9DVu+BQbwcXBwAoLCke
<InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
<Value index="0">
42.426406871
</Value>
<Value index="1">
94.339811321
</Value>
</InformationKey>
</DataArray>
</Points>
<Cells>
<DataArray type="Int64" Name="connectivity" format="binary" RangeMin="0" RangeMax="20">
AQAAAACAAACoAAAAMQAAAA==eJwtxUECQCAAALAQRSLx/7d2sF0Wwm/y7MXRqzcnZ+8+XHy6+vLt5sfdrz8PMMgA0w==
</DataArray>
<DataArray type="Int64" Name="offsets" format="binary" RangeMin="1" RangeMax="21">
AQAAAACAAACoAAAAMgAAAA==eJwtxbcBgCAAADArFsAC8v+rDiRLxqGbPHvx6uDNuw+fjk7Ovnz78evi6s/NPzgAAOg=
</DataArray>
<DataArray type="UInt8" Name="types" format="binary" RangeMin="1" RangeMax="1">
AQAAAACAAAAVAAAACwAAAA==eJxjZMQCAAD8ABY=
</DataArray>
</Cells>
</Piece>
</UnstructuredGrid>
</VTKFile>
25 changes: 25 additions & 0 deletions lib/galaxy/datatypes/test/solid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<phase>
<type>Solid</type>
<properties>
<property>
<name>density</name>
<type>Constant</type>
<value>2300</value>
</property>
<property>
<name>thermal_conductivity</name>
<type>Constant</type>
<value>1.9</value>
</property>
<property>
<name>specific_heat_capacity</name>
<type>Constant</type>
<value>800</value>
</property>
<property>
<name>thermal_expansivity</name>
<type>Constant</type>
<value>1.7e-5</value>
</property>
</properties>
</phase>
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,4 @@ def sniff_prefix(self, file_prefix: FilePrefix) -> bool:
"""
Checking for keyword - '<sbml' in the first 200 lines.
"""
return file_prefix.search(SBML_MARKER)
return file_prefix.search(SBML_MARKER)
tStehling marked this conversation as resolved.
Show resolved Hide resolved