def save_data(self):
        convo_finished = True
        for ag in self.agents:
            if (ag.hit_is_abandoned or ag.hit_is_returned or ag.disconnected
                    or ag.hit_is_expired):
                convo_finished = False
        if not convo_finished:
            if not self.second_resp:
                ag.personality_generator.push_personality(self.pers_idx)
            ag.example_generator.push_example(self.example_num)
            print('\n**Push personality {} back to stack. **\n'.format(
                self.pers_idx))
            print('\n**Push image {} back to stack. **\n'.format(
                self.example_num))
        if not self.second_resp:
            self.agents[0].personality_generator.save_idx_stack()
        self.agents[0].example_generator.save_idx_stack()
        data_path = self.opt['data_path']
        if not os.path.exists(data_path):
            os.makedirs(data_path)
        if convo_finished:
            filename = os.path.join(
                data_path,
                '{}_{}_{}.pkl'.format(
                    time.strftime('%Y%m%d-%H%M%S'),
                    np.random.randint(0, 1000),
                    self.task_type,
                ),
            )
        else:
            filename = os.path.join(
                data_path,
                '{}_{}_{}_incomplete.pkl'.format(
                    time.strftime('%Y%m%d-%H%M%S'),
                    np.random.randint(0, 1000),
                    self.task_type,
                ),
            )
        key = 'second_response' if self.second_resp else 'first_response'
        responses = [d[key] for d in self.data]

        if len(responses) >= 2:
            c = responses[0]
            if _exact_match(c, responses[1:]):
                self.exact_match = True
        data_to_save = [
            d for d in self.data if d['contains_offensive_language'] is None
        ]
        pickle.dump(
            {
                'data': data_to_save,
                'worker': self.agents[0].worker_id,
                'hit_id': self.agents[0].hit_id,
                'assignment_id': self.agents[0].assignment_id,
                'exact_match': self.exact_match,
            },
            open(filename, 'wb'),
        )
        print('{}: Data successfully saved at {}.'.format(
            self.world_tag, filename))
Esempio n. 2
0
    def save_data(self):
        convo_finished = True
        for ag in self.agents:
            if (ag.hit_is_abandoned or ag.hit_is_returned or ag.disconnected
                    or ag.hit_is_expired):
                convo_finished = False
        if not convo_finished:
            if not self.multiple_personality:
                ag.personality_generator.push_personality(self.pers_idx)
                ag.image_generator.push_image(self.image_hash)
                print('\n**Push personality {} back to stack. **\n'.format(
                    self.pers_idx))
                print('\n**Push image {} back to stack. **\n'.format(
                    self.image_hash))
            else:
                ag.personality_and_image_generator.push_pair(self.pair_idx)
                print('\n**Push pair {} back to stack. **\n'.format(
                    self.pair_idx))
        self.agents[0].personality_generator.save_idx_stack()
        self.agents[0].image_generator.save_idx_stack()
        self.agents[0].personality_and_image_generator.save_idx_stack()
        data_path = self.opt['data_path']
        if not os.path.exists(data_path):
            os.makedirs(data_path)
        if convo_finished:
            filename = os.path.join(
                data_path,
                '{}_{}_{}.pkl'.format(time.strftime('%Y%m%d-%H%M%S'),
                                      np.random.randint(0, 1000),
                                      self.task_type))
        else:
            filename = os.path.join(
                data_path, '{}_{}_{}_incomplete.pkl'.format(
                    time.strftime('%Y%m%d-%H%M%S'), np.random.randint(0, 1000),
                    self.task_type))
        comments = [d['comment'] for d in self.data]

        if len(comments) >= 2:
            c = comments[0]
            if _exact_match(c, comments[1:]):
                self.exact_match = True

        pickle.dump(
            {
                'data': self.data,
                'worker': self.agents[0].worker_id,
                'hit_id': self.agents[0].hit_id,
                'assignment_id': self.agents[0].assignment_id,
                'exact_match': self.exact_match,
                'task_eval': self.eval
            }, open(filename, 'wb'))
        print('{}: Data successfully saved at {}.'.format(
            self.world_tag, filename))