Skip to content

Commit

Permalink
improved python code
Browse files Browse the repository at this point in the history
  • Loading branch information
woutdenolf committed Jun 15, 2022
1 parent 7b92b6c commit 0aba057
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This example is based on ``writer_2_1``.
"""

import os
from pathlib import Path
import h5py
import numpy

Expand All @@ -16,58 +16,53 @@
# ---------------------------

# get some data
filename = os.path.join(os.path.dirname(__file__), "..", "simple_example.dat")
filename = str(Path(__file__).parent.parent / "simple_example.dat")
buffer = numpy.loadtxt(filename).T
tthData = buffer[0] # float[]
countsData = numpy.asarray(buffer[1], "int32") # int[]

# put the angle data in an external (non-NeXus) HDF5 data file
f = h5py.File(FILE_HDF5_ANGLES, "w")
ds = f.create_dataset("angles", data=tthData)
ds.attrs["units"] = "degrees"
f.close() # be CERTAIN to close the file

with h5py.File(FILE_HDF5_ANGLES, "w") as f:
ds = f.create_dataset("angles", data=tthData)
ds.attrs["units"] = "degrees"

# put the detector counts in an external HDF5 data file
# with *incomplete* NeXus structure (no NXdata group)
f = h5py.File(FILE_HDF5_COUNTS, "w")
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxinstrument = nxentry.create_group("instrument")
nxinstrument.attrs["NX_class"] = "NXinstrument"
nxdetector = nxinstrument.create_group("detector")
nxdetector.attrs["NX_class"] = "NXdetector"
ds = nxdetector.create_dataset("counts", data=countsData)
ds.attrs["units"] = "counts"
# link the "two_theta" data stored in separate file
local_addr = nxdetector.name + "/two_theta"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, "/angles")
f.close()
with h5py.File(FILE_HDF5_COUNTS, "w") as f:
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxinstrument = nxentry.create_group("instrument")
nxinstrument.attrs["NX_class"] = "NXinstrument"
nxdetector = nxinstrument.create_group("detector")
nxdetector.attrs["NX_class"] = "NXdetector"
ds = nxdetector.create_dataset("counts", data=countsData)
ds.attrs["units"] = "counts"
# link the "two_theta" data stored in separate file
local_addr = nxdetector.name + "/two_theta"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, "/angles")

# create a master NeXus HDF5 file
f = h5py.File(FILE_HDF5_MASTER, "w")
f.attrs["default"] = "entry"
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxentry.attrs["default"] = "data"
nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"

# link in the signal data
local_addr = "/entry/data/counts"
external_addr = "/entry/instrument/detector/counts"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, external_addr)
nxdata.attrs["signal"] = "counts"
with h5py.File(FILE_HDF5_MASTER, "w") as f:
f.attrs["default"] = "entry"
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxentry.attrs["default"] = "data"
nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"

# link in the axes data
local_addr = "/entry/data/two_theta"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, "/angles")
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]
# link in the signal data
local_addr = "/entry/data/counts"
external_addr = "/entry/instrument/detector/counts"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, external_addr)
nxdata.attrs["signal"] = "counts"

local_addr = "/entry/instrument"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, "/entry/instrument")
# link in the axes data
local_addr = "/entry/data/two_theta"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_ANGLES, "/angles")
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]

f.close()
local_addr = "/entry/instrument"
f[local_addr] = h5py.ExternalLink(FILE_HDF5_COUNTS, "/entry/instrument")
11 changes: 5 additions & 6 deletions manual/source/examples/h5py/plotting/reader_attributes_trail.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import os
from pathlib import Path
import h5py

filename = os.path.join(
os.path.dirname(__file__),
"..",
"simple_example_basic",
"simple_example_basic.nexus.hdf5",
filename = str(
Path(__file__).parent.parent
/ "simple_example_basic"
/ "simple_example_basic.nexus.hdf5"
)
with h5py.File(filename, "r") as nx:
# find the default NXentry group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import h5py # HDF5 support

fileName = "simple_example_basic.nexus.hdf5"
f = h5py.File(fileName, "r")
for item in f.attrs.keys():
print(item + ":", f.attrs[item])
mr = f["/entry/mr_scan/mr"]
i00 = f["/entry/mr_scan/I00"]
print("%s\t%s\t%s" % ("#", "mr", "I00"))
for i in range(len(mr)):
print("%d\t%g\t%d" % (i, mr[i], i00[i]))
f.close()
with h5py.File(fileName, "r") as f:
for item in f.attrs.keys():
print(item + ":", f.attrs[item])
mr = f["/entry/mr_scan/mr"]
i00 = f["/entry/mr_scan/I00"]
print("%s\t%s\t%s" % ("#", "mr", "I00"))
for i in range(len(mr)):
print("%d\t%g\t%d" % (i, mr[i], i00[i]))
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
"""Writes a NeXus HDF5 file using h5py and numpy"""

import os
from pathlib import Path
import datetime
import h5py # HDF5 support
import numpy
Expand All @@ -11,51 +11,47 @@
timestamp = datetime.datetime.now().astimezone().isoformat()

# load data from two column format
infilename = filename = os.path.join(
os.path.dirname(__file__), "..", "simple_example.dat"
)
data = numpy.loadtxt(infilename).T
data_filename = str(Path(__file__).parent.parent / "simple_example.dat")
data = numpy.loadtxt(data_filename).T
mr_arr = data[0]
i00_arr = numpy.asarray(data[1], "int32")

# create the HDF5 NeXus file
f = h5py.File(fileName, "w")
# point to the default data to be plotted
f.attrs["default"] = "entry"
# give the HDF5 root some more attributes
f.attrs["file_name"] = fileName
f.attrs["file_time"] = timestamp
f.attrs["instrument"] = "APS USAXS at 32ID-B"
f.attrs["creator"] = "simple_example_basic_write.py"
f.attrs["NeXus_version"] = "4.3.0"
f.attrs["HDF5_Version"] = h5py.version.hdf5_version
f.attrs["h5py_version"] = h5py.version.version

# create the NXentry group
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxentry.attrs["default"] = "mr_scan"
nxentry.create_dataset("title", data="1-D scan of I00 v. mr")

# create the NXentry group
nxdata = nxentry.create_group("mr_scan")
nxdata.attrs["NX_class"] = "NXdata"
nxdata.attrs["signal"] = "I00" # Y axis of default plot
nxdata.attrs["axes"] = "mr" # X axis of default plot
nxdata.attrs["mr_indices"] = [
0,
] # use "mr" as the first dimension of I00

# X axis data
ds = nxdata.create_dataset("mr", data=mr_arr)
ds.attrs["units"] = "degrees"
ds.attrs["long_name"] = "USAXS mr (degrees)" # suggested X axis plot label

# Y axis data
ds = nxdata.create_dataset("I00", data=i00_arr)
ds.attrs["units"] = "counts"
ds.attrs["long_name"] = "USAXS I00 (counts)" # suggested Y axis plot label

f.close() # be CERTAIN to close the file
with h5py.File(fileName, "w") as f:
# point to the default data to be plotted
f.attrs["default"] = "entry"
# give the HDF5 root some more attributes
f.attrs["file_name"] = fileName
f.attrs["file_time"] = timestamp
f.attrs["instrument"] = "APS USAXS at 32ID-B"
f.attrs["creator"] = "simple_example_basic_write.py"
f.attrs["NeXus_version"] = "4.3.0"
f.attrs["HDF5_Version"] = h5py.version.hdf5_version
f.attrs["h5py_version"] = h5py.version.version

# create the NXentry group
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry"
nxentry.attrs["default"] = "mr_scan"
nxentry.create_dataset("title", data="1-D scan of I00 v. mr")

# create the NXentry group
nxdata = nxentry.create_group("mr_scan")
nxdata.attrs["NX_class"] = "NXdata"
nxdata.attrs["signal"] = "I00" # Y axis of default plot
nxdata.attrs["axes"] = "mr" # X axis of default plot
nxdata.attrs["mr_indices"] = [
0,
] # use "mr" as the first dimension of I00

# X axis data
ds = nxdata.create_dataset("mr", data=mr_arr)
ds.attrs["units"] = "degrees"
ds.attrs["long_name"] = "USAXS mr (degrees)" # suggested X axis plot label

# Y axis data
ds = nxdata.create_dataset("I00", data=i00_arr)
ds.attrs["units"] = "counts"
ds.attrs["long_name"] = "USAXS I00 (counts)" # suggested Y axis plot label

print("wrote file:", fileName)
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ def process(fileName):
f = h5py.File(fileName, "r")
except Exception:
return False
print(f.filename)
print("keys: ", f.keys())
print_attr(f, "f")
for k in f.keys():
print_child(f[k], "%s://%s" % (fileName, k))
f.close()
try:
print(f.filename)
print("keys: ", f.keys())
print_attr(f, "f")
for k in f.keys():
print_child(f[k], "%s://%s" % (fileName, k))
finally:
f.close()
return True


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,40 @@
data["I00"].append(float(y))

# create the HDF5 NeXus file
f = h5py.File(fileName, "w")
f.attrs["file_name"] = fileName
f.attrs["creator"] = "Pete R. Jemian <[email protected]> using h5py"
f.attrs["HDF5_Version"] = h5py.version.hdf5_version
f.attrs["NeXus_version"] = "4.2.1"
f.attrs["h5py_version"] = h5py.version.version
f.attrs["file_time"] = timestamp
f.attrs["file_update_time"] = timestamp
f.attrs["default"] = "entry" # identify default NXentry group
with h5py.File(fileName, "w") as f:
f.attrs["file_name"] = fileName
f.attrs["creator"] = "Pete R. Jemian <[email protected]> using h5py"
f.attrs["HDF5_Version"] = h5py.version.hdf5_version
f.attrs["NeXus_version"] = "4.2.1"
f.attrs["h5py_version"] = h5py.version.version
f.attrs["file_time"] = timestamp
f.attrs["file_update_time"] = timestamp
f.attrs["default"] = "entry" # identify default NXentry group

nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry" # identify NeXus base class
nxentry.attrs["default"] = "mr_scan" # identify default NXdata group
nxentry = f.create_group("entry")
nxentry.attrs["NX_class"] = "NXentry" # identify NeXus base class
nxentry.attrs["default"] = "mr_scan" # identify default NXdata group

# store the scan data
nxdata = nxentry.create_group("mr_scan")
nxdata.attrs["NX_class"] = "NXdata" # identify NeXus base class
nxdata.attrs["signal"] = "I00" # identify default data to plot
nxdata.attrs["axes"] = "mr" # identify default dimension scale to plot
# store the scan data
nxdata = nxentry.create_group("mr_scan")
nxdata.attrs["NX_class"] = "NXdata" # identify NeXus base class
nxdata.attrs["signal"] = "I00" # identify default data to plot
nxdata.attrs["axes"] = "mr" # identify default dimension scale to plot

mr = nxdata.create_dataset("mr", data=data["mr"])
mr.attrs["units"] = "degrees"
mr = nxdata.create_dataset("mr", data=data["mr"])
mr.attrs["units"] = "degrees"

i00 = nxdata.create_dataset("I00", data=data["I00"])
i00.attrs["units"] = "counts"
i00 = nxdata.create_dataset("I00", data=data["I00"])
i00.attrs["units"] = "counts"

# fill in some optional metadata
nxentry.create_dataset("title", data="APS USAXS instrument MR (alignment) scan")
nxentry.create_dataset("start_time", data="2010-04-25T10:20:56-0500")
nxentry.create_dataset("end_time", data="2010-04-25T10:21:16-0500")
nxentry.create_dataset("experiment_identifier", data="spec file 04_25.dat, scan #8")
nxentry.create_dataset(
"experiment_description", data="alignment scan of the USAXS collimating optics"
)

# be CERTAIN to close the file
f.close()
# fill in some optional metadata
nxentry.create_dataset("title", data="APS USAXS instrument MR (alignment) scan")
nxentry.create_dataset("start_time", data="2010-04-25T10:20:56-0500")
nxentry.create_dataset("end_time", data="2010-04-25T10:21:16-0500")
nxentry.create_dataset(
"experiment_identifier", data="spec file 04_25.dat, scan #8"
)
nxentry.create_dataset(
"experiment_description",
data="alignment scan of the USAXS collimating optics",
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@
in the Introduction chapter
"""

