Interactive masking tool#
This notebook show how to use the interactive masking tool.
[1]:
%matplotlib widget
from scippneutron import MaskingTool
import plopp as pp
import scipp as sc
import numpy as np
We generate some fake data:
[2]:
rng = np.random.default_rng(seed=4321)
position = 10.0 * rng.standard_normal(size=[50_000, 2])
values = np.linalg.norm(position, axis=1)
da = sc.DataArray(
data=sc.array(dims=["row"], values=values, unit="K"),
coords={
"x": sc.array(dims=["row"], unit="m", values=position[:, 0]),
"y": sc.array(dims=["row"], unit="m", values=position[:, 1]),
},
).hist(y=300, x=300)
da
[2]:
- y: 300
- x: 300
- x(x [bin-edge])float64m-39.956, -39.675, ..., 44.035, 44.316
Values:
array([-39.9562149 , -39.67530742, -39.39439993, -39.11349245, -38.83258496, -38.55167748, -38.27077 , -37.98986251, -37.70895503, -37.42804754, -37.14714006, -36.86623258, -36.58532509, -36.30441761, -36.02351012, -35.74260264, -35.46169516, -35.18078767, -34.89988019, -34.6189727 , -34.33806522, -34.05715773, -33.77625025, -33.49534277, -33.21443528, -32.9335278 , -32.65262031, -32.37171283, -32.09080535, -31.80989786, -31.52899038, -31.24808289, -30.96717541, -30.68626793, -30.40536044, -30.12445296, -29.84354547, -29.56263799, -29.2817305 , -29.00082302, -28.71991554, -28.43900805, -28.15810057, -27.87719308, -27.5962856 , -27.31537812, -27.03447063, -26.75356315, -26.47265566, -26.19174818, -25.9108407 , -25.62993321, -25.34902573, -25.06811824, -24.78721076, -24.50630327, -24.22539579, -23.94448831, -23.66358082, -23.38267334, -23.10176585, -22.82085837, -22.53995089, -22.2590434 , -21.97813592, -21.69722843, -21.41632095, -21.13541347, -20.85450598, -20.5735985 , -20.29269101, -20.01178353, -19.73087604, -19.44996856, -19.16906108, -18.88815359, -18.60724611, -18.32633862, -18.04543114, -17.76452366, -17.48361617, -17.20270869, -16.9218012 , -16.64089372, -16.35998624, -16.07907875, -15.79817127, -15.51726378, -15.2363563 , -14.95544882, -14.67454133, -14.39363385, -14.11272636, -13.83181888, -13.55091139, -13.27000391, -12.98909643, -12.70818894, -12.42728146, -12.14637397, -11.86546649, -11.58455901, -11.30365152, -11.02274404, -10.74183655, -10.46092907, -10.18002159, -9.8991141 , -9.61820662, -9.33729913, -9.05639165, -8.77548416, -8.49457668, -8.2136692 , -7.93276171, -7.65185423, -7.37094674, -7.09003926, -6.80913178, -6.52822429, -6.24731681, -5.96640932, -5.68550184, -5.40459436, -5.12368687, -4.84277939, -4.5618719 , -4.28096442, -4.00005693, -3.71914945, -3.43824197, -3.15733448, -2.876427 , -2.59551951, -2.31461203, -2.03370455, -1.75279706, -1.47188958, -1.19098209, -0.91007461, -0.62916713, -0.34825964, -0.06735216, 0.21355533, 0.49446281, 0.7753703 , 1.05627778, 1.33718526, 1.61809275, 1.89900023, 2.17990772, 2.4608152 , 2.74172268, 3.02263017, 3.30353765, 3.58444514, 3.86535262, 4.1462601 , 4.42716759, 4.70807507, 4.98898256, 5.26989004, 5.55079753, 5.83170501, 6.11261249, 6.39351998, 6.67442746, 6.95533495, 7.23624243, 7.51714991, 7.7980574 , 8.07896488, 8.35987237, 8.64077985, 8.92168733, 9.20259482, 9.4835023 , 9.76440979, 10.04531727, 10.32622476, 10.60713224, 10.88803972, 11.16894721, 11.44985469, 11.73076218, 12.01166966, 12.29257714, 12.57348463, 12.85439211, 13.1352996 , 13.41620708, 13.69711456, 13.97802205, 14.25892953, 14.53983702, 14.8207445 , 15.10165199, 15.38255947, 15.66346695, 15.94437444, 16.22528192, 16.50618941, 16.78709689, 17.06800437, 17.34891186, 17.62981934, 17.91072683, 18.19163431, 18.47254179, 18.75344928, 19.03435676, 19.31526425, 19.59617173, 19.87707921, 20.1579867 , 20.43889418, 20.71980167, 21.00070915, 21.28161664, 21.56252412, 21.8434316 , 22.12433909, 22.40524657, 22.68615406, 22.96706154, 23.24796902, 23.52887651, 23.80978399, 24.09069148, 24.37159896, 24.65250644, 24.93341393, 25.21432141, 25.4952289 , 25.77613638, 26.05704387, 26.33795135, 26.61885883, 26.89976632, 27.1806738 , 27.46158129, 27.74248877, 28.02339625, 28.30430374, 28.58521122, 28.86611871, 29.14702619, 29.42793367, 29.70884116, 29.98974864, 30.27065613, 30.55156361, 30.8324711 , 31.11337858, 31.39428606, 31.67519355, 31.95610103, 32.23700852, 32.517916 , 32.79882348, 33.07973097, 33.36063845, 33.64154594, 33.92245342, 34.2033609 , 34.48426839, 34.76517587, 35.04608336, 35.32699084, 35.60789833, 35.88880581, 36.16971329, 36.45062078, 36.73152826, 37.01243575, 37.29334323, 37.57425071, 37.8551582 , 38.13606568, 38.41697317, 38.69788065, 38.97878813, 39.25969562, 39.5406031 , 39.82151059, 40.10241807, 40.38332556, 40.66423304, 40.94514052, 41.22604801, 41.50695549, 41.78786298, 42.06877046, 42.34967794, 42.63058543, 42.91149291, 43.1924004 , 43.47330788, 43.75421536, 44.03512285, 44.31603033]) - y(y [bin-edge])float64m-41.534, -41.257, ..., 41.460, 41.738
Values:
array([-41.53439127, -41.25681817, -40.97924506, -40.70167195, -40.42409884, -40.14652573, -39.86895263, -39.59137952, -39.31380641, -39.0362333 , -38.75866019, -38.48108709, -38.20351398, -37.92594087, -37.64836776, -37.37079465, -37.09322155, -36.81564844, -36.53807533, -36.26050222, -35.98292912, -35.70535601, -35.4277829 , -35.15020979, -34.87263668, -34.59506358, -34.31749047, -34.03991736, -33.76234425, -33.48477114, -33.20719804, -32.92962493, -32.65205182, -32.37447871, -32.0969056 , -31.8193325 , -31.54175939, -31.26418628, -30.98661317, -30.70904006, -30.43146696, -30.15389385, -29.87632074, -29.59874763, -29.32117453, -29.04360142, -28.76602831, -28.4884552 , -28.21088209, -27.93330899, -27.65573588, -27.37816277, -27.10058966, -26.82301655, -26.54544345, -26.26787034, -25.99029723, -25.71272412, -25.43515101, -25.15757791, -24.8800048 , -24.60243169, -24.32485858, -24.04728548, -23.76971237, -23.49213926, -23.21456615, -22.93699304, -22.65941994, -22.38184683, -22.10427372, -21.82670061, -21.5491275 , -21.2715544 , -20.99398129, -20.71640818, -20.43883507, -20.16126196, -19.88368886, -19.60611575, -19.32854264, -19.05096953, -18.77339642, -18.49582332, -18.21825021, -17.9406771 , -17.66310399, -17.38553089, -17.10795778, -16.83038467, -16.55281156, -16.27523845, -15.99766535, -15.72009224, -15.44251913, -15.16494602, -14.88737291, -14.60979981, -14.3322267 , -14.05465359, -13.77708048, -13.49950737, -13.22193427, -12.94436116, -12.66678805, -12.38921494, -12.11164184, -11.83406873, -11.55649562, -11.27892251, -11.0013494 , -10.7237763 , -10.44620319, -10.16863008, -9.89105697, -9.61348386, -9.33591076, -9.05833765, -8.78076454, -8.50319143, -8.22561832, -7.94804522, -7.67047211, -7.392899 , -7.11532589, -6.83775278, -6.56017968, -6.28260657, -6.00503346, -5.72746035, -5.44988725, -5.17231414, -4.89474103, -4.61716792, -4.33959481, -4.06202171, -3.7844486 , -3.50687549, -3.22930238, -2.95172927, -2.67415617, -2.39658306, -2.11900995, -1.84143684, -1.56386373, -1.28629063, -1.00871752, -0.73114441, -0.4535713 , -0.1759982 , 0.10157491, 0.37914802, 0.65672113, 0.93429424, 1.21186734, 1.48944045, 1.76701356, 2.04458667, 2.32215978, 2.59973288, 2.87730599, 3.1548791 , 3.43245221, 3.71002532, 3.98759842, 4.26517153, 4.54274464, 4.82031775, 5.09789086, 5.37546396, 5.65303707, 5.93061018, 6.20818329, 6.48575639, 6.7633295 , 7.04090261, 7.31847572, 7.59604883, 7.87362193, 8.15119504, 8.42876815, 8.70634126, 8.98391437, 9.26148747, 9.53906058, 9.81663369, 10.0942068 , 10.37177991, 10.64935301, 10.92692612, 11.20449923, 11.48207234, 11.75964544, 12.03721855, 12.31479166, 12.59236477, 12.86993788, 13.14751098, 13.42508409, 13.7026572 , 13.98023031, 14.25780342, 14.53537652, 14.81294963, 15.09052274, 15.36809585, 15.64566896, 15.92324206, 16.20081517, 16.47838828, 16.75596139, 17.0335345 , 17.3111076 , 17.58868071, 17.86625382, 18.14382693, 18.42140003, 18.69897314, 18.97654625, 19.25411936, 19.53169247, 19.80926557, 20.08683868, 20.36441179, 20.6419849 , 20.91955801, 21.19713111, 21.47470422, 21.75227733, 22.02985044, 22.30742355, 22.58499665, 22.86256976, 23.14014287, 23.41771598, 23.69528908, 23.97286219, 24.2504353 , 24.52800841, 24.80558152, 25.08315462, 25.36072773, 25.63830084, 25.91587395, 26.19344706, 26.47102016, 26.74859327, 27.02616638, 27.30373949, 27.5813126 , 27.8588857 , 28.13645881, 28.41403192, 28.69160503, 28.96917814, 29.24675124, 29.52432435, 29.80189746, 30.07947057, 30.35704367, 30.63461678, 30.91218989, 31.189763 , 31.46733611, 31.74490921, 32.02248232, 32.30005543, 32.57762854, 32.85520165, 33.13277475, 33.41034786, 33.68792097, 33.96549408, 34.24306719, 34.52064029, 34.7982134 , 35.07578651, 35.35335962, 35.63093272, 35.90850583, 36.18607894, 36.46365205, 36.74122516, 37.01879826, 37.29637137, 37.57394448, 37.85151759, 38.1290907 , 38.4066638 , 38.68423691, 38.96181002, 39.23938313, 39.51695624, 39.79452934, 40.07210245, 40.34967556, 40.62724867, 40.90482178, 41.18239488, 41.45996799, 41.7375411 ])
- (y, x)float64K0.0, 0.0, ..., 0.0, 0.0
Values:
array([[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]])
Two-dimensional masks#
We then load it into the MaskingTool
.
Instructions:
Use the buttons in the top bar to add masks to the data
Left-click to add a new shape, and left-click again to persist the shape
Left-click a vertex to edit a shape
Right-click and hold to drag a shape
Middle-click (or Ctrl + left-click) to delete a shape
Save the masks to a file when the “Save” button is clicked
[3]:
masking_tool = MaskingTool(da, norm="log")
[5]:
masking_tool
[5]:
You can either save the masks to a file by filling in the text field and clicking the “Save” button, or get the masks as a dict
by calling:
[6]:
masking_tool.get_masks()
[6]:
{'xy_0': {'kind': 'rectangle',
'bounds': {'x': {'min': {'value': -20.0, 'unit': 'm'},
'max': {'value': 1.0, 'unit': 'm'}},
'y': {'min': {'value': 5.0, 'unit': 'm'},
'max': {'value': 30.0, 'unit': 'm'}}}},
'x_1': {'kind': 'vspan',
'bounds': {'x': {'min': {'value': 7, 'unit': 'm'},
'max': {'value': 15, 'unit': 'm'}}}},
'x_2': {'kind': 'vspan',
'bounds': {'x': {'min': {'value': 20, 'unit': 'm'},
'max': {'value': 30, 'unit': 'm'}}}},
'y_3': {'kind': 'hspan',
'bounds': {'y': {'min': {'value': -20, 'unit': 'm'},
'max': {'value': -10, 'unit': 'm'}}}}}
One-dimensional masks#
The tool also works with one-dimensional data. In this case, only the vertical span tool is active.
[7]:
masking_tool = MaskingTool(da.sum("y"), norm="log")
[9]:
masking_tool
[9]:
Using the masks from the tool#
We will now show how we can connect the output of the masking tool to further computation, applying the masks and processing the data further.
We first begin by setting up the same tool as above (in 2D).
[10]:
masking_tool = MaskingTool(da, norm="log")
We then create a node that will listen to the changes in the masking tool and apply the masks onto the data before summing along the vertical dimension.
[11]:
def apply_masks(da, trigger_node):
# Get the current masks
masks = masking_tool.get_masks()
# Make a shallow copy and add the masks
out = da.copy(deep=False)
for name in masks:
cond = sc.ones(sizes=da.sizes, dtype=bool)
for dim, bounds in masks[name]["bounds"].items():
mids = sc.midpoints(da.coords[dim])
m = (
mids > sc.scalar(bounds["min"]["value"], unit=bounds["min"]["unit"])
) & (mids < sc.scalar(bounds["max"]["value"], unit=bounds["max"]["unit"]))
cond = cond & m
out.masks[name] = cond
# Reduce data and return
return out.sum("y")
apply = pp.Node(apply_masks, da=da, trigger_node=masking_tool.masking_node)
fig = pp.plot(apply, norm="log")
We now show the two figures, and changes in the tool should update the reduced result. Adding a rectangle lowers the signal in the same x range but does not bring it to zero. Adding a vertical span zeros the data.
[13]:
pp.widgets.Box([masking_tool.fig, fig])
[13]: