Analysis › Process · version 1 · has a preview
A new localization field from an expression in the existing ones.
The Math Parser makes a new attribute of the localization table from an expression in the attributes it already has:
on_time_ms = n_in_group * 20 z_true_nm = z_nm * 0.8 within = (xy_err_nm < 25) & (sigma_nm > 100)
The result is an ordinary attribute. The filter (also called bounds: the lowest and highest values of attributes a localization must lie between to be shown), the renderer, the colour coding and every other plugin use it like any attribute the fitter wrote. That is what makes the plugin useful for everything there is no button for: a refractive-index correction on z, an on-time in milliseconds, a ratio of two fitted quantities, or a flag (0 or 1) marking the localizations that a range filter on one attribute cannot express.
The expression is kept with the table, as a recipe. That matters once the blinks are merged – merge blinks, also called grouping or linking, combines the localizations in consecutive frames that come from one blinking event into one: the recipe says what the new attribute means for a blink, so it is not averaged by accident, and it is still there, with how it was computed, when the file is opened again.
1. Reading the expression. The text after = is read as a formula, not run as a program: only the attribute names, numbers, operators and functions of the table under In detail are allowed, and anything else is refused with a message pointing at the piece that is wrong. Every attribute the expression names must be in the table; if one is missing, the message lists the attributes the table does have.
2. Computing it. The formula is evaluated over whole attributes at once, one value per localization. A comparison gives 1 where it holds and 0 where it does not, so within above is a flag. A result that is a single number – a constant, or median(z_nm) – is copied to every localization.
3. Writing it. With apply to set to all localizations, the attribute is written for the whole table. With the selection, only the localizations of the current selection get the new value (the layer's filter, the drawn ROI – region of interest – and the 3D view's slab together); the others keep what the attribute had, or get no value (NaN) if the attribute is new.
4. The recipe. The expression is stored in the table together with merged blinks, the rule for what the attribute is once blinks are merged. Running again into the same new attribute replaces its recipe. An expression that reads the field it writes – photons = photons * 2 – is applied once and kept as no recipe at all: evaluated again at every merge it would compound. The attribute then goes on being combined per blink by its own rule (photons are summed).
5. On merged blinks. When the localizations of a blink are merged, each ordinary attribute is combined by a fixed rule (positions averaged, photons summed). A computed attribute instead follows its recipe:
on_time_ms = n_in_group * 20 is then the blink's on-time.The same attribute can mean quite different things per blink, which is why the choice is asked for rather than guessed:

The flag bright = photons > 1000 on a simulated table, taken to the merged blinks by four rules. Left: the fraction of blinks that come out bright – recomputing asks whether the blink's summed photons exceed 1000, which more blinks do than have all their frames above 1000. Right: with mean, the flag becomes the fraction of a blink's frames that were bright, shown for the blinks of more than one frame.
On regions. With on set to the ROIs or to an id attribute such as cluster_id or tid, the expression is over what was measured per region, one row per region instead of one per localization: rel = id_statistics.n_locs / median(n_locs) gives each cluster's size over the median cluster's. Each evaluation of those regions is named by its label made one word – NPC Corners is npc_corners, Id statistics id_statistics – and a value as npc_corners.n_corners; the form lists the names there are. A value only one evaluation has may be written bare. The result goes into the regions' maths region table, where the summaries, the region filter and Regions to attribute find it, and is computed again whenever what it reads is measured again. also onto the localizations writes it onto each region's rows as well, to colour by it.
On regions, the evaluations meet by region id: a region one of them did not measure has NaN there, and a reduction (median(n_locs)) is over the regions. An ROI's annotations are annotations.use (1 or 0) and its lines' lengths and angles; its keywords are text and cannot be computed with. The expressions are kept with the maths table as recipes, in the order written, so a later one may read an earlier one (maths.rel); when an evaluation they read is measured again they are all evaluated again, and a region whose inputs are out of date gets no current row, as a block's would not. The attribute written onto the localizations is a snapshot, as Write results' is.
Which table. The localizations, whatever the layer shows: the attribute is computed for each localization, and the selection is the layer's bounds applied to each localization. The merged blinks take the attribute by the merged blinks rule (below) when they are merged again.
What an expression may contain. Attribute names are the table's own (x_nm, photons, xy_err_nm, ...; the new attribute of an earlier run counts as one). Everything else:
| what | allowed |
|---|---|
| numbers | 20, 0.8, 1e3; constants pi, e, nan, inf, True, False |
| arithmetic | + - * / // (integer division) % (remainder) ** (power), unary - and + |
| comparisons | < <= > >= == !=, one per bracket |
| combining flags | & (and), ^ (exclusive or), ~ (not), and the vertical bar for or |
| choice | a if condition else b, per localization |
| elementwise functions | sqrt, abs, exp, log, log2, log10, sin, cos, tan, arcsin, arccos, arctan, arctan2, hypot, floor, ceil, round, sign, mod, rem, minimum, maximum, clip, where, isfinite, isnan, float, int |
| reductions (one number) | mean, median, std, sum, min, max, percentile, count |
Or is written with a vertical bar: (photons > 5000) | (sigma_nm > 150). The functions are numpy's (sqrt is numpy.sqrt, rem is numpy.remainder; float and int convert the type), called with their arguments in order: clip(photons, 0, 5000), percentile(z_nm, 90), where(z_nm > 0, 1, -1). The reductions ignore NaN, as numpy.nanmedian does; count is the number of values, NaN included. With apply to set to the selection, the expression is evaluated on the selected localizations only, so a reduction is the selection's own; recomputed on the merged blinks, it is theirs.
What is refused, and why. Each of these is a mistake that numpy would answer with either a crash far from the cause or, worse, a plausible wrong number, so it is caught with a message instead:
and, or, not – they ask a whole attribute whether it is true. Write &, |, ~.0 < x_nm < 5, a chained comparison – the same question in disguise. Write (0 < x_nm) & (x_nm < 5).xy_err_nm < 25 & sigma_nm > 100 – & binds tighter than <, so this would read as xy_err_nm < (25 & sigma_nm) > 100. Put each comparison in brackets. (In MATLAB, and so in SMAP, the precedence is the other way round.)&, |, ^ on a measured, non-integer attribute: a flag written by this plugin is stored as 0.0 and 1.0, so compare it first, (within > 0) & ....clip(a_min=...)), text in quotes, in, is, @, <<, >>, and anything that is not arithmetic: an attribute (x_nm.max()), an index (x_nm[0]), a lambda, a list.The expression is walked piece by piece rather than handed to Python's eval. It is saved with the table, in the history and in the files of chains (plugins run one after another as one), so it travels between people, and evaluating a string from someone else's file would let it do anything.
The result as an attribute. A boolean result is stored as 0/1 in float32, so the filter can take a range of it and merging blinks can reduce it. A floating-point result is stored as float32, like the rest of the table. An integer result – frame // 100 – keeps its integer type, since a frame number stops being exact in float32 above . A single number is copied to every row; a result of the wrong length, or one that is not a number, is refused.
The new attribute's name. It must be a word of letters, digits and underscores that does not start with a digit, so that a later expression can use it. The name of one of the functions is refused, and so are group_id and n_in_group, which merging blinks writes and would overwrite.
The recipe. Stored in the table's metadata, metadata["derived"], in the order the fields were defined (a field may be computed from an earlier one), as the field, the expression and the rule. It is saved in the file with the table. When blinks are merged, a computed attribute is left out of the usual per-attribute rules and either reduced by its rule – mean is the plain average of the localizations' values, not weighted by their precision as the positions are; any and all give 1 where the largest or smallest value is not zero – or recomputed from the expression on the merged blinks. Each time blinks are merged, the expressions are also evaluated again on the single localizations, which is what keeps a field in n_in_group up to date with the latest merge. An expression in n_in_group or group_id needs the blinks to have been merged once, since merging writes those attributes; before that the plugin says so. A recipe that cannot be recomputed because a attribute is missing is skipped with a message, and the table is still built.
A field computed for the selection is not a function of the table, so its expression is never recomputed. If merged blinks is recompute the expression, the rule used instead is mean, and the text says so.
The history. The last 20 pairs of field and expression are kept in math_parser.yaml in the settings directory, most recent first, each pair once, and offered under history. A history that cannot be read or written is treated as empty; it is never an error.
| setting | default | what it does |
|---|---|---|
onsource | localizations | The localizations, or the regions – ROIs or an id attribute – whose measured values the expression reads. Every source that has been measured is offered: the ROIs once an evaluation has run on them, an id attribute once a block or Id statistics has. On regions, apply to and merged blinks do not apply. |
new attributefield | within | The attribute the result is written to. Choose a name that says what the number is, with its unit, like |
=expression | (xy_err_nm < 25) & (sigma_nm > 100) | An expression in the attribute names; & and | combine comparisons, each in brackets. Use Preview first: it computes the expression and reports the range and median of the result without writing anything. A precedence mistake gives numbers, not an error. |
apply towhere | all localizations | The selection is the layer's filter, the ROI and the slab together; the rest of the field is NaN. A field written only for the selection has NaN elsewhere, and is only reduced, never recomputed, on the merged blinks: over the localizations of each blink that have a value, and NaN for a blink that has none. Choices: all localizations; the selection |
merged blinksgrouped | recompute the expression | What the field means once the blinks are merged: the expression again, or a rule to combine their values. recompute for an expression in |
historyrecall | An expression used before; choosing one fills the field, the expression and the merged-blinks rule. Picking an entry also restores the merged blinks rule it was used with. | |
also onto the localizationsto_locs | off | On regions: write the value onto each region's localizations too, NaN outside. Only for colouring by the value or using it per localization: to keep or drop regions by it, filter on |
where ROIs overlap (more)overlap | last | On ROIs, with also onto the localizations: the value of a localization in two ROIs. Choices: last; first; mean |
on_time_ms: 24693 values, 20 to 360, median 60. With the selection it adds how large the table was.A result whose range is not what was expected – all zeros for a flag, or values a thousand times too large – is the sign of a mistake in the expression; Preview shows it before the table has it.
During a live fit the attribute follows the table as it grows: it is computed for the localizations so far and then for every block the fit adds, and once more for the finished table. An expression with a reduction (median(z_nm)) is computed again over the whole table with every block, so its earlier values move as data arrives. This works only with apply to set to all localizations, and not for a field the expression itself reads or for a position; such a run is offered a new window instead (File > Open in a new window). It cannot be undone while the fit runs.
Based on SMAP's Process/Modify/MathParser (Ries 2020). What differs:
locs. in front of every field name in the string and runs it with MATLAB's eval, so any MATLAB is allowed. Here only arithmetic over attributes is run (see In detail), and a missing attribute or a precedence mistake is reported before anything is computed.& and | bind tighter than a comparison here and looser in MATLAB, which is why an unbracketed comparison is refused.