# ---
# 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 

#

# +
# Dependencies and Setup
# %matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np

# 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_df = pd.merge(mouse_metadata, study_results, how="outer", on="Mouse ID")
# Display the data table for preview
combined_df
# -

# Checking the number of mice.
unique_count = len(combined_df["Mouse ID"].value_counts())
num_dict = {
    "Number of Unique Mice": unique_count
}
num_of_mice = pd.DataFrame([num_dict])
num_of_mice

# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicated_mice = combined_df.loc[combined_df.duplicated(subset=['Mouse ID', 'Timepoint',]),:].value_counts()

# Optional: Get all the data for the duplicate mouse ID. 
duplicated_df = pd.DataFrame(duplicated_mice)
duplicated_df.head()

# Create a clean DataFrame by dropping the duplicate mouse by its ID
clean_df = combined_df[combined_df["Mouse ID"].isin(duplicated_df) == False]
clean_df.head()

# Checking the number of mice in the clean DataFrame
len(combined_df["Mouse ID"].value_counts())-1

# ## Summary Statistics

clean_df.columns

# +
# 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_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].mean()
median = clean_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].median()
variance = clean_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].var()
sd = clean_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].std()
sem = clean_df.groupby("Drug Regimen")["Tumor Volume (mm3)"].sem()

tumor_volume_stats = pd.DataFrame({
    "Mean": round(mean,2),
     "Median": round(median, 2),
     "Variance": round(variance, 2),
    "Standard Deviation": round(sd, 2),
    "SEM": round(sem, 2)
})

tumor_volume_stats
# -

# Using the aggregation method, produce the same summary statistics in a single line
drug_groupby = clean_df.groupby('Drug Regimen')
tumor_volume_stats_aggr = drug_groupby.agg(['mean','median','var','std','sem'])["Tumor Volume (mm3)"]
tumor_volume_stats_aggr

# ## Bar and Pie Charts

# Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
count_drugs = clean_df["Drug Regimen"].value_counts()
count_drugs.plot(kind="bar", title="Total Number of Measurements per Drug Regimen", xlabel="Drug Regimen", 
            ylabel="Total Number of Measurements (Count)", legend="best")

count_per_drug = pd.DataFrame(clean_df['Drug Regimen'].value_counts())
count_per_drug

# Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot
drugs = ["Capomulin", "Ramicane", "Ketapril", "Naftisol", "Zoniferol", "Stelasyn", "Placebo", "Infubinol", "Ceftamin", "Propriva"]
plt.xticks(rotation=45)
plt.bar(drugs, count_per_drug["Drug Regimen"], color="orange", align="center")
plt.title("Total Number of Measurements per Drug Regimen")
plt.xlabel("Drug Regimen")
plt.ylabel("Total Number of Measurements (Count)")

clean_df.head()

# Generate a pie plot showing the distribution of female versus male mice using pandas
groupby_sex = clean_df.groupby("Sex")["Mouse ID"].nunique()
groupby_sex_df = pd.DataFrame(groupby_sex)
explode = (0.1, 0)
groupby_sex_df["Mouse ID"].plot(kind="pie", title="Count Per Gender", legend="best", shadow = True, explode=explode, autopct="%1.1f%%")

groupby_sex_df

# Generate a pie plot showing the distribution of female versus male mice using pyplot
labels = ["Female", "Male"]
sizes = [124, 125]
colors = ["blue", "orange"]
explode = (0.1, 0)
plt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct="%1.1f%%", shadow=True)
plt.title("Count Per Gender")
plt.legend(loc="best")


# ## Quartiles, Outliers and Boxplots

clean_df.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
cap_df = clean_df.loc[clean_df["Drug Regimen"] == "Capomulin",:]
ram_df = clean_df.loc[clean_df["Drug Regimen"] == "Ramicane", :]
inf_df = clean_df.loc[clean_df["Drug Regimen"] == "Infubinol", :]
ceft_df = clean_df.loc[clean_df["Drug Regimen"] == "Ceftamin", :]    

#capomulin
cap_max = cap_df.groupby("Mouse ID")["Timepoint"].max()
cap_max = pd.DataFrame(cap_max)
cap_merge = pd.merge(cap_max, clean_df, on=("Mouse ID", "Timepoint"), how="left")
cap_merge.head()
# -

#ramicane
ram_max = ram_df.groupby("Mouse ID")["Timepoint"].max()
ram_max = pd.DataFrame(ram_max)
ram_merge = pd.merge(ram_max, clean_df, on=("Mouse ID", "Timepoint"), how="left")
ram_merge.head()

#infubinol
inf_max = inf_df.groupby("Mouse ID")["Timepoint"].max()
inf_max = pd.DataFrame(inf_max)
inf_merge = pd.merge(inf_max, clean_df, on=("Mouse ID", "Timepoint"), how="left")
inf_merge.head()

#ceftamin
ceft_max = ceft_df.groupby("Mouse ID")["Timepoint"].max()
ceft_max = pd.DataFrame(ceft_max)
ceft_merge = pd.merge(ceft_max, clean_df, on=("Mouse ID", "Timepoint"), how="left")
ceft_merge.head()

