Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import numpy as np
  2. from prysm.geometry import mcache
  3.  
  4. def make_random_subaperture_mask(ary, ary_diam, mask_diam, shape='circle', seed=None):
  5. """Make a mask of a given diameter that is a random subaperture of the given array.
  6.  
  7. Parameters
  8. ----------
  9. ary : `numpy.ndarray`
  10. an array, notionally containing phase data. Only used for its shape.
  11. ary_diam : `float`
  12. the diameter of the array on its long side, if it is not square
  13. mask_diam : `float`
  14. the desired mask diameter, in the same units as ary_diam
  15. `shape` : `str`
  16. a string accepted by prysm.geometry.MCache.__call__, for example 'circle', or 'square' or 'octogon'
  17. seed : `int`
  18. a random number seed, None will be a random seed, provide one to make the mask deterministic.
  19.  
  20. Returns
  21. -------
  22. `numpy.ndarray`
  23. an array that can be used to mask `ary`. Use as:
  24. ary[ret == 0] = np.nan
  25.  
  26. """
  27. s = ary.shape
  28. plate_scale = ary_diam / max(s)
  29. max_shift_mm = (ary_diam - mask_diam) / 2
  30. max_shift_px = int(np.floor(max_shift_mm / plate_scale))
  31.  
  32. # get random offsets
  33. rng_y = (gen.random() - 0.5) * 2 # shift [0,1] => [-1, 1]
  34. rng_x = (gen.random() - 0.5) * 2
  35. dy = int(np.floor(rng_y * max_shift_px))
  36. dx = int(np.floor(rng_x * max_shift_px))
  37.  
  38. # get the current center pixel and then offset by the RNG
  39. cy, cx = (v // 2 for v in s)
  40. cy += dy
  41. cx += dx
  42.  
  43. # generate the mask and calculate the insertion point
  44. mask_semidiam = mask_diam / plate_scale / 2
  45. half_low = int(np.floor(mask_semidiam))
  46. half_high = int(np.floor(mask_semidiam))
  47.  
  48. # generate the mask in an array of only its size (e.g., 128x128 for a 128x128 mask in a 900x900 phase array)
  49. mask = mcache(shape, mask_diam_px)
  50.  
  51. # make the output array and insert the mask itself
  52. out = np.zeros_like(ary)
  53. print(cy, half_low, half_high, cx)
  54. out[cy-half_low:cy+half_high, cx-half_low:cx+half_high] = mask
  55. return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement