-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #819 from danielballan/shape-fixer-patch
Update shape fixer and add example
- Loading branch information
Showing
7 changed files
with
162 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,3 +135,6 @@ docs/output_directory | |
docs/data.csv | ||
docs/data.xlsx | ||
docs/data.h5 | ||
|
||
# generated by docker-compose | ||
data/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# This is intended to be used with docker-compose.yml. | ||
trees: | ||
- path: / | ||
- path: /raw | ||
tree: databroker.mongo_normalized:MongoAdapter.from_uri | ||
args: | ||
uri: mongodb://mongo:27017/example_database | ||
handler_registry: | ||
NPY_SEQ: "handlers:NumpySeqHandler" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"This is vendored from ophyd.sim" | ||
import os | ||
|
||
import numpy as np | ||
|
||
|
||
class NumpySeqHandler: | ||
|
||
def __init__(self, filename, root=""): | ||
self._name = os.path.join(root, filename) | ||
|
||
def __call__(self, index): | ||
return np.load("{}_{}.npy".format(self._name, index), allow_pickle=False) | ||
|
||
def get_file_list(self, datum_kwarg_gen): | ||
"This method is optional. It is not needed for access, but for export." | ||
return [ | ||
"{name}_{index}.npy".format(name=self._name, **kwargs) | ||
for kwargs in datum_kwarg_gen | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
# Generate data with wrong shape in a demo MongoDB and Tiled server. | ||
TILED_SINGLE_USER_API_KEY podman-compose up | ||
python examples/generate_data_with_wrong_shape.py | ||
# Try to load the data in the Tiled Python client. | ||
# It will fail because the shape metadata is wrong. | ||
from tiled.client import from_uri | ||
c = from_uri('http://localhost:8000', api_key='secret') | ||
c['raw'].values().last()['primary']['data']['img'][:] # ERROR! | ||
# The server logs should show: | ||
# databroker.mongo_normalized.BadShapeMetadata: | ||
# For data key img shape (5, 7) does not match expected shape (1, 11, 3). | ||
# Run the shape-fixer CLI. Start with a dry run. | ||
# The `--strict` mode ensures that errors are raised, not skipped. | ||
databroker admin shape-fixer mongodb://localhost:27017/example_database --strict --dry-run | ||
databroker admin shape-fixer mongodb://localhost:27017/example_database --strict | ||
# The output should include something like this. | ||
# (Of course, the uid(s) will be different.) | ||
Edited 90b7ffa8-ba02-4163-a2aa-5f47d1eb322b primary: {'img': [1, 11, 3]} -> {'img': [5, 7]} | ||
Migrating... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 | ||
# Back in the Python client, try loading again. | ||
# There is no need to reconnect; just run this line again: | ||
c['raw'].values().last()['primary']['data']['img'][:] # Now it works! | ||
""" | ||
|
||
import numpy | ||
from ophyd.sim import SynSignalWithRegistry | ||
from bluesky import RunEngine | ||
from bluesky.plans import count | ||
from tiled.client import from_uri | ||
|
||
|
||
RE = RunEngine() | ||
client = from_uri("http://localhost:8000?api_key=secret")["raw"] | ||
|
||
|
||
def post_document(name, doc): | ||
client.post_document(name, doc) | ||
|
||
|
||
RE.subscribe(post_document) | ||
|
||
|
||
class LyingDetector(SynSignalWithRegistry): | ||
def describe(self): | ||
res = super().describe() | ||
res["img"]["shape"] = (1, 11, 3) | ||
return res | ||
|
||
|
||
img = LyingDetector( | ||
func=lambda: numpy.ones((5, 7), dtype=numpy.uint8), | ||
name="img", | ||
save_path="./data", | ||
) | ||
RE(count([img])) |