Last active
September 5, 2018 21:32
-
-
Save danielballan/8885c63343b3ad1c5f95706ef9e29a89 to your computer and use it in GitHub Desktop.
toy example for getting sample tracking going at CHX
This file contains hidden or 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
# Do this part whether you acquiring data or accessing it. | |
import pymongo | |
cli = pymongo.MongoClient('xf11id-ca') | |
samples = cli.get_database('samples').get_collection('samples') | |
from databroker import Broker | |
db = Broker.named('temp') # for real applications, 'temp' would be 'chx' | |
# Do this part when you are entering new samples. | |
# I'm going to assume that 'name' is unique. We should be more careful about | |
# ensuring that and maybe come up with a richer system, but this is good enough | |
# for a toy exmaple. | |
samples.insert(dict(name='A', composition='Ni')) | |
samples.insert(dict(name='B', composition='Au')) | |
# Here I import something things and set up RE to send documents to db, as the | |
# IPython profile (profile_collection) does, so that data acquisition works. | |
from bluesky import RunEngine | |
RE = RunEngine({}) | |
from ophyd.sim import det | |
from bluesky.plans import count | |
RE.subscribe(db.insert) | |
# Take some simulated data. | |
RE(count([det]), sample='A', purpose='dan is testing for Lutz') | |
# This is the part you have to add to make data access work. | |
def fetch_sample(start, stop): | |
if 'sample' in start: | |
query = dict(name=start['sample']) | |
return samples.find_one(query) | |
# This tells the databroker to addd some custom stuff to the `.ext` attribute | |
# of each Header. | |
db.external_fetchers['sample_info'] = fetch_sample | |
print(db[-1].ext.sample_info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment