Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CATKIT-96: Reduce file IO overhead for sending commands to BMC #236

Merged
merged 5 commits into from
May 14, 2021

Conversation

ehpor
Copy link
Collaborator

@ehpor ehpor commented May 12, 2021

This PR caches the flat maps and dm mask which were read every time a DM command was sent to the Boston. This reduces the timing:

Before this PR:
My machine on simulator: 6.4-7.0ms.
Hicat2 on hardware: 4.7-4.8 ms.

After this PR:
My machine on simulator: 1.0ms.
Hicat2 on hardware: 0.71-0.81 ms.

  • Tested strokemin and performance on simulator.
  • Tested performance on hardware.
  • Full strokemin on hardware? (is this necessary?)

ehpor added 4 commits May 12, 2021 12:08
Also fix typo in find_repo_location().
Also simplify dm image to command with boolean indexing.
@ehpor ehpor requested a review from jamienoss as a code owner May 12, 2021 16:30
Copy link
Contributor

@jamienoss jamienoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this, just some minor changes, please, thanks.

Comment on lines 192 to 198
if flat_map_dm1 is None:
fname = CONFIG_INI.get("boston_kilo952", "flat_map_dm1")
flat_map_dm1 = fits.getdata(os.path.join(calibration_data_path, fname))

if flat_map_dm2 is None:
fname = CONFIG_INI.get("boston_kilo952", "flat_map_dm2")
flat_map_dm2 = fits.getdata(os.path.join(calibration_data_path, fname))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still load them conditionally to account for single DM usage. There's no strong reason to do so, only the weak example case of us messing around with a single DM and not even having the second file on disk.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do the same for the get_m_per_volt_map() function just below this one then.

@@ -25,14 +25,19 @@ def find_package_location(package='catkit'):
return importlib.util.find_spec(package).submodule_search_locations[0]


def find_repo_location(package='cakit'):
def find_repo_location(package='catkit'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. 👍

catkit/util.py Outdated
mask_path = os.path.join(find_package_location("catkit"), "hardware", "boston", "kiloCdm_2Dmask.fits")
mask = fits.open(mask_path)[0].data
return mask
if get_dm_mask.mask is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do the following just to keep code contained to the func def, please.

if getattr(get_dm_mask, "mask", None) is None:
  ...

catkit/util.py Outdated
return get_dm_mask.mask


get_dm_mask.mask = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

Comment on lines -265 to +268
# Flatten the mask using index952
mask = catkit.util.get_dm_mask()
index952 = np.flatnonzero(mask)
mask = catkit.util.get_dm_mask().astype('bool')
number_of_actuators = np.count_nonzero(mask)

number_of_actuators_per_dimension = CONFIG_INI.getint('boston_kilo952', 'dm_length_actuators')
number_of_actuators = CONFIG_INI.getint("boston_kilo952", "number_of_actuators")
image = np.zeros((number_of_actuators_per_dimension, number_of_actuators_per_dimension))
image[np.unravel_index(index952, image.shape)] = dm_command[:number_of_actuators]
image = np.zeros(mask.shape)
image[mask] = dm_command[:number_of_actuators]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good reduction! It took me way too long to even figure out what the old code was doing.

@jamienoss
Copy link
Contributor

@ehpor RE

Full strokemin on hardware? (is this necessary?)

Yes, please, thanks.

I also added a docstring.
@ehpor ehpor marked this pull request as draft May 13, 2021 16:36
@ehpor ehpor marked this pull request as ready for review May 13, 2021 16:37
@ehpor
Copy link
Collaborator Author

ehpor commented May 14, 2021

Tested on hardware: 2021-05-13T15-12-07_broadband_stroke_minimization.

@ehpor ehpor requested a review from jamienoss May 14, 2021 00:40
return flat_map_dm1 if dm_num == 1 else flat_map_dm2
return flat_map_dm2

raise ValueError('dm_num should be either 1 or 2.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not gonna ask you to redo this, but this sort of logic pattern is neater and more resilient to future changes when self-contained as an else to an elif, i.e.,

if dm_num == 1:
  ...
elif dm_num == 2:
  ...
else:
   raise

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered doing a dictionary approach too. That would have been even cleaner. PEP 634 is gonna be so good for this type of stuff too. I've been waiting for that for a long time.

@jamienoss jamienoss merged commit 319bad2 into develop May 14, 2021
@jamienoss jamienoss deleted the feature/CATKIT-96-reduce-bmc-overhead branch May 14, 2021 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants