import ConfigParser
import csv
import datetime
import time

import numpy as np

import util

# Read config
config = ConfigParser.RawConfigParser()
config.read('app.config')

exp_id = time.time()

videos = dict()

# Open data file
with open('data/%s' % config.get('data', 'filename'), 'rb') as f:
    reader = csv.reader(f)
    # Skip header
    reader.next()
    # Load rows
    for row in reader:
        date = row[0].strip()
        loc = row[1].strip()
        vid_id = row[2].strip()
        videos[vid_id] = videos.get(vid_id, dict())
        videos[vid_id][loc] = videos[vid_id].get(loc, list())
        y,m,d = date.split('-')
        videos[vid_id][loc].append(datetime.date(int(y), int(m), int(d)))

# Calculate spread and longevity
results = list()
spread_values = list()
span_values = list()
for vid_id, countries in videos.iteritems():
    # Calculate country spread
    spread = len(countries)
    spread_values.append(spread)
    # Calculate longevity span
    low = min([min(dates) for dates in countries.itervalues()])
    high = max([max(dates) for dates in countries.itervalues()])
    span = (high - low).days + 1
    span_values.append(span)
    results.append((vid_id, spread, span))
# Write results csv
util.write_results_csv('findstatistics', exp_id, 'spread_span', results, ('Video ID', 'Spread', 'Span'))
# Calculate spread histogram
spread_hist = list()
spread_counts, spread_bins = np.histogram(spread_values, range(1,max(spread_values)+1))
for i, count in enumerate(spread_counts):
    spread_hist.append((spread_bins[i], count))
util.write_results_csv('findstatistics', exp_id, 'spread_histogram', spread_hist, ('Spread', 'Count'))
# Calculate span histogram
span_hist = list()
span_counts, span_bins = np.histogram(span_values, range(1, max(span_values)+1))
for i, count in enumerate(span_counts):
    span_hist.append((span_bins[i], count))
util.write_results_csv('findstatistics', exp_id, 'span_histogram', span_hist, ('Span', 'Count'))

@@ -223,16 +223,75 @@
 ta file%0A
+all_dates = set()%0Aall_countries = set()%0Aall_videos = set()%0A
 with ope
@@ -533,32 +533,122 @@
  row%5B2%5D.strip()%0A
+        all_dates.add(date)%0A        all_countries.add(loc)%0A        all_videos.add(vid_id)%0A
         videos%5Bv
@@ -777,16 +777,16 @@
 it('-')%0A
-
         
@@ -852,16 +852,219 @@
 t(d)))%0A%0A
+# Find basic counts%0Afields = ('Videos', 'Countries', 'Dates')%0Acounts = %5B(len(all_videos), len(all_countries), len(all_dates))%5D%0Autil.write_results_csv('findstatistics', exp_id, 'counts', counts, fields)%0A%0A
 # Calcul

Add raw video/country/date counts.