def get_queryset(self): queryset = self.queryset main_dataset_id = self.request.query_params.get('main', None) if main_dataset_id is not None: query_set = queryset.filter( Q(main_dataset_id=main_dataset_id, training_data=True), q_shared_or_user_is_owner(self.request)) else: query_set = queryset.filter( Q(training_data=True), q_shared_or_user_is_owner(self.request)) return query_set
def post(self, request, dataset_id): dataset = DataSet.objects.filter( Q(id=dataset_id, training_data=False), q_shared_or_user_is_owner(request)).first() if dataset is None: messages.error(request, dataset_not_found_msg(dataset_id)) response = Response({}, status=status.HTTP_404_NOT_FOUND) response['Location'] = reverse('vadetisweb:index') return response serializer = InjectionSerializer(context={ 'dataset_selected': dataset_id, 'request': request }, data=request.data) if serializer.is_valid(): data = {} settings = get_settings(request) df_inject, df_inject_class = anomaly_injection( serializer.validated_data) type = get_type_from_dataset_json( serializer.validated_data['dataset_series_json']) data['series'] = dataset_to_json(dataset, df_inject, df_inject_class, settings, type) return Response(data, status=status.HTTP_200_OK) else: json_messages = [] json_message_utils.error(json_messages, 'Form was invalid') return response_invalid_form(serializer, json_messages)
def get(self, request, dataset_id): dataset = DataSet.objects.filter( Q(id=dataset_id, type=REAL_WORLD), q_shared_or_user_is_owner(request)).first() if dataset is None: messages.error(request, dataset_not_found_msg(dataset_id)) return redirect('vadetisweb:detection_real_world_datasets') selected_button = get_highcharts_range_button_preselector( dataset.granularity) detection_serializer = AlgorithmSerializer( context={'dataset': dataset}) injection_serializer = InjectionSerializer(context={ 'dataset_selected': dataset_id, 'request': request }) threshold_serializer = ThresholdSerializer() return Response( { 'dataset': dataset, 'selected_button': selected_button, 'detection_serializer': detection_serializer, 'injection_serializer': injection_serializer, 'threshold_serializer': threshold_serializer, }, status=status.HTTP_200_OK)
def get(self, request, dataset_id): dataset = DataSet.objects.filter( Q(id=dataset_id, type=SYNTHETIC), q_shared_or_user_is_owner(request)).first() if dataset is None: messages.error(request, dataset_not_found_msg(dataset_id)) return redirect('vadetisweb:recommendation_synthetic_datasets') recommendation_serializer = RecommendationSerializer( context={'dataset': dataset}) selected_button = get_highcharts_range_button_preselector( dataset.granularity) return Response( { 'dataset': dataset, 'selected_button': selected_button, 'recommendation_serializer': recommendation_serializer, }, status=status.HTTP_200_OK)
def post(self, request, dataset_id, format=None): portlet_serializer = BasePortletSerializer(data=request.data) dataset = DataSet.objects.filter( Q(id=dataset_id), q_shared_or_user_is_owner(request)).first() if dataset is None: messages.error(request, dataset_not_found_msg(dataset_id)) response = Response({}, status=status.HTTP_404_NOT_FOUND) response['Location'] = reverse('vadetisweb:index') return response if portlet_serializer.is_valid( ) and request.accepted_renderer.format == 'html': # rendered template: validated_data = portlet_serializer.validated_data injection_serializer = InjectionSerializer(context={ 'dataset_selected': dataset_id, 'request': request }) return Response( { 'id': validated_data['id'], 'title': validated_data['title'], 'serializer_formid': 'anomaly_injection_form', 'serializer_url': reverse('vadetisweb:injection_anomaly', args=[dataset.id]), 'serializer': injection_serializer, 'serializer_submit_label': 'Update', }, status=status.HTTP_200_OK) else: return Response({}, status=status.HTTP_400_BAD_REQUEST)
def get_queryset(self): queryset = self.queryset query_set = queryset.filter(Q(type=REAL_WORLD, training_data=False), q_shared_or_user_is_owner(self.request)) return query_set
def post(self, request, dataset_id): dataset = DataSet.objects.filter( Q(id=dataset_id, training_data=False), q_shared_or_user_is_owner(request)).first() if dataset is None: messages.error(request, dataset_not_found_msg(dataset_id)) response = Response({}, status=status.HTTP_404_NOT_FOUND) response['Location'] = reverse('vadetisweb:index') return response serializer = RecommendationSerializer(context={'dataset': dataset}, data=request.data) if serializer.is_valid(): algorithms = serializer.validated_data['algorithm'] maximize_score = serializer.validated_data['maximize_score'] data = {'recommendations': []} for algorithm in algorithms: default_configuration = get_default_configuration( algorithm, maximize_score, dataset) try: if algorithm == LISA_PEARSON: serializer = LisaPearsonSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = lisa_pearson_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == LISA_DTW_PEARSON: serializer = LisaDtwPearsonSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = lisa_dtw_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == LISA_SPATIAL: serializer = LisaGeoDistanceSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = lisa_geo_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == RPCA_HUBER_LOSS: serializer = RPCAMEstimatorLossSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = rpca_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == HISTOGRAM: serializer = HistogramSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = histogram_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == CLUSTER_GAUSSIAN_MIXTURE: serializer = ClusterSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = cluster_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == SVM: serializer = SVMSerializer(context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = svm_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) elif algorithm == ISOLATION_FOREST: serializer = IsolationForestSerializer( context={ 'dataset_selected': dataset_id, 'dataset_series_json_required': False, 'request': request }, data=default_configuration) if serializer.is_valid(): info = isolation_forest_recommendation( dataset.dataframe, dataset.dataframe_class, serializer.validated_data) data['recommendations'].append({ 'algorithm': algorithm, 'info': info, 'conf': serializer.data }) except Exception as e: logging.error(e) return Response({}, status=status.HTTP_400_BAD_REQUEST) return Response(data, status=status.HTTP_200_OK) else: json_messages = [] json_message_utils.error(json_messages, 'Invalid request') return response_invalid_form(serializer, json_messages)