Skip to content

Commit 4962f03

Browse files
authored
python(refactor): update HDF5 import examples (#597)
1 parent 2b13f10 commit 4962f03

15 files changed

Lines changed: 221 additions & 74 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SIFT_GRPC_URI=
2+
SIFT_REST_URI=
3+
SIFT_API_KEY=
4+
ASSET_NAME=
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Import an HDF5 file with the compound schema into Sift.
2+
3+
In this layout, each dataset is a compound (struct-like) type whose first
4+
field is time and whose remaining fields each become a channel. Channel
5+
name, units, and description are read from the dataset's attributes when
6+
present; multi-value datasets emit channels named ``<dataset>.<field>``.
7+
"""
8+
9+
import os
10+
11+
from dotenv import load_dotenv
12+
from sift_client import SiftClient
13+
from sift_client.sift_types.data_import import DataTypeKey
14+
15+
if __name__ == "__main__":
16+
load_dotenv()
17+
18+
grpc_uri = os.getenv("SIFT_GRPC_URI")
19+
assert grpc_uri, "expected 'SIFT_GRPC_URI' environment variable to be set"
20+
21+
rest_uri = os.getenv("SIFT_REST_URI")
22+
assert rest_uri, "expected 'SIFT_REST_URI' environment variable to be set"
23+
24+
apikey = os.getenv("SIFT_API_KEY")
25+
assert apikey, "expected 'SIFT_API_KEY' environment variable to be set"
26+
27+
asset_name = os.getenv("ASSET_NAME")
28+
assert asset_name, "expected 'ASSET_NAME' environment variable to be set"
29+
30+
client = SiftClient(api_key=apikey, grpc_url=grpc_uri, rest_url=rest_uri)
31+
32+
# Auto-detect the config and import the file. HDF5 requires the layout
33+
# (data_type) to be specified since the extension alone is ambiguous.
34+
# HDF5 timestamps aren't self-describing, so time_format defaults to
35+
# TimeFormat.ABSOLUTE_UNIX_NANOSECONDS; override it if your timestamps
36+
# are in a different format.
37+
import_job = client.data_import.import_from_path(
38+
"sample_data.h5",
39+
asset=asset_name,
40+
data_type=DataTypeKey.HDF5_COMPOUND,
41+
)
42+
43+
import_job.wait_until_complete()
44+
45+
# If auto-detect doesn't quite match your file, inspect the config and patch
46+
# it before importing. Common fixes: change the time format, override a
47+
# channel's data type, or drop a channel that shouldn't be imported.
48+
#
49+
# from sift_client.sift_types.data_import import TimeFormat
50+
#
51+
# config = client.data_import.detect_config(
52+
# "sample_data.h5",
53+
# data_type=DataTypeKey.HDF5_COMPOUND,
54+
# )
55+
# print(config) # inspect what was auto-detected
56+
#
57+
# # Example: timestamps are unix microseconds instead of unix nanoseconds
58+
# config.time_format = TimeFormat.ABSOLUTE_UNIX_MICROSECONDS
59+
#
60+
# # Example: drop a channel from the import
61+
# config.data = [d for d in config.data if d.name != "sensors.temperature"]
62+
#
63+
# import_job = client.data_import.import_from_path(
64+
# "sample_data.h5",
65+
# asset=asset_name,
66+
# config=config,
67+
# )
68+
# import_job.wait_until_complete()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv
2+
sift-stack-py[hdf5]
7.37 KB
Binary file not shown.

python/examples/data_import/hdf5/main.py

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SIFT_GRPC_URI=
2+
SIFT_REST_URI=
3+
SIFT_API_KEY=
4+
ASSET_NAME=
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Import an HDF5 file with the one-dimensional schema into Sift.
2+
3+
In this layout, each value channel is its own 1D dataset that pairs with a
4+
time dataset in the same group (or an ancestor group's time dataset, walking
5+
up to the root). Channel name, units, and description are read from the
6+
dataset's attributes when present.
7+
"""
8+
9+
import os
10+
11+
from dotenv import load_dotenv
12+
from sift_client import SiftClient
13+
from sift_client.sift_types.data_import import DataTypeKey
14+
15+
if __name__ == "__main__":
16+
load_dotenv()
17+
18+
grpc_uri = os.getenv("SIFT_GRPC_URI")
19+
assert grpc_uri, "expected 'SIFT_GRPC_URI' environment variable to be set"
20+
21+
rest_uri = os.getenv("SIFT_REST_URI")
22+
assert rest_uri, "expected 'SIFT_REST_URI' environment variable to be set"
23+
24+
apikey = os.getenv("SIFT_API_KEY")
25+
assert apikey, "expected 'SIFT_API_KEY' environment variable to be set"
26+
27+
asset_name = os.getenv("ASSET_NAME")
28+
assert asset_name, "expected 'ASSET_NAME' environment variable to be set"
29+
30+
client = SiftClient(api_key=apikey, grpc_url=grpc_uri, rest_url=rest_uri)
31+
32+
# Auto-detect the config and import the file. HDF5 requires the layout
33+
# (data_type) to be specified since the extension alone is ambiguous.
34+
# HDF5 timestamps aren't self-describing, so time_format defaults to
35+
# TimeFormat.ABSOLUTE_UNIX_NANOSECONDS; override it if your timestamps
36+
# are in a different format.
37+
import_job = client.data_import.import_from_path(
38+
"sample_data.h5",
39+
asset=asset_name,
40+
data_type=DataTypeKey.HDF5_ONE_D,
41+
)
42+
43+
import_job.wait_until_complete()
44+
45+
# If auto-detect doesn't quite match your file, inspect the config and patch
46+
# it before importing. Common fixes: change the time format, override a
47+
# channel's data type, or drop a channel that shouldn't be imported.
48+
#
49+
# from sift_client.sift_types.data_import import TimeFormat
50+
#
51+
# config = client.data_import.detect_config(
52+
# "sample_data.h5",
53+
# data_type=DataTypeKey.HDF5_ONE_D,
54+
# )
55+
# print(config) # inspect what was auto-detected
56+
#
57+
# # Example: timestamps are ISO-8601 strings instead of unix nanoseconds
58+
# config.time_format = TimeFormat.ABSOLUTE_DATETIME
59+
#
60+
# # Example: drop a channel from the import
61+
# config.data = [d for d in config.data if d.name != "channel_0"]
62+
#
63+
# import_job = client.data_import.import_from_path(
64+
# "sample_data.h5",
65+
# asset=asset_name,
66+
# config=config,
67+
# )
68+
# import_job.wait_until_complete()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv
2+
sift-stack-py[hdf5]
9.56 KB
Binary file not shown.

python/examples/data_import/hdf5/requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)