Example #1
0
def figures_mesa_three_class():
    classifiers = utils.get_classifiers()

    # Uncomment to just use MLP:
    # classifiers = [AttributedClassifier(name='Neural Net', classifier=MLPClassifier(activation='relu', hidden_layer_sizes=(15, 15, 15),
    #                                                            max_iter=1000, alpha=0.01, solver='lbfgs'))]

    feature_sets = utils.get_base_feature_sets()
    three_class_performance_summaries = []

    for attributed_classifier in classifiers:
        if Constants.VERBOSE:
            print('Running ' + attributed_classifier.name + '...')
        classifier_summary = ThreeClassClassifierSummaryBuilder.build_mesa_leave_one_out(
            attributed_classifier, feature_sets)
        PerformancePlotBuilder.make_bland_altman(classifier_summary, '_mesa')
        PerformancePlotBuilder.make_single_threshold_histograms(
            classifier_summary, '_mesa')

    for attributed_classifier in classifiers:
        if Constants.VERBOSE:
            print('Running ' + attributed_classifier.name + '...')
        classifier_summary = ThreeClassClassifierSummaryBuilder.build_mesa_all_combined(
            attributed_classifier, feature_sets)
        three_class_performance_dictionary = CurvePlotBuilder.make_three_class_roc(
            classifier_summary, '_mesa')
        classifier_summary.performance_dictionary = three_class_performance_dictionary
        three_class_performance_summaries.append(classifier_summary)
        CurvePlotBuilder.combine_sw_and_three_class_plots(
            attributed_classifier, 1, 'mesa')

    TableBuilder.print_table_three_class(three_class_performance_summaries)
    CurvePlotBuilder.combine_plots_as_grid(classifiers, 1,
                                           '_mesa_three_class_roc')
Example #2
0
def figures_leave_one_out_three_class_performance():
    attributed_classifier = AttributedClassifier(
        name='Neural Net',
        classifier=MLPClassifier(activation='relu',
                                 hidden_layer_sizes=(15, 15, 15),
                                 max_iter=1000,
                                 alpha=0.01,
                                 solver='lbfgs'))

    feature_sets = utils.get_base_feature_sets()

    if Constants.VERBOSE:
        print('Running ' + attributed_classifier.name + '...')
    classifier_summary = ThreeClassClassifierSummaryBuilder.build_leave_one_out(
        attributed_classifier, feature_sets)
    PerformancePlotBuilder.make_bland_altman(classifier_summary)
