Skip to content

Commit

Permalink
Revert "Flatten results dictionary for DQM"
Browse files Browse the repository at this point in the history
This reverts commit e508007.
  • Loading branch information
jlenain committed Aug 23, 2024
1 parent a4be0a8 commit 71e5900
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 47 deletions.
17 changes: 10 additions & 7 deletions src/nectarchain/dqm/bokeh_app/app_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ def get_rundata(src, runid):

def make_camera_displays(db, source, runid):
displays = collections.defaultdict(dict)
for key in db[runid].keys():
if not re.match(TEST_PATTERN, key):
print(f"Run id {runid} Preparing plot for {key}")
displays[key] = make_camera_display(source, key=key)
for parentkey in db[runid].keys():
if not re.match(TEST_PATTERN, parentkey):
for childkey in db[runid][parentkey].keys():
print(f"Run id {runid} Preparing plot for {parentkey}, {childkey}")
displays[parentkey][childkey] = make_camera_display(
source, parent_key=parentkey, child_key=childkey
)
return dict(displays)


def make_camera_display(source, key):
def make_camera_display(source, parent_key, child_key):
# Example camera display
image = source[key]
image = source[parent_key][child_key]
image = np.nan_to_num(image, nan=0.0)
display = CameraDisplay(geometry=geom)
try:
Expand All @@ -50,5 +53,5 @@ def make_camera_display(source, key):
image = np.zeros(shape=constants.N_PIXELS)
display.image = image
display.add_colorbar()
display.figure.title = key
display.figure.title = child_key
return display
62 changes: 34 additions & 28 deletions src/nectarchain/dqm/bokeh_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,35 @@ def update_camera_displays(attr, old, new):

# Reset each display
for k in displays.keys():
displays[k].image = np.zeros(shape=constants.N_PIXELS)

for key in db[runid].keys():
if not re.match(TEST_PATTERN, key):
print(f"Run id {runid} Updating plot for {key}")

image = new_rundata[key]
image = np.nan_to_num(image, nan=0.0)
try:
displays[key].image = image
except ValueError as e:
print(
f"Caught {type(e).__name__} for {key}, filling display"
f"with zeros. Details: {e}"
)
image = np.zeros(shape=displays[key].image.shape)
displays[key].image = image
except KeyError as e:
print(
f"Caught {type(e).__name__} for {key}, filling display"
f"with zeros. Details: {e}"
)
image = np.zeros(shape=constants.N_PIXELS)
displays[key].image = image
# TODO: TRY TO USE `stream` INSTEAD, ON UPDATES:
# display.datasource.stream(new_data)
# displays[key].datasource.stream(image)
for kk in displays[k].keys():
displays[k][kk].image = np.zeros(shape=constants.N_PIXELS)

for parentkey in db[runid].keys():
if not re.match(TEST_PATTERN, parentkey):
for childkey in db[runid][parentkey].keys():

Check warning on line 33 in src/nectarchain/dqm/bokeh_app/main.py

View check run for this annotation

Codecov / codecov/patch

src/nectarchain/dqm/bokeh_app/main.py#L32-L33

Added lines #L32 - L33 were not covered by tests
print(f"Run id {runid} Updating plot for {parentkey}, {childkey}")

image = new_rundata[parentkey][childkey]
image = np.nan_to_num(image, nan=0.0)
try:
displays[parentkey][childkey].image = image
except ValueError as e:
print(
f"Caught {type(e).__name__} for {childkey}, filling display"
f"with zeros. Details: {e}"
)
image = np.zeros(shape=displays[parentkey][childkey].image.shape)
displays[parentkey][childkey].image = image
except KeyError as e:
print(
f"Caught {type(e).__name__} for {childkey}, filling display"
f"with zeros. Details: {e}"
)
image = np.zeros(shape=constants.N_PIXELS)
displays[parentkey][childkey].image = image
# TODO: TRY TO USE `stream` INSTEAD, ON UPDATES:
# display.datasource.stream(new_data)
# displays[parentkey][childkey].datasource.stream(image)


db = DQMDB(read_only=True).root
Expand Down Expand Up @@ -88,7 +90,11 @@ def update_camera_displays(attr, old, new):
# update_camera_displays(attr, old, new)

ncols = 3
plots = [displays[key].figure for key in displays.keys()]
plots = [
displays[parentkey][childkey].figure
for parentkey in displays.keys()
for childkey in displays[parentkey].keys()
]
curdoc().add_root(
layout(
[[controls], [[plots[x : x + ncols] for x in range(0, len(plots), ncols)]]],
Expand Down
18 changes: 14 additions & 4 deletions src/nectarchain/dqm/bokeh_app/tests/test_app_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@

test_dict = {
"run1": {
"mykey1": np.random.normal(size=geom.n_pixels),
"mykey2": np.random.normal(size=geom.n_pixels),
"mykey1": {
"mysubkey1": np.random.normal(size=geom.n_pixels),
"mysubkey2": np.random.normal(size=geom.n_pixels),
},
"mykey2": {
"mysubkey1": np.random.normal(size=geom.n_pixels),
"mysubkey2": np.random.normal(size=geom.n_pixels),
},
}
}
# Renders the second image incomplete
test_dict["run1"]["mykey2"][10:20] = np.nan
test_dict["run1"]["mykey2"]["mysubkey2"][10:20] = np.nan


def test_make_camera_displays():
Expand Down Expand Up @@ -48,7 +54,11 @@ def test_bokeh(tmp_path):
displays = make_camera_displays(root, source, runid)

ncols = 3
plots = [displays[key].figure for key in displays.keys()]
plots = [
displays[parentkey][childkey].figure
for parentkey in displays.keys()
for childkey in displays[parentkey].keys()
]
curdoc().add_root(
layout(
[[[plots[x : x + ncols] for x in range(0, len(plots), ncols)]]],
Expand Down
11 changes: 3 additions & 8 deletions src/nectarchain/user_scripts/jlenain/parse_dqm_fits_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,9 @@
for h in range(1, len(hdu)):
extname = hdu[h].header["EXTNAME"]
outdict[extname] = dict()
# for i in range(hdu[extname].header["TFIELDS"]):
if len(hdu[extname].header["TFIELDS"]) > 1:
raise ValueError(
f"Wrong DQM format, {hdu[extname]} should contain only one element"
)
# keyname = hdu[extname].header[f"TTYPE{i+1}"]
keyname = hdu[extname].header[f"TTYPE1"]
outdict[extname] = hdu[extname].data[keyname]
for i in range(hdu[extname].header["TFIELDS"]):
keyname = hdu[extname].header[f"TTYPE{i+1}"]
outdict[extname][keyname] = hdu[extname].data[keyname]

try:
db = DQMDB(read_only=False)
Expand Down

0 comments on commit 71e5900

Please sign in to comment.