# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Observations and Insights 

# ### 1.	There is a positive correlation between the mouse weight and the average tumor volume.
#
# ### 2.	The Drug Regimen “Capomulin” has the maximum mice number followed by the drug Regimen “Ramicane”.  The Drug Regimen “Propriva” has the smallest number of mice. There is a total number of 249 mice. 
#
#
# ### 3.	There are more male mice than female. There is 51% of male mice and there is 49% of female mice. 
#

# %matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# +
# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
from scipy.stats import linregress

# Study data files
mouse_metadata_path = "data/Mouse_metadata.csv"
study_results_path = "data/Study_results.csv"

# Read the mouse data and the study results
mouse_metadata = pd.read_csv(mouse_metadata_path)
study_results = pd.read_csv(study_results_path)

# Combine the data into a single dataset
combined = pd.merge(mouse_metadata, study_results, on=('Mouse ID'))

# Display the data table for preview
combined.head()
# -

# Checking the number of mice.
mice = len(combined['Mouse ID'].value_counts())
print(mice)

# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_mice = combined.loc[combined.duplicated(subset=['Mouse ID', 'Timepoint',]),'Mouse ID'].unique()
print(duplicate_mice)

# Optional: Get all the data for the duplicate mouse ID. 
duplicate_mice_data = combined.loc[combined['Mouse ID'] == 'g989']
duplicate_mice_data.head(20)

combined.rename(columns = {'Mouse ID':'Mouse_ID'}, inplace = True)

# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean = combined[combined.Mouse_ID != 'g989']
clean.head()

clean.to_csv("clean.csv")

# Checking the number of mice in the clean DataFrame.
mice_number = len(clean['Mouse_ID'].value_counts())
print(mice_number)

# ## Summary Statistics

# +
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen

# 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. 
# Assemble the resulting series into a single summary dataframe.

mean = clean.groupby(['Drug Regimen']).mean()['Tumor Volume (mm3)']
mean_df = mean.to_frame()
mean_df.rename(columns = {'Tumor Volume (mm3)':'Mean'}, inplace = True)


median = clean.groupby(['Drug Regimen']).median()['Tumor Volume (mm3)']
median_df = median.to_frame()
median_df.rename(columns = {'Tumor Volume (mm3)':'Median'}, inplace = True)


variance = clean.groupby(['Drug Regimen']).var()['Tumor Volume (mm3)']
variance_df = variance.to_frame()
variance_df.rename(columns = {'Tumor Volume (mm3)':'Variance'}, inplace = True)

standard_deviation = clean.groupby(['Drug Regimen']).std()['Tumor Volume (mm3)']
standard_deviation_df = standard_deviation.to_frame()
standard_deviation_df.rename(columns = {'Tumor Volume (mm3)':'Standard Deviation'}, inplace = True)

SEM = clean.groupby(['Drug Regimen']).sem()['Tumor Volume (mm3)']
SEM_df = SEM.to_frame()
SEM_df.rename(columns = {'Tumor Volume (mm3)':'SEM'}, inplace = True)

# Merging DataFrames
merge1 = pd.merge(mean_df, median_df, on="Drug Regimen")
merge2 = pd.merge(merge1, variance_df, on="Drug Regimen")
merge3 = pd.merge(merge2, standard_deviation_df, on="Drug Regimen")
summary_table = pd.merge(merge3, SEM_df, on="Drug Regimen")
summary_table.head()

# +
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen

# Using the aggregation method, produce the same summary statistics in a single line

clean.groupby('Drug Regimen').aggregate(['mean', 'median', 'var', 'std', 'sem'])['Tumor Volume (mm3)']
# -

# ## Bar and Pie Charts

# +
# Generate a bar plot showing the total number of unique mice tested on each drug regimen using pandas.

# Grouping the data by DataFrame by Drug Regimen and calculating the total number of mice using the count function
# Creating a DataFrame using the results
# Using DataFrame.plot() in order to create a bar chart of the data
# Setting a title for the chart
# Setting labels
mice_drug_regimen = clean.groupby("Drug Regimen").count()["Mouse_ID"]
mice_drug_regimen.plot(kind="bar", figsize = (10,5))
plt.title("Total number of Mice per Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Number of Mice")
# Generate a bar plot showing the total number of unique mice tested on each drug regimen using pyplot.plt.show()

# +
# Generate a bar plot showing the total number of unique mice tested on each drug regimen using pyplot.

# Renaming the Drug Regimen column in order to use it later in the "unique" function
# Getting the unique values in the Drug Regimen column
# Converting the data into a list
# Printing the list
clean.rename(columns = {'Drug Regimen':'Drug_Regimen'}, inplace = True)
drug_regimen = clean.Drug_Regimen.unique()
drug_regimen_list = drug_regimen.tolist()
drug_regimen_list

mice_drug_regimen_list = mice_drug_regimen.tolist()
mice_drug_regimen_list
fig1, ax1 = plt.subplots(figsize=(10, 5))
x_axis = np.arange(len(mice_drug_regimen))
plt.bar(x_axis, mice_drug_regimen_list, color='b', alpha=0.8, align='center')
tick_locations = [value for value in x_axis]
plt.xticks(tick_locations, drug_regimen_list)
# -

gender = clean.Sex.unique()
gender_list = gender.tolist()
gender_list

# +
# Generate a pie plot showing the distribution of female versus male mice using pandas

# Calculating the total number of mice
# Calculating the total number of mice by gender
# Calculating the percentage of the total number of mice by gender
# Putting results into a DataFrame
# Renaming the "Mouse_ID" column

total_mice = clean['Mouse_ID'].count()
mice_sex = clean.groupby("Sex").count()["Mouse_ID"]
mice_sex_per = (mice_sex / total_mice) * 100
mice_sex_per = mice_sex_per.to_frame()
mice_sex_per.rename(columns = {'Mouse_ID':'Total_Mice_Gender %'}, inplace = True)
mice_sex_per.head()

# Creating Pie chart 
colors = ['yellow','green']
explode = (0.1,0)
plot = mice_sex_per.plot.pie(y='Total_Mice_Gender %',figsize=(10,5), colors = colors, startangle=140, explode = explode, shadow = True, autopct="%1.1f%%")
plt.axis("equal")
plt.title("Distribution of Female vs Male mice")
plt.show()

# +
# Generate a pie plot showing the distribution of female versus male mice using pyplot

# Getting the unique values in the "Sex" column
# Converting the results into a list to be used when charting
gender = clean.Sex.unique()
gender_list = gender.tolist()
gender_list


# Calculating the total number of mice
# Calculating the total number of mice by gender
# Calculating the percentage of the total number of mice by gender
# Putting results into a DataFrame
# Renaming the "Mouse_ID" column
# Converting the new column to a list to be used when charting
total_mice = clean['Mouse_ID'].count()
mice_sex = clean.groupby("Sex").count()["Mouse_ID"]
mice_sex_per = (mice_sex / total_mice) * 100
mice_sex_per2 = mice_sex_per.to_frame()
mice_sex_per2.rename(columns = {'Mouse_ID':'Total_Mice_Gender'}, inplace = True)
mice_sex_list = mice_sex_per2['Total_Mice_Gender'].tolist()

# Telling matplotlib to create a pie chart based upon the above data
colors = ['yellow','green']
explode = (0.1,0)
fig1, ax1 = plt.subplots(figsize=(10, 5))
plt.pie(mice_sex_per, explode=explode, labels = gender_list, colors=colors,
        autopct="%1.1f%%", shadow=True, startangle=140)
plt.axis("equal")
plt.title("Distribution of Female vs Male mice")
plt.show()
# -

# ## Quartiles, Outliers and Boxplots

clean.head()

# +
# 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
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint

# Finding the greatest value in the Timepoint column using the max function

Capomulin = clean.loc[clean["Drug_Regimen"] == "Capomulin",:]
Ramicane = clean.loc[clean["Drug_Regimen"] == "Ramicane", :]
Infubinol = clean.loc[clean["Drug_Regimen"] == "Infubinol", :]
Ceftamin = clean.loc[clean["Drug_Regimen"] == "Ceftamin", :]

# Capomulin
final_Capomulin = Capomulin.groupby('Mouse_ID').max()['Timepoint']
Capomulin_volume = pd.DataFrame(final_Capomulin)
Capomulin_merged = pd.merge(Capomulin_volume, clean, on=("Mouse_ID","Timepoint"),how="left")
Capomulin_merged.head()

# +
# Capomulin quartiles and IQR
# Determining outliers using upper and lower bounds
Capomulin_tum = Capomulin_merged["Tumor Volume (mm3)"]

quartiles = Capomulin_tum.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq


print(f"The lower quartile of Capomulin tumors {lowerq}")
print(f"The upper quartile of Capomulin tumors {upperq}")
print(f"The interquartile range of Capomulin tumors {iqr}")
print(f"The median of Capomulin tumors: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)

print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# -

# Ramicane
final_Ramicane = Ramicane.groupby('Mouse_ID').max()['Timepoint']
Ramicane_volume = pd.DataFrame(final_Ramicane)
Ramicane_merged = pd.merge(Ramicane_volume, clean, on=("Mouse_ID","Timepoint"),how="left")
Ramicane_merged.head()

# +
# Ramicane quartiles and IQR
# Determining outliers using upper and lower bounds
Ramicane_tum = Ramicane_merged["Tumor Volume (mm3)"]

quartiles = Ramicane_tum.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq


print(f"The lower quartile of Ramicane tumors is: {lowerq}")
print(f"The upper quartile of Ramicane tumors is: {upperq}")
print(f"The interquartile range of Ramicane tumors is: {iqr}")
print(f"The median of Ramicane tumors is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)

print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# -

# Infubinol quartiles and IQR
# Determining outliers using upper and lower bounds
final_Infubinol = Infubinol.groupby('Mouse_ID').max()['Timepoint']
Infubinol_volume = pd.DataFrame(final_Infubinol)
Infubinol_merged = pd.merge(Infubinol_volume, clean, on=("Mouse_ID","Timepoint"),how="left")
Infubinol_merged.head()

# +
# Infubinol quartiles and IQR
# Determine outliers using upper and lower bounds
Infubinol_tum = Infubinol_merged["Tumor Volume (mm3)"]

quartiles = Infubinol_tum.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq


print(f"The lower quartile of Infubinol tumors is {lowerq}")
print(f"The upper quartile of Infubinol tumors is {upperq}")
print(f"The interquartile range of Infubinol tumors is {iqr}")
print(f"The median of Infubinol tumors is {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)


print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")
# -

# Ceftamin
final_Ceftamin = Ceftamin.groupby('Mouse_ID').max()['Timepoint']
Ceftamin_volume = pd.DataFrame(final_Ceftamin)
Ceftamin_merged = pd.merge(Ceftamin_volume, clean, on=("Mouse_ID","Timepoint"),how="left")
Ceftamin_merged.head()

# +
# Ceftamin quartiles and IQR
# Determining outliers using upper and lower bounds

Ceftamin_tum = Ceftamin_merged["Tumor Volume (mm3)"]

quartiles = Ceftamin_tum.quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of treatment is {lowerq}")
print(f"The upper quartile of temperatures is {upperq}")
print(f"The interquartile range of temperatures is {iqr}")
print(f"The the median of temperatures is {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)

print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")

# +
# Generating a box plot of the final tumor volume of each mouse across four regimens of interest
box_plot = [Capomulin_tum, Ramicane_tum, Infubinol_tum, Ceftamin_tum]
Regimen= ['Capomulin', 'Ramicane', 'Infubinol','Ceftamin']

fig1, ax1 = plt.subplots(figsize=(15, 10))
ax1.set_title('Final Tumor Volume',fontsize = 25)
ax1.set_ylabel('Final Tumor Volume (mm3)',fontsize = 14)
ax1.set_xlabel('Drug_Regimen',fontsize = 14)
ax1.boxplot(box_plot, labels=Regimen, widths = 0.4, patch_artist=True,vert=True)

plt.ylim(10, 80)

plt.show()
# -

# Saving image
plt.savefig("Images/box_plot.png", bbox_inches = "tight")

# ## Line and Scatter Plots

# Identifying a Capomulin "Mouse_ID"
Capomulin['Mouse_ID']

# Generating a line plot of tumor volume vs. time point for a mouse treated with Capomulin
line_df = Capomulin.loc[Capomulin["Mouse_ID"] == "s185",:]
line_df.head()

# +
# Plotting
x_axis = line_df["Timepoint"]
tumor_size = line_df["Tumor Volume (mm3)"]

fig1, ax1 = plt.subplots(figsize=(15, 10))
plt.title('Capomulin treatment of mouse s185',fontsize =25)
plt.plot(x_axis, tumor_size,linewidth=2, markersize=15,marker="o",color="blue", label="Fahreneit")
plt.xlabel('Timepoint (Days)',fontsize =14)
plt.ylabel('Tumor Volume (mm3)',fontsize =14)

plt.show()
# -

# Saving image
plt.savefig("Images/line_graph.png", bbox_inches = "tight")

# +
# Generating a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen

fig1, ax1 = plt.subplots(figsize=(15, 10))
average_vol_capomulin = Capomulin.groupby(['Mouse_ID']).mean()

marker_size=15
plt.scatter(average_vol_capomulin['Weight (g)'],average_vol_capomulin['Tumor Volume (mm3)'],s=175, color="blue")
plt.title('Mouse Weight vs. Average Tumor Volume',fontsize =25)
plt.xlabel('Weight (g)',fontsize =14)
plt.ylabel('Averag Tumor Volume (mm3)',fontsize =14)

plt.show()
# -

plt.savefig("Images/scatterplot.png", bbox_inches = "tight")

# ## Correlation and Regression

# +
# Calculatng the correlation coefficient and linear regression model 
# for mouse weight and average tumor volume for the Capomulin regimen

correlation_coefficient = round(st.pearsonr(average_vol_capomulin['Weight (g)'],average_vol_capomulin['Tumor Volume (mm3)'])[0],2)
print(f"The correlation between mouse weight and average tumor volume is {correlation_coefficient}")

# +
# Plotting the linear regression model on top of the previous scatter plot.

x_values = average_vol_capomulin['Weight (g)']
y_values = average_vol_capomulin['Tumor Volume (mm3)']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regression_values = x_values * slope + intercept

print(f"Slope:{slope}")
print(f"Intercept:{intercept}")
print(f"rvalue (Correlation coefficient) is {rvalue}")
print(f"pandas (Correlation coefficient)is {correlation_coefficient}")
print(f"stderr is {stderr}")

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

print(line_eq)

# +
# Adding the linear regression equation and line to plot

fig1, ax1 = plt.subplots(figsize= (15, 10))
plt.scatter(x_values, y_values, s = 175, color="blue")
plt.plot(x_values, regression_values, "r-")
plt.title('Regression Plot of Mouse Weight vs. Average Tumor Volume',fontsize =20)
plt.xlabel('Weight(g)',fontsize =14)
plt.ylabel('Average Tumor Volume (mm3)',fontsize =14)
ax1.annotate(line_eq, xy= (20, 40), xycoords ='data', xytext = (0.8, 0.95), textcoords = 'axes fraction', horizontalalignment = 'right', verticalalignment = 'top',fontsize = 30, color = "red")

print(f"The r-squared is: {rvalue**2}")

plt.show()
# -

# Saving image
plt.savefig("Images/linear_regression.png", bbox_inches = "tight")

StarcoderdataJupyterScriptsDedupFiltered