()
| 811 | |
| 812 | |
| 813 | def test_reshape2d(): |
| 814 | |
| 815 | class Dummy: |
| 816 | pass |
| 817 | |
| 818 | xnew = cbook._reshape_2D([], 'x') |
| 819 | assert np.shape(xnew) == (1, 0) |
| 820 | |
| 821 | x = [Dummy() for _ in range(5)] |
| 822 | |
| 823 | xnew = cbook._reshape_2D(x, 'x') |
| 824 | assert np.shape(xnew) == (1, 5) |
| 825 | |
| 826 | x = np.arange(5) |
| 827 | xnew = cbook._reshape_2D(x, 'x') |
| 828 | assert np.shape(xnew) == (1, 5) |
| 829 | |
| 830 | x = [[Dummy() for _ in range(5)] for _ in range(3)] |
| 831 | xnew = cbook._reshape_2D(x, 'x') |
| 832 | assert np.shape(xnew) == (3, 5) |
| 833 | |
| 834 | # this is strange behaviour, but... |
| 835 | x = np.random.rand(3, 5) |
| 836 | xnew = cbook._reshape_2D(x, 'x') |
| 837 | assert np.shape(xnew) == (5, 3) |
| 838 | |
| 839 | # Test a list of lists which are all of length 1 |
| 840 | x = [[1], [2], [3]] |
| 841 | xnew = cbook._reshape_2D(x, 'x') |
| 842 | assert isinstance(xnew, list) |
| 843 | assert isinstance(xnew[0], np.ndarray) and xnew[0].shape == (1,) |
| 844 | assert isinstance(xnew[1], np.ndarray) and xnew[1].shape == (1,) |
| 845 | assert isinstance(xnew[2], np.ndarray) and xnew[2].shape == (1,) |
| 846 | |
| 847 | # Test a list of zero-dimensional arrays |
| 848 | x = [np.array(0), np.array(1), np.array(2)] |
| 849 | xnew = cbook._reshape_2D(x, 'x') |
| 850 | assert isinstance(xnew, list) |
| 851 | assert len(xnew) == 1 |
| 852 | assert isinstance(xnew[0], np.ndarray) and xnew[0].shape == (3,) |
| 853 | |
| 854 | # Now test with a list of lists with different lengths, which means the |
| 855 | # array will internally be converted to a 1D object array of lists |
| 856 | x = [[1, 2, 3], [3, 4], [2]] |
| 857 | xnew = cbook._reshape_2D(x, 'x') |
| 858 | assert isinstance(xnew, list) |
| 859 | assert isinstance(xnew[0], np.ndarray) and xnew[0].shape == (3,) |
| 860 | assert isinstance(xnew[1], np.ndarray) and xnew[1].shape == (2,) |
| 861 | assert isinstance(xnew[2], np.ndarray) and xnew[2].shape == (1,) |
| 862 | |
| 863 | # We now need to make sure that this works correctly for Numpy subclasses |
| 864 | # where iterating over items can return subclasses too, which may be |
| 865 | # iterable even if they are scalars. To emulate this, we make a Numpy |
| 866 | # array subclass that returns Numpy 'scalars' when iterating or accessing |
| 867 | # values, and these are technically iterable if checking for example |
| 868 | # isinstance(x, collections.abc.Iterable). |
| 869 | |
| 870 | class ArraySubclass(np.ndarray): |
nothing calls this directly
no test coverage detected
searching dependent graphs…