<jupyter_start><jupyter_text>Observations and Insights<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import os as _dir
import numpy as np

#gets the working directory and opens both csv files to a variable
working_dir = _dir.getcwd()
mouse_metadata_path = "Mouse_metadata.csv"
study_results_path = "Study_results.csv"

for root, dirs, files in _dir.walk(working_dir):
    if mouse_metadata_path in files:
        mouse_metadata_path = (_dir.path.join(root, mouse_metadata_path))
    if study_results_path in files:
        study_results_path = (_dir.path.join(root, study_results_path)) 
        
mouse_Metadata_df = pd.read_csv(mouse_metadata_path)
study_Results_df = pd.read_csv(study_results_path)

# Combine the data into a single dataset
merged_df = pd.merge(study_Results_df, mouse_Metadata_df, on="Mouse ID", how="left"  )
# Display the data table for preview
merged_df.head()

# Checking the number of mice.
mice_count= len(merged_df["Mouse ID"].unique())
mice_count
# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicated_Mice = merged_df.loc[merged_df.duplicated(subset=["Mouse ID", "Timepoint"]),"Mouse ID"].unique()
duplicated_Mice

# Create a clean DataFrame by dropping the duplicate mouse by its ID.
cleaned_df = merged_df[merged_df["Mouse ID"].isin(duplicated_Mice)== False]
cleaned_df

# Checking the number of mice in the clean DataFrame.
Clean_count = len(cleaned_df["Mouse ID"].unique())
Clean_count<jupyter_output><empty_output><jupyter_text>Summary Statistics<jupyter_code># Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
summary_df = cleaned_df.loc[:,["Mouse ID", "Drug Regimen" , "Tumor Volume (mm3)"]]

# Use groupby and summary statistical methods to calculate the following properties of each drug regimen: 
# mean, median, variance, standard deviation, and SEM of the tumor volume. 
summary_Mean = summary_df.groupby(["Drug Regimen"]).mean()["Tumor Volume (mm3)"]
summary_Median = summary_df.groupby(["Drug Regimen"]).median()["Tumor Volume (mm3)"]
summary_Variance = summary_df.groupby(["Drug Regimen"]).var()["Tumor Volume (mm3)"]
summary_Standard_Deviation = summary_df.groupby(["Drug Regimen"]).std()["Tumor Volume (mm3)"]
summary_SEM = summary_df.groupby(["Drug Regimen"]).sem()["Tumor Volume (mm3)"]


# Assemble the resulting series into a single summary dataframe.
summary_final_df = pd.DataFrame({
                                 "Mean of Tumor Volume": summary_Mean,
                                 "Median of Tumor Volume": summary_Median,
                                 "Variance of Tumor Volume": summary_Variance,
                                 "Standard Deviation of Tumor Volume": summary_Standard_Deviation,
                                 "SEM of Tumor Volume": summary_SEM,
                                 })


# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
summary_final_df=summary_final_df.round(2)
summary_final_df<jupyter_output><empty_output><jupyter_text>Bar and Pie Charts<jupyter_code># Generate a bar plot showing the total number of unique mice tested on each drug regimen using pandas.
bar_Counts = cleaned_df["Drug Regimen"].value_counts()
#y values set to values of drug regimen 
y_axis = bar_Counts.values
#x values set to the index of drug regimens
x_axis = bar_Counts.index
#created bar graph, with lables, and titles, set to green color for personal visual preferance, and tick rotation at 45 for readability
bar_Counts.plot(kind = "bar", color= "green")
plt.ylabel("Count")
plt.xlabel("Name of Drugs")
plt.title("Mice Drug Regimen")
plt.xticks(rotation=45)
plt.show()
#saving chart to file
plt.savefig('Mice_Drug_Regimen_bargraph.png')
# Generate a bar plot showing the total number of unique mice tested on each drug regimen using pyplot.
bar_Counts = cleaned_df["Drug Regimen"].value_counts()

x_axis = np.arange(0,len(y_axis))

#y values set to values of drug regimen 
y_axis = bar_Counts.values
#x values set to the index of drug regimens
x_value = bar_Counts.index

#created bar graph, with lables, and titles, set to green color for personal visual preferance, and tick rotation at 45 for readability
plt.bar(x_value, y_axis, facecolor="green", alpha=0.75, align="center")
plt.ylabel("Count")
plt.xlabel("Name of Drugs")
plt.title("Mice Drug Regimen")
plt.xticks(rotation=45)
plt.show()
#saving chart to file
plt.savefig('Mice_Drug_Regimen_bargraph_pyplot.png')
# Generate a pie plot showing the distribution of female versus male mice using pandas
gender_count = cleaned_df["Sex"].value_counts()
#set the Percents of the each gender
gender_Percent = gender_count.values
#set explode to .05 to create small explode gap
gender_Explode = [0.05, 0]
#created labes for data group
gender_Labels = gender_count.index
#set colors to green and pink
gender_Colors = ["green", "pink"]
#set title
plt.title("Gender of Mice")
#created pie plot with variables above
gender_count.plot(kind= "pie", explode=gender_Explode, colors=gender_Colors, autopct="%1.1f%%",shadow=True, startangle=45 )
#got ride of the label that showed "sex"
plt.ylabel(" ")
#print plot
plt.show() 
#save to file
plt.savefig('Gender_of_Mice_Pie.png')
# Generate a pie plot showing the distribution of female versus male mice using pyplot
gender_count = cleaned_df["Sex"].value_counts()
#set the Percents of the each gender
gender_Percent = gender_count.values
#set explode to .05 to create small explode gap
gender_Explode = [0.05, 0]
#created labes for data group
gender_Labels = gender_count.index
#set colors to green and pink
gender_Colors = ["green", "pink"]
#set title
plt.title("Gender of Mice")
#created pie plot with variables above
plt.pie(gender_Percent, explode=gender_Explode, labels=gender_Labels, colors=gender_Colors,
        autopct="%1.1f%%", shadow=True, startangle=45)
#print plot
plt.show()
#save to file
plt.savefig('Gender_of_Mice_Pie_pyplot.png')<jupyter_output><empty_output><jupyter_text>Quartiles, Outliers and Boxplots<jupyter_code># Calculate the final tumor volume of each mouse across four of the treatment regimens:  # Capomulin, Ramicane, Infubinol, and Ceftamin
# Start by getting the last (greatest) timepoint for each mouse
final_Tumor_Volume = cleaned_df.groupby("Mouse ID").max().reset_index()
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
ftv_merged= final_Tumor_Volume[["Mouse ID", "Timepoint"]].merge(cleaned_df, on=["Mouse ID", "Timepoint"], how="left")
#rounded to 2 decimals
ftv_merged = ftv_merged.round(2)
#view data
ftv_merged
# Put treatments into a list for for loop (and later for plot labels)
# Create empty list to fill with tumor vol data (for plotting)
# Calculate the IQR and quantitatively determine if there are any potential outliers. 
# Determine outliers using upper and lower bounds
# done exact same for each of the 4 medications chosen below
capomulin_df= ftv_merged.loc[ftv_merged['Drug Regimen'] == 'Capomulin']['Tumor Volume (mm3)']
#set quartiles
cap_quartile= capomulin_df.quantile([.25,.5,.75])
#set lower quartile 
cap_lower= cap_quartile[.25]
#set upper quartile
cap_upper= cap_quartile[.75]
#set iqr using uper and lower 
cap_iqr= cap_upper-cap_lower
#calculations for lower and uper outlier information
cap_out_lower= (cap_lower-(1.5*cap_iqr))
cap_out_upper= cap_upper+(1.5*cap_iqr)
#out put outlier limits
print(f'Below : {round(cap_out_lower,2)} is an outlier')
print(f'Above : {round(cap_out_upper,2)} is an outlier')
ramicane_df= ftv_merged.loc[ftv_merged['Drug Regimen'] == 'Ramicane']['Tumor Volume (mm3)']
#set quartiles
ram_quartile= ramicane_df.quantile([.25,.5,.75])
#set lower quartile 
ram_lower= ram_quartile[.25]
#set upper quartile
ram_upper= ram_quartile[.75]
#set iqr using uper and lower 
ram_iqr= ram_upper-ram_lower
#calculations for lower and uper outlier information
ram_out_lower= (ram_lower-(1.5*ram_iqr))
ram_out_upper= ram_upper+(1.5*ram_iqr)
#out put outlier limits
print(f'Below : {round(ram_out_lower,2)} is an outlier')
print(f'Above : {round(ram_out_upper,2)} is an outlier')
infubinol_df=ftv_merged.loc[ftv_merged['Drug Regimen'] == 'Infubinol']['Tumor Volume (mm3)']
#set quartiles
inf_quartile= infubinol_df.quantile([.25,.5,.75])
#set lower quartile 
inf_lower= inf_quartile[.25]
#set upper quartile
inf_upper= inf_quartile[.75]
#set iqr using uper and lower
inf_iqr= inf_upper-inf_lower
#calculations for lower and uper outlier information
inf_out_lower= (inf_lower-(1.5*inf_iqr))
inf_out_upper= inf_upper+(1.5*inf_iqr)
#out put outlier limits
print(f'Below : {round(inf_out_lower,2)} is an outlier')
print(f'Above : {round(inf_out_upper,2)} is an outlier')
ceftamin_df= ftv_merged.loc[ftv_merged['Drug Regimen'] == 'Ceftamin']['Tumor Volume (mm3)']
#set quartiles
cef_quartile= ceftamin_df.quantile([.25,.5,.75])
#set lower quartile 
cef_lower= cef_quartile[.25]
#set upper quartile
cef_upper= cef_quartile[.75]
#set iqr using uper and lower
cef_iqr= cef_upper-cef_lower
#calculations for lower and uper outlier information
cef_out_lower= (cef_lower-(1.5*cef_iqr))
cef_out_upper= cef_upper+(1.5*cef_iqr)
#out put outlier limits
print(f'Below : {round(cef_out_lower,2)} is an outlier')
print(f'Above : {round(cef_out_upper,2)} is an outlier')

# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
#creating dict for flierprops
green_tri = dict(markerfacecolor='g', marker='^')
#setting subplots ( first try at this )
fig, ax = plt.subplots()
#setting title
ax.set_title('Final Tumor Volumes')
#setting y label
ax.set_ylabel("Tumor Volume (mm3)")
#creating boxplot using above variables
ax.boxplot([capomulin_df, ramicane_df, infubinol_df, ceftamin_df], labels=["Capomulin", "ramicane", "Infubinol", "Ceftamin"], flierprops=green_tri)
#saving plot to file
plt.savefig('Final_Tumor_Volume_BoxPlot.png')<jupyter_output><empty_output><jupyter_text>Line and Scatter Plots<jupyter_code># Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
# setting specific Rat used
rat= cleaned_df.loc[cleaned_df['Mouse ID'] == 'l509']
#plotting line graf of time pont vs tumor volume
plt.plot(rat["Timepoint"], rat["Tumor Volume (mm3)"])
#labels of x and y
plt.xlabel("Time point")
plt.ylabel("Tumor Volume (mm3)")
#title
plt.title("tumor volume vs. time point")
#printing out the plot
plt.show()
#save file
plt.savefig('Tumor_Volume_Vs_Time_Line.png')
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
# using the drug regeimen capomulin
scatter_Capomulin= cleaned_df.loc[cleaned_df['Drug Regimen'] == 'Capomulin']
#getting single mouse ID for the group 
scatter_mean= scatter_Capomulin.groupby(["Mouse ID"]).mean()
#setting up scatter plot with green stars
plt.scatter(scatter_mean["Weight (g)"],scatter_mean["Tumor Volume (mm3)"], c="g", marker="*")
#set title, and y, x labels
plt.title("Mouse Weight vs Tumor Volume")
plt.ylabel("Tumor volume")
plt.xlabel("Weight of Mouse")
#show plot
plt.show()
#save to file
plt.savefig('Weight_vs_Tumor_Volume_ scatter.png')<jupyter_output><empty_output><jupyter_text>Correlation and Regression<jupyter_code># Calculate the correlation coefficient and linear regression model 
# for mouse weight and average tumor volume for the Capomulin regimen
#followed linregress settup for weight and TVMM3
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(scatter_mean["Weight (g)"], scatter_mean["Tumor Volume (mm3)"])
#set regress value to formula
r_v= scatter_mean["Weight (g)"]* slope + intercept
#set up scatter plot 
plt.scatter(scatter_mean["Weight (g)"],scatter_mean["Tumor Volume (mm3)"], c="g", marker="*")
#ploted line based of mean weight, in black
plt.plot(scatter_mean["Weight (g)"], r_v, color='black')
#labes and title
plt.title("Mouse Weight vs Tumor Volume")
plt.ylabel("Tumor volume")
plt.xlabel("Weight of Mouse")
#show plot
plt.show()
#save file
plt.savefig('Weight_vs_Tumor_Volume_Correlation_Regression.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10745091596155426, 'fraction_numerical': 0.009529286125030807, 'mean_word_length': 5.847019122609674, 'pattern_counts': {'":': 5, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '463959', 'license': 'ADSL', 'n_tokens_mistral': 4285, 'n_tokens_neox': 3990, 'n_words': 1242, 'path': 'Pymaceuticals_pymaceuticals_InProgress.ipynb', 'repo_name': 'Gerovrik/Matplotlib_Challenge', 'size': '12173'}