Skip to content

Commit b9cdfbc

Browse files
committed
add parameter outside to filter()
1 parent 41d2886 commit b9cdfbc

6 files changed

Lines changed: 279 additions & 61 deletions

File tree

EidosScribe/EidosHelpFunctions.rtf

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@
915915
\f3\fs20 with a matrix or array argument, because the desired behavior in that case has not yet been implemented.\
916916
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
917917

918-
\f1\fs18 \cf2 \kerning1\expnd0\expndtw0 (float)filter(numeric\'a0x, float\'a0filter)\
918+
\f1\fs18 \cf2 \kerning1\expnd0\expndtw0 (float)filter(numeric\'a0x, float\'a0filter, [lif$\'a0outside\'a0=\'a0F])\
919919
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
920920

921921
\f3\fs20 \cf2 Returns the result of convolving
@@ -932,24 +932,59 @@
932932
\f1\fs18 filter
933933
\f3\fs20 value with each
934934
\f1\fs18 x
935-
\f3\fs20 value within the filter\'92s range. If the filter, centered over a given value of
935+
\f3\fs20 value within the filter\'92s range. The length of
936+
\f1\fs18 filter
937+
\f3\fs20 is required to be odd, so that the filter has a central value (and can thus be centered over each value of
938+
\f1\fs18 x
939+
\f3\fs20 ).\
940+
If the filter, centered over a given value of
936941
\f1\fs18 x
937942
\f3\fs20 , extends beyond the end of
938943
\f1\fs18 x
939-
\f3\fs20 the corresponding result element will be
944+
\f3\fs20 then the calculation of the corresponding element of the result is governed by the
945+
\f1\fs18 outside
946+
\f3\fs20 parameter. When
947+
\f1\fs18 outside
948+
\f3\fs20 is
949+
\f1\fs18 F
950+
\f3\fs20 (the default), the corresponding element in the result will be
940951
\f1\fs18 NAN
941-
\f3\fs20 since its value is undefined. The length of
942-
\f1\fs18 filter
943-
\f3\fs20 is required to be odd, so that the filter has a central value (and can thus be centered over each value of
952+
\f3\fs20 ; this matches the behavior of the R
953+
\f1\fs18 filter()
954+
\f3\fs20 function (except that R uses
955+
\f1\fs18 NA
956+
\f3\fs20 ). If
957+
\f1\fs18 outside
958+
\f3\fs20 is
959+
\f1\fs18 T
960+
\f3\fs20 , values outside
944961
\f1\fs18 x
945-
\f3\fs20 ).\
946-
This function is useful for computing running means and similar transformations of an input vector,. For a simple running mean of width
962+
\f3\fs20 will be excluded from the calculation (the filter value covering that position will be considered to be
963+
\f1\fs18 0
964+
\f3\fs20 ), and the other values in the filter will be adjusted so that the sum of the absolute values of the filter weights used is unchanged, to compensate for the excluded values by giving the positions inside
965+
\f1\fs18 x
966+
\f3\fs20 more weight. Finally, if
967+
\f1\fs18 outside
968+
\f3\fs20 is
969+
\f1\fs18 integer
970+
\f3\fs20 or
971+
\f1\fs18 float
972+
\f3\fs20 , that value will be used as the value of
973+
\f1\fs18 x
974+
\f3\fs20 for all positions outside
975+
\f1\fs18 x
976+
\f3\fs20 ; one might pass an expected value or mean value in this way, to be used for all outside positions.\
977+
This function is useful for computing running means and similar transformations of an input vector. For a simple running mean of width
947978
\f1\fs18 w
948979
\f3\fs20 , pass r
949980
\f1\fs18 ep(1/w, w)
950981
\f3\fs20 for
951982
\f1\fs18 filter
952-
\f3\fs20 .\
983+
\f3\fs20 . That case is automatically detected and handled efficiently; otherwise, the runtime of this function is proportional to the length of
984+
\f1\fs18 x
985+
\f3\fs20 times the length of
986+
\f1\fs18 filter
987+
\f3\fs20 , and so will be slow for long filters.\
953988
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
954989

955990
\f1\fs18 \cf0 (+$)max(+\'a0x, ...)\

QtSLiM/help/EidosHelpFunctions.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@
118118
<p class="p5"><span class="s5">Returns the <b>sample Pearson’s correlation coefficient</b> between </span><span class="s6">x</span><span class="s5"> and </span><span class="s6">y</span><span class="s5">, usually denoted <i>r</i>.<span class="Apple-converted-space">  </span>The sizes of </span><span class="s6">x</span><span class="s5"> and </span><span class="s6">y</span><span class="s5"> must be identical.<span class="Apple-converted-space">  </span>If </span><span class="s6">x</span><span class="s5"> and </span><span class="s6">y</span><span class="s5"> have a size of </span><span class="s6">0</span><span class="s5"> or </span><span class="s6">1</span><span class="s5">, the return value will be </span><span class="s6">NULL</span><span class="s5">.<span class="Apple-converted-space">  </span>At present it is illegal to call </span><span class="s6">cor()</span><span class="s5"> with a matrix or array argument, because the desired behavior in that case has not yet been implemented.</span></p>
119119
<p class="p4"><span class="s5">(float$)cov(numeric x, numeric y)</span></p>
120120
<p class="p5"><span class="s5">Returns the <b>corrected sample covariance</b> between </span><span class="s6">x</span><span class="s5"> and </span><span class="s6">y</span><span class="s5">.<span class="Apple-converted-space">  </span>The sizes of </span><span class="s6">x</span><span class="s5"> and </span><span class="s6">y</span><span class="s5"> must be identical.<span class="Apple-converted-space">  </span>If </span><span class="s6">x</span><span class="s5"> and </span><span class="s6">y</span><span class="s5"> have a size of </span><span class="s6">0</span><span class="s5"> or </span><span class="s6">1</span><span class="s5">, the return value will be </span><span class="s6">NULL</span><span class="s5">.<span class="Apple-converted-space">  </span>At present it is illegal to call </span><span class="s6">cov()</span><span class="s5"> with a matrix or array argument, because the desired behavior in that case has not yet been implemented.</span></p>
121-
<p class="p4">(float)filter(numeric x, float filter)</p>
122-
<p class="p5">Returns the result of convolving <span class="s2">x</span> with <span class="s2">filter</span>.<span class="Apple-converted-space">  </span>The returned vector will be the same length as <span class="s2">x</span>.<span class="Apple-converted-space">  </span>The convolution is performed by centering <span class="s2">filter</span> on each position of <span class="s2">x</span> to produce a corresponding result element that is the sum over the products of each <span class="s2">filter</span> value with each <span class="s2">x</span> value within the filter’s range.<span class="Apple-converted-space">  </span>If the filter, centered over a given value of <span class="s2">x</span>, extends beyond the end of <span class="s2">x</span> the corresponding result element will be <span class="s2">NAN</span> since its value is undefined.<span class="Apple-converted-space">  </span>The length of <span class="s2">filter</span> is required to be odd, so that the filter has a central value (and can thus be centered over each value of <span class="s2">x</span>).</p>
123-
<p class="p5">This function is useful for computing running means and similar transformations of an input vector,.<span class="Apple-converted-space">  </span>For a simple running mean of width <span class="s2">w</span>, pass r<span class="s2">ep(1/w, w)</span> for <span class="s2">filter</span>.</p>
121+
<p class="p4">(float)filter(numeric x, float filter, [lif$ outside = F])</p>
122+
<p class="p5">Returns the result of convolving <span class="s2">x</span> with <span class="s2">filter</span>.<span class="Apple-converted-space">  </span>The returned vector will be the same length as <span class="s2">x</span>.<span class="Apple-converted-space">  </span>The convolution is performed by centering <span class="s2">filter</span> on each position of <span class="s2">x</span> to produce a corresponding result element that is the sum over the products of each <span class="s2">filter</span> value with each <span class="s2">x</span> value within the filter’s range.<span class="Apple-converted-space">  </span>The length of <span class="s2">filter</span> is required to be odd, so that the filter has a central value (and can thus be centered over each value of <span class="s2">x</span>).</p>
123+
<p class="p5">If the filter, centered over a given value of <span class="s2">x</span>, extends beyond the end of <span class="s2">x</span> then the calculation of the corresponding element of the result is governed by the <span class="s2">outside</span> parameter.<span class="Apple-converted-space">  </span>When <span class="s2">outside</span> is <span class="s2">F</span> (the default), the corresponding element in the result will be <span class="s2">NAN</span>; this matches the behavior of the R <span class="s2">filter()</span> function (except that R uses <span class="s2">NA</span>).<span class="Apple-converted-space">  </span>If <span class="s2">outside</span> is <span class="s2">T</span>, values outside <span class="s2">x</span> will be excluded from the calculation (the filter value covering that position will be considered to be <span class="s2">0</span>), and the other values in the filter will be adjusted so that the sum of the absolute values of the filter weights used is unchanged, to compensate for the excluded values by giving the positions inside <span class="s2">x</span> more weight. Finally, if <span class="s2">outside</span> is <span class="s2">integer</span> or <span class="s2">float</span>, that value will be used as the value of <span class="s2">x</span> for all positions outside <span class="s2">x</span>; one might pass an expected value or mean value in this way, to be used for all outside positions.</p>
124+
<p class="p5">This function is useful for computing running means and similar transformations of an input vector.<span class="Apple-converted-space">  </span>For a simple running mean of width <span class="s2">w</span>, pass r<span class="s2">ep(1/w, w)</span> for <span class="s2">filter</span>.<span class="Apple-converted-space">  </span>That case is automatically detected and handled efficiently; otherwise, the runtime of this function is proportional to the length of <span class="s2">x</span> times the length of <span class="s2">filter</span>, and so will be slow for long filters.</p>
124125
<p class="p2">(+$)max(+ x, ...)</p>
125126
<p class="p3">Returns the <b>maximum</b> of <span class="s2">x</span> and the other arguments supplied: the single greatest value contained by all of them.<span class="Apple-converted-space">  </span>All of the arguments must be the same type as <span class="s2">x</span>, and the return type will match that of <span class="s2">x</span><span class="s3">.</span><span class="Apple-converted-space">  </span>If all of the arguments have a size of <span class="s2">0</span>, the return value will be <span class="s2">NULL</span>; note that this means that <span class="s2">max(x, max(y))</span> may produce an error, if <span class="s2">max(y)</span> is <span class="s2">NULL</span>, in cases where <span class="s2">max(x, y)</span> does not.</p>
126127
<p class="p2">(float$)mean(lif<span class="s1"> </span>x)</p>

VERSIONS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ development head (in the master branch):
2626
fix #534, support uniparentally-transmitted chromosomes in hermaphrodite populations
2727
add new Plot legendTitleEntry() method for making a title line in a plot legend
2828
click-drag in the SLiMgui script view's line number area now drag-selects whole lines
29-
fix #550, add filter() function to Eidos, which is essentially a subset of R's filter(): (float)filter(numeric x, float filter)
29+
fix #550, add filter() function to Eidos, which is essentially a subset of R's filter(): (float)filter(numeric x, float filter, [lif$ outside = F])
3030
partial fix for #530, add function (numeric$)tr(numeric x) to obtain the trace of a square matrix
3131
partial fix for #530, add function (numeric)outerProduct(numeric x, numeric y) to obtain the outer product of vectors x and y
3232
partial fix for #530, add function (numeric$)det(numeric x) to obtain the determinant of a square matrix

eidos/eidos_functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const std::vector<EidosFunctionSignature_CSP> &EidosInterpreter::BuiltInFunction
147147

148148
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("cor", Eidos_ExecuteFunction_cor, kEidosValueMaskFloat | kEidosValueMaskSingleton))->AddNumeric("x")->AddNumeric("y"));
149149
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("cov", Eidos_ExecuteFunction_cov, kEidosValueMaskFloat | kEidosValueMaskSingleton))->AddNumeric("x")->AddNumeric("y"));
150-
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("filter", Eidos_ExecuteFunction_filter, kEidosValueMaskFloat))->AddNumeric("x")->AddFloat("filter"));
150+
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("filter", Eidos_ExecuteFunction_filter, kEidosValueMaskFloat))->AddNumeric("x")->AddFloat("filter")->AddLogicalEquiv_OS("outside", gStaticEidosValue_LogicalF));
151151
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("max", Eidos_ExecuteFunction_max, kEidosValueMaskAnyBase | kEidosValueMaskSingleton))->AddAnyBase("x")->AddEllipsis());
152152
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("mean", Eidos_ExecuteFunction_mean, kEidosValueMaskFloat | kEidosValueMaskSingleton))->AddLogicalEquiv("x"));
153153
signatures->emplace_back((EidosFunctionSignature *)(new EidosFunctionSignature("min", Eidos_ExecuteFunction_min, kEidosValueMaskAnyBase | kEidosValueMaskSingleton))->AddAnyBase("x")->AddEllipsis());

0 commit comments

Comments
 (0)