import os
from pathlib import Path
import h5py
import numpy

filename = os.path.join(os.path.dirname(__file__), "..", "simple_example.dat")
filename = str(Path(__file__).parent.parent / "simple_example.dat")
buffer = numpy.loadtxt(filename).T
tthData = buffer[0] # float[]
countsData = numpy.asarray(buffer[1], "int32") # int[]

f = h5py.File("simple_example_write1.hdf5", "w") # create the HDF5 NeXus file
# since this is a simple example, no attributes are used at this point
with h5py.File("simple_example_write1.hdf5", "w") as f: # create the HDF5 NeXus file
# since this is a simple example, no attributes are used at this point

nxentry = f.create_group("Scan")
nxentry.attrs["NX_class"] = "NXentry"
nxentry = f.create_group("Scan")
nxentry.attrs["NX_class"] = "NXentry"

nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"
nxdata.attrs["signal"] = "counts"
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]
nxdata = nxentry.create_group("data")
nxdata.attrs["NX_class"] = "NXdata"
nxdata.attrs["signal"] = "counts"
nxdata.attrs["axes"] = "two_theta"
nxdata.attrs["two_theta_indices"] = [
0,
]

tth = nxdata.create_dataset("two_theta", data=tthData)
tth.attrs["units"] = "degrees"
tth = nxdata.create_dataset("two_theta", data=tthData)
tth.attrs["units"] = "degrees"

counts = nxdata.create_dataset("counts", data=countsData)
counts.attrs["units"] = "counts"

f.close() # be CERTAIN to close the file
counts = nxdata.create_dataset("counts", data=countsData)
counts.attrs["units"] = "counts"
Loading

0 comments on commit 0aba057

Please sign in to comment.