# +
# Put treatments into a list for for loop (and later for plot labels)
treatments = ["Capomulin", "Ramicane", "Infubinol", "Ceftamin"]
# Create empty list to fill with tumor vol data (for plotting)
tumor_volumes = []

# Calculate the IQR and quantitatively determine if there are any potential outliers. 
cap_volumes = cap_merge["Tumor Volume (mm3)"]
cap_quartiles = cap_volumes.quantile([0.25, 0.5, 0.75])
cap_iqr = cap_quartiles[0.75] - cap_quartiles[0.25]
tumor_volumes.append(cap_volumes)

ram_volumes = ram_merge["Tumor Volume (mm3)"]
ram_quartiles = ram_volumes.quantile([0.25, 0.5, 0.75])
ram_iqr = ram_quartiles[0.75] - ram_quartiles[0.25]
tumor_volumes.append(ram_volumes)

inf_volumes = inf_merge["Tumor Volume (mm3)"]
inf_quartiles = inf_volumes.quantile([0.25, 0.5, 0.75])
inf_iqr = inf_quartiles[0.75] - inf_quartiles[0.25]
tumor_volumes.append(inf_volumes)

ceft_volumes = ceft_merge["Tumor Volume (mm3)"]
ceft_quartiles = ceft_volumes.quantile([0.25, 0.5, 0.75])
ceft_iqr = ceft_quartiles[0.75] - ceft_quartiles[0.25]
tumor_volumes.append(ceft_volumes)
    
    # Determine outliers using upper and lower bounds
cap_lowerbound = cap_quartiles[0.25] - (1.5*cap_iqr)
cap_uperbound = cap_quartiles[0.75] + (1.5*cap_iqr)
    
ram_lowerbound = ram_quartiles[0.25] - (1.5*ram_iqr)
ram_uperbound = ram_quartiles[0.75] + (1.5*ram_iqr)
    
inf_lowerbound = inf_quartiles[0.25] - (1.5*inf_iqr)
inf_uperbound = inf_quartiles[0.75] + (1.5*inf_iqr)

ceft_lowerbound = ceft_quartiles[0.25] - (1.5*ceft_iqr)
ceft_uperbound = ceft_quartiles[0.75] + (1.5*ceft_iqr)
# -

# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
fig1, ax1 = plt.subplots()
ax1.set_title("Tumor Volume of Each Mouse for Each Durg Regimen")
ax1.set_ylabel("Tumor Volume (mm3)")
ax1.set_xlabel("Drug Regimen")
ax1.boxplot(tumor_volumes, labels=treatments, sym="b")

# ## Line and Scatter Plots

cap_df.head()

only_b742 = cap_df.loc[cap_df["Mouse ID"] == "b742",:]
only_b742.head()


# +
# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
time = only_b742["Timepoint"]
tumor_volume = only_b742["Tumor Volume (mm3)"]

volume_time_df = pd.DataFrame({
    "Time": time,
    "Tumor Volume": tumor_volume
})

volume_time_df.plot("Time", "Tumor Volume", kind="line", title="Tumor Volume for Mouse ID b742 vs. Time", xlabel="Time (Days)", ylabel="Tumor Volume (mm3)", marker="o")
# -

cap_df.head()

# +
# Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
avg_tumor = cap_df.groupby("Mouse ID")["Tumor Volume (mm3)"].mean()
mouse_weight = cap_df.groupby("Mouse ID")["Weight (g)"].mean()

tumor_volume_weight = pd.DataFrame({
    "Average Tumor Volume": avg_tumor,
    "Mouse Weight": mouse_weight
})
tumor_volume_weight.head()
# -

tumor_volume_weight.plot("Mouse Weight", "Average Tumor Volume", kind="scatter", title="Average Tumor Volume vs. Mouse Weight", xlabel= "Weight (g)", ylabel="Average Tumor Volume (mm3)")

# ## Correlation and Regression

# Calculate the correlation coefficient and linear regression model for mouse weight and average tumor volume for the Capomulin regimen
correlation = st.pearsonr(tumor_volume_weight["Mouse Weight"], tumor_volume_weight["Average Tumor Volume"])[0]
print(f"The correlation coefficient is {correlation}")

line_regress = st.linregress(tumor_volume_weight["Mouse Weight"], tumor_volume_weight["Average Tumor Volume"])
line_regress

x_value = tumor_volume_weight["Mouse Weight"]
y_value = tumor_volume_weight["Average Tumor Volume"]
regress_values = x_value * line_regress.slope + line_regress.intercept
line_eq = "y = " + str(round(line_regress.slope,2)) + "x + " + str(round(line_regress.intercept,2))
line_eq

tumor_volume_weight.plot("Mouse Weight", "Average Tumor Volume", kind="scatter", title="Average Tumor Volume vs. Mouse Weight", xlabel= "Weight (g)", ylabel="Average Tumor Volume (mm3)")
plt.plot(x_value, regress_values, "r-")
print(line_eq)



StarcoderdataJupyterScriptsDedupFiltered