Exemple #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
    })
Exemple #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"})
def create_dummy_experiment(experiment_id, start_time=None):
    label = "dummy_%d" % experiment_id
    return Experiment(
        name = label,
        controller = label,
        start_time = start_time
    )
Exemple #4
0
            def post(self, experiment_uuid):

                experiment_uuid = str(uuid.uuid4())

                pipeline_path = pipeline_uuid_to_path(
                    request.json["pipeline_uuid"],
                    request.json["project_uuid"])

                new_ex = Experiment(
                    uuid=experiment_uuid,
                    name=request.json["name"],
                    pipeline_uuid=request.json["pipeline_uuid"],
                    project_uuid=request.json["project_uuid"],
                    pipeline_name=request.json["pipeline_name"],
                    pipeline_path=pipeline_path,
                    strategy_json="{}",
                    draft=request.json["draft"],
                )

                db.session.add(new_ex)
                db.session.commit()

                create_experiment_directory(
                    experiment_uuid,
                    request.json["pipeline_uuid"],
                    request.json["project_uuid"],
                )

                return experiment_schema.dump(new_ex)
Exemple #5
0
def add_experiment():
    form = AddExperimentForm()
    if form.validate_on_submit():
        experiment = Experiment(description=form.description.data,
                                owner=current_user,
                                columns='')
        db.session.add(experiment)
        db.session.commit()
        return redirect(url_for('main.experiment',
                                experiment_id=experiment.id))
    # TODO flash an error?
    return redirect(url_for('main.index'))
Exemple #6
0
            def post(self, experiment_uuid):

                new_ex = Experiment(
                    uuid=experiment_uuid,
                    name=request.json["name"],
                    pipeline_uuid=request.json["pipeline_uuid"],
                    pipeline_name=request.json["pipeline_name"],
                    strategy_json=request.json["strategy_json"],
                    draft=request.json["draft"],
                )

                db.session.add(new_ex)
                db.session.commit()

                return experiment_schema.dump(new_ex)
    def load_experiment_config(self, required_keys, experiment_name):
        experiment_config = self.get_experiment_config(required_keys,
                                                       experiment_name)
        experiment = self.db_session.query(Experiment).filter(
            Experiment.name == experiment_name).first()
        if (experiment is None):

            condition_keys = []

            ## LOAD RANDOMIZED CONDITIONS (see CivilServant-Analysis)
            for condition in experiment_config['conditions'].values():
                with open(
                        os.path.join(BASE_DIR, "config", "experiments",
                                     condition['randomizations']), "r") as f:
                    reader = csv.DictReader(f)
                    randomizations = []
                    for row in reader:
                        randomizations.append(row)
                        condition['randomizations'] = randomizations

            experiment = Experiment(
                name=experiment_name,
                controller=self.__class__.__name__,
                start_time=parser.parse(experiment_config['start_time']),
                end_time=parser.parse(experiment_config['end_time']),
                settings_json=json.dumps(experiment_config))
            self.db_session.add(experiment)
            self.db_session.commit()

        ### SET UP INSTANCE PROPERTIES
        self.experiment = experiment
        self.experiment_settings = json.loads(self.experiment.settings_json)

        self.experiment_name = experiment_name

        self.subreddit = experiment_config['subreddit']
        self.subreddit_id = experiment_config['subreddit_id']
        self.username = experiment_config['username']
        self.max_eligibility_age = experiment_config['max_eligibility_age']
        self.min_eligibility_age = experiment_config['min_eligibility_age']

        ## LOAD SUBREDDIT PAGE CONTROLLER
        self.subreddit_page_controller = SubredditPageController(
            self.subreddit, self.db_session, self.r, self.log)

        # LOAD EVENT HOOKS
        self.load_event_hooks(experiment_config)
Exemple #8
0
            def post(self, experiment_uuid):

                if Experiment.query.filter(
                        Experiment.uuid == experiment_uuid).count() > 0:
                    raise ExperimentUuidInUse()

                new_ex = Experiment(
                    uuid=experiment_uuid,
                    name=request.json["name"],
                    pipeline_uuid=request.json["pipeline_uuid"],
                    pipeline_name=request.json["pipeline_name"],
                    strategy_json=request.json["strategy_json"],
                    draft=request.json["draft"],
                )

                db.session.add(new_ex)
                db.session.commit()

                return experiment_schema.dump(new_ex)
Exemple #9
0
    def load_experiment_config(self, required_keys, experiment_name):
        experiment_config = self.get_experiment_config(required_keys, experiment_name)
        experiment = self.db_session.query(Experiment).filter(Experiment.name == experiment_name).first()
        if(experiment is None):

            condition_keys = []

            ## LOAD RANDOMIZED CONDITIONS (see CivilServant-Analysis)
            for condition in experiment_config['conditions'].values():
                with open(os.path.join(BASE_DIR, "config", "experiments", condition['randomizations']), "r") as f:
                    reader = csv.DictReader(f)
                    randomizations = []
                    for row in reader:
                        randomizations.append(row)
                        condition['randomizations']  = randomizations

            experiment = Experiment(
                name = experiment_name,
                controller = self.__class__.__name__,
                start_time = parser.parse(experiment_config['start_time']),
                end_time = parser.parse(experiment_config['end_time']),
                settings_json = json.dumps(experiment_config)
            )
            self.db_session.add(experiment)
            self.db_session.commit()
        
        ### SET UP INSTANCE PROPERTIES
        self.experiment = experiment
        self.experiment_settings = json.loads(self.experiment.settings_json)
        

        self.experiment_name = experiment_name
        self.dry_run = experiment_config.get("dry_run", False)

        for key in ['subreddit', 'subreddit_id', 'shadow_subreddit', 'shadow_subreddit_id', 
                    'username', 'max_eligibility_age', 'min_eligibility_age']:
            if key in required_keys:
                setattr(self, key, experiment_config[key])

        # LOAD EVENT HOOKS
        if 'event_hooks' in required_keys:
            self.load_event_hooks(experiment_config)
Exemple #10
0
    def experiments_create():

        experiment_uuid = str(uuid.uuid4())

        new_ex = Experiment(
            uuid=experiment_uuid,
            name=request.json["name"],
            pipeline_uuid=request.json["pipeline_uuid"],
            project_uuid=request.json["project_uuid"],
            pipeline_name=request.json["pipeline_name"],
            strategy_json="",
            draft=True,
        )

        db.session.add(new_ex)
        db.session.commit()

        create_experiment_directory(experiment_uuid,
                                    request.json["pipeline_uuid"],
                                    request.json["project_uuid"])

        return jsonify(experiment_schema.dump(new_ex))
Exemple #11
0
    def test_save_experiment(self):
        dev_id = 'dev_id_01'
        device = Device(id=dev_id,
                        device_class='PSI',
                        device_type='PBR',
                        address='home')
        self.DM.insert(device, Device)

        current_time = now()
        experiment = Experiment(id=1,
                                dev_id=dev_id,
                                start=current_time,
                                description=None,
                                end=None)

        # empty
        exists = Experiment.query.filter_by(dev_id=dev_id).first()
        self.assertIsNone(exists)

        # new experiment
        self.DM.save_experiment(dev_id, current_time)
        result = Experiment.query.filter_by(dev_id=dev_id).first()
        self.assertEqual(result, experiment)
