Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cb-util
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christoph Alt
cb-util
Commits
7f5285fe
Commit
7f5285fe
authored
1 year ago
by
Christoph Alt
Browse files
Options
Downloads
Patches
Plain Diff
added functionality to parse and compare to vtu files
parent
8f62a819
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#56113
passed
1 year ago
Stage: test
Stage: deploy
Stage: trigger
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
cbutil/__init__.py
+1
-0
1 addition, 0 deletions
cbutil/__init__.py
cbutil/compare_vtu.py
+39
-0
39 additions, 0 deletions
cbutil/compare_vtu.py
pyproject.toml
+1
-0
1 addition, 0 deletions
pyproject.toml
tests/test_compare_vtu.py
+56
-0
56 additions, 0 deletions
tests/test_compare_vtu.py
with
97 additions
and
0 deletions
cbutil/__init__.py
+
1
−
0
View file @
7f5285fe
...
@@ -6,3 +6,4 @@ from .data_points import DataPoint, data_point_factory
...
@@ -6,3 +6,4 @@ from .data_points import DataPoint, data_point_factory
from
.get_job_info
import
get_url_from_env
,
get_job_datapoints
from
.get_job_info
import
get_url_from_env
,
get_job_datapoints
from
.gitlab_api
import
get_git_infos_from_api
from
.gitlab_api
import
get_git_infos_from_api
from
.update_data
import
get_updated_data
from
.update_data
import
get_updated_data
from
.compare_vtu
import
compare_vtu_files
This diff is collapsed.
Click to expand it.
cbutil/compare_vtu.py
0 → 100644
+
39
−
0
View file @
7f5285fe
import
xml.dom.minidom
import
logging
import
numpy
as
np
logger
=
logging
.
getLogger
(
__name__
)
def
read_vtu_file
(
file_name
):
try
:
return
xml
.
dom
.
minidom
.
parse
(
file_name
)
except
Exception
as
e
:
logger
.
error
(
f
"
Error parsing
{
file_name
}
:
{
str
(
e
)
}
"
)
raise
def
extract_data_array
(
output
,
name
):
data_arrays
=
output
.
getElementsByTagName
(
"
DataArray
"
)
data_array
=
next
(
(
da
for
da
in
data_arrays
if
da
.
attributes
[
'
Name
'
].
value
==
name
),
None
)
if
data_array
is
None
:
logging
.
error
(
f
"
Data array
'
{
name
}
'
not found in VTU file.
"
)
return
None
floats
=
[
float
(
n
)
for
n
in
data_array
.
firstChild
.
data
.
strip
().
split
()]
return
np
.
array
(
floats
,
dtype
=
data_array
.
attributes
[
"
type
"
].
value
.
lower
())
def
calculate_L2_norm
(
ref
,
data
):
return
np
.
linalg
.
norm
(
ref
-
data
)
def
compare_vtu_files
(
ref_file
,
result_file
,
arrays
):
ref_output
=
read_vtu_file
(
ref_file
)
result_output
=
read_vtu_file
(
result_file
)
differences
=
{}
for
array
in
arrays
:
ref
=
extract_data_array
(
ref_output
,
array
)
result
=
extract_data_array
(
result_output
,
array
)
if
ref
is
not
None
and
result
is
not
None
:
differences
[
f
'
{
array
}
_L2_diff
'
]
=
calculate_L2_norm
(
ref
,
result
)
return
differences
This diff is collapsed.
Click to expand it.
pyproject.toml
+
1
−
0
View file @
7f5285fe
...
@@ -15,6 +15,7 @@ dependencies = [
...
@@ -15,6 +15,7 @@ dependencies = [
"gitpython"
,
"gitpython"
,
"requests"
,
"requests"
,
"kadi-apy"
,
"kadi-apy"
,
"numpy"
,
"importlib_resources ; python_version<'3.7'"
,
"importlib_resources ; python_version<'3.7'"
,
]
]
...
...
This diff is collapsed.
Click to expand it.
tests/test_compare_vtu.py
0 → 100644
+
56
−
0
View file @
7f5285fe
import
os
import
pytest
import
tempfile
from
cbutil.compare_vtu
import
compare_vtu_files
@pytest.fixture
def
setup_test_files
():
# Create a reference VTU file and a result VTU file for testing
# Assuming the reference VTU file contains data arrays 'U1', 'U2', and 'U3'
# with values [1.0, 2.0, 3.0], [4.0, 5.0, 6.0], and [7.0, 8.0, 9.0], respectively.
# The result VTU file contains identical data arrays.
# Create the reference VTU file
with
tempfile
.
TemporaryDirectory
()
as
tmp_dir
:
ref_file
=
f
"
{
tmp_dir
}
/reference_test_file.vtu
"
result_file
=
f
"
{
tmp_dir
}
/result_test_file.vtu
"
with
open
(
ref_file
,
"
w
"
)
as
f
:
f
.
write
(
'
<?xml version=
"
1.0
"
?>
\n
<VTKFile type=
"
UnstructuredGrid
"
version=
"
0.1
"
byte_order=
"
LittleEndian
"
>
\n
'
)
f
.
write
(
'
<UnstructuredGrid>
\n
<PointData>
\n
'
)
for
array_name
,
values
in
zip
([
'
U1
'
,
'
U2
'
,
'
U3
'
],
[[
1.0
,
2.0
,
3.0
],
[
4.0
,
5.0
,
6.0
],
[
7.0
,
8.0
,
9.0
]]):
f
.
write
(
f
'
<DataArray type=
"
Float64
"
Name=
"
{
array_name
}
"
format=
"
ascii
"
>
\n
'
)
f
.
write
(
"
"
.
join
(
map
(
str
,
values
))
+
"
\n
"
)
f
.
write
(
"
</DataArray>
\n
"
)
f
.
write
(
'
</PointData>
\n
</UnstructuredGrid>
\n
</VTKFile>
\n
'
)
# Create the result VTU file (identical to the reference)
with
open
(
result_file
,
"
w
"
)
as
f
:
f
.
write
(
'
<?xml version=
"
1.0
"
?>
\n
<VTKFile type=
"
UnstructuredGrid
"
version=
"
0.1
"
byte_order=
"
LittleEndian
"
>
\n
'
)
f
.
write
(
'
<UnstructuredGrid>
\n
<PointData>
\n
'
)
for
array_name
,
values
in
zip
([
'
U1
'
,
'
U2
'
,
'
U3
'
],
[[
1.0
,
2.0
,
3.0
],
[
4.0
,
5.0
,
6.0
],
[
7.0
,
8.0
,
9.0
]]):
f
.
write
(
f
'
<DataArray type=
"
Float64
"
Name=
"
{
array_name
}
"
format=
"
ascii
"
>
\n
'
)
f
.
write
(
"
"
.
join
(
map
(
str
,
values
))
+
"
\n
"
)
f
.
write
(
"
</DataArray>
\n
"
)
f
.
write
(
'
</PointData>
\n
</UnstructuredGrid>
\n
</VTKFile>
\n
'
)
yield
ref_file
,
result_file
def
test_valid_comparison
(
setup_test_files
):
ref_file
,
result_file
=
setup_test_files
result
=
compare_vtu_files
(
ref_file
,
result_file
,
[
'
U1
'
,
'
U2
'
,
'
U3
'
])
assert
result
[
'
U1_L2_diff
'
]
==
pytest
.
approx
(
0.0
)
assert
result
[
'
U2_L2_diff
'
]
==
pytest
.
approx
(
0.0
)
assert
result
[
'
U3_L2_diff
'
]
==
pytest
.
approx
(
0.0
)
def
test_missing_reference_file
():
with
pytest
.
raises
(
FileNotFoundError
):
compare_vtu_files
(
"
non_existent_file.vtu
"
,
"
other_file.vtu
"
,
[
"
U1
"
])
def
test_missing_data_array
(
setup_test_files
):
ref_file
,
result_file
=
setup_test_files
result
=
compare_vtu_files
(
ref_file
,
result_file
,
[
'
U1
'
,
'
U2
'
,
'
U3
'
,
'
U4
'
])
assert
'
U4_L2_diff
'
not
in
result
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment