npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slayer

v1.0.1

Published

JavaScript time series spike detection for Node.js; like the Octave findpeaks function.

Downloads

84

Readme

Slayer Build Status

JavaScript time series spike detection for Node.js; like the Octave findpeaks function.

Time series are often displayed as bar, line or area charts. A peak or spike in a time series is a the highest value before the trend start to decrease.

In reality you do not need all the spikes. You only need the highest spike amongst them. Slayer helps to identify these local peaks easily.

Install

Usage

Slayer exposes a fluent JavaScript API and requires as little configuration as possible. The following examples illustrates common use cases.

Flat array peak detection

var slayer = require('slayer');
var arrayData = [0, 0, 0, 12, 0, …];

slayer().fromArray(arrayData).then(spikes => {
  console.log(spikes);      // [ { x: 4, y: 12 }, { x: 12, y: 25 } ]
});

Object based peak detection

var slayer = require('slayer');
var arrayData = […, { date: '…', value: 12 }, …];

slayer()
  .y(item => item.value)
  .fromArray(arrayData)
  .then(spikes => {
    console.log(spikes);    // [ { x: 4, y: 12 }, { x: 12, y: 25 } ]
  });

Streaming detection

someStream
  .pipe(slayer().createReadStream())
  .on('error', err => console.error(err))
  .on('data', spike => {
    console.log(spike);      // { x: 4, y: 12 }
  });

API

Access the Slayer API by requiring the CommonJS module:

var slayer = require('slayer');

A spike object is an object composed of two keys:

  • x: the index value within the time series array;
  • y: the spike value within the time series array.

slayer(config)

The slayer() factory returns a new chainable instance of Slayer.

The optional config object enables you to adjust its behaviour according to your needs:

  • minPeakDistance (Integer): size of the values overlooked window. Default is 30;
  • minPeakHeight (Number): discard any value below that threshold. Default is 0.

Returns a slayer chainable object.

.y(fn)

Data accessor applied to each series item and used to determine spike values.

It will return this value as the y value of a spike object.

slayer()
  .y(item => item.value)   // considering item looks like `{ value: 12 }`
  .fromArray(arrayData)
  .then(spikes => {
    console.log(spikes);   // { x: 4, y: 12 }
  });

Returns a mutated slayer chainable object.

.x(fn)

Index accessor applied to each series item.

It will return this value as the x value of a spike object.

slayer()
  .x((item, i) => item.date)  // considering item looks like `{ date: '2014-04-12T17:31:40.000Z', value: 12 }`
  .fromArray(arrayData)
  .then(spikes => {
    console.log(spikes);      // { x: '2014-04-12T17:31:40.000Z', y: 12 }
  });

Returns a mutated slayer chainable object.

.transform(fn)

Transforms the spike object before returning it as part of the found spike collections.

It is useful if you want to add extra data to the returned spike object.

slayer()
  .transform((xyItem, originalItem, i) => {
    xyItem.id = originalItem.id;

    return xyItem;
  })
  .fromArray(arrayData)
  .then(spikes => {
    console.log(spikes);   // { x: 4, y: 12, id: '21232f297a57a5a743894a0e4a801fc3' }
  });

Returns a mutated slayer chainable object.

.fromArray(data)

Processes a finite array of data and returns them at once.

slayer()
  .fromArray(arrayData)
  .then(spikes => {

    console.log(spikes);   // { x: 4, y: 12, id: '21232f297a57a5a743894a0e4a801fc3' }
  });

Returns an ES2015 Promise object.

.createReadStream(options)

Processes a stream of data and emits a data event each time a spike is found. Although you might notice a slight delay as spike deduplication happens under the hood.

You can optionally pass an options object to tweak and adjust the precision of the analysis:

  • bufferingFactor (Number): buffer ratio of the sliding window, against minPeakDistance. Default is 4;
  • lookAheadFactor (Number): additional buffer ratio to look ahead, against the sliding window size. Default is 0.33.

With this setup, slayer will buffer 4 times the amount of minPeakDistance with an additional 0.33 times before performing an analysis before moving from 4 times the amount of minPeakDistance.

The following example demonstrates the streaming analysis of a file containing single values on each row of the document:

var split = require('split');

fs.createReadStream('./big-big-data.txt')
  .pipe(split())
  .pipe(slayer().createReadStream())
  .on('data', spike => console.log(spike));

Returns a ReadableStream.

Contributing and testing

If you wish to contribute the project with code but you fear to break something, no worries as TravisCI takes care of this for each Pull Request.

Nobody will blame your code. And feel free to ask before making a pull request. We will try to provide you guidance for code design etc.

If you want to run the tests locally, simply run:

npm test

If you want to run them continuously, then run:

npm test -- --watch

Licence

Copyright 2016 British Broadcasting Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.