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

# # Executive Summary
#
# - There is no significant differences between mice gender in the study.
# - There is significant evidence that shows that drugs have a direct influence in tumor sizes.
# - Capomulin succesfully reduces tumor sizes in mices with lower variances, standard deviations and SEM.
# - There is a positive correlation between weight and tumor size.
# - Tumor size can be predicted using linear regression using weight as an input.

# # Observations and Insights

# +
# Dependencies and setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np
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
single_dataset = pd.merge (mouse_metadata, study_results, how = "outer", on="Mouse ID")

# Display the data table for preview
display(single_dataset)
# -

 # Checking the number of mice.
unique_id = len(single_dataset["Mouse ID"].unique())
print("Number of mice: " + str(unique_id))


 # Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint.
 # Comment: Eliminating duplicate Mice ID and Timepoint combo does not alter the number of mice.
non_duplicates = single_dataset.drop_duplicates(subset = ['Mouse ID', 'Timepoint'], keep = False).reset_index(drop = True)
non_duplicates

# Optional: Get all the data for the duplicate mouse ID.
the_duplicates = single_dataset[single_dataset[['Mouse ID', 'Timepoint']].duplicated(keep = False) == True]
the_duplicates

# Generate a summary statistics table consisting of the mean, median, variance, standard deviation, 
# and SEM of the tumor volume for *each drug regimen*.
# Data frame is non_duplicates
tv_mean = non_duplicates.groupby(["Drug Regimen"]).mean()["Tumor Volume (mm3)"]
# NOT NECESSARY ANYMORE Drop extra columns df.drop(['B', 'C'], axis=1)
# Rename Column
tv_mean = tv_mean.reset_index()
tv_mean = tv_mean.rename(columns={"Tumor Volume (mm3)":"Mean"})
# Same for the rest of statistics
# Median
tv_median = non_duplicates.groupby(["Drug Regimen"]).median()["Tumor Volume (mm3)"]
tv_median = tv_median.reset_index()
tv_median = tv_median.rename(columns={"Tumor Volume (mm3)":"Median"})
# Variance
tv_var = non_duplicates.groupby(["Drug Regimen"]).var()["Tumor Volume (mm3)"]
tv_var = tv_var.reset_index()
tv_var = tv_var.rename(columns={"Tumor Volume (mm3)":"Variance"})
# Standard Deviation
tv_std = non_duplicates.groupby(["Drug Regimen"]).std()["Tumor Volume (mm3)"]
tv_std = tv_std.reset_index()
tv_std = tv_std.rename(columns={"Tumor Volume (mm3)":"St Dev"})
# SEM
tv_sem = non_duplicates.groupby(["Drug Regimen"]).sem()["Tumor Volume (mm3)"]
tv_sem = tv_sem.reset_index()
tv_sem = tv_sem.rename(columns={"Tumor Volume (mm3)":"SEM"})
# Using the aggregation method, produce the same summary statistics in a single line
# Integrate the table
# Just realized in could've a lot simpler
tv_mean["Median"] = tv_median["Median"]
tv_mean["Variance"] = tv_var["Variance"]
tv_mean["St Dev"] = tv_std["St Dev"]
tv_mean["SEM"] = tv_sem["SEM"]
tv_mean

# Generate a bar plot showing the total number of measurements taken on each drug regimen using pandas.
counts = non_duplicates["Drug Regimen"].value_counts()
counts.plot(kind="bar")
plt.xlabel("Drug Regimen")
plt.xticks(rotation=45)
plt.ylabel("Count")
plt.show()

 # Generate a bar plot showing the total number of measurements taken on each drug regimen using pyplot.
counts = non_duplicates["Drug Regimen"].value_counts()
plt.bar(counts.index.values,counts.values)
plt.xlabel("Drug Regimen")
plt.xticks(rotation=45)
plt.ylabel("Count")
plt.show()

# Generate a pie plot showing the distribution of female versus male mice using pandas
m_f = non_duplicates["Sex"].value_counts()
pie_sex_mice = m_f.plot.pie(autopct="%1.2f%%", startangle=66, title="Gender Distribution")
pie_sex_mice.set_ylabel('');

 # Generate a pie plot showing the distribution of female versus male mice using pyplot
sex_mice = non_duplicates['Sex'].value_counts()
pie_sex_mice
plt.title("Gender Distribuition")
labels = ["Male", "Female"]
plt.pie(sex_mice, autopct="%1.2f%%",  startangle=66);

#  # Quartiles, Outliers and Boxplots
#

# 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
greatest_tp = pd.DataFrame(non_duplicates.groupby('Mouse ID')['Timepoint'].max())
greatest_tp.rename(columns = {'Timepoint':'Greatest TP'}, inplace = True)
# greatest_tp
# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
gr_tp_m = pd.merge(non_duplicates, greatest_tp, how='outer', on='Mouse ID')
gr_tp_m

# +
# Put treatments into a list for for loop (and later for plot labels)
regimen = ['Capomulin', 'Ramicane', 'Infubinol', 'Ceftamin']

# Create empty list to fill with tumor vol data (for plotting)
treatment = []

# Calculate the IQR and quantitatively determine if there are any potential outliers. 

    
    # Locate the rows which contain mice on each drug and get the tumor volumes
for drug in regimen:
    max_per_regimen = gr_tp_m.loc[gr_tp_m['Drug Regimen'] == drug]
    fin_volume = max_per_regimen.loc[max_per_regimen['Timepoint'] == max_per_regimen['Greatest TP']]
    t_size = fin_volume['Tumor Volume (mm3)']
    treatment.append(t_size)
    
    # add subset 
quartiles = t_size.quantile([.25,.5,.75])
lo_q = quartiles[0.25]
up_q = quartiles[0.75]
iqr = up_q-lo_q
print(f'IQR for {drug}: {iqr}')

lo_bound = lo_q - (1.5*iqr)
up_bound = up_q + (1.5*iqr)
print(f'Lower Bound for {drug}: {lo_bound}')
print(f'Upper Bound for {drug}: {up_bound}')

    # Determine outliers using upper and lower bounds
outliers = (t_size.loc[(fin_volume['Tumor Volume (mm3)'] >= up_bound) | (fin_volume['Tumor Volume (mm3)'] <= lo_bound)]).count()
print(f'Number of {drug} outliers: {outliers}')
# -

# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
plt.boxplot(treatment)
plt.title('Final Tumor Volume by Regimen')
plt.ylabel('Volume (mm3)')
plt.xticks([1,2,3,4], ["Capomulin", "Ceftamin","Infubinol", "Naftisol"]);

# # Line and Scatter Plots

# Generate a line plot of tumor volume vs. time point for a mouse treated with Capomulin
m_x401 = gr_tp_m.loc[gr_tp_m["Mouse ID"] == "x401"]
plt.plot(m_x401["Timepoint"], m_x401["Tumor Volume (mm3)"])
plt.xlabel("Timepoints")
plt.ylabel("Volume (mm3)")
plt.title("Capomulin Mouse x401: Timepoints vs Tumor Sizes");

# +
 # Generate a scatter plot of average tumor volume vs. mouse weight for the Capomulin regimen
capomulin_df = non_duplicates.loc[non_duplicates["Drug Regimen"] == "Capomulin"]
t_avg = pd.DataFrame(capomulin_df.groupby('Mouse ID')['Tumor Volume (mm3)'].mean())
w_avg = pd.DataFrame(capomulin_df.groupby("Mouse ID")["Weight (g)"].mean())
capolumin_avg = pd.merge(t_avg, w_avg,on="Mouse ID")
capolumin_avg
x = capolumin_avg['Weight (g)']
y = capolumin_avg['Tumor Volume (mm3)']
plt.scatter(x, y, marker='o')
plt.title("Capomulin Regimen: Average Weight vs Average Tumor Size")
plt.xlabel("Average Weight (g)")
plt.ylabel("Average Tumor (mm3)")

plt.show()
# -

# # Correlation and Regression

# +
# Calculate the correlation coefficient and linear regression model 
# for mouse weight and average tumor volume for the Capomulin regimen
grs = capomulin_df.iloc[:,3]
mm3 = capomulin_df.iloc[:,4]

vol_slope, vol_int, vol_r, vol_p, vol_std_err = linregress(tumor, weight)
vol_fit = vol_slope * tumor + vol_int

plt.plot(tumor,vol_fit)
line = "y = " + str(vol_slope) + "x + " + str(vc_int)

plt.scatter(tumor, weight)
plt.ylabel('Mouse Weight (g)')
plt.xlabel('Tumor Volume (mm3)')
plt.show()
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered