Пример #1
0
    def test_properties_exposures(self):
        loader = ConvSccsFeatureDriver(**self.kwargs)
        exposures_ = loader.exposures
        self.assertTrue(
            data_frame_equality(exposures_.subjects, self.exposures.subjects))
        self.assertTrue(
            data_frame_equality(exposures_.events, self.exposures.events))

        mock_dataframe = MagicMock()
        mock_dataframe.take = lambda x: True
        mock_cohort = MagicMock()
        mock_cohort.subjects = mock_dataframe
        mock_cohort.events = mock_dataframe
        with patch.object(
                ConvSccsFeatureDriver,
                "_find_events_not_in_followup_bounds",
                return_value=mock_cohort,
        ) as mock_find_events:
            with patch.object(
                    ConvSccsFeatureDriver,
                    "_log_invalid_events_cohort",
                    return_value="Ooops, error here!",
            ) as mock_log_invalid:
                loader_ = ConvSccsFeatureDriver(**self.kwargs)
                loader_.run_checks = True
                with self.assertRaises(ValueError) as context:
                    loader_.exposures = mock_cohort
                mock_find_events.assert_called_once_with(mock_cohort)
                mock_log_invalid.assert_called_once_with(
                    mock_cohort, log_invalid_events=True)
                self.assertTrue("Ooops, error here!" == str(context.exception))
Пример #2
0
    def test_properties_followups(self):
        loader = BaseFeatureDriver(**self.kwargs)
        loader.run_checks = True
        self.assertTrue(
            data_frame_equality(self.followups.subjects,
                                loader.followups.subjects))
        self.assertTrue(
            data_frame_equality(self.followups.events,
                                loader.followups.events))

        with patch.object(
                BaseFeatureDriver,
                "_log_invalid_events_cohort",
                return_value="Ooops, error here!",
        ) as mock_log_invalid:
            with patch.object(
                    BaseFeatureDriver,
                    "_find_events_not_in_study_dates",
                    return_value=self.mock_cohort,
            ) as mock_find_events_study_dates:
                loader = BaseFeatureDriver(**self.kwargs)
                loader.run_checks = True
                with self.assertRaises(ValueError) as context:
                    loader.followups = self.mock_cohort
                mock_find_events_study_dates.assert_called_once_with(
                    self.mock_cohort)
                mock_log_invalid.assert_called_once_with(
                    self.mock_cohort, log_invalid_events=True)
                self.assertTrue("Ooops, error here!" == str(context.exception))

        with patch.object(
                BaseFeatureDriver,
                "_log_invalid_events_cohort",
                return_value="Ooops, error here!",
        ) as mock_log_invalid:
            with patch.object(
                    BaseFeatureDriver,
                    "_find_events_not_in_study_dates",
                    return_value=self.mock_empty_cohort,
            ) as mock_did_not_find_events_study_dates:
                with patch.object(
                        BaseFeatureDriver,
                        "_find_inconsistent_start_end_ordering",
                        return_value=self.mock_cohort,
                ) as mock_find_inconsistent_ordering:
                    loader = BaseFeatureDriver(**self.kwargs)
                    loader.run_checks = True
                    with self.assertRaises(ValueError) as context:
                        loader.followups = self.mock_cohort
                    mock_did_not_find_events_study_dates.assert_called_once_with(
                        self.mock_cohort)
                    mock_find_inconsistent_ordering.assert_called_once_with(
                        self.mock_cohort)
                    mock_log_invalid.assert_called_once_with(
                        self.mock_cohort, log_invalid_events=True)
                    self.assertTrue(
                        "Ooops, error here!" == str(context.exception))
Пример #3
0
 def __eq__(self, other):
     if isinstance(other, Cohort):
         if self.events is not None and other.events is not None:
             return data_frame_equality(
                 self.subjects, other.subjects) and data_frame_equality(
                     self.events, other.events)
         else:
             return data_frame_equality(self.subjects, other.subjects)
     else:
         return False
Пример #4
0
 def __eq__(self, other):
     """ Return self==value."""
     if isinstance(other, Table) and is_same_struct_type(
             self.source, other.source):
         return data_frame_equality(self.source, other.source)
     else:
         return False
Пример #5
0
    def test_properties_base_population(self):
        loader = BaseFeatureDriver(**self.kwargs)
        loader.run_checks = True

        self.assertTrue(
            data_frame_equality(self.base_population.subjects,
                                loader.base_population.subjects))
        self.assertTrue(loader.base_population.events is None)

        with patch.object(
                BaseFeatureDriver,
                "_find_subjects_with_age_inconsistent_w_age_groups",
                return_value=self.mock_cohort,
        ) as mock_find_subjects:
            with patch.object(
                    BaseFeatureDriver,
                    "_log_invalid_events_cohort",
                    return_value="Ooops, error here!",
            ) as mock_log_invalid:
                loader = BaseFeatureDriver(**self.kwargs)
                loader.run_checks = True
                with self.assertRaises(ValueError) as context:
                    loader.base_population = self.mock_cohort
                mock_find_subjects.assert_called_once_with(self.mock_cohort)
                mock_log_invalid.assert_called_once_with(
                    self.mock_cohort, log_invalid_subjects=True)
                self.assertTrue("Ooops, error here!" == str(context.exception))
