Data Analysis with Python Projects - Medical Data Visualizer

I am stuck in something I am not sure I can fix.

When running the code I have a fail in:
"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 different values in heat map.”)
AssertionError: Lists differ: != [‘0.0’, ‘0.0’, ‘-0.0’, ‘0.0’, ‘-0.1’, ‘0.5[616 chars]0.1’]

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

Diff is 941 characters long. Set self.maxDiff to None to see it. : Expected different values in heat map."

For start, it appears the ‘actual’ list is empty? (‘Lists differ: != [‘0.0’, ‘0.0’, ‘-0.0’, ‘0.0’, ‘-0.1’, ‘0.5[616 chars]0.1’]’)
This cannot be as the correlation is performed and sns.heatmap creates the heatmap with values every time.

In any case, comparing the value of the correlation (executed in visual studio) to the “expected” values, I see they differ (for instance at the last value I have 0.2, rounded up by heatmap from corr=0.150, and it is expected 0.1, in position 10th I have 0.2, rounded down by heatmap from corr=0.240, and it is expected 0.3,…)

I am blocked as I don’t see how I could fix the issue.

Thank you in advance for any light that could be shared

I include the code for the function “draw_heatmap”

Your code so far

def draw_heat_map():
# Clean the data
print(df.shape)
df_heat = df.loc[(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))]
#print(df_heat.shape)
# Calculate the correlation matrix
corr = df_heat.corr()

# Generate a mask for the upper triangle

mask = np.triu(np.ones_like(corr).astype(bool))

# Set up the matplotlib figure

ax = sns.heatmap(data=corr, mask=mask, fmt=‘.1f’, annot=True)
fig= ax.get_figure()

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

# Do not modify the next two lines

fig.savefig(‘heatmap.png’)
return fig

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Data Analysis with Python Projects - Medical Data Visualizer

Link to the challenge:

Please just post the link to your repl.it or use code blocks (the </> button on the post editor if you do post code directly.

Run your project and look at the heatmap and it looks like

The fig object is a singleton. When you don’t reinitialize it, it just gets reused. If you reinitialize fig, then you just have one plot on your fig.

Next, you have problems with your data cleaning. First,

df['overweight'] = (df['weight']/(df['height']/100)**2).astype(int)

rounds each value down as it converts to int. That makes some values that would be above 25 equal to 25. Second,

  df_heat = df.loc[(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))]

contains incorrect comparisons. since you want to exclude stuff outside the quantiles, not on the quantiles. pandas warns on those * operators here and prefers &.

Thank you again…

Sorry about the copy paste (I had seen others so I though it was the way to do it)

I saw that problem with the figure but I wasn’t able to fix it… I tried restarting the variables but didn’t do the trick. Being different functions I expected catplot and heatmap not to intersect.

I thought “and”, “&” and “*” had the same use as operators. I know better now.

Good learning. Thank you.

Kindest regards

Hello again,

I still have the list = when it should be full of data. The data that is being printed in the figures.
I am trying to navigate ‘test_module’ but it is getting confusing.

About the reinitialising of fig. I am trying to find a solution.
On Visual Studio works perfectly with ‘fig= sns.reset.orig()’ but the same in Repl doesn’t work (which is very frustrating, the same language works one place and not the other)

Thank you

I have looked into "test_module’. copied the part (failing me) to Visual Studio (copied into main.py) and I made it work (get a non empty list) by adding ‘()’ to:
text.get_text in ‘def test_heat_map_values’

However when I did that in Repl it didn’t do anything… still empty

This is what I added to “main” in Visual to replicate what I think we have in Repl:
fig=medical.draw_heat_map()

#print(fig.axes[0].get_default_bbox_extra_artists())

ax=fig.axes[0]

#print(ax.get_default_bbox_extra_artists()[1].get_text())

actual = [text.get_text() for text in ax.get_default_bbox_extra_artists() if isinstance(text, plt.text.Text)]

and this gave me the list of values expected.
[‘0.0’, ‘0.0’, ‘-0.0’, ‘0.0’, ‘-0.1’, ‘0.5’, ‘0.0’, …, ‘0.1’]

But not in Repl

After this I did:

print(self.ax.get_default_bbox_extra_artists()) and in Repl I had:

[<matplotlib.patches.Rectangle object at 0x7fcf90d5fb80>, <matplotlib.patches.Rectangle object at 0x7fcf90d5fdc0>, <matplotlib.patches.Rectangle object at 0x7fcf90d5ff40>,…]

but in Visual I had:

[<matplotlib.collections.QuadMesh object at 0x000001D7FD8CF5E0>, Text(0.5, 1.5, ‘0.0’), Text(0.5, 2.5, ‘0.0’), Text(1.5, 2.5, ‘-0.0’), Text(0.5, 3.5, ‘0.0’), Text(1.5, 3.5, ‘-0.1’), Text(2.5, 3.5, ‘0.5’),…]

Strange

Regards,

German

You first created one with

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

This code also creates one

since draw_heat_map() returns a matplotlib figure.

Any function like that that will create a new figure will work, so other calls, with assignment, to sns.* or fig, ax = plt.subplots(...) from matplotlib will create a new figure. You just don’t need to reuse the figure or axes between plots.

As far as the rest, I see so many questions and problems from visual studio, pycharm, and jupyter users when they try to migrate to repl.it or their local system python. They must have some method of automagically configuring their environment and dependencies, but unfortunately you have to handle all that on repl.it or using a system python. I would avoid changing the test module for debugging at this point as you are likely to cause more issues than you solve until you get your heat map code working although sometimes it’s a good idea to copy that code to run separately from the test to attempt debugging.

1 Like

I managed finally to reinitialize the plot.

Now to fix other issues.