Пример #1
0
    def save_file(self, file_path, file_name, dest_dir):
        if file_path and not os.path.exists(file_path):
            raise Exception("File %s does not found!" % file_path)

        mdf = self.__get_metadata()

        dir_obj = mdf.find(dest_dir)
        if not dir_obj.is_dir():
            raise Exception("%s is a file!" % dest_dir)

        if file_path:
            file_size = os.stat(file_path).st_size
        else:
            file_size = 0

        if isinstance(file_name, FileMD):
            file_md = file_name
            file_md.size = file_size
        else:
            file_md = FileMD(file_name, file_size)

        file_md.parent_dir = dir_obj

        empty = file_size == 0
        if not empty:
            logger.info("Saving file %s to fabnet" % file_md.name)
            self.put_manager.put_file(file_md, file_path)
Пример #2
0
    def rankings(self, worker_id):
        data_file_path = self.data_folder_path + '/' + worker_id + '.csv'
        score = 0
        weighted_score = 0
        rank = 'Unranked'
        if os.path.exists(data_file_path):
            logger.info("'%s' does not exist", data_file_path)
            score = read_last_row(data_file_path)["score"]
            weighted_score = self.simple_weighted_score_formula(worker_id)

        rankings, num_scores = self.get_top_ten_rankings()
        try:
            rank = next(i + 1 for i, v in enumerate(rankings)
                        if worker_id == v[0])
        except StopIteration:
            logger.warn("No matching score found")
            rank = 1

        return {
            "rankings": rankings,
            "score": score,
            "rank": rank,
            "weighted_score": weighted_score,
            "num_scores": num_scores
        }
Пример #3
0
    def generate_trials(self, worker_id, num_categories=None):
        image_folders = os.listdir(self.images_folder_path)
        logger.info("num_categories: %s", num_categories)
        images = get_next_min_keys(
            update_images_counts_file_lock, self.image_counts_file_path,
            image_folders, num_categories
            if num_categories is not None else len(image_folders))
        logger.info("images %s", images)
        trials = []
        trial_number = 0
        for image in images:
            image_trials = []
            for image_file in os.listdir(self.images_folder_path + '/' +
                                         image):
                image_trials.append({
                    'category':
                    image,
                    'image':
                    image_file,
                    'trial_number':
                    trial_number,
                    'keys':
                    "1,2,3,4,5",
                    'labels':
                    "1 (Very typical),2,3,4,5 (Very atypical)"
                })
                trial_number += 1
            trials.extend(image_trials)

        return trials
Пример #4
0
    def data(self, worker_id, **data):
        if not os.path.exists(self.data_folder_path):
            os.mkdir(self.data_folder_path)

        data_file_path = self.data_folder_path + '/' + worker_id + '.csv'

        you_response = int(data['you_choice'])
        others_response = int(data['others_choice'])

        score = 0
        reward = 0
        mean = None
        if os.path.exists(data_file_path):
            last_row = read_last_row(data_file_path)
            score = int(last_row['score'])

        concept = data['concept']
        anchor_neg = data['anchor_neg']
        anchor_pos = data['anchor_pos']

        num_responses = 1
        response_counts = {}
        choice_counts = [0, 0, 0, 0, 0, 0, 0]
        key = ','.join([concept, anchor_neg, anchor_pos])
        with update_responses_file_lock:
            if os.path.exists(self.concept_responses):
                response_counts = read_key_value(self.concept_responses)
                if key in response_counts:
                    choice_counts = [
                        int(count) for count in response_counts[key].split(",")
                    ]
                logger.info('choice_counts: %s', choice_counts)
                reward, mean = self.compute_reward(others_response,
                                                   choice_counts)
                score += reward
            choice_counts[int(you_response) - 1] += 1
            response_counts[key] = ','.join(
                [str(count) for count in choice_counts])
            write_key_value(self.concept_responses, response_counts)

        logger.info('num_responses: ' + str(num_responses))
        # Assumes score and reward will always be ints
        data['score'] = int(score)
        data['reward'] = int(reward)

        append_to_csv(data_file_path, data)

        if not os.path.exists(self.trial_counts_file_path):
            self.create_trial_counts_file()

        increment_key_count(update_images_counts_file_lock,
                            self.trial_counts_file_path,
                            self.get_trial_key(data))

        return {"score": score, "reward": reward, "mean": mean}
Пример #5
0
    def move(self, s_path, d_path):
        logger.info("mv %s to %s" % (s_path, d_path))
        mdf, d_obj, source, new_name, dst_path = self._cpmv_int(s_path, d_path)

        base_path, s_name = os.path.split(s_path)
        mdf.find(base_path).remove(s_name)

        if new_name:
            source.name = new_name

        d_obj.append(source)
        self.__save_metadata()
