Skip to content

Instantly share code, notes, and snippets.

@PierreZ
Last active September 21, 2017 09:15
Show Gist options
  • Save PierreZ/d6c39f3fd28a139ded947a05a6fb0f86 to your computer and use it in GitHub Desktop.
Save PierreZ/d6c39f3fd28a139ded947a05a6fb0f86 to your computer and use it in GitHub Desktop.
Cool Warpscripts
// Macro to show outliers or strange points
// Usage:
// [GTS ... ] a list of GTS
// 10 an upperbound (a LONG) of the number of outliers to detect.
// T a BOOLEAN indicating whether to use the mean (False), or the median (True) to calculate the Z-score
// @FIND_OUTLIERS
// Use example: $gts 10 T @FIND_OUTLIERS
<%
[] 'strange_gts' STORE
'mean' STORE // BOOL
'upperbound' STORE // LONG
<%
'gts' STORE
$gts $upperbound $mean ESDTEST 'outliers' STORE
<% $outliers SIZE 0 == %>
<% CONTINUE %> IFT
NEWGTS 'annotation' RENAME 'annotation' STORE
$outliers
<%
'timestamp' STORE
$annotation $timestamp NaN NaN NaN T ADDVALUE 'annotation' STORE
%> FOREACH
[ $annotation mapper.toboolean 0 0 0 ] MAP 'annotation' STORE
$strange_gts [ [ $gts $annotation 0 GET ] ] APPEND '$strange_gts' STORE
%> FOREACH
$strange_gts
%> 'FIND_OUTLIERS' STORE

How to detect outliers

// Macro to show outliers or strange points
// Usage:

//    [GTS ... ] a list of GTS
//    10 an upperbound (a LONG) of the number of outliers to detect.
//    T a BOOLEAN indicating whether to use the mean (False), or the median (True) to calculate the Z-score
//    @FIND_OUTLIERS

// Use example: $gts 10 T @FIND_OUTLIERS
<%
    [] 'strange_gts' STORE
    'mean' STORE // BOOL
    'upperbound' STORE // LONG
    <%
        'gts' STORE
        $gts $upperbound $mean ESDTEST 'outliers' STORE
        <% $outliers SIZE 0 == %>
        <% CONTINUE %> IFT
        NEWGTS  'annotation' RENAME  'annotation' STORE
        $outliers
        <%
            'timestamp' STORE
            $annotation $timestamp NaN NaN NaN T ADDVALUE 'annotation' STORE
        %> FOREACH
        [ $annotation mapper.toboolean 0 0 0 ] MAP 'annotation' STORE

        $strange_gts [ [ $gts $annotation  0 GET ] ] APPEND  '$strange_gts' STORE
%> FOREACH
$strange_gts
%> 'FIND_OUTLIERS' STORE

Example

NEWGTS 'cos' RENAME
1 1080
<% DUP 'i' STORE DUP 2 * PI * 360 / COS $i * NaN NaN NaN 4 ROLL ADDVALUE %> FOR
[ SWAP bucketizer.last 1080 1 0 ] BUCKETIZE 'cos' STORE
NEWGTS 'pattern.to.detect' RENAME
200 370
<% DUP 'i' STORE DUP 2 * PI * 360 / COS $i * NaN NaN NaN 4 ROLL ADDVALUE %> FOR
[ SWAP bucketizer.last 2160 1 0 ] BUCKETIZE 'pattern.to.detect' STORE
// Create Pattern
32 'windowSize' STORE
8 'patternLength' STORE
16 'quantizationScale' STORE
$pattern.to.detect 0 GET $windowSize $patternLength $quantizationScale PATTERNS VALUES 'patterns' STORE
$cos $patterns $windowSize $patternLength $quantizationScale PATTERNDETECTION 'cos.detection' RENAME 'cos.detection' STORE
$cos.detection
// Let's create a gts for each trip
10 // Quiet period
5 // Min number of values
'subPattern' // Label
TIMESPLIT
$cos
<% RAND RAND RAND RAND RAND RAND + + + + + 3.0 - %> 'normal' STORE

// we generate a GTS with an approximately normal distribution
[ 


// generate periodic GTS of mean 0
[
    NEWGTS 'cos' RENAME
    1 1080
    <% DUP 'i' STORE DUP 2 * PI * 360 / COS $i * NaN NaN NaN 4 ROLL ADDVALUE %> FOR
    
    NEWGTS 1 1080 <% NaN NaN NaN @normal 100 * ADDVALUE %> FOR 'a' RENAME 
]

// sum up
[ SWAP [] reducer.sum ] REDUCE 'sum' RENAME 

// bucketize
[ SWAP bucketizer.first 0 1 1024 ] BUCKETIZE 0 GET
DUP
// call STL
{ 'PERIOD' 10 } STL 2 ->LIST

STL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment