#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
        absolute_import, division, print_function, unicode_literals)
import six
from six.moves import zip, range, map

import sys
import os.path
import argparse

import numpy as np

import multiworm
import where
import blob_info

def longest_10(experiment, ten=10):
    blob_lifespan = [(blob['bid'], blob['died'] - blob['born']) for blob in experiment.summary]
    blob_lifespan = sorted(blob_lifespan, key=lambda x: x[1], reverse=True)
    return blob_lifespan[:ten]

def assess_noise(experiment, npoints=200, plot=False):
    xmeans, ymeans = [], []
    xsds, ysds = [], []

    for n, blob in enumerate(longest_10(experiment, ten=npoints), start=1):
        bid, life = blob

        blob = experiment.parse_blob(bid)
        if blob is None:
            continue

        steps = blob_info.centroid_steps(blob['centroid'])
        (xmean, xsd), (ymean, ysd) = blob_info.centroid_stats(steps)

        xmeans.append(xmean)
        xsds.append(xsd)
        ymeans.append(ymean)
        ysds.append(ysd)

    means = [np.mean(v) for v in [xmeans, xsds, ymeans, ysds]]

    if plot:
        import matplotlib.pyplot as plt
        f, axs = plt.subplots(ncols=2)
        f.suptitle('Normal summary statistics for frame-by-frame centroid steps')
        for ax, data, title in zip(axs, [[xmeans, ymeans], [xsds, ysds]], 
                ['X/Y means', 'X/Y stddev']):
            ax.scatter(*data)
            ax.set_title(title)
            ax.axvline()
            ax.axhline()

        axs[1].set_xlim(left=0)
        axs[1].set_ylim(bottom=0)

    return means

def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = argparse.ArgumentParser(description='Get basic information '
        'about an experiment.')

    parser.add_argument('data_set', help='The location of the data set. If '
        'names specified in a lookup file can be selected with a prefix of '
        '{0}.'.format(where.named_location).replace('%', '%%'))
    parser.add_argument('-n', '--noise', action='store_true', 
        help='Estimate the amount of noise present in the centroid data.  '
        'Plot available.')
    parser.add_argument('-l', '--longest', type=int, help='Display the '
        'longest-lived blobs', default=10)
    parser.add_argument('-p', '--plot', action='store_true', help='Show a '
        'plot (if supported by another command)')

    args = parser.parse_args()

    args.data_set = where.where(args.data_set)

    experiment = multiworm.Experiment(args.data_set)
    experiment.load_summary()

    print('Experiment summary file: {}'.format(experiment.summary_file))
    print('Number of blobs        : {}'.format(len(experiment.summary)))

    if args.noise:
        means = assess_noise(experiment, plot=args.plot)

        print('X mean of means/stddevs: {:0.3e} \u00B1 {:0.3e}'.format(*means[0:2]))
        print('Y mean of means/stddevs: {:0.3e} \u00B1 {:0.3e}'.format(*means[2:4]))

    if args.longest:
        print('   {:>5s} | {}'.format('ID', 'Life (s)'))
        print('  {:->7s}+{:-<10s}'.format('', ''))
        for bid, lifespan in longest_10(experiment, args.longest):
            print('   {:5d} | {:7.2f}'.format(bid, lifespan))


    if args.plot:
        import matplotlib.pyplot as plt
        plt.show()

if __name__ == '__main__':
    sys.exit(main())

@@ -263,16 +263,43 @@
 ltiworm%0A
+import multiworm.analytics%0A
 import w
@@ -602,10 +602,9 @@
 nts=
-20
+1
 0, p
@@ -623,547 +623,469 @@
     
-xmeans, ymeans = %5B%5D, %5B%5D%0A    xsds, ysds = %5B%5D, %5B%5D%0A%0A    for n, blob in enumerate(longest_10(experiment, ten=npoints), start=1):%0A        bid, life = blob%0A%0A        blob = experiment.parse_blob(bid)%0A        if blob is None:%0A            continue%0A%0A        steps = blob_info.centroid_steps(blob%5B'centroid'%5D)%0A        (xmean, xsd), (ymean, ysd) = blob_info.centroid_stats(steps
+noise_est = multiworm.analytics.NoiseEstimator()%0A%0A    analyzer = multiworm.analytics.ExperimentAnalyzer()%0A    analyzer.add_analysis_method(noise_est)%0A%0A    analyzer.analyze((bid, experiment.parse_blob(bid)) for (bid, lifetime) in longest_10(experiment, ten=npoints)
 )%0A%0A    
-  
-  xmeans.append(xmean)%0A        xsds.append(xsd)%0A        ymeans.append(ymean)%0A        ysds.append(ysd)%0A%0A    means = %5Bnp.mean(v) for v in %5Bxmeans, xsds, ymeans, ysds%5D
+data = analyzer.result_dict()%0A%0A    mean_xy = data%5B'noise'%5D%5B'mean_xy'%5D%0A    std_dev_xy = data%5B'noise'%5D%5B'std_dev_xy'%5D%0A%0A    means = data%5B'noise'%5D%5B'means'%5D%0A    std_devs = data%5B'noise'%5D%5B'std_devs'
 %5D%0A%0A 
@@ -1302,41 +1302,25 @@
 s, %5B
-%5Bx
 means, 
-ymeans%5D, %5Bxsds, ysds%5D
+std_devs
 %5D,
- 
 %0A   
@@ -1362,16 +1362,40 @@
 dev'%5D):%0A
+            print(data)%0A
         
@@ -1410,21 +1410,27 @@
 catter(*
+zip(*
 data)
+)
 %0A       
@@ -1587,17 +1587,31 @@
 urn mean
-s
+_xy, std_dev_xy
 %0A%0Adef ma
@@ -2057,17 +2057,16 @@
 e_true',
- 
 %0A       
@@ -2746,17 +2746,31 @@
     mean
-s
+_xy, std_dev_xy
  = asses
@@ -2804,16 +2804,68 @@
 gs.plot)
+%0A        x_stats, y_stats = zip(mean_xy, std_dev_xy)
 %0A%0A      
@@ -2934,18 +2934,15 @@
 at(*
-means%5B0:2%5D
+x_stats
 ))%0A 
@@ -3016,18 +3016,15 @@
 at(*
-means%5B2:4%5D
+y_stats
 ))%0A%0A

Use the multiworm analysis package with the exp_info.py script