<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Running and evaluating a block algorithm
In this notebook we run a block-wise algorithm on a training data set and evaluate performance.
Setup environment
Step1:  Setup plotting
Step2:  Load data
set the path
Step3:  load and cache the raw data (we only load first 100 time points because we're on a single node)
Step4:  load the sources
Step5:  estimate the mean
Step6:  Run a block algorithm
Step7:  estimate score (fraction of matches based on centroid distance)
Step8:  estimate overlap and exactness (based on degree of pixel overlap for matching sources)
<ASSISTANT_TASK:>
Python Code:
import numpy as np
from scipy.stats import norm
from thunder import SourceExtraction
from thunder.extraction import OverlapBlockMerger
Explanation: Running and evaluating a block algorithm
In this notebook we run a block-wise algorithm on a training data set and evaluate performance.
Setup environment
End of explanation
import matplotlib.pyplot as plt
%matplotlib inline
from thunder import Colorize
image = Colorize.image
Explanation: Setup plotting
End of explanation
path = 's3://neuro.datasets/challenges/neurofinder/02.00/'
Explanation: Load data
set the path
End of explanation
data = tsc.loadImages(path + 'images', startIdx=0, stopIdx=100)
data.cache()
data.count();
Explanation: load and cache the raw data (we only load first 100 time points because we're on a single node)
End of explanation
truth = tsc.loadSources(path + 'sources/sources.json')
Explanation: load the sources
End of explanation
im = data.mean()
Explanation: estimate the mean
End of explanation
merger = OverlapBlockMerger(0.1)
model = SourceExtraction('nmf', merger=merger, componentsPerBlock=5, percentile=95, minArea=100, maxArea=500)
sources = model.fit(data, size=(32, 32), padding=8)
image(sources.masks(im.shape, base=truth, color='random', outline=True), size=10)
Explanation: Run a block algorithm
End of explanation
recall, precision, score = truth.similarity(sources, metric='distance', minDistance=5)
print('recall: %.2f' % recall)
print('precision: %.2f' % precision)
print('score: %.2f' % score)
Explanation: estimate score (fraction of matches based on centroid distance)
End of explanation
overlap, exactness = tuple(np.nanmean(truth.overlap(sources, method='rates', minDistance=5), axis=0))
print('overlap: %.2f' % overlap)
print('exactness: %.2f' % exactness)
Explanation: estimate overlap and exactness (based on degree of pixel overlap for matching sources)
End of explanation
<END_TASK>