Пример #6
0
    def register_user(self):
        if self.metadata:
            logger.warning("Trying register user in fabnet, but it is already registered!")
            return

        metadata = self.fabnet_gateway.get(self.metadata_key)
        if metadata is not None:
            logger.warning("Trying register user in fabnet, but it is already registered!")
            return

        mdf = MetadataFile()
        mdf.load("{}")
        self.fabnet_gateway.put(mdf.dump(), key=self.metadata_key)
        self.metadata = mdf
        logger.info("User is registered in fabnet successfully")
Пример #7
0
    def copy(self, s_path, d_path):
        logger.info("cp %s to %s" % (s_path, d_path))
        mdf, d_obj, source, new_name, dst_path = self._cpmv_int(s_path, d_path)
        if not new_name:
            new_name = source.name

        if source.is_file():
            fhdl = self.load_file(s_path)
            try:
                self.save_file(fhdl.name, new_name, dst_path)
            finally:
                fhdl.close()
        else:
            dst_dir = os.path.join(dst_path, new_name)
            self.mkdir(dst_dir)
            for i_name, dummy in source.items():
                self.copy(os.path.join(s_path, i_name), dst_dir)

        self.__save_metadata()
Пример #8
0
    def data(self, worker_id, **data):
        if not os.path.exists(self.data_folder_path):
            os.mkdir(self.data_folder_path)

        data_file_path = self.data_folder_path + '/' + worker_id + '.csv'

        response = data['response']

        score = 0
        if os.path.exists(data_file_path):
            last_row = read_last_row(data_file_path)
            score = int(last_row['score'])

        image = data['stim_to_show']

        num_responses = 1
        num_same_responses = 1
        response_counts = {}
        with update_responses_file_lock:
            if os.path.exists(self.vcs_names_file_path):
                rows = read_rows(self.vcs_names_file_path)
                logger.info(rows)
                for row in rows:
                    if image == row['image']:
                        if response == row['naming_response']:
                            num_same_responses += 1
                        num_responses += 1

            append_to_csv(self.vcs_names_file_path, {
                'image': image,
                'naming_response': response
            })

        logger.info('num_same_responses: ' + str(num_same_responses))
        logger.info('num_responses: ' + str(num_responses))
        score += self.compute_score(num_same_responses, num_responses)
        bonus = self.compute_bonus(score)
        data['score'] = score
        data['bonus'] = bonus

        logger.info("num_responses: " + str(num_responses))

        append_to_csv(data_file_path, data)

        return {'score': score, 'bonus': bonus}
Пример #9
0
    def trials(self,
               worker_id,
               new_batch=None,
               max_batch_num=None,
               reset=False):
        if not os.path.exists(self.trials_folder_path):
            os.mkdir(self.trials_folder_path)
        if not os.path.exists(self.batch_num_folder_path):
            os.mkdir(self.batch_num_folder_path)
        trials_file_path = self.trials_folder_path + '/' + worker_id + '.csv'
        demographics_file_path = self.demographics_folder_path + '/' + worker_id + '.csv'
        consent_file_path = self.consent_folder_path + '/' + worker_id + '.txt'
        data_file_path = self.data_folder_path + '/' + worker_id + '_data.csv'
        batch_num_file_path = self.batch_num_folder_path + '/' + worker_id + '.csv'

        if reset or not os.path.exists(trials_file_path):
            remove_files(demographics_file_path, consent_file_path,
                         data_file_path, trials_file_path, batch_num_file_path)
            trials = self.generate_trials(worker_id)
            num_trials = len(trials)
            write_to_csv(trials_file_path, trials)
            curr_batch_num = 1
            write_key_value(batch_num_file_path, {
                "curr_batch_num": curr_batch_num,
                "max_batch_num": max_batch_num
            })
            completed_demographics = False
            consent_agreed = False
        else:
            batch_num_data = read_key_value(batch_num_file_path)
            curr_batch_num = int(batch_num_data["curr_batch_num"])
            max_batch_num = None if len(
                batch_num_data["max_batch_num"]
            ) == 0 else batch_num_data["max_batch_num"]

            trials = read_rows(trials_file_path)
            num_trials = len(trials)
            completed_demographics = os.path.exists(demographics_file_path)
            consent_agreed = os.path.exists(consent_file_path)

            if new_batch is not None and new_batch:
                trials = self.generate_trials(worker_id, len(trials))
                num_trials += len(trials)
                append_to_csv(trials_file_path, trials)

                curr_batch_num += 1
                logger.info("curr_batch_num: %s", curr_batch_num)
                write_key_value(
                    batch_num_file_path, {
                        "curr_batch_num": curr_batch_num,
                        "max_batch_num": max_batch_num
                    })

            else:
                num_trials = len(trials)
                if os.path.exists(data_file_path):
                    data = read_rows(data_file_path)
                    num_data_rows = len(data)
                    trials = trials[num_data_rows:]

        return {
            "trials": trials,
            "num_trials": num_trials,
            "completed_demographics": completed_demographics,
            "consent_agreed": consent_agreed,
            "curr_batch_num": curr_batch_num,
            "max_batch_num": max_batch_num,
        }