Interactive masking#
In this example, we will use a custom drawing tool to draw rectangles on a 2D figure. The data inside the rectangles will be masked.
[1]:
%matplotlib widget
import plopp as pp
import scipp as sc
We first generate some data that contains three bands of peaks that all have different spreads.
[2]:
from plopp.data.examples import three_bands
da = three_bands()
We then construct our custom tool, using Mpltoolbox’s Rectangles tool, and inheriting from Plopp’s DrawingTool.
[3]:
from plopp.widgets.drawing import DrawingTool
from functools import partial
from mpltoolbox import Rectangles
def define_mask(da, rect_info):
"""
Function that creates a mask inside the area
covered by the rectangle.
"""
x = rect_info['x']
y = rect_info['y']
b = min(y['bottom'], y['top'])
t = max(y['bottom'], y['top'])
l = min(x['left'], x['right'])
r = max(x['left'], x['right'])
xcoord = sc.midpoints(da.coords[x['dim']])
ycoord = sc.midpoints(da.coords[y['dim']])
return (xcoord >= l) & (xcoord <= r) & (ycoord >= b) & (ycoord <= t)
def _get_rect_info(artist, figure):
"""
Convert the raw rectangle info to a dict containing the dimensions of
each axis, and values with units.
"""
return lambda: {
'x': {
'dim': figure.canvas.dims['x'],
'left': sc.scalar(artist.xy[0], unit=figure.canvas.units['x']),
'right': sc.scalar(
artist.xy[0] + artist.width, unit=figure.canvas.units['x']
),
},
'y': {
'dim': figure.canvas.dims['y'],
'bottom': sc.scalar(artist.xy[1], unit=figure.canvas.units['y']),
'top': sc.scalar(
artist.xy[1] + artist.height, unit=figure.canvas.units['y']
),
},
}
RectangleTool = partial(
DrawingTool, tool=Rectangles, get_artist_info=_get_rect_info, icon='vector-square'
)
Finally, we create our visualization interface with the figure, adding our new tool to the toolbar.
[4]:
data_node = pp.Node(da)
def apply_masks(da, *masks):
out = da.copy(deep=False)
for i, mask in enumerate(masks):
out.masks[str(i)] = mask
return out
masking_node = pp.Node(apply_masks, data_node)
fig = pp.imagefigure(masking_node, norm='log')
r = RectangleTool(
figure=fig, input_node=data_node, func=define_mask, destination=masking_node
)
fig.toolbar['roi'] = r
[6]:
fig
[6]:
[7]:
pp.show_graph(fig)
[7]:
To retrieve the masked data array, simply call the node that is applying the masks:
[8]:
masking_node()
[8]:
scipp.DataArray (885.36 KB)
- y: 300
- x: 300
- x(x [bin-edge])float64cm-84.006, -82.459, ..., 378.366, 379.912
Values:
array([-84.00573984, -82.45934697, -80.91295411, -79.36656124, -77.82016838, -76.27377552, -74.72738265, -73.18098979, -71.63459692, -70.08820406, -68.54181119, -66.99541833, -65.44902546, -63.9026326 , -62.35623973, -60.80984687, -59.26345401, -57.71706114, -56.17066828, -54.62427541, -53.07788255, -51.53148968, -49.98509682, -48.43870395, -46.89231109, -45.34591823, -43.79952536, -42.2531325 , -40.70673963, -39.16034677, -37.6139539 , -36.06756104, -34.52116817, -32.97477531, -31.42838244, -29.88198958, -28.33559672, -26.78920385, -25.24281099, -23.69641812, -22.15002526, -20.60363239, -19.05723953, -17.51084666, -15.9644538 , -14.41806093, -12.87166807, -11.32527521, -9.77888234, -8.23248948, -6.68609661, -5.13970375, -3.59331088, -2.04691802, -0.50052515, 1.04586771, 2.59226057, 4.13865344, 5.6850463 , 7.23143917, 8.77783203, 10.3242249 , 11.87061776, 13.41701063, 14.96340349, 16.50979636, 18.05618922, 19.60258208, 21.14897495, 22.69536781, 24.24176068, 25.78815354, 27.33454641, 28.88093927, 30.42733214, 31.973725 , 33.52011787, 35.06651073, 36.61290359, 38.15929646, 39.70568932, 41.25208219, 42.79847505, 44.34486792, 45.89126078, 47.43765365, 48.98404651, 50.53043937, 52.07683224, 53.6232251 , 55.16961797, 56.71601083, 58.2624037 , 59.80879656, 61.35518943, 62.90158229, 64.44797516, 65.99436802, 67.54076088, 69.08715375, 70.63354661, 72.17993948, 73.72633234, 75.27272521, 76.81911807, 78.36551094, 79.9119038 , 81.45829667, 83.00468953, 84.55108239, 86.09747526, 87.64386812, 89.19026099, 90.73665385, 92.28304672, 93.82943958, 95.37583245, 96.92222531, 98.46861817, 100.01501104, 101.5614039 , 103.10779677, 104.65418963, 106.2005825 , 107.74697536, 109.29336823, 110.83976109, 112.38615396, 113.93254682, 115.47893968, 117.02533255, 118.57172541, 120.11811828, 121.66451114, 123.21090401, 124.75729687, 126.30368974, 127.8500826 , 129.39647547, 130.94286833, 132.48926119, 134.03565406, 135.58204692, 137.12843979, 138.67483265, 140.22122552, 141.76761838, 143.31401125, 144.86040411, 146.40679697, 147.95318984, 149.4995827 , 151.04597557, 152.59236843, 154.1387613 , 155.68515416, 157.23154703, 158.77793989, 160.32433276, 161.87072562, 163.41711848, 164.96351135, 166.50990421, 168.05629708, 169.60268994, 171.14908281, 172.69547567, 174.24186854, 175.7882614 , 177.33465426, 178.88104713, 180.42743999, 181.97383286, 183.52022572, 185.06661859, 186.61301145, 188.15940432, 189.70579718, 191.25219005, 192.79858291, 194.34497577, 195.89136864, 197.4377615 , 198.98415437, 200.53054723, 202.0769401 , 203.62333296, 205.16972583, 206.71611869, 208.26251156, 209.80890442, 211.35529728, 212.90169015, 214.44808301, 215.99447588, 217.54086874, 219.08726161, 220.63365447, 222.18004734, 223.7264402 , 225.27283306, 226.81922593, 228.36561879, 229.91201166, 231.45840452, 233.00479739, 234.55119025, 236.09758312, 237.64397598, 239.19036885, 240.73676171, 242.28315457, 243.82954744, 245.3759403 , 246.92233317, 248.46872603, 250.0151189 , 251.56151176, 253.10790463, 254.65429749, 256.20069036, 257.74708322, 259.29347608, 260.83986895, 262.38626181, 263.93265468, 265.47904754, 267.02544041, 268.57183327, 270.11822614, 271.664619 , 273.21101186, 274.75740473, 276.30379759, 277.85019046, 279.39658332, 280.94297619, 282.48936905, 284.03576192, 285.58215478, 287.12854765, 288.67494051, 290.22133337, 291.76772624, 293.3141191 , 294.86051197, 296.40690483, 297.9532977 , 299.49969056, 301.04608343, 302.59247629, 304.13886916, 305.68526202, 307.23165488, 308.77804775, 310.32444061, 311.87083348, 313.41722634, 314.96361921, 316.51001207, 318.05640494, 319.6027978 , 321.14919066, 322.69558353, 324.24197639, 325.78836926, 327.33476212, 328.88115499, 330.42754785, 331.97394072, 333.52033358, 335.06672645, 336.61311931, 338.15951217, 339.70590504, 341.2522979 , 342.79869077, 344.34508363, 345.8914765 , 347.43786936, 348.98426223, 350.53065509, 352.07704796, 353.62344082, 355.16983368, 356.71622655, 358.26261941, 359.80901228, 361.35540514, 362.90179801, 364.44819087, 365.99458374, 367.5409766 , 369.08736946, 370.63376233, 372.18015519, 373.72654806, 375.27294092, 376.81933379, 378.36572665, 379.91211952]) - y(y [bin-edge])float64cm-20.493, -19.354, ..., 320.140, 321.279
Values:
array([-2.04930768e+01, -1.93538362e+01, -1.82145957e+01, -1.70753551e+01, -1.59361145e+01, -1.47968740e+01, -1.36576334e+01, -1.25183928e+01, -1.13791523e+01, -1.02399117e+01, -9.10067112e+00, -7.96143056e+00, -6.82218999e+00, -5.68294943e+00, -4.54370886e+00, -3.40446829e+00, -2.26522773e+00, -1.12598716e+00, 1.32534047e-02, 1.15249397e+00, 2.29173454e+00, 3.43097510e+00, 4.57021567e+00, 5.70945624e+00, 6.84869680e+00, 7.98793737e+00, 9.12717793e+00, 1.02664185e+01, 1.14056591e+01, 1.25448996e+01, 1.36841402e+01, 1.48233808e+01, 1.59626213e+01, 1.71018619e+01, 1.82411025e+01, 1.93803430e+01, 2.05195836e+01, 2.16588242e+01, 2.27980647e+01, 2.39373053e+01, 2.50765459e+01, 2.62157864e+01, 2.73550270e+01, 2.84942676e+01, 2.96335081e+01, 3.07727487e+01, 3.19119893e+01, 3.30512298e+01, 3.41904704e+01, 3.53297110e+01, 3.64689515e+01, 3.76081921e+01, 3.87474327e+01, 3.98866732e+01, 4.10259138e+01, 4.21651543e+01, 4.33043949e+01, 4.44436355e+01, 4.55828760e+01, 4.67221166e+01, 4.78613572e+01, 4.90005977e+01, 5.01398383e+01, 5.12790789e+01, 5.24183194e+01, 5.35575600e+01, 5.46968006e+01, 5.58360411e+01, 5.69752817e+01, 5.81145223e+01, 5.92537628e+01, 6.03930034e+01, 6.15322440e+01, 6.26714845e+01, 6.38107251e+01, 6.49499657e+01, 6.60892062e+01, 6.72284468e+01, 6.83676874e+01, 6.95069279e+01, 7.06461685e+01, 7.17854091e+01, 7.29246496e+01, 7.40638902e+01, 7.52031308e+01, 7.63423713e+01, 7.74816119e+01, 7.86208525e+01, 7.97600930e+01, 8.08993336e+01, 8.20385742e+01, 8.31778147e+01, 8.43170553e+01, 8.54562959e+01, 8.65955364e+01, 8.77347770e+01, 8.88740176e+01, 9.00132581e+01, 9.11524987e+01, 9.22917393e+01, 9.34309798e+01, 9.45702204e+01, 9.57094610e+01, 9.68487015e+01, 9.79879421e+01, 9.91271827e+01, 1.00266423e+02, 1.01405664e+02, 1.02544904e+02, 1.03684145e+02, 1.04823385e+02, 1.05962626e+02, 1.07101867e+02, 1.08241107e+02, 1.09380348e+02, 1.10519588e+02, 1.11658829e+02, 1.12798069e+02, 1.13937310e+02, 1.15076551e+02, 1.16215791e+02, 1.17355032e+02, 1.18494272e+02, 1.19633513e+02, 1.20772753e+02, 1.21911994e+02, 1.23051235e+02, 1.24190475e+02, 1.25329716e+02, 1.26468956e+02, 1.27608197e+02, 1.28747437e+02, 1.29886678e+02, 1.31025919e+02, 1.32165159e+02, 1.33304400e+02, 1.34443640e+02, 1.35582881e+02, 1.36722121e+02, 1.37861362e+02, 1.39000602e+02, 1.40139843e+02, 1.41279084e+02, 1.42418324e+02, 1.43557565e+02, 1.44696805e+02, 1.45836046e+02, 1.46975286e+02, 1.48114527e+02, 1.49253768e+02, 1.50393008e+02, 1.51532249e+02, 1.52671489e+02, 1.53810730e+02, 1.54949970e+02, 1.56089211e+02, 1.57228452e+02, 1.58367692e+02, 1.59506933e+02, 1.60646173e+02, 1.61785414e+02, 1.62924654e+02, 1.64063895e+02, 1.65203135e+02, 1.66342376e+02, 1.67481617e+02, 1.68620857e+02, 1.69760098e+02, 1.70899338e+02, 1.72038579e+02, 1.73177819e+02, 1.74317060e+02, 1.75456301e+02, 1.76595541e+02, 1.77734782e+02, 1.78874022e+02, 1.80013263e+02, 1.81152503e+02, 1.82291744e+02, 1.83430985e+02, 1.84570225e+02, 1.85709466e+02, 1.86848706e+02, 1.87987947e+02, 1.89127187e+02, 1.90266428e+02, 1.91405669e+02, 1.92544909e+02, 1.93684150e+02, 1.94823390e+02, 1.95962631e+02, 1.97101871e+02, 1.98241112e+02, 1.99380352e+02, 2.00519593e+02, 2.01658834e+02, 2.02798074e+02, 2.03937315e+02, 2.05076555e+02, 2.06215796e+02, 2.07355036e+02, 2.08494277e+02, 2.09633518e+02, 2.10772758e+02, 2.11911999e+02, 2.13051239e+02, 2.14190480e+02, 2.15329720e+02, 2.16468961e+02, 2.17608202e+02, 2.18747442e+02, 2.19886683e+02, 2.21025923e+02, 2.22165164e+02, 2.23304404e+02, 2.24443645e+02, 2.25582885e+02, 2.26722126e+02, 2.27861367e+02, 2.29000607e+02, 2.30139848e+02, 2.31279088e+02, 2.32418329e+02, 2.33557569e+02, 2.34696810e+02, 2.35836051e+02, 2.36975291e+02, 2.38114532e+02, 2.39253772e+02, 2.40393013e+02, 2.41532253e+02, 2.42671494e+02, 2.43810735e+02, 2.44949975e+02, 2.46089216e+02, 2.47228456e+02, 2.48367697e+02, 2.49506937e+02, 2.50646178e+02, 2.51785419e+02, 2.52924659e+02, 2.54063900e+02, 2.55203140e+02, 2.56342381e+02, 2.57481621e+02, 2.58620862e+02, 2.59760102e+02, 2.60899343e+02, 2.62038584e+02, 2.63177824e+02, 2.64317065e+02, 2.65456305e+02, 2.66595546e+02, 2.67734786e+02, 2.68874027e+02, 2.70013268e+02, 2.71152508e+02, 2.72291749e+02, 2.73430989e+02, 2.74570230e+02, 2.75709470e+02, 2.76848711e+02, 2.77987952e+02, 2.79127192e+02, 2.80266433e+02, 2.81405673e+02, 2.82544914e+02, 2.83684154e+02, 2.84823395e+02, 2.85962635e+02, 2.87101876e+02, 2.88241117e+02, 2.89380357e+02, 2.90519598e+02, 2.91658838e+02, 2.92798079e+02, 2.93937319e+02, 2.95076560e+02, 2.96215801e+02, 2.97355041e+02, 2.98494282e+02, 2.99633522e+02, 3.00772763e+02, 3.01912003e+02, 3.03051244e+02, 3.04190485e+02, 3.05329725e+02, 3.06468966e+02, 3.07608206e+02, 3.08747447e+02, 3.09886687e+02, 3.11025928e+02, 3.12165169e+02, 3.13304409e+02, 3.14443650e+02, 3.15582890e+02, 3.16722131e+02, 3.17861371e+02, 3.19000612e+02, 3.20139852e+02, 3.21279093e+02])
- (y, x)float64counts1.0, 1.0, ..., 1.0, 1.0
Values:
array([[1., 1., 1., ..., 1., 1., 1.], [1., 1., 1., ..., 1., 1., 1.], [1., 1., 1., ..., 1., 1., 1.], ..., [1., 1., 1., ..., 1., 1., 1.], [1., 1., 1., ..., 1., 1., 1.], [1., 1., 1., ..., 1., 1., 1.]])
- 0(x, y)boolFalse, False, ..., False, False
Values:
array([[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]]) - 1(x, y)boolFalse, False, ..., False, False
Values:
array([[False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], ..., [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]])