Пример #1
0
def detect_outliers(request):
    """
    Detect outliers end point
    """
    dataset_id = int(request.GET.get("dataset_id"))

    if dataset_id is None:
        return JsonResponse({
            "status": "failure",
            "message": "Dataset id is not provided"
        })

    dataset = Dataset.objects.get(pk=dataset_id)
    file_path = dataset.path
    delete_features = json.loads(dataset.deleted_features)

    # Create a detection experiment and start outlier detection
    process = Process.objects.get(name='Detection')
    process_status = ProcessStatus.objects.get(name='Running')
    experiment = Experiment(dataset=dataset,
                            process=process,
                            process_status=process_status)
    experiment.save()
    results = delayed(detect_all)(os.path.join(settings.MEDIA_ROOT,
                                               file_path), experiment.id,
                                  settings.RESULTS_ROOT, delete_features)
    dask.compute(results)

    return JsonResponse({
        'status': 'success',
        'message': 'Detection started successfully',
        'experiment_id': experiment.id
    })
Пример #2
0
def treat_outliers(request):
    request_obj = json.loads(request.body.decode("utf-8"))
    experiment_id = request_obj["experiment_id"]

    if experiment_id is None:
        return JsonResponse({
            "status": "failure",
            "message": 'Experiment id is missing'
        })

    experiment = Experiment.objects.get(pk=experiment_id)
    results_file_path = os.path.join(settings.RESULTS_ROOT,
                                     experiment.results_path)

    if experiment.process_status_id == 2:
        return JsonResponse({
            'status':
            'success',
            'message':
            'Experiment is still running. Please wait for sometime'
        })

    try:
        with open(results_file_path, "r") as fp:
            outliers = json.load(fp)
            final_outliers = get_final_outliers(outliers)

            process = Process.objects.get(name='Treatment')
            process_status = ProcessStatus.objects.get(name='Running')
            experiment2 = Experiment(dataset=experiment.dataset,
                                     process=process,
                                     process_status=process_status)
            experiment2.save()

            results = delayed(treat)(os.path.join(settings.MEDIA_ROOT,
                                                  experiment2.dataset.path),
                                     final_outliers, experiment2.id,
                                     settings.MEDIA_ROOT)
            dask.compute(results)

            return JsonResponse({
                "status": "success",
                "message": "Outlier treatment started",
                "experiment_id": experiment2.id
            })
    except Exception as e:
        print("Exception:", e)
        return JsonResponse({'status': "failure", "message": "Error"})