Exemple #12
0
    def experiments_create():

        experiment_uuid = str(uuid.uuid4())

        if Experiment.query.filter(
                Experiment.uuid == experiment_uuid).count() > 0:
            raise ExperimentUuidInUse()

        new_ex = Experiment(
            uuid=experiment_uuid,
            name=request.json["name"],
            pipeline_uuid=request.json["pipeline_uuid"],
            pipeline_name=request.json["pipeline_name"],
            strategy_json="",
            draft=True,
        )

        db.session.add(new_ex)
        db.session.commit()

        create_experiment_directory(experiment_uuid,
                                    request.json["pipeline_uuid"])

        return experiment_schema.dump(new_ex)
    def __init__(self, experiment_name, db_session, r, log):
        self.db_session = db_session
        self.log = log

        required_keys = ['subreddit', 'ama_comment_text', "nonama_comment_text",
                         'username', 'start_time', 'ama_conditions',
                         'nonama_conditions', 'max_eligibility_age']

        experiment_file_path = os.path.join(BASE_DIR, "config", "experiments", experiment_name) + ".yml"
        with open(experiment_file_path, 'r') as f:
            try:
                experiment_config_all = yaml.load(f)
            except yaml.YAMLError as exc:
                self.log.error("Failure loading experiment yaml {0}".format(experiment_file_path), str(exc))
                sys.exit(1)
        if(ENV not in experiment_config_all.keys()):
            log.error("Cannot find experiment settings for {0} in {1}".format(ENV, experiment_file_path))
            sys.exit(1)

        experiment_config = experiment_config_all[ENV]
        for key in required_keys:
            if key not in experiment_config.keys():
                self.log.error("Value missing from {0}: {1}".format(experiment_file_path, key))
                sys.exit(1)

        experiment = self.db_session.query(Experiment).filter(Experiment.name == experiment_name).first()
        if(experiment is None):
            ## LOAD RANDOMIZED CONDITIONS (see CivilServant-Analysis)
            with open(os.path.join(BASE_DIR, "config", "experiments", experiment_config['ama_conditions']), "r") as f:
                reader = csv.DictReader(f)
                ama_conditions = []
                for row in reader:
                    ama_conditions.append(row)

            with open(os.path.join(BASE_DIR, "config", "experiments", experiment_config['nonama_conditions']), "r") as f:
                reader = csv.DictReader(f)
                nonama_conditions = []
                for row in reader:
                    nonama_conditions.append(row)
            
            settings = {
                "ama_comment_text": experiment_config['ama_comment_text'],
                "nonama_comment_text": experiment_config['nonama_comment_text'],
                "username": experiment_config['username'],
                "subreddit": experiment_config['subreddit'],
                "subreddit_id": experiment_config['subreddit_id'],
                "max_eligibility_age": experiment_config['max_eligibility_age'],
                "next_ama_condition":  0,
                "ama_conditions": ama_conditions,
                "next_nonama_condition": 0,
                "nonama_conditions": nonama_conditions
            }
            experiment = Experiment(
                name = experiment_name,
                controller = "StickyCommentExperimentController",
                start_time = parser.parse(experiment_config['start_time']),
                end_time = parser.parse(experiment_config['end_time']),
                settings_json = json.dumps(settings)
            )
            self.db_session.add(experiment)
            self.db_session.commit()
        
        ### SET UP INSTANCE PROPERTIES
        self.experiment = experiment
        experiment_settings = json.loads(self.experiment.settings_json)
        
        self.ama_conditions        = experiment_settings['ama_conditions']
        self.nonama_conditions     = experiment_settings['nonama_conditions']
        self.next_ama_condition    = experiment_settings['next_ama_condition']
        self.next_nonama_condition = experiment_settings['next_nonama_condition']

        self.experiment_name = experiment_name

        self.subreddit = experiment_config['subreddit']
        self.subreddit_id = experiment_config['subreddit_id']
        self.username = experiment_config['username']
        self.ama_comment_text = experiment_config['ama_comment_text']
        self.nonama_comment_text = experiment_config['nonama_comment_text']
        self.max_eligibility_age = experiment_config['max_eligibility_age']
        self.min_eligibility_age = 120 ##TODO: refactor this into the configuration file
        self.r = r

        ## LOAD SUBREDDIT PAGE CONTROLLER
        self.subreddit_page_controller = SubredditPageController(self.subreddit,self.db_session, self.r, self.log)