%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('darkgrid')
sns.set_context('notebook')
from thunder import Colorize
image = Colorize.image

"""
Explanation: svoboda lab / tactile navigation / 1
Setup plotting
End of explanation
"""


bucket = "s3n://neuro.datasets/"
path = "svoboda.lab/tactile.navigation/1/"

"""
Explanation: Load the data
We load the data by first specifying a bucket and a path on Amazon S3, and then loading the different pieces separately.
End of explanation
"""


raw = tsc.loadImages(bucket + path + 'images', recursive=True, startIdx=0, stopIdx=500, npartitions=1)

"""
Explanation: Images
The raw data are images of calcium flouresence, which we load into an Images object. We restrict the range of data between the 0 and 500th time point. This is only because we are working on a small machine and want to avoid loading the entire data set.
End of explanation
"""


images = raw.planes(2,2).gaussianFilter(3).subsample(8)
images

"""
Explanation: We also subselect a single plane, blur, and downsample; this is again because we are on a single, small machine.
End of explanation
"""


images.cache()
images.count();

"""
Explanation: Cache the data to keep it in RAM (and prevent reloading from S3)
End of explanation
"""


params = tsc.loadParams(bucket + path + 'params/covariates.json')

params

"""
Explanation: Parameters
We can load and inspect covariates from the experiment, in this case, the mouse's running speed.
End of explanation
"""


metadata = tsc.loadJSON(bucket + path + 'info.json')

for k in metadata.keys():
    print('%s: %s' % (k, metadata[k]))

"""
Explanation: Metadata
Finally, we can load and display the metadata (stored as a JSON file)
End of explanation
"""


image(images.values().first(), clim=(0,500))

image(images.mean(), clim=(0,500))

image(images.stdev(), clim=(0,100))

"""
Explanation: Inspect images
An Images object is a collection of records, each one an image. We can inspect individual images or compute simple summary statistics.
End of explanation
"""


series = images.toTimeSeries()
series.cache()
series.count();

"""
Explanation: Convert to time series
We can also explore the data as a collection of time series. Here we convert to time series and cache the result.
End of explanation
"""


sample = series.squelch(100).normalize().subset(100, thresh=0.8)
plt.plot(sample.T);

"""
Explanation: Inspect as time series
Plot a random subset of pixel time series after simple filtering
End of explanation
"""


im = series.correlate(params['runningSpeed'][0:500]).pack()

image(im, clim=(0, 0.4))

"""
Explanation: Pixel-wise correlation
Correlate every time series to the running speed and pack into a local image
End of explanation
"""


