Medical data visualizer - heat map

Hi all
i am trying to get the medical data visualizer done and am noticing an issue.
Basically, I am getting a 967 character difference (guess that is from rounding off) but a difference of -0.1 in one item of the correlation.
This is causing the failure that I am getting. The rest seems fine.

I am getting this error (difference is highlighted):

...['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.1', '0.0', '0.2', '0.1', '0.0', '0.1', '-0.0', '-0.1', '0.1', '0.0', '0.2', '0.0', '0.1', '-0.0', '-0.0', '0.1', '0.0', '0.1', '0.4', '-0.0', '-0.0', '0.3', '0.2', '0.1', '-0.0', '0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.2', '0.1', '0.1', '0.0', '0.0', '0.0', '0.0', '0.3', '0.0', '-0.0', '0.0', '-0.0', '-0.0', '-0.0', '0.0', '0.0', '-0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '-0.0', '0.2', '0.1', '0.3', '0.2', '0.1', '-0.0', '-0.0', '-0.0', '-0.0', '0.1', '-0.1', '**-0.2'**, '0.7', '0.0', '0.2', '0.1', '0.1', '-0.0', '0.0', '-0.0', '0.1']

F

FAIL: test_heat_map_values (test_module.HeatMapTestCase)

Traceback (most recent call last):
File β€œ/home/runner/boilerplate-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[585 chars] β€˜-0.2’, β€˜0.7’, β€˜0.0’, β€˜0.2’, β€˜0.1’, β€˜0.1’, β€˜-[22 chars]0.1’] != ['0.0[585 chars] β€˜-0.1’, β€˜0.7’, β€˜0.0’, β€˜0.2’, β€˜0.1’, β€˜0.1’, β€˜-[22 chars]0.1’]

First differing element 81:
β€˜-0.2’
β€˜-0.1’

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


Ran 4 tests in 6.488s

FAILED (failures=1)

this is my code:

def draw_heat_map():
    # Clean the data
    df_heat = df[
    (df['ap_lo'] <= df['ap_hi']) & 
    (df['height'] >= (df['height'].quantile(0.025))) &
    (df['height'] <= (df['height'].quantile(0.975))) &
    (df['weight'] >= (df['weight'].quantile(0.025))) &
    (df['weight'] <= (df['weight'].quantile(0.975)))
    ]

    # Calculate the correlation matrix
    corr = df_heat.corr()

    # Generate a mask for the upper triangle
    mask = np.triu(corr)

    # Set up the matplotlib figure
    fig, ax = plt.subplots(figsize=(9,9))

    # Draw the heatmap with 'sns.heatmap()'

    sns.heatmap(corr,annot=True, fmt='.1f', linewidths=1, mask=mask, vmax=.8, center=0,square=True, cbar_kws = {'shrink':0.5})

thanks for anyone that can help and kind regards

This is one of the more important things you can do when a test fails and you don’t know why. When I set this and rerun your tests, the errors are much better:

FAIL: test_heat_map_labels (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-da-medical-visualizer/test_module.py", line 89, in test_heat_map_labels
    self.assertEqual(
AssertionError: Lists differ: ['id'[74 chars]luc', 'smoke', 'alco', 'active', 'cardio', 'bmi', 'overweight'] != ['id'[74 chars]luc', 'smoke', 'alco', 'active', 'cardio', 'overweight']

First differing element 13:
'bmi'
'overweight'

First list contains 1 additional elements.
First extra element 14:
FAIL: test_heat_map_values (test_module.HeatMapTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/gray/src/work/fcc-da-medical-visualizer/test_module.py", line 196, in test_heat_map_values
    self.assertEqual(
AssertionError: Lists differ: ['0.0[585 chars] '-0.3', '0.9', '0.0', '0.2', '0.2', '0.1', '-[125 chars]0.7'] != ['0.0[585 chars] '-0.1', '0.7', '0.0', '0.2', '0.1', '0.1', '-[22 chars]0.1']

First differing element 81:
'-0.3'
'-0.1'

First list contains 14 additional elements.

Additionally, if you compare your generated heatmap with the example, you see that you have generated an extra column and row, which correlates with the output of the tests. The first test is telling you which extra row/column you included:

First differing element 13:
'bmi'

which makes sense since bmi was used to generate overweight and is not in the example heatmap.

hi Jeremy,
thanks, where did you do this?

thanks

For the heatmap test I added it to the test setup:

class HeatMapTestCase(unittest.TestCase):
    def setUp(self):
        self.fig = medical_data_visualizer.draw_heat_map()
        self.ax = self.fig.axes[0]
        self.maxDiff = None

The unittest docs say it is an attribute of the unittest class, so you should be able to set it in any individual test you want or for all the tests in a class if you put it in the class setUp() method.

Hi Jeremy,

The self.maxDiff = None tip is helpful. I manage to pin point the errors for my heatmap to an issue with rounding off and solved it. But I couldn’t use it to understand the error thrown by the bar plot tests.

It seems like the object created does not have certain attributes?

My code is as follows:

def draw_cat_plot():
    # Create DataFrame for cat plot using `pd.melt` using just the values from 'cholesterol', 'gluc', 'smoke', 'alco', 'active', and 'overweight'.
    df_cat = pd.melt(df, value_vars=["active", "alco", "cholesterol", "gluc", "overweight", "smoke"], id_vars="cardio")
    

    # Group and reformat the data to split it by 'cardio'. Show the counts of each feature. You will have to rename one of the collumns for the catplot to work correctly.
    df_cat = df_cat.groupby(['cardio', 'variable', 'value'], as_index = False).size().rename(columns={'size':'total'})
    

    # Draw the catplot with 'sns.catplot()'
    fig = sns.catplot(
    x = 'variable',
    y = 'total',
    kind = 'bar',
    hue = 'value',
    col = 'cardio',
    data = df_cat
    )


    # Do not modify the next two lines
    fig.savefig('catplot.png')
    return fig

The error i received which i can’t solve is as follow:

Thanks in advance for your help.

The seaborn documentation is slightly helpful if you dig through it. sns.catplot() returns a FacetGrid object, which contains a matplotlib fig object, which has the axes object the test wants.

So your fig is actually a seaborn FacetGrid and the test actually needs fig.fig in your case.

1 Like

Thanks Jeremy, it solved the errors with the tests.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.