Source code for examples.migrations.0005_add_avg_times_clusters

# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-09 03:20
from __future__ import unicode_literals
import numpy as np

from django.db import migrations, models
from django.conf import settings


[docs]def populate_avg_times(apps, schema_editor): UserInfo = apps.get_model("examples", "UserInfo") # Use a fixed seed for generate content np.random.seed(123456) table_size = getattr(settings, "DJANGO_AI_EXAMPLES_USERINFO_SIZE", 200) group_size = int(table_size / 4) # Equal sizes for all groups # Generate the clusters y0 = np.random.multivariate_normal([20, 20], [[2, 0], [0, 0.1]], size=group_size) y1 = np.random.multivariate_normal([20, 20], [[0.1, 0], [0, 2]], size=group_size) y2 = np.random.multivariate_normal([25, 25], [[2, -1.5], [-1.5, 2]], size=group_size) y3 = np.random.multivariate_normal([16, 16], [[0.5, 0], [0, 0.5]], size=group_size) y = np.vstack([y0, y1, y2, y3]) uis = UserInfo.objects.all() # Update the objects in the Model for index, ui in enumerate(uis): ui.avg_time_pages_a = y[index][0] ui.avg_time_logged = y[index][1] ui.save(update_fields=['avg_time_logged', 'avg_time_pages_a'])
def unpopulate_avg_times(apps, schema_editor): """ This is for making the migration reversible, it doesn't do anything because the fields will be removed after by the reverse of AddField. """ pass class Migration(migrations.Migration): dependencies = [ ('examples', '0004_bn_example'), ] operations = [ migrations.AddField( model_name='userinfo', name='avg_time_pages_a', field=models.FloatField( blank=True, null=True, verbose_name='Average Time spent on Pages A'), ), migrations.AddField( model_name='userinfo', name='avg_time_logged', field=models.FloatField( blank=True, null=True, verbose_name='Average Weekly Time Logged In'), ), migrations.RunPython(populate_avg_times, unpopulate_avg_times), ]