{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true, "nbsphinx": "hidden" }, "outputs": [], "source": [ "from __future__ import print_function, unicode_literals\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "import seaborn as sns\n", "sns.set()\n", "\n", "plt.rcParams[\"figure.figsize\"] = 9, 4.51\n", "\n", "import expectexception" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Extractors Tutorial\n", "\n", "## Introduction\n", "\n", "This tutorial is a guide of how you can create your custom feature extraction routine\n", "and add this extractor to feets.\n", "\n", "## Fundamentals\n", "\n", "- The feature extraction as modeled as a class.\n", "- The class must inerith from `feets.Extractor`\n", "- The extractor class must has at least three elements\n", " 1. **data**: list with at least one valid feets data \n", " (*time*, *magnitude*, *error*, *magnitude2*, *aligned_time*, *aligned_magnitude*, *aligned_magnitude2*, \n", " *aligned_error* or *aligned_error2*)\n", " 2. **features**: list with the name of the features that this extractor generates.\n", " 3. **fit**: a method with the same parameters defined in the `data` list. `fit()` must return\n", " a dictionary with keys equals to the `features` list.\n", "\n", "## Example 1: `MaxMagMinTime` extractor\n", "\n", "Let's say we need to create a feature extractor called **MaxMagMinTime** that must return 2 features:\n", "\n", "1. **magmax**: The maximun magnitude \n", "2. **mintime**: the minimun time" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "import feets\n", "\n", "class MaxMagMinTime(feets.Extractor): # must inherit from Extractor\n", "\n", " data = ['magnitude', 'time'] # Which data is needed \n", " # to calculate this feature\n", " \n", " features = [\"magmax\", \"mintime\"] # The names of the expected \n", " # feature\n", " \n", " # The values of data are the params\n", " def fit(self, magnitude, time): \n", " # The return value must be a dict with the same values \n", " # defined in features\n", " return {\"magmax\": magnitude.max(), \"mintime\": time.min()} " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally to make the extractor available for the `FeaturSpace` class, you need to register it with the command:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.MaxMagMinTime" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "feets.register_extractor(MaxMagMinTime)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the extractor are available as any other provided in feets:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# let's create the feature-space\n", "fs = feets.FeatureSpace(only=[\"magmax\", \"mintime\"])\n", "fs" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([u'magmax', u'mintime'], \n", " dtype='