def create_input_output(self, num_courses, num_responses):
        """
        Create a set of inputs and outputs for the given number of courses and responses.

        Both the returned 'inputs' and 'expected' dicts are keyed by course_id.
        """
        inputs = {}
        expected = {}
        report_fields = ProblemResponseRecord.get_fields().keys()

        idx = 0
        for course_idx in range(num_courses):
            course_id = self.course_id.format(course_idx)
            for _ in range(num_responses):
                idx += 1
                input_row = (
                    course_id,
                    'answer_{}'.format(idx),
                    'problem_{}'.format(idx),
                    'user_{}'.format(idx),
                    'problem {}'.format(idx),
                    'question {}'.format(idx),
                    '4.0',
                    '10.0',
                    'False',
                    'answer {}'.format(idx),
                    '20',
                    self.DATE,
                    self.DATE,
                    '',
                    '{}'.format(idx),
                )

                # First row is the header: field names
                if course_id not in expected:
                    inputs[course_id] = []
                    expected[course_id] = [
                        ','.join(report_fields),
                    ]

                inputs[course_id].append(input_row)
                expected[course_id].append(','.join(input_row))

        return inputs, expected
Exemple #2
0
    def create_input_output(self, num_courses, num_responses):
        """
        Create a set of inputs and outputs for the given number of courses and responses.

        Both the returned 'inputs' and 'expected' dicts are keyed by course_id.
        """
        inputs = {}
        expected = {}
        report_fields = ProblemResponseRecord.get_fields().keys()

        idx = 0
        for course_idx in range(num_courses):
            course_id = self.course_id.format(course_idx)
            for _ in range(num_responses):
                idx += 1
                input_row = (
                    course_id,
                    'answer_{}'.format(idx),
                    'problem_{}'.format(idx),
                    'user_{}'.format(idx),
                    'problem {}'.format(idx),
                    'question {}'.format(idx),
                    '4.0',
                    '10.0',
                    'False',
                    'answer {}'.format(idx),
                    '20',
                    self.DATE,
                    self.DATE,
                    '',
                    '{}'.format(idx),
                )

                # First row is the header: field names
                if course_id not in expected:
                    inputs[course_id] = []
                    expected[course_id] = [
                        ','.join(report_fields),
                    ]

                inputs[course_id].append(input_row)
                expected[course_id].append(','.join(input_row))

        return inputs, expected
    def create_input_output(self, attempts):
        """Returns an array of input problem attempts, and the expected output tuple."""

        # Incremented as we loop through the attempts
        total_attempts = 0
        first_attempt_date = None
        latest_attempt = {}

        inputs = []
        for idx, attempt in enumerate(attempts):
            answer_id = 'answer_{}'.format(idx)
            time = '2013-01-01 00:{0:02d}:00.0'.format(idx)
            correctness = None
            if attempt['correct'] is None:
                correctness = 'unknown'
            elif attempt['correct']:
                correctness = 'correct'
            else:
                correctness = 'incorrect'

            # Append the problem data to the inputs list
            problem_data = {
                'username': self.USERNAME,
                'context': {
                    'course_id': self.course_id,
                    'module': {
                        'display_name': self.problem,
                    },
                },
                'problem_id': self.problem_id,
                'attempts': idx + 1,
                'submission': {
                    answer_id: {
                        'question': self.question,
                    }
                },
                'answers': {
                    answer_id: attempt['answer'],
                },
                'correct_map': {
                    answer_id: {
                        'correctness': correctness,
                    }
                },
                'grade': 0,
                'max_grade': 1,
                'time': time,
            }
            inputs.append((time, json.dumps(problem_data)))

            # Update the summary data, and keep track of the "latest" attempt
            total_attempts += 1
            if not first_attempt_date or time < first_attempt_date:
                first_attempt_date = time
            if not latest_attempt or time > latest_attempt['last_attempt_date']:
                latest_attempt = attempt.copy()
                latest_attempt.update(dict(
                    answer_id=answer_id,
                    last_attempt_date=time,
                ))

        # Construct the expected problem response record
        date_field = DateTimeField()
        expected = ProblemResponseRecord(
            course_id=self.course_id,
            answer_id=latest_attempt['answer_id'],
            username=self.USERNAME,
            problem_id=self.problem_id,
            problem=self.problem,
            question=self.question,
            score=0,
            max_score=1,
            correct=latest_attempt['correct'],
            answer=latest_attempt.get('expected_answer', latest_attempt.get('answer')),
            total_attempts=total_attempts,
            first_attempt_date=date_field.deserialize_from_string(first_attempt_date),
            last_attempt_date=date_field.deserialize_from_string(latest_attempt['last_attempt_date']),
            location='',
            sort_idx=0,
        )
        # randomize the inputs, because their order shouldn't matter.
        random.shuffle(inputs)
        return (inputs, (expected.to_string_tuple(),))
Exemple #4
0
    def create_input_output(self, attempts):
        """Returns an array of input problem attempts, and the expected output tuple."""

        # Incremented as we loop through the attempts
        total_attempts = 0
        first_attempt_date = None
        latest_attempt = {}

        inputs = []
        for idx, attempt in enumerate(attempts):
            answer_id = 'answer_{}'.format(idx)
            time = '2013-01-01 00:{0:02d}:00.0'.format(idx)
            correctness = None
            if attempt['correct'] is None:
                correctness = 'unknown'
            elif attempt['correct']:
                correctness = 'correct'
            else:
                correctness = 'incorrect'

            # Append the problem data to the inputs list
            problem_data = {
                'username': self.USERNAME,
                'context': {
                    'course_id': self.course_id,
                    'module': {
                        'display_name': self.problem,
                    },
                },
                'problem_id': self.problem_id,
                'attempts': idx + 1,
                'submission': {
                    answer_id: {
                        'question': self.question,
                    }
                },
                'answers': {
                    answer_id: attempt['answer'],
                },
                'correct_map': {
                    answer_id: {
                        'correctness': correctness,
                    }
                },
                'grade': 0,
                'max_grade': 1,
                'time': time,
            }
            inputs.append((time, json.dumps(problem_data)))

            # Update the summary data, and keep track of the "latest" attempt
            total_attempts += 1
            if not first_attempt_date or time < first_attempt_date:
                first_attempt_date = time
            if not latest_attempt or time > latest_attempt['last_attempt_date']:
                latest_attempt = attempt.copy()
                latest_attempt.update(
                    dict(
                        answer_id=answer_id,
                        last_attempt_date=time,
                    ))

        # Construct the expected problem response record
        date_field = DateTimeField()
        expected = ProblemResponseRecord(
            course_id=self.course_id,
            answer_id=latest_attempt['answer_id'],
            username=self.USERNAME,
            problem_id=self.problem_id,
            problem=self.problem,
            question=self.question,
            score=0,
            max_score=1,
            correct=latest_attempt['correct'],
            answer=latest_attempt.get('expected_answer',
                                      latest_attempt.get('answer')),
            total_attempts=total_attempts,
            first_attempt_date=date_field.deserialize_from_string(
                first_attempt_date),
            last_attempt_date=date_field.deserialize_from_string(
                latest_attempt['last_attempt_date']),
            location='',
            sort_idx=0,
        )
        # randomize the inputs, because their order shouldn't matter.
        random.shuffle(inputs)
        return (inputs, (expected.to_string_tuple(), ))