generated from BCDA-APS/tiled-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyapi_client.py
154 lines (123 loc) · 4.87 KB
/
pyapi_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Demo of a tiled Python client using the tiled.client Python API.
Use this Python client to generate requests from the tiled server.
Then, inspect the server's logs for the specific URIs that were used.
Produce some specific URI queries and responses.
* [x] Find all runs in a catalog between these two ISO8601 dates.
* [x] Find run(s) which match given metadata.
* [ ] Get overall metadata from given run.
* [ ] What are the data streams in this run?
* [ ] What is the metadata for this stream?
* [ ] Get the data from the data stream named primary (the canonical main data).
* [ ] What about loose matches? Maybe not now. Might require some deeper expertise.
"""
import datetime
from tiled.client import from_uri
from tiled.client.cache import Cache
from tiled.utils import tree
import tiled.queries
def overview(host="localhost", port=8000):
client = from_uri(f"http://{host}:{port}", cache=Cache.in_memory(2e9))
print(f"{client=}")
for catalog in client:
print(f"{catalog=} {client[catalog]=}")
print(f"{client.search(tiled.queries.FullText('bdp'))}")
# tree(client)
def demo2(host="localhost", port=8000):
client = from_uri(f"http://{host}:{port}", cache=Cache.in_memory(2e9))
# cat = client["20idb_usaxs"]
cat = client["class_2021_03"]
print(f"{cat=}")
def iso2time(isotime):
return datetime.datetime.timestamp(datetime.datetime.fromisoformat(isotime))
def QueryTimeSince(isotime):
return tiled.queries.Key("time") >= iso2time(isotime)
def QueryTimeUntil(isotime):
return tiled.queries.Key("time") < iso2time(isotime)
# Find all runs in the catalog between these two ISO8601 dates.
start_time = "2021-03-17 00:30"
end_time = "2021-05-19 15:15"
cat = cat.search(QueryTimeSince(start_time)).search(QueryTimeUntil(end_time))
print(f"{cat=}")
# Find run(s) which match given metadata: given plan_name
plan_name = "rel_scan"
# case_sensitive = False
# cat = cat.search(tiled.queries.FullText(plan_name), case_sensitive=case_sensitive)
cat = cat.search(tiled.queries.Key("plan_name") == plan_name)
print(f"{cat=}")
# With latest run:
run = cat.values()[-1]
print(f"last run: {run=}")
# Get overall metadata from this run.
print(f"Run metadata: {run.metadata=}")
# What are the data streams in this run?
stream_names = run.metadata["summary"]["stream_names"]
print(f"streams: {stream_names=}")
# Get the data from the data stream named primary (the canonical main data).
if "primary" in stream_names:
stream_data = run["primary"]
print(f"{stream_data['data']=}")
# What is the metadata for this stream?
print(f"{stream_data.metadata=}")
ideas_from_json_api = {
"queries": [
"fulltext",
"lookup",
"keys_filter",
"regex",
"eq",
"noteq",
"comparison",
"contains",
"in",
"notin",
"specs",
"structure_family",
"scan_id",
"scan_id_range",
"partial_uid",
"duration",
"time_range",
]
}
def get_run(catalog, uid, host="localhost", port=8000):
client = from_uri(f"http://{host}:{port}", cache=Cache.in_memory(2e9))
return client[catalog][uid]
def get_run_data(catalog, uid, host="localhost", port=8000):
run = get_run(catalog, uid, host=host, port=port)
# for k, v in run.primary.data.items():
# print(f"{k=} {v.shape=} {v.size=} {len(v)=}")
# data = v.read() # a really big bite for the image data!
# # /api/v1/array/block
# # /{catalog}
# # /{uid}
# # /{stream}
# # /data/adsimdet_image
# # ?block=0,0,0,0
# # &format=application/octet-stream
# http://localhost:8000/api/v1/array/block/bdp2022/00714a91-c33e-4e7b-90fd-2e8f385bebc9/primary/data/adsimdet_image?block=0,0,0,0
return run
def some_run_data(host="localhost", port=8000):
# run = get_run_data(
# "bdp2022", "00714a91-c33e-4e7b-90fd-2e8f385bebc9", host=host, port=port
# )
run = get_run_data(
"bdp2022", "43044b6e-f6ba-48cb-a975-90d236dcbaaa", host=host, port=port
)
print(f"{run.metadata=}")
print(f"{run.primary.metadata=}")
print(f"{list(run.primary.data.keys())=}")
signal_name = "time" # or adsimdet_image which has shape [1, 1, 1024, 1024]
arr = run.primary.data["time"]
print(f"{type(arr)=}")
print(f"{arr=}")
def external_files(catalog, uid, host="localhost", port=8000):
run = get_run(catalog, uid, host=host, port=port)
docs = [doc for name, doc in run.documents() if name in ('resource', 'datum_page')]
print(f"{len(docs)=}")
def main():
r = external_files("bdp2022", "43044b6e-f6ba-48cb-a975-90d236dcbaaa")
print(f"{r=}")
if __name__ == "__main__":
# main()
demo2()