Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
calibration
pycalibration
Commits
f86be23d
Commit
f86be23d
authored
2 years ago
by
Thomas Kluyver
Browse files
Options
Downloads
Patches
Plain Diff
Add support for saving metadata fragments & merging into calibration_metadata.yml
parent
e73a5bc9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!781
Support for saving metadata fragments & merging into calibration_metadata.yml
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cal_tools/tools.py
+31
-0
31 additions, 0 deletions
src/cal_tools/tools.py
with
31 additions
and
0 deletions
src/cal_tools/tools.py
+
31
−
0
View file @
f86be23d
...
...
@@ -10,6 +10,7 @@ from os import environ, listdir, path
from
os.path
import
isfile
from
pathlib
import
Path
from
queue
import
Queue
from
tempfile
import
NamedTemporaryFile
from
time
import
sleep
from
typing
import
List
,
Optional
,
Tuple
,
Union
from
urllib.parse
import
urljoin
...
...
@@ -811,6 +812,14 @@ def module_index_to_qm(index: int, total_modules: int = 16):
return
f
"
Q
{
quad
+
1
}
M
{
mod
+
1
}
"
def
recursive_update
(
target
:
dict
,
source
:
dict
):
for
k
,
v2
in
source
.
items
():
v1
=
target
.
get
(
k
,
None
)
if
isinstance
(
v1
,
dict
)
and
isinstance
(
v2
,
dict
):
recursive_update
(
v1
,
v2
)
else
:
target
[
k
]
=
v2
class
CalibrationMetadata
(
dict
):
"""
Convenience class: dictionary stored in metadata YAML file
...
...
@@ -847,6 +856,28 @@ class CalibrationMetadata(dict):
with
(
copy_dir
/
self
.
_yaml_fn
.
name
).
open
(
"
w
"
)
as
fd
:
yaml
.
safe_dump
(
dict
(
self
),
fd
)
def
add_fragment
(
self
,
data
:
dict
):
"""
Save metadata to a separate
'
fragment
'
file to be merged later
Avoids a risk of corrupting the main file by writing in parallel.
"""
with
NamedTemporaryFile
(
"
w
"
,
dir
=
self
.
_yaml_fn
.
parent
,
prefix
=
'
metadata_frag_
'
,
suffix
=
'
.yml
'
,
delete
=
False
)
as
fd
:
yaml
.
safe_dump
(
data
,
fd
)
def
gather_fragments
(
self
):
"""
Merge in fragments saved by add_fragment(), then delete them
"""
frag_files
=
list
(
self
.
_yaml_fn
.
parent
.
glob
(
'
metadata_frag_*.yml
'
))
for
fn
in
frag_files
:
with
fn
.
open
(
"
r
"
)
as
fd
:
data
=
yaml
.
safe_load
(
fd
)
recursive_update
(
self
,
data
)
self
.
save
()
for
fn
in
frag_files
:
fn
.
unlink
()
def
save_constant_metadata
(
retrieved_constants
:
dict
,
...
...
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