Skip to content

Commit

Permalink
added _bg_index_to_camera_index, #41
Browse files Browse the repository at this point in the history
  • Loading branch information
tjlane committed Jun 19, 2020
1 parent 81ed275 commit a7fe3e5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
49 changes: 49 additions & 0 deletions psgeom/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,55 @@ def to_hdf5(self, filename):

def from_hdf5(self, filename):
raise NotImplementedError()


def _bg_index_to_camera_index(self, bg_index):
"""
Map a basisgrid panel index to a tuple of "camera" indices:
(leaf_index, subpanel_index)
This allows one to map "backwards" from a basisgrid to a
specific subpanel. For example, a JUNGFRAU sensor element
is composed of a 4x2 array of subpanels that each become
their own basisgrid elements. Each subpanel will belong
to the same "leaf" but have a unique subpanel index.
Subpanel indices go in slow/fast order as might be expected.
The leaf index gives the position of the basisgrid panel
in CompoundAreaCamrea.leaves
Parameters
----------
bg_index : int
An index of the basisgrid that would be generated from
CompoundAreaCamera.to_basisgrid()
Returns
-------
camera_index : tuple of ints
(leaf_index, subpanel_index)
"""

# just count subpanels until we reach "bg_index" number of subpanels

subpanels_per_leaf = [ np.product(l.subpanel_shape) for l in self.leaves ]

if bg_index >= np.sum(subpanels_per_leaf):
raise ValueError('bg_index requested: %d, but Camera only has %d subpanels'
'' % (bg_index, np.sum(subpanels_per_leaf)))

c = 0
for leaf_index, n_panels in enumerate(subpanels_per_leaf):
c += n_panels

if c > bg_index: # the bg_index is in this leaf
subpanels_before_this_leaf = c - n_panels
subpanel_index = bg_index - subpanels_before_this_leaf
break

return (leaf_index, subpanel_index)


def to_basisgrid(self):
Expand Down
23 changes: 21 additions & 2 deletions test/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,17 @@ def test_jungfrau_vs_geometry_access(self):

assert err < 10.0, 'error greater than 10um avg per px (%f)' % err
assert num_more_than_1px_err < 7500, '>7500 pix w err > 1 px'


def test_bg_index_to_camera_index(self):
# we have a cspad 2M: 4x4=16 leaves, 2 subpanels/leaf

assert self.geom._bg_index_to_camera_index(0) == (0,0)
assert self.geom._bg_index_to_camera_index(1) == (0,1)
assert self.geom._bg_index_to_camera_index(2) == (1,0)
assert self.geom._bg_index_to_camera_index(3) == (1,1)
assert self.geom._bg_index_to_camera_index(31) == (15,1)


class TestCspad:

Expand Down Expand Up @@ -153,8 +163,8 @@ def test_hdf5_file(self):
xyz2 = np.array(f['/xyz'])
f.close()

# we re-shape the xyz to match the way psana
# presents CSPAD data
# we re-shape the xyz to match the way psana
# presents CSPAD data
assert xyz2.shape == self.geom.xyz.shape
np.testing.assert_allclose(self.geom.xyz, xyz2)

Expand All @@ -167,6 +177,15 @@ def setup(self):

self.geom = camera.CompoundAreaCamera.from_psana_file('ref_files/jungfrau/jungfrau4m.data')
self.klass = camera.CompoundAreaCamera

def test_bg_index_to_camera_index(self):
# we have a JF 4M: 4x2=8 leaves, 8 subpanels/leaf

for i in range(8):
assert self.geom._bg_index_to_camera_index(i) == (0,i)
assert self.geom._bg_index_to_camera_index(8) == (1,0)
assert self.geom._bg_index_to_camera_index(63) == (7,7)


def test_to_basis_grid(self):

Expand Down

0 comments on commit a7fe3e5

Please sign in to comment.