|
9 | 9 |
|
10 | 10 | import numpy as np
|
11 | 11 | from scipy.spatial import Delaunay
|
| 12 | +import pandas as pd |
12 | 13 |
|
13 | 14 |
|
14 | 15 | class TestDistplot(TestCase):
|
@@ -706,3 +707,309 @@ def test_trisurf_all_args(self):
|
706 | 707 |
|
707 | 708 | self.assert_dict_equal(test_trisurf_plot['data'][1],
|
708 | 709 | exp_trisurf_plot['data'][1])
|
| 710 | + |
| 711 | + |
| 712 | +class TestScatterPlotMatrix(NumpyTestUtilsMixin, TestCase): |
| 713 | + |
| 714 | + def test_dataframe_input(self): |
| 715 | + |
| 716 | + # check: dataframe is imported |
| 717 | + df = 'foo' |
| 718 | + |
| 719 | + pattern = ( |
| 720 | + "Dataframe not inputed. Please use a pandas dataframe to produce " |
| 721 | + "a scatterplot matrix." |
| 722 | + ) |
| 723 | + |
| 724 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 725 | + tls.FigureFactory.create_scatterplotmatrix, |
| 726 | + df) |
| 727 | + |
| 728 | + def test_one_column_dataframe(self): |
| 729 | + |
| 730 | + # check: dataframe has 1 column or less |
| 731 | + df = pd.DataFrame([1, 2, 3]) |
| 732 | + |
| 733 | + pattern = ( |
| 734 | + "Dataframe has only one column. To use the scatterplot matrix, " |
| 735 | + "use at least 2 columns." |
| 736 | + ) |
| 737 | + |
| 738 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 739 | + tls.FigureFactory.create_scatterplotmatrix, |
| 740 | + df) |
| 741 | + |
| 742 | + def test_valid_diag_choice(self): |
| 743 | + |
| 744 | + # make sure that the diagonal param is valid |
| 745 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]]) |
| 746 | + |
| 747 | + self.assertRaises(PlotlyError, |
| 748 | + tls.FigureFactory.create_scatterplotmatrix, |
| 749 | + df, diag='foo') |
| 750 | + |
| 751 | + def test_forbidden_params(self): |
| 752 | + |
| 753 | + # check: the forbidden params of 'marker' in **kwargs |
| 754 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]]) |
| 755 | + |
| 756 | + kwargs = {'marker': {'size': 15}} |
| 757 | + |
| 758 | + pattern = ( |
| 759 | + "Your kwargs dictionary cannot include the 'size', 'color' or " |
| 760 | + "'colorscale' key words inside the marker dict since 'size' is " |
| 761 | + "already an argument of the scatterplot matrix function and both " |
| 762 | + "'color' and 'colorscale are set internally." |
| 763 | + ) |
| 764 | + |
| 765 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 766 | + tls.FigureFactory.create_scatterplotmatrix, |
| 767 | + df, **kwargs) |
| 768 | + |
| 769 | + def test_valid_index_choice(self): |
| 770 | + |
| 771 | + # check: index is a column name |
| 772 | + df = pd.DataFrame([[1, 2], [3, 4]], columns=['apple', 'pear']) |
| 773 | + |
| 774 | + pattern = ( |
| 775 | + "Make sure you set the index input variable to one of the column " |
| 776 | + "names of your dataframe." |
| 777 | + ) |
| 778 | + |
| 779 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 780 | + tls.FigureFactory.create_scatterplotmatrix, |
| 781 | + df, index='grape') |
| 782 | + |
| 783 | + def test_same_data_in_dataframe_columns(self): |
| 784 | + |
| 785 | + # check: either all numbers or strings in each dataframe column |
| 786 | + df = pd.DataFrame([['a', 2], [3, 4]]) |
| 787 | + |
| 788 | + pattern = ( |
| 789 | + "Error in dataframe. Make sure all entries of each column are " |
| 790 | + "either numbers or strings." |
| 791 | + ) |
| 792 | + |
| 793 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 794 | + tls.FigureFactory.create_scatterplotmatrix, |
| 795 | + df) |
| 796 | + |
| 797 | + df = pd.DataFrame([[1, 2], ['a', 4]]) |
| 798 | + |
| 799 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 800 | + tls.FigureFactory.create_scatterplotmatrix, |
| 801 | + df) |
| 802 | + |
| 803 | + def test_same_data_in_index(self): |
| 804 | + |
| 805 | + # check: either all numbers or strings in index column |
| 806 | + df = pd.DataFrame([['a', 2], [3, 4]], columns=['apple', 'pear']) |
| 807 | + |
| 808 | + pattern = ( |
| 809 | + "Error in indexing column. Make sure all entries of each column " |
| 810 | + "are all numbers or all strings." |
| 811 | + ) |
| 812 | + |
| 813 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 814 | + tls.FigureFactory.create_scatterplotmatrix, |
| 815 | + df, index='apple') |
| 816 | + |
| 817 | + df = pd.DataFrame([[1, 2], ['a', 4]], columns=['apple', 'pear']) |
| 818 | + |
| 819 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 820 | + tls.FigureFactory.create_scatterplotmatrix, |
| 821 | + df, index='apple') |
| 822 | + |
| 823 | + def test_valid_palette(self): |
| 824 | + |
| 825 | + # check: the palette argument is in a acceptable form |
| 826 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], |
| 827 | + columns=['a', 'b', 'c']) |
| 828 | + |
| 829 | + self.assertRaisesRegexp(PlotlyError, "You must pick a valid " |
| 830 | + "plotly colorscale name.", |
| 831 | + tls.FigureFactory.create_scatterplotmatrix, |
| 832 | + df, use_theme=True, index='a', |
| 833 | + palette='fake_scale') |
| 834 | + |
| 835 | + pattern = ( |
| 836 | + "The items of 'palette' must be tripets of the form a,b,c or " |
| 837 | + "'rgbx,y,z' where a,b,c belong to the interval 0,1 and x,y,z " |
| 838 | + "belong to 0,255." |
| 839 | + ) |
| 840 | + |
| 841 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 842 | + tls.FigureFactory.create_scatterplotmatrix, |
| 843 | + df, use_theme=True, palette=1, index='c') |
| 844 | + |
| 845 | + def test_valid_endpts(self): |
| 846 | + |
| 847 | + # check: the endpts is a list or a tuple |
| 848 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], |
| 849 | + columns=['a', 'b', 'c']) |
| 850 | + |
| 851 | + pattern = ( |
| 852 | + "The intervals_endpts argument must be a list or tuple of a " |
| 853 | + "sequence of increasing numbers." |
| 854 | + ) |
| 855 | + |
| 856 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 857 | + tls.FigureFactory.create_scatterplotmatrix, |
| 858 | + df, use_theme=True, index='a', |
| 859 | + palette='Blues', endpts='foo') |
| 860 | + |
| 861 | + # check: the endpts are a list of numbers |
| 862 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 863 | + tls.FigureFactory.create_scatterplotmatrix, |
| 864 | + df, use_theme=True, index='a', |
| 865 | + palette='Blues', endpts=['a']) |
| 866 | + |
| 867 | + # check: endpts is a list of INCREASING numbers |
| 868 | + self.assertRaisesRegexp(PlotlyError, pattern, |
| 869 | + tls.FigureFactory.create_scatterplotmatrix, |
| 870 | + df, use_theme=True, index='a', |
| 871 | + palette='Blues', endpts=[2, 1]) |
| 872 | + |
| 873 | + def test_scatter_plot_matrix(self): |
| 874 | + |
| 875 | + # check if test scatter plot matrix without index or theme matches |
| 876 | + # with the expected output |
| 877 | + df = pd.DataFrame([[2, 'Apple'], [6, 'Pear'], |
| 878 | + [-15, 'Apple'], [5, 'Pear'], |
| 879 | + [-2, 'Apple'], [0, 'Apple']], |
| 880 | + columns=['Numbers', 'Fruit']) |
| 881 | + |
| 882 | + test_scatter_plot_matrix = tls.FigureFactory.create_scatterplotmatrix( |
| 883 | + df, diag='scatter', height=1000, width=1000, size=13, |
| 884 | + title='Scatterplot Matrix', use_theme=False |
| 885 | + ) |
| 886 | + |
| 887 | + exp_scatter_plot_matrix = { |
| 888 | + 'data': [{'marker': {'size': 13}, |
| 889 | + 'mode': 'markers', |
| 890 | + 'showlegend': False, |
| 891 | + 'type': 'scatter', |
| 892 | + 'x': [2, 6, -15, 5, -2, 0], |
| 893 | + 'xaxis': 'x1', |
| 894 | + 'y': [2, 6, -15, 5, -2, 0], |
| 895 | + 'yaxis': 'y1'}, |
| 896 | + {'marker': {'size': 13}, |
| 897 | + 'mode': 'markers', |
| 898 | + 'showlegend': False, |
| 899 | + 'type': 'scatter', |
| 900 | + 'x': ['Apple', |
| 901 | + 'Pear', |
| 902 | + 'Apple', |
| 903 | + 'Pear', |
| 904 | + 'Apple', |
| 905 | + 'Apple'], |
| 906 | + 'xaxis': 'x2', |
| 907 | + 'y': [2, 6, -15, 5, -2, 0], |
| 908 | + 'yaxis': 'y2'}, |
| 909 | + {'marker': {'size': 13}, |
| 910 | + 'mode': 'markers', |
| 911 | + 'showlegend': False, |
| 912 | + 'type': 'scatter', |
| 913 | + 'x': [2, 6, -15, 5, -2, 0], |
| 914 | + 'xaxis': 'x3', |
| 915 | + 'y': ['Apple', |
| 916 | + 'Pear', |
| 917 | + 'Apple', |
| 918 | + 'Pear', |
| 919 | + 'Apple', |
| 920 | + 'Apple'], |
| 921 | + 'yaxis': 'y3'}, |
| 922 | + {'marker': {'size': 13}, |
| 923 | + 'mode': 'markers', |
| 924 | + 'showlegend': False, |
| 925 | + 'type': 'scatter', |
| 926 | + 'x': ['Apple', |
| 927 | + 'Pear', |
| 928 | + 'Apple', |
| 929 | + 'Pear', |
| 930 | + 'Apple', |
| 931 | + 'Apple'], |
| 932 | + 'xaxis': 'x4', |
| 933 | + 'y': ['Apple', 'Pear', 'Apple', 'Pear', 'Apple', 'Apple'], |
| 934 | + 'yaxis': 'y4'}], |
| 935 | + 'layout': {'height': 1000, |
| 936 | + 'showlegend': True, |
| 937 | + 'title': 'Scatterplot Matrix', |
| 938 | + 'width': 1000, |
| 939 | + 'xaxis1': {'anchor': 'y1', |
| 940 | + 'domain': [0.0, 0.45]}, |
| 941 | + 'xaxis2': {'anchor': 'y2', |
| 942 | + 'domain': [0.55, 1.0]}, |
| 943 | + 'xaxis3': {'anchor': 'y3', |
| 944 | + 'domain': [0.0, 0.45], 'title': 'Numbers'}, |
| 945 | + 'xaxis4': {'anchor': 'y4', |
| 946 | + 'domain': [0.55, 1.0], 'title': 'Fruit'}, |
| 947 | + 'yaxis1': {'anchor': 'x1', |
| 948 | + 'domain': [0.575, 1.0], 'title': 'Numbers'}, |
| 949 | + 'yaxis2': {'anchor': 'x2', |
| 950 | + 'domain': [0.575, 1.0]}, |
| 951 | + 'yaxis3': {'anchor': 'x3', |
| 952 | + 'domain': [0.0, 0.425], 'title': 'Fruit'}, |
| 953 | + 'yaxis4': {'anchor': 'x4', |
| 954 | + 'domain': [0.0, 0.425]}} |
| 955 | + } |
| 956 | + |
| 957 | + self.assert_dict_equal(test_scatter_plot_matrix['data'][0], |
| 958 | + exp_scatter_plot_matrix['data'][0]) |
| 959 | + |
| 960 | + self.assert_dict_equal(test_scatter_plot_matrix['data'][1], |
| 961 | + exp_scatter_plot_matrix['data'][1]) |
| 962 | + |
| 963 | + self.assert_dict_equal(test_scatter_plot_matrix['layout'], |
| 964 | + exp_scatter_plot_matrix['layout']) |
| 965 | + |
| 966 | + def test_scatter_plot_matrix_kwargs(self): |
| 967 | + |
| 968 | + # check if test scatter plot matrix matches with |
| 969 | + # the expected output |
| 970 | + df = pd.DataFrame([[2, 'Apple'], [6, 'Pear'], |
| 971 | + [-15, 'Apple'], [5, 'Pear'], |
| 972 | + [-2, 'Apple'], [0, 'Apple']], |
| 973 | + columns=['Numbers', 'Fruit']) |
| 974 | + |
| 975 | + test_scatter_plot_matrix = tls.FigureFactory.create_scatterplotmatrix( |
| 976 | + df, index='Fruit', endpts=[-10, -1], diag='histogram', |
| 977 | + height=1000, width=1000, size=13, title='Scatterplot Matrix', |
| 978 | + use_theme=True, palette='YlOrRd', marker=dict(symbol=136) |
| 979 | + ) |
| 980 | + |
| 981 | + exp_scatter_plot_matrix = { |
| 982 | + 'data': [{'marker': {'color': 'rgb(128.0, 0.0, 38.0)'}, |
| 983 | + 'showlegend': False, |
| 984 | + 'type': 'histogram', |
| 985 | + 'x': [2, -15, -2, 0], |
| 986 | + 'xaxis': 'x1', |
| 987 | + 'yaxis': 'y1'}, |
| 988 | + {'marker': {'color': 'rgb(255.0, 255.0, 204.0)'}, |
| 989 | + 'showlegend': False, |
| 990 | + 'type': 'histogram', |
| 991 | + 'x': [6, 5], |
| 992 | + 'xaxis': 'x1', |
| 993 | + 'yaxis': 'y1'}], |
| 994 | + 'layout': {'barmode': 'stack', |
| 995 | + 'height': 1000, |
| 996 | + 'showlegend': True, |
| 997 | + 'title': 'Scatterplot Matrix', |
| 998 | + 'width': 1000, |
| 999 | + 'xaxis1': {'anchor': 'y1', |
| 1000 | + 'domain': [0.0, 1.0], |
| 1001 | + 'title': 'Numbers'}, |
| 1002 | + 'yaxis1': {'anchor': 'x1', |
| 1003 | + 'domain': [0.0, 1.0], |
| 1004 | + 'title': 'Numbers'}} |
| 1005 | + } |
| 1006 | + |
| 1007 | + self.assert_dict_equal(test_scatter_plot_matrix['data'][0], |
| 1008 | + exp_scatter_plot_matrix['data'][0]) |
| 1009 | + |
| 1010 | + self.assert_dict_equal(test_scatter_plot_matrix['data'][1], |
| 1011 | + exp_scatter_plot_matrix['data'][1]) |
| 1012 | + |
| 1013 | + self.assert_dict_equal(test_scatter_plot_matrix['layout'], |
| 1014 | + exp_scatter_plot_matrix['layout']) |
| 1015 | + |
0 commit comments