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

Merged
merged 17 commits into from
Nov 17, 2024
62 changes: 62 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,63 @@ 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.file_name, "r") as file:
tree = ET.parse(file)
tStehling marked this conversation as resolved.
Show resolved Hide resolved
root = tree.getroot()

# Suche nach dem UnstructuredGrid-Knoten
unstructured_grid = root.find(".//UnstructuredGrid")
if unstructured_grid is not None:
# Suche nach Zell- und Punktinformationen
piece = unstructured_grid.find(".//Piece")
if piece is not None:
num_cells = piece.get("NumberOfCells")
num_points = piece.get("NumberOfPoints")

# Setze die Metadaten
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:
# Logge das Problem und setze Metadaten auf None, falls Parsing fehlschlägt
dataset.metadata.file_format = None
dataset.metadata.num_cells = None
dataset.metadata.num_points = None
log.warning(f"Fehler beim Setzen der Metadaten für VTU-Datei: {e}")
tStehling marked this conversation as resolved.
Show resolved Hide resolved

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>
35 changes: 0 additions & 35 deletions lib/galaxy/datatypes/test/output.vtu

This file was deleted.

35 changes: 1 addition & 34 deletions lib/galaxy/datatypes/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,37 +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)

class Vtu(GenericXml):
"""Format for defining VTU (VTK Unstructured Grid) data https://docs.vtk.org/"""

edam_data = "edam:data_3671"
edam_format = "edam:format_3621"
file_ext = "vtu"

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_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( 'output.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"')
return file_prefix.search(SBML_MARKER)
Loading