import cPickle as pickle

from django.conf import settings

from map.models import CityBorder
from crime.models import CriminalRecord


def vectorize(grid_size, period, new=False):
    file = str(grid_size) + 'grid_size_' + period + 'period_vector.p'
    path = settings.VECTORS_DIR + file
    try:
        if new:
            raise EnvironmentError()
        else:
            vectors = pickle.load(open(path, "rb"))
    except EnvironmentError as e:
        city = CityBorder.objects.get(name='Chicago')
        grid = city.generateGrid(grid_size)
        if period == 'monthly':
            vectors = vectorize_monthly(grid)
        if period == 'yearly':
            vectors = vectorize_yearly(grid)
        pickle.dump(vectors, open(path, "wb"))
    return vectors

def vectorize_monthly(grid):
    first_data = CriminalRecord.objects.first()
    last_data = CriminalRecord.objects.last()
    first_year = first_data.date.year
    first_month = first_data.date.month
    last_year = last_data.date.year
    last_month = last_data.date.month
    start = 12 * first_year + first_month - 1
    end = 12 * last_year + last_month
    vectors = []
    for ym in range(start, end ):
        year, month = divmod( ym, 12 )
        month += 1
        print year, month
        vector = []
        for i in xrange(len(grid)):
            g = grid[i]
            crimes = CriminalRecord.objects.filter(
                date__month=month, date__year=year,
                location__intersects=g).count()
            has_crime = int(crimes > 0)
            vector.append(has_crime)
        vectors.append(vector)
    return vectors

def vectorize_yearly(grid):
    first_data = CriminalRecord.objects.first()
    last_data = CriminalRecord.objects.last()
    first_year = first_data.date.year
    last_year = last_data.date.year
    vectors = []
    for year in range(first_year, last_year + 1):
        print year
        vector = []
        for i in xrange(len(grid)):
            g = grid[i]
            crimes = CriminalRecord.objects.filter(
                date__year=year, location__intersects=g).count()
            has_crime = int(crimes > 0)
            vector.append(has_crime)
        vectors.append(vector)
    return vectors
@@ -51,16 +51,72 @@
 settings
+%0Afrom django.core.exceptions import ImproperlyConfigured
 %0A%0Afrom m
@@ -184,16 +184,168 @@
 Record%0A%0A
+if not hasattr(settings, 'VECTORS_DIR'):%0A    raise ImproperlyConfigured(%0A        'The directory to save vector pickles is missing from your settings')%0A%0A
 %0Adef vec

check if settings variable VECTORS_DIR is set