def get_peer()

in clutrr/utils/data_backend.py [0:0]


    def get_peer(self, worker_id='test', relation_length=2):
        """
        Get an annotation which is not done by the current worker, and which isn't reviewed
        Also, no need to choose relation of length 1
        With some probability, choose our test records
        :param worker_id:
        :param relation_length:
        :return: None if no suitable candidate found
        """
        using_test = False
        record = None
        if relation_length == 1:
            relation_length = random.choice([2,3])
        print("Choosing records with test probability {}".format(self.test_prob))
        if random.uniform(0,1) <= self.test_prob:
            using_test = True
            record_cursor = self.mturk.find({'worker_id': self.test_worker, 'relation_length': relation_length},
                                            sort=[("used",1)])
            print("Choosing a test record to annotate")
        else:
            record_cursor = self.mturk.find({'worker_id': {"$nin": [worker_id, self.test_worker]}, 'relation_length': relation_length, 'used':1})
            print("Choosing a review record to annotate")
        rec_found = False
        if record_cursor.count() > 0:
            rec_found = True
        print("Found a record to annotate")
        if not using_test and not rec_found:
            # if no candidate peer is found, default to test
            record_cursor = self.mturk.find({'worker_id': self.test_worker, 'relation_length': relation_length},
                    sort=[("used",1)])
            print("No records found, reverting back to test")
        if record_cursor.count() > 0:
            record = random.choice(list(record_cursor))
        if not record:
            # did not find either candidate peer nor test, raise error
            raise FileNotFoundError("no candidate found in db")
        return record