Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycroscopy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
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
Code review analytics
Issue 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
Vasudevan, Rama K.
pycroscopy
Commits
d784476c
Commit
d784476c
authored
7 years ago
by
Chris Smith
Browse files
Options
Downloads
Patches
Plain Diff
Add microdata_example
parent
07fe31c5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/microdata_example.py
+77
-0
77 additions, 0 deletions
examples/microdata_example.py
with
77 additions
and
0 deletions
examples/microdata_example.py
0 → 100644
+
77
−
0
View file @
d784476c
"""
===========================================
Writing to hdf5 using the Microdata objects
===========================================
"""
# Code source: Chris Smith -- cq6@ornl.gov
# Liscense: MIT
import
numpy
as
np
import
pycroscopy
as
px
# Create some MicroDatasets and MicroDataGroups that will be written to the file.
# With h5py, groups and datasets must be created from the top down,
# but the Microdata objects allow us to build them in any order and link them later.
# First create some data
data1
=
np
.
random
.
rand
(
5
,
7
)
# Now use the array to build the dataset. This dataset will live
# directly under the root of the file. The MicroDataset class also implements the
# compression and chunking parameters from h5py.Dataset.
ds_main
=
px
.
MicroDataset
(
'
Main_Data
'
,
data
=
data1
,
parent
=
'
/
'
)
# We can also create an empty dataset and write the values in later
# With this method, it is neccessary to specify the dtype and maxshape kwarg parameters.
ds_empty
=
px
.
MicroDataset
(
'
Empty_Data
'
,
data
=
[],
dtype
=
np
.
float32
,
maxshape
=
[
7
,
5
,
3
])
# We can also create groups and add other MicroData objects as children.
# If the group's parent is not given, it will be set to root.
data_group
=
px
.
MicroDataGroup
(
'
Data_Group
'
,
parent
=
'
/
'
)
root_group
=
px
.
MicroDataGroup
(
'
/
'
)
# After creating the group, we then add an existing object as its child.
data_group
.
addChildren
([
ds_empty
])
root_group
.
addChildren
([
ds_main
,
data_group
])
# The showTree method allows us to view the data structure before the hdf5 file is
# created.
root_group
.
showTree
()
# Now that we have created the objects, we can write them to an hdf5 file
# First we specify the path to the file
h5_path
=
'
microdata_test.h5
'
# Then we use the ioHDF5 class to build the file from our objects.
hdf
=
px
.
ioHDF5
(
h5_path
)
# The writeData method builds the hdf5 file using the structure defined by the
# MicroData objects. It returns a list of references to all h5py objects in the
# new file.
h5_refs
=
hdf
.
writeData
(
root_group
,
print_log
=
True
)
# We can use these references to get the h5py dataset and group objects
h5_main
=
px
.
io
.
hdf_utils
.
getH5DsetRefs
([
'
Main_Data
'
],
h5_refs
)[
0
]
h5_empty
=
px
.
io
.
hdf_utils
.
getH5DsetRefs
([
'
Empty_Data
'
],
h5_refs
)[
0
]
print
(
np
.
allclose
(
h5_main
[()],
data1
))
# As mentioned above, we can now write to the Empty_Data object
data2
=
np
.
random
.
rand
(
*
h5_empty
.
shape
)
h5_empty
[:]
=
data2
[:]
# Now that we are using h5py objects, we must use flush to write the data to file
# after it has been altered.
# We need the file object to do this. It can be accessed as an attribute of the
# hdf object.
h5_file
=
hdf
.
file
h5_file
.
flush
()
# Now that we are done, we should close the file so that it can be accessed elsewhere.
h5_file
.
close
()
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