Skip to content

Instantly share code, notes, and snippets.

@neuromusic
Created August 7, 2015 21:27
Show Gist options
  • Save neuromusic/2bd16aa8176b700c66e8 to your computer and use it in GitHub Desktop.
Save neuromusic/2bd16aa8176b700c66e8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Correlation between TimeSeries vectors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook we are trying to calculate the pearson correlation between two timeseries vectors"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from thunder import Colorize\n",
"image = Colorize.image\n",
"import seaborn as sns\n",
"sns.set_context(\"poster\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load the images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For all the NEUROFINDER datasets, you can get the images, as well as the ground-truth sources. When running on just one machine, only grab a subset of the data by specifying a range of indices (say 0 through 100)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"bucket = \"s3n://neuro.datasets/\"\n",
"path = \"challenges/neurofinder/00.00/\"\n",
"images = tsc.loadImages(bucket + path + 'images', recursive=True, startIdx=0, stopIdx=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Images\n",
"nrecords: 100\n",
"dtype: uint16\n",
"dims: min=(0, 0), max=(511, 511), count=(512, 512)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cache the data set into memory to prevent reloading"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"images.cache()\n",
"images.count()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Look at both a single image and simple statistics (mean and standard deviation)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"image(images[0], clim=(0, 1000))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"im = images.mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"image(im, clim=(0, 1000))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from pyspark.mllib.stat import Statistics\n",
"ts = images.toTimeSeries()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try to grab two vectors and run the pearson correlation there"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = sc.parallelize(ts.rdd.take(1))\n",
"y = sc.parallelize(ts.rdd.take(2))\n",
"Statistics.corr(x,y,method='pearson')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That didn't work.\n",
"\n",
"What about an all-to-all correlation?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Statistics.corr(ts.rdd,method='pearson')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment