def form_valid(self, form): user = self.request.user form_data = form.cleaned_data ord_id = form_data.get('ordination', None) dm_id = form_data.get('data_matrix', None) has_error = False if (not ord_id and not dm_id) or (ord_id and dm_id): form.add_error( 'ordination', 'Either ordination or data matrix must be chosen, but not both' ) form.add_error( 'data_matrix', 'Either ordination or data matrix must be chosen, but not both' ) has_error = True if not has_error: if dm_id: dm = get_or_error(DataMatrix, dict(id=dm_id)) si = SimilarityIndex.objects.filter(dm=dm).first() if si is not None: form.add_error('data_matrix', 'Already extracted') has_error = True else: si = SimilarityIndex(dm=dm) else: ord = get_or_error(Ordination, dict(id=ord_id)) si = SimilarityIndex.objects.filter(ord=ord).first() if si is not None: form.add_error('ordination', 'Already extracted') has_error = True else: si = SimilarityIndex(ord=ord, dm=ord.dm) if has_error: context = self.get_context_data() context['form'] = form rendered = render_to_string( 'partials/similarity-selection-form.html', context=context) return HttpResponse( json.dumps(dict(message=dict(success=False, html=rendered)))) si.save() task = Task(user=user, target='{}:{}'.format(SimilarityIndex.__name__, si.id)) task.save() si.task = task si.save() delay_in_production(calculate_similarity, task.id) context = self.get_context_data() context['task'] = task rendered = render_to_string( 'partials/similarity-extraction-tasks.html', context=context) return HttpResponse( json.dumps(dict(message=dict(success=True, html=rendered))))
def setUp(self): django.setup() from koe.models import Task from root.models import User user = User.objects.get(username='******') task = Task(user=user) task.save() self.task_id = task.id print('Task ID = {}'.format(self.task_id))
def form_valid(self, form): user = self.request.user form_data = form.cleaned_data ord_id = form_data.get('ordination', None) has_error = False if ord_id: form.add_error('ordination', 'Already extracted') has_error = True dm_id = form_data['data_matrix'] method = form_data['method'] ndims = form_data['ndims'] params = form_data['params'] params = Ordination.clean_params(params) dm = get_or_error(DataMatrix, dict(id=dm_id)) if Ordination.objects.filter(dm=dm, method=method, ndims=ndims, params=params).exists(): form.add_error('ordination', 'Already extracted') has_error = True if has_error: context = self.get_context_data() context['form'] = form rendered = render_to_string( 'partials/ordination-selection-form.html', context=context) return HttpResponse( json.dumps( dict(success=True, payload=dict(success=False, html=rendered)))) ord = Ordination(dm=dm, method=method, ndims=ndims, params=params) ord.save() task = Task(user=user, target='{}:{}'.format(Ordination.__name__, ord.id)) task.save() ord.task = task ord.save() delay_in_production(construct_ordination, task.id) context = self.get_context_data() context['task'] = task rendered = render_to_string( 'partials/ordination-extraction-tasks.html', context=context) return HttpResponse( json.dumps( dict(success=True, payload=dict(success=True, html=rendered))))
def recreate_associated_similarity_tasks(dmid, user): """ This is to facilitate datamatrix recreation: if the datamatrix is to be created, it will invalidate all associated similarity indices. :param dmid: id of the datamatrix :return: a list of task ids """ tasks = [] for si in SimilarityIndex.objects.filter(dm=dmid): task = si.task if task is None or task.user != user: task = Task(user=user, target='{}:{}'.format(SimilarityIndex.__name__, si.id)) task.save() si.task = task si.save() tasks.append(task) return tasks
def recreate_associated_ordination_tasks(dmid, user): """ This is to facilitate datamatrix recreation: if the datamatrix is to be created, it will invalidate all associated ordinations. They need to be rerun as well. :param dmid: id of the datamatrix :return: a list of task ids """ tasks = [] for ord in Ordination.objects.filter(dm=dmid): task = ord.task if task is None or task.user != user: task = Task(user=user, target='{}:{}'.format(Ordination.__name__, ord.id)) task.save() ord.task = task ord.save() tasks.append(task) return tasks
def handle(self, *args, **options): database_name = options['database_name'] celery = options['celery'] save_db = options['save_db'] if not save_db and celery: warning('celery reverted to False because save_db is False') database = get_or_error(Database, dict(name__iexact=database_name)) features = Feature.objects.all().order_by('id') aggregations = Aggregation.objects.filter(enabled=True).order_by('id') enabled_features = [] for f in features: if f.name in feature_map: enabled_features.append(f) features_hash = '-'.join( list(map(str, [x.id for x in enabled_features]))) aggregations_hash = '-'.join( list(map(str, aggregations.values_list('id', flat=True)))) user = User.objects.get(username='******') if save_db: dm = DataMatrix(database=database) dm.ndims = 0 dm.name = uuid.uuid4().hex dm.features_hash = features_hash dm.aggregations_hash = aggregations_hash dm.save() task = Task(user=user, target='{}:{}'.format(DataMatrix.__name__, dm.id)) task.save() dm.task = task dm.save() else: task = NonDbTask(user=user) segments = Segment.objects.filter(audio_file__database=database) sids = segments.values_list('id', flat=True) task.sids = sids task.features_hash = features_hash task.aggregations_hash = aggregations_hash if celery: extract_database_measurements.delay(task.id) else: extract_database_measurements(task, force=True)
def form_valid(self, form): post_data = self.request.POST user = self.request.user form_data = form.cleaned_data name = form_data.get('name', None) dmid = form_data.get('data_matrix', None) has_error = False is_recreating = False if dmid: dm = get_or_error(DataMatrix, dict(id=dmid)) is_recreating = True else: if 'database' in post_data: database_id = int(post_data['database']) database = get_or_error(Database, dict(id=int(database_id))) if DataMatrix.objects.filter(database=database, name=name).exists(): form.add_error('name', 'This name is already taken') has_error = True dm = DataMatrix(database=database) else: database_id = get_or_error(post_data, 'tmpdb') database = get_or_error(TemporaryDatabase, dict(id=int(database_id))) if DataMatrix.objects.filter(tmpdb=database, name=name).exists(): form.add_error('name', 'This name is already taken') has_error = True dm = DataMatrix(tmpdb=database) if has_error: context = self.get_context_data() context['form'] = form rendered = render_to_string( 'partials/feature-selection-form.html', context=context) return HttpResponse( json.dumps( dict(message=dict(success=False, html=rendered)))) features = form_data['features'].order_by('id') aggregations = form_data['aggregations'].order_by('id') dm.name = name dm.ndims = 0 dm.features_hash = '-'.join( list(map(str, features.values_list('id', flat=True)))) dm.aggregations_hash = '-'.join( list(map(str, aggregations.values_list('id', flat=True)))) dm.save() task = Task(user=user, target='{}:{}'.format(DataMatrix.__name__, dm.id)) task.save() dm.task = task dm.save() delay_in_production(extract_database_measurements, task.id) if is_recreating: ord_tasks = recreate_associated_ordination_tasks(dmid, user) for task in ord_tasks: delay_in_production(construct_ordination, task.id) sim_tasks = recreate_associated_similarity_tasks(dmid, user) for task in sim_tasks: delay_in_production(calculate_similarity, task.id) context = self.get_context_data() context['task'] = task rendered = render_to_string('partials/feature-extraction-tasks.html', context=context) return HttpResponse( json.dumps(dict(message=dict(success=True, html=rendered))))
def handle(self, *args, **options): user = User.objects.get(username='******') task = Task(user=user) task.save() celery_task_test.delay(task.id)