def test_plot_individual_conditional_expectation(): """ Tests ICE plotting. Tests :func:`fatf.vis.feature_influence.plot_individual_conditional_expectation` function. """ feature_name = 'some feature' class_index = 1 class_name = 'middle' figure, axis = fvfi.plot_individual_conditional_expectation( FAKE_ICE_ARRAY, FAKE_LINESPACE, class_index, feature_name, class_name) assert isinstance(figure, plt.Figure) p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( axis) # ...check title assert p_title == 'Individual Conditional Expectation' # ...check x range assert np.array_equal(p_x_range, [FAKE_LINESPACE[0], FAKE_LINESPACE[-1]]) # ...check x label assert p_x_label == feature_name # ...check y range assert np.array_equal(p_y_range, [-0.05, 1.05]) # ...check y label assert p_y_label == '{} class probability'.format(class_name) # Test the line assert len(axis.collections) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.collections[0], is_collection=True) assert len(l_data) == FAKE_ICE_ARRAY.shape[0] for i, line_array in enumerate(l_data): line_data = np.stack( [FAKE_LINESPACE, FAKE_ICE_ARRAY[i, :, class_index]], axis=1) assert np.array_equal(line_array, line_data) assert np.isclose(l_colour, np.array([[0.412, 0.412, 0.412, 0.5]]), atol=1e-2).all() # dimgray mapping apparently assert l_alpha == 0.5 assert l_label == 'ICE' assert l_width == 1.75 # Validate plot legend legend = [ i for i in axis.get_children() if isinstance(i, matplotlib.legend.Legend) ] assert len(legend) == 1 legend_texts = legend[0].get_texts() assert len(legend_texts) == 1 assert legend_texts[0].get_text() == 'ICE'
def test_plot_partial_dependence(): """ Tests :func:`fatf.vis.feature_influence.plot_partial_dependence` function. """ feature_name = 'some feature' class_index = 1 class_name = 'middle' figure, axis = fvfi.plot_partial_dependence(FAKE_PD_ARRAY, FAKE_LINESPACE, class_index, feature_name, class_name) assert isinstance(figure, plt.Figure) p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( axis) # ...check title assert p_title == 'Partial Dependence' # ...check x range assert np.array_equal(p_x_range, [FAKE_LINESPACE[0], FAKE_LINESPACE[-1]]) # ...check x label assert p_x_label == feature_name # ...check y range assert np.array_equal(p_y_range, [-0.05, 1.05]) # ...check y label assert p_y_label == '{} class probability'.format(class_name) # Test the line assert len(axis.lines) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.lines[0]) line_data = np.stack([FAKE_LINESPACE, FAKE_PD_ARRAY[:, class_index]], axis=1) assert np.array_equal(l_data, line_data) assert l_colour == 'lightsalmon' assert l_alpha == 0.6 assert l_label == 'PD' assert l_width == 7 # Validate plot legend legend = [ i for i in axis.get_children() if isinstance(i, matplotlib.legend.Legend) ] assert len(legend) == 1 legend_texts = legend[0].get_texts() assert len(legend_texts) == 1 assert legend_texts[0].get_text() == 'PD'
def test_get_plot_data(): """ Tests importing :mod:`fatf.utils.testing.vis.get_plot_data` function. """ true_title = 'my title' true_x_label = 'x label' true_x_range = [-0.42, 12.34] true_y_label = 'y label' true_y_range = [-7, 7] plot_figure, plot_axis = plt.subplots(1, 1) plot_axis.set_title(true_title) plot_axis.set_xlim(true_x_range) plot_axis.set_xlabel(true_x_label) plot_axis.set_ylim(true_y_range) plot_axis.set_ylabel(true_y_label) title, x_label, x_range, y_label, y_range = futv.get_plot_data(plot_axis) assert true_title == title assert true_x_label == x_label assert true_y_label == y_label assert np.array_equal(true_y_range, y_range) assert np.array_equal(true_x_range, x_range)
def test_ice_pd_overlay(): """ Tests overlaying PD plot on top of an ICE plot. """ f_name = 'some feature' c_index = 1 c_name = 'middle' figure, axis = fvfi.plot_individual_conditional_expectation( FAKE_ICE_ARRAY, FAKE_LINESPACE, c_index, f_name, c_name) assert isinstance(figure, plt.Figure) assert isinstance(axis, plt.Axes) none, axis = fvfi.plot_partial_dependence(FAKE_PD_ARRAY, FAKE_LINESPACE, c_index, f_name, c_name, axis) assert none is None assert isinstance(axis, plt.Axes) # Inspect the canvas p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( axis) # ...check title assert p_title == ('Individual Conditional Expectation &\nPartial ' 'Dependence') # ...check x range assert np.array_equal(p_x_range, [FAKE_LINESPACE[0], FAKE_LINESPACE[-1]]) # ...check x label assert p_x_label == f_name # ...check y range assert np.array_equal(p_y_range, [-0.05, 1.05]) # ...check y label assert p_y_label == '{} class probability'.format(c_name) # Check ICE assert len(axis.collections) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.collections[0], is_collection=True) assert len(l_data) == FAKE_ICE_ARRAY.shape[0] for i, line_array in enumerate(l_data): line_data = np.stack([FAKE_LINESPACE, FAKE_ICE_ARRAY[i, :, c_index]], axis=1) assert np.array_equal(line_array, line_data) assert np.isclose(l_colour, np.array([[0.412, 0.412, 0.412, 0.5]]), atol=1e-2).all() # dimgray mapping apparently assert l_alpha == 0.5 assert l_label == 'ICE' assert l_width == 1.75 # Check PD assert len(axis.lines) == 1 l_data, l_colour, l_alpha, l_label, l_width = futv.get_line_data( axis.lines[0]) line_data = np.stack([FAKE_LINESPACE, FAKE_PD_ARRAY[:, c_index]], axis=1) assert np.array_equal(l_data, line_data) assert l_colour == 'lightsalmon' assert l_alpha == 0.6 assert l_label == 'PD' assert l_width == 7 # Validate plot legend legend = [ i for i in axis.get_children() if isinstance(i, matplotlib.legend.Legend) ] assert len(legend) == 1 legend_texts = legend[0].get_texts() assert len(legend_texts) == 2 assert legend_texts[0].get_text() == 'PD' assert legend_texts[1].get_text() == 'ICE'
def test_prepare_a_canvas(): """ Tests :func:`fatf.vis.feature_influence._prepare_a_canvas`. This test checks for the plot title, x range, x label, y range and y label. """ title = 'plot title' title_custom = 'custom plot title' class_index = 0 x_range = [-5, 3] y_range = [-0.05, 1.05] class_name_n = None class_name_s = 'class name' feature_name_n = None feature_name_s = 'feature name' # Plotting from scratch axis = None # figure, plot = fvfi._prepare_a_canvas(title, axis, class_index, class_name_s, feature_name_n, x_range) assert isinstance(figure, plt.Figure) p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( plot) # ...check title assert p_title == title # ...check x range assert np.array_equal(p_x_range, x_range) # ...check x label assert p_x_label == "Selected Feature's Linespace" # ...check y range assert np.array_equal(p_y_range, y_range) # ...check y label assert p_y_label == '{} class probability'.format(class_name_s) # figure, plot = fvfi._prepare_a_canvas(title, axis, class_index, class_name_n, feature_name_s, x_range) assert isinstance(figure, plt.Figure) p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( plot) # ...check title assert p_title == title # ...check x range assert np.array_equal(p_x_range, x_range) # ...check x label assert p_x_label == feature_name_s # ...check y range assert np.array_equal(p_y_range, y_range) # ...check y label assert p_y_label == '{} (class index) class probability'.format( class_index) # Plotting on an existing axis fig, axis = plt.subplots(1, 1) # axis.set_xlim(np.array([-3, 3])) msg = ('The x-axis range of the plot given in the plot_axis parameter ' 'differs from the x-axis range of this plot.') with pytest.raises(ValueError) as exin: fvfi._prepare_a_canvas(title, axis, class_index, class_name_n, feature_name_n, x_range) assert str(exin.value) == msg # axis.set_xlim(np.array(x_range)) axis.set_ylim(np.array([0, 1])) msg = ('The y-axis range of the plot given in the plot_axis parameter ' 'differs from the y-axis range of this plot.') with pytest.raises(ValueError) as exin: fvfi._prepare_a_canvas(title, axis, class_index, class_name_n, feature_name_n, x_range) assert str(exin.value) == msg # axis.set_ylim(np.array(y_range)) axis.set_title(title_custom) # # Do not extend plot title; new feature name and new class name. figure, plot = fvfi._prepare_a_canvas('', axis, class_index, class_name_s, feature_name_s, x_range) assert figure is None p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( plot) # ...check title assert p_title == title_custom # ...check x range assert np.array_equal(p_x_range, x_range) # ...check x label assert p_x_label == feature_name_s # ...check y range assert np.array_equal(p_y_range, y_range) # ...check y label assert p_y_label == '{} class probability'.format(class_name_s) # # Do not extend plot title; no new feature name & existing feature name and # no new class name and existing class name. figure, plot = fvfi._prepare_a_canvas('', plot, class_index, class_name_n, feature_name_n, x_range) assert figure is None p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( plot) # ...check title assert p_title == title_custom # ...check x range assert np.array_equal(p_x_range, x_range) # ...check x label assert p_x_label == feature_name_s # ...check y range assert np.array_equal(p_y_range, y_range) # ...check y label assert p_y_label == '{} class probability'.format(class_name_s) # # Do not extend plot title; no new feature name & no existing feature name # and no new class name and no existing class name. axis.set_ylabel(None) axis.set_xlabel(None) figure, plot = fvfi._prepare_a_canvas('extension', axis, class_index, class_name_n, feature_name_n, x_range) assert figure is None p_title, p_x_label, p_x_range, p_y_label, p_y_range = futv.get_plot_data( plot) # ...check title assert p_title == '{} &\nextension'.format(title_custom) # ...check x range assert np.array_equal(p_x_range, x_range) # ...check x label assert p_x_label == "Selected Feature's Linespace" # ...check y range assert np.array_equal(p_y_range, y_range) # ...check y label assert p_y_label == '{} (class index) class probability'.format( class_index)