Medical Data Visualizer - Errors in test_module.py

I have written my program for this challenge, however when I run it with the test module I am getting the following errors:

EEEE
======================================================================
ERROR: test_bar_plot_number_of_bars (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py", line 26, in test_bar_plot_number_of_bars
    actual = len([rect for rect in self.ax.get_children() if isinstance(rect, mpl.patches.Rectangle)])
AttributeError: 'numpy.ndarray' object has no attribute 'get_children'

======================================================================
ERROR: test_line_plot_labels (test_module.CatPlotTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py", line 13, in test_line_plot_labels
    actual = self.ax.get_xlabel()
AttributeError: 'numpy.ndarray' object has no attribute 'get_xlabel'

======================================================================
ERROR: test_heat_map_labels (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py", line 34, in setUp
    self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object does not support indexing

======================================================================
ERROR: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py", line 34, in setUp
    self.ax = self.fig.axes[0]
TypeError: 'AxesSubplot' object does not support indexing

----------------------------------------------------------------------
Ran 4 tests in 2.290s

FAILED (errors=4)

To my mind, which is still learning python, these look like errors with the python code in the test module rather than with anything I have done (i.e. they are not assertion errors).

I suspect the explanation is one of either
a) there is a problem with my code which is provoking these errors
or
b) I am not using the correct version of numpy.

If anyone has seen this before or is able to provide me with some help or direction it would be much appreciated.

For reference my code can be found here

@craig.lunney

i also think it may have to do with the outdated version of numpy…

sns.catplot doesn’t return figure object, but FacetGrid object. Similarly sns.heatmap returns ax, not figure.

For the catplot function I am using the variable name fig because that is what is provided in the challenge template. It is just a variable name though, for example:

fig = sns.catplot(x=“variable”, y=“total”, hue=“value”, col=“cardio”, data=grouped_df_cat, kind=“bar”)

print (fig)

returns

<seaborn.axisgrid.FacetGrid object at 0x000001BB762767F0>

So my function is returning a FacetGrid object but if you look at the errors for the catplot function (errors on lines 13 and 26 above), it seems to think self.ax is a numpy.nparray whereas I’d expect it to be a matplotlib.axes object (because self in the test case is the fig that I have previously created).

To confirm this, I looked at the test module. This code is from the test module:

class CatPlotTestCase(unittest.TestCase):

def setUp(self):

    self.fig = medical_data_visualizer.draw_cat_plot()

    self.ax = self.fig.axes[0]

If I then edit the test module to add print ("self.ax dtype\n", self.ax) I get the following output:

self.ax dtype
[<AxesSubplot:title={‘center’:‘cardio = 0’}, xlabel=‘variable’, ylabel=‘total’>
<AxesSubplot:title={‘center’:‘cardio = 1’}, xlabel=‘variable’>]

So it appears that this is as expected. I am completely baffled as to why it seems to think this is an npparray in the test case error messages.

It’s not as expected, tests expect figure object. Notice that you aren’t actually printing type of the self.ax, but just str representation of it. Using type(self.ax) instead likely would reveal numpy.ndarray.

You are correct, using type(self.ax) returned numpy.ndarray

You say that the tests expect figure object, however the medical_data_visualizer.py template states that we should use the sns.catplot() and sns.heatmap() methods which, as you have previously stated, return FacetGrid and ax. This doesn’t seem consistent to me but I’ll keep looking at it.

In case of sns.catplot it’s just needed to use certain attribute of the FacetGrid, which contains the figure.

@sanity is correct. If you dig through the seaborn and matplotlib documentation, you’ll find that many (not all) seaborn methods return a FacetGrid, which contains a matplotlib Figure object, which in turn holds an Axes object.

Looking at my code, I access the Figure in the FacetGrid returned by sns.catplot() by using its .fig property, and then you can get to the Axes objects by the Figure object’s .axes[...] list.

All the tests are checking things on the Axes object, and the tests get that object by calling your function, and assuming it returns a matplotlib Figure, from which it then gets the appropriate Axes object, and runs the tests. So if you return a FacetGrid when the test assumes a Figure, you get these errors because the FacetGrid has a numpy array stored at ax, and when self.ax.whatever() gets called, it’s not there because it is a numpy array and not a matplotlib Axes. Return the Figure from the FacetGrid once you are finished processing it and you should be in better shape although I found I needed to activate the Axes object on a Figure to get some tests to work.

1 Like

I finally got the catplot() aspect working, just had to add .fig to the end of the sns.catplot() method call (which I admit I saw on another thread).

I’ll look into the heatmap error now. Many thanks for your help with this topic it is much appreciated.

1 Like

I managed to get the heatmap errors cleared using .figure at the end of the heatmap call.

I have a different problem now.

When the test_heat_map_values test is executed I am getting an error because the expected list has an extra three empty values at the end.

======================================================================
FAIL: test_heat_map_values (test_module.HeatMapTestCase)

Traceback (most recent call last):
File “c:\Users\craig\Python\FCCProjects\Data Analysis with Python\Medical Data Visualizer\test_module.py”, line 47, in test_heat_map_values
self.assertEqual(actual, expected, “Expected differnt values in heat map.”)
AssertionError: Lists differ: ['0.0[607 chars] ‘0.2’, ‘0.1’, ‘0.1’, ‘-0.0’, ‘0.0’, ‘-0.0’, ‘0.1’] != ['0.0[607 chars] ‘0.2’, ‘0.1’, ‘0.1’, ‘-0.0’, ‘0.0’, ‘-0.0’, ‘0.1’, ‘’, ‘’, ‘’]

Second list contains 3 additional elements.
First extra element 91:
‘’

Diff is 989 characters long. Set self.maxDiff to None to see it. : Expected differnt values in heat map.


I had a look at previous posts for this issue and they suggested that I set the matplotlib version to “3.2.2” in the repl.it repository poetry.lock file. I don’t think this will help me as I am running the tests in VS Code and I am still seeing this issue, my python environment is currently using matplotlib version 3.3.2.

Edit. I have resolved this as well, had to revert matplotlib back to 3.1.3.

1 Like

@craig.lunney

now that your code is squared away… deleting the empty strings in test_module.py will fix the issue

my apologies for withdrawing the solution previously…