Пример #6
0
    def test_add_age_information(self):
        subjects, df = self.create_spark_df(
            {"birthDate": [datetime(1993, 10, 9),
                           datetime(1992, 3, 14)]})

        input = Cohort("liberal_fractures", "liberal_fractures", subjects,
                       None)

        input.add_age_information(datetime(2013, 1, 1))
        result = input
        expected_subjects, _ = self.create_spark_df({"age": [19, 20]})
        expected = Cohort("liberal_fractures", "liberal_fractures",
                          expected_subjects, None)
        self.assertTrue(
            data_frame_equality(result.subjects.select("age"),
                                expected.subjects.select("age")))
Пример #7
0
    def test_properties_outcomes(self):
        loader = ConvSccsFeatureDriver(**self.kwargs)
        outcomes_ = loader.outcomes
        self.assertTrue(
            data_frame_equality(outcomes_.subjects, self.outcomes.subjects))
        self.assertTrue(
            data_frame_equality(outcomes_.events, self.outcomes.events))

        loader_ = ConvSccsFeatureDriver(**self.kwargs)
        loader_.run_checks = True

        bad_outcomes_df, _ = self.create_spark_df({
            "patientID": ["0", "4"],  # uuid
            "start": [
                pytz.datetime.datetime(2010, 6, 8, tzinfo=pytz.UTC),
                pytz.datetime.datetime(2011, 3, 29, tzinfo=pytz.UTC),
            ],
            "end":
            [None, pytz.datetime.datetime(2010, 11, 24, tzinfo=pytz.UTC)],
            "value": ["bar", "baz"],
            "category": ["outcome"] * 2,
            "groupID": [0] * 2,
            "weight": [1] * 2,
        })
        bad_outcomes_cohort = Cohort(
            "", "",
            bad_outcomes_df.select("patientID").distinct(), bad_outcomes_df)

        with self.assertRaises(AssertionError) as context:
            loader_.outcomes = bad_outcomes_cohort

        self.assertTrue(
            "There are more than one type of outcomes, check the 'value' field of "
            "outcomes cohort events." in str(context.exception))

        mock_dataframe = MagicMock()
        mock_dataframe.take = lambda x: True
        mock_cohort = MagicMock()
        mock_cohort.subjects = mock_dataframe
        mock_cohort.events = mock_dataframe
        mock_empty_df = MagicMock()
        mock_empty_df.take = lambda x: []
        mock_empty_cohort = MagicMock
        mock_empty_cohort.subjects = mock_empty_df
        mock_empty_cohort.events = mock_empty_df
        with patch.object(
                ConvSccsFeatureDriver,
                "_log_invalid_events_cohort",
                return_value="Ooops, error here!",
        ) as mock_log_invalid:
            with patch.object(
                    ConvSccsFeatureDriver,
                    "_find_events_not_in_followup_bounds",
                    return_value=mock_cohort,
            ) as mock_find_events_outcome_bounds:
                loader = ConvSccsFeatureDriver(**self.kwargs)
                loader.run_checks = True
                with self.assertRaises(ValueError) as context:
                    loader.outcomes = self.outcomes
                mock_find_events_outcome_bounds.assert_called_once_with(
                    self.outcomes)
                mock_log_invalid.assert_called_once_with(
                    mock_cohort, log_invalid_events=True)
                self.assertTrue("Ooops, error here!" == str(context.exception))
        with patch.object(
                ConvSccsFeatureDriver,
                "_log_invalid_events_cohort",
                return_value="Ooops, error here!",
        ) as mock_log_invalid:
            with patch.object(
                    ConvSccsFeatureDriver,
                    "_find_events_not_in_followup_bounds",
                    return_value=mock_empty_cohort,
            ) as mock_did_not_find_outcome_bounds:
                with patch.object(
                        ConvSccsFeatureDriver,
                        "_find_subjects_with_many_outcomes",
                        return_value=mock_cohort,
                ) as mock_find_many_outcomes:
                    loader = ConvSccsFeatureDriver(**self.kwargs)
                    loader.run_checks = True
                    with self.assertRaises(ValueError) as context:
                        loader.outcomes = self.outcomes
                    mock_did_not_find_outcome_bounds.assert_called_once_with(
                        self.outcomes)
                    mock_find_many_outcomes.assert_called_once_with(
                        self.outcomes)
                    mock_log_invalid.assert_called_once_with(
                        mock_cohort, log_invalid_subjects=True)
                    self.assertTrue(
                        "Ooops, error here!" == str(context.exception))
Пример #8
0
    def test_data_frame_equality(self):
        df, _ = self.create_spark_df({"patientID": [1, 2, 3]})

        self.assertTrue(data_frame_equality(df, df))
        self.assertFalse(data_frame_equality(df, None))