Example #3
0
def figures_leave_one_out_sleep_wake_performance():
    attributed_classifier = AttributedClassifier(
        name='Neural Net',
        classifier=MLPClassifier(activation='relu',
                                 hidden_layer_sizes=(15, 15, 15),
                                 max_iter=1000,
                                 alpha=0.01,
                                 solver='lbfgs'))

    feature_sets = [[
        FeatureType.count, FeatureType.heart_rate, FeatureType.circadian_model
    ]]

    if Constants.VERBOSE:
        print('Running ' + attributed_classifier.name + '...')
    classifier_summary = SleepWakeClassifierSummaryBuilder.build_leave_one_out(
        attributed_classifier, feature_sets)
    PerformancePlotBuilder.make_histogram_with_thresholds(classifier_summary)
    PerformancePlotBuilder.make_single_threshold_histograms(classifier_summary)
    def test_make_histogram_with_thresholds(self, mock_plt, mock_np,
                                            mock_performance_summarizer,
                                            mock_image, mock_image_draw,
                                            mock_image_font):
        raw_performances = [
            'raw_performance_placeholder_1', 'raw_performance_placeholder_2'
        ]

        performance_dictionary = {tuple([FeatureType.count]): raw_performances}
        attributed_classifier = AttributedClassifier(
            name="Logistic Regression", classifier=LogisticRegression())
        classifier_summary = ClassifierSummary(
            attributed_classifier=attributed_classifier,
            performance_dictionary=performance_dictionary)

        mock_ax = [[MagicMock(), MagicMock()], [MagicMock(),
                                                MagicMock()],
                   [MagicMock(), MagicMock()], [MagicMock(),
                                                MagicMock()]]

        number_of_subjects = 2
        mock_plt.subplots.return_value = 'placeholder1', mock_ax
        mock_np.zeros.side_effect = [
            np.zeros((number_of_subjects, 4)),
            np.zeros((number_of_subjects, 4))
        ]

        dt = 0.02
        expected_range = np.arange(0, 1 + dt, dt)
        mock_np.arange.return_value = expected_range

        mock_subject1_threshold1 = MagicMock()
        mock_subject1_threshold2 = MagicMock()
        mock_subject1_threshold3 = MagicMock()
        mock_subject1_threshold4 = MagicMock()
        mock_subject2_threshold1 = MagicMock()
        mock_subject2_threshold2 = MagicMock()
        mock_subject2_threshold3 = MagicMock()
        mock_subject2_threshold4 = MagicMock()

        mock_subject1_threshold1.accuracy = 0
        mock_subject1_threshold1.wake_correct = 1
        mock_subject1_threshold2.accuracy = 2
        mock_subject1_threshold2.wake_correct = 3
        mock_subject1_threshold3.accuracy = 4
        mock_subject1_threshold3.wake_correct = 5
        mock_subject1_threshold4.accuracy = 6
        mock_subject1_threshold4.wake_correct = 7

        mock_subject2_threshold1.accuracy = 10
        mock_subject2_threshold1.wake_correct = 11
        mock_subject2_threshold2.accuracy = 12
        mock_subject2_threshold2.wake_correct = 13
        mock_subject2_threshold3.accuracy = 14
        mock_subject2_threshold3.wake_correct = 15
        mock_subject2_threshold4.accuracy = 16
        mock_subject2_threshold4.wake_correct = 17

        mock_performance_summarizer.summarize_thresholds.side_effect = [
            ([1, 2, 3, 4], [
                mock_subject1_threshold1, mock_subject1_threshold2,
                mock_subject1_threshold3, mock_subject1_threshold4
            ]),
            ([1, 2, 3, 4], [
                mock_subject2_threshold1, mock_subject2_threshold2,
                mock_subject2_threshold3, mock_subject2_threshold4
            ])
        ]

        file_save_name = str(
            Constants.FIGURE_FILE_PATH
        ) + '/' + 'Motion only_Logistic Regression_histograms_with_thresholds.png'
        mock_image.open.return_value = mock_opened_image = MagicMock()
        mock_opened_image.size = 100, 200

        mock_image.new.return_value = new_image = MagicMock()
        mock_image_draw.Draw.return_value = image_draw = MagicMock()
        mock_image_font.truetype.return_value = font = "true type font"
        PerformancePlotBuilder.make_histogram_with_thresholds(
            classifier_summary)

        mock_plt.subplots.assert_called_once_with(nrows=4,
                                                  ncols=2,
                                                  figsize=(8, 8),
                                                  sharex=True,
                                                  sharey=True)

        mock_np.zeros.assert_has_calls([call((2, 4)), call((2, 4))])
        mock_performance_summarizer.summarize_thresholds.assert_has_calls(
            [call([raw_performances[0]]),
             call([raw_performances[1]])])

        mock_ax[0][0].hist.assert_called_once_with([0, 10],
                                                   bins=expected_range,
                                                   color="skyblue",
                                                   ec="skyblue")

        mock_ax[0][1].hist.assert_called_once_with([1, 11],
                                                   bins=expected_range,
                                                   color="lightsalmon",
                                                   ec="lightsalmon")

        mock_ax[1][0].hist.assert_called_once_with([2, 12],
                                                   bins=expected_range,
                                                   color="skyblue",
                                                   ec="skyblue")

        mock_ax[1][1].hist.assert_called_once_with([3, 13],
                                                   bins=expected_range,
                                                   color="lightsalmon",
                                                   ec="lightsalmon")

        mock_ax[2][0].hist.assert_called_once_with([4, 14],
                                                   bins=expected_range,
                                                   color="skyblue",
                                                   ec="skyblue")

        mock_ax[2][1].hist.assert_called_once_with([5, 15],
                                                   bins=expected_range,
                                                   color="lightsalmon",
                                                   ec="lightsalmon")

        mock_ax[3][0].hist.assert_called_once_with([6, 16],
                                                   bins=expected_range,
                                                   color="skyblue",
                                                   ec="skyblue")

        mock_ax[3][1].hist.assert_called_once_with([7, 17],
                                                   bins=expected_range,
                                                   color="lightsalmon",
                                                   ec="lightsalmon")

        mock_ax[3][0].set_xlabel.assert_called_once_with('Accuracy',
                                                         fontsize=16,
                                                         fontname='Arial')
        mock_ax[3][1].set_xlabel.assert_called_once_with('Wake correct',
                                                         fontsize=16,
                                                         fontname='Arial')
        mock_ax[3][0].set_ylabel.assert_called_once_with('Count',
                                                         fontsize=16,
                                                         fontname='Arial')

        mock_ax[3][0].set_xlim.assert_called_once_with((0, 1))
        mock_ax[3][1].set_xlim.assert_called_once_with((0, 1))

        mock_plt.tight_layout.assert_called_once_with()
        mock_plt.savefig.assert_called_once_with(file_save_name, dpi=300)
        mock_plt.close.assert_called_once_with()

        mock_image.open.assert_called_once_with(file_save_name)
        mock_image.new.assert_called_once_with('RGB', (int(
            (1 + 0.3) * 100), 200), "white")

        new_image.paste.assert_called_once_with(mock_opened_image,
                                                (int(0.3 * 100), 0))
        mock_image_draw.Draw.assert_called_once_with(new_image)
        mock_image_font.truetype.assert_called_once_with(
            '/Library/Fonts/Arial Unicode.ttf', 75)
        image_draw.text.assert_has_calls([
            call((int(0.3 * 100 / 3), int((200 * 0.9) * 0.125)),
                 "TPR = 0.8", (0, 0, 0),
                 font=font),
            call((int(0.3 * 100 / 3), int((200 * 0.9) * 0.375)),
                 "TPR = 0.9", (0, 0, 0),
                 font=font),
            call((int(0.3 * 100 / 3), int((200 * 0.9) * 0.625)),
                 "TPR = 0.93", (0, 0, 0),
                 font=font),
            call((int(0.3 * 100 / 3), int((200 * 0.9) * 0.875)),
                 "TPR = 0.95", (0, 0, 0),
                 font=font)
        ])
        new_image.save.assert_called_once_with(
            str(Constants.FIGURE_FILE_PATH) + '/' +
            'figure_threshold_histogram.png')