# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:PythonData] *
#     language: python
#     name: conda-env-PythonData-py
# ---

# ## Observations and Insights 

# Among the several drug treatment regimens, the for most promissing treatments for squamous cell carcinoma (SCC), a commonly occurring form of skin cancer, are Capomulin, Ramicane, Infubinol, and Ceftamin, and from these, Ramicane appears to be the most successful treatment, with an average post treatment tumor volume of 36.56 mm<sup>3</sup>. This is followed closly by Capomulin with an average post treatment tumor volume of 32.38 mm<sup>3</sup>. 
#
#
# Ovre 45 days of treatment, the SSC tumor volume in mice receiving Campomulin decreased by about 9mm<sup>3</sup>.
#
# There is a strong positive correlation of 0.842 (r<sup>2</sup> = 0.709) between mouse weight and tumor volume. Inidcating that mice who are overweight, have a much higher perpensity to develop SCC.
#
# Areas for further analysis would be to see if sex and age has an impact on the efficacy of the drug regimens.

# +
# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
from scipy.stats import linregress
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
merged_data_df =pd.merge(mouse_metadata, study_results, on='Mouse ID', how='left')

# Display the data table for preview
merged_data_df
# -

# Checking the number of mice.
merged_data_df['Mouse ID'].nunique()

# Getting the duplicate mice by ID number that shows up for Mouse ID and Timepoint. 
duplicate_data = merged_data_df.loc[merged_data_df.duplicated(subset=['Mouse ID', 'Timepoint'], keep=False)]
duplicate_data

# Optional: Get all the data for the duplicate mouse ID. 
merged_data_df.loc[merged_data_df['Mouse ID']=='g989']

# Create a clean DataFrame by dropping the duplicate mouse by its ID.
clean_data_df = merged_data_df.loc[merged_data_df['Mouse ID']!='g989']
clean_data_df

# Checking the number of mice in the clean DataFrame.
clean_data_df['Mouse ID'].nunique()

# ## Summary Statistics

# +
# Generate a summary statistics table of mean, median, variance, standard deviation, and SEM of the tumor volume for each regimen
# Grouping data by drug regimen and statistics calculations
mean = clean_data_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].mean()
median = clean_data_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].median()
variance = clean_data_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].var()
std = clean_data_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].std()
std_error_means = clean_data_df.groupby('Drug Regimen')['Tumor Volume (mm3)'].sem()
       
# This method is the most straighforward, creating multiple series and putting them all together at the end.
summary_stats_df = pd.DataFrame({'Mean': mean,
                                'Median': median,
                                'Variance': variance,
                                'Standard Deviation': std,
                                'SEM': std_error_means})

# Display formatted summary statistics
summary_stats_df.style.format({'Mean':'{:.3f}',
                              'Median':'{:.3f}',
                               'Variance':'{:.3f}',
                               'Standard Deviation':'{:.3f}',
                               'SEM':'{:.3f}'})


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

# This method produces everything in a single groupby function

# -

# ## Bar and Pie Charts

# +
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pandas. 
# Group data by drug treatment # mice on drug treatments
mice_per_treatment = clean_data_df.groupby('Drug Regimen')['Mouse ID'].count()

# Generate bar plot and set labels
mice_per_treatment_plot = mice_per_treatment.plot(kind='bar', title='Mice per Drug Regimen')
mice_per_treatment_plot.set_ylabel('Number of Mice')

plt.show()


# +
# Generate a bar plot showing the total number of mice for each treatment throughout the course of the study using pyplot.
# Create x-ticker loactions
x_axis = np.arange(len(mice_per_treatment))

# Create list of x-axis labels
drug_regimens = summary_stats_df.index.tolist()

# Generate bar plot and add labels
plt.bar(x_axis, mice_per_treatment, align='center')
plt.xticks(x_axis, drug_regimens, rotation=90)
plt.title('Total Mice per Drug Regimen')
plt.xlabel('Drug Regimen')
plt.ylabel('Number of Mice')
plt.show()

# +
# Generate a pie plot showing the distribution of female versus male mice using pandas
# Grouping data by sex and counting number of mice per sex
mice_sex = clean_data_df.groupby('Sex')['Mouse ID'].nunique()

# The colors of each section of the pie chart
colors = ["pink", "blue"]

# Generating pie plot and add formatting and labels
mice_sex_plot = mice_sex.plot(kind='pie', title='Male vs Female Mice', autopct="%1.1f%%", figsize=(5,5), fontsize=11, colors=colors)
mice_sex_plot.set_ylabel('Percentage of Mice')
plt.show()

# +
# Generate a pie plot showing the distribution of female versus male mice using pyplot
# Create list of x-axis labels
labels_sex = sorted(clean_data_df['Sex'].unique())

# The colors of each section of the pie chart
colors = ["pink", "blue"]

# Generating pie plot and add formatting and labels
plt.pie(mice_sex, autopct="%1.1f%%", labels=labels_sex, colors=colors)
plt.title('Male vs Female Mice')
plt.ylabel('Percentage of Mice')
plt.axis('equal')
plt.show()
# -

# ## 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
max_timepoint = clean_data_df.groupby('Mouse ID')['Timepoint'].max()
max_timepoint_df = pd.DataFrame(max_timepoint)

# Merge this group df with the original dataframe to get the tumor volume at the last timepoint
merged_data2_df =pd.merge(clean_data_df, max_timepoint_df, on='Mouse ID')
merged_data2_df

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

# Create empty list to fill with tumor vol data (for plotting)
tumor_vol_data = []
    
# Locate the rows which contain mice on each drug and get the tumor volumes
mouse_maxTime_df = merged_data2_df.loc[merged_data2_df['Timepoint_x'] == merged_data2_df['Timepoint_y']]
mouse_maxTime_df = mouse_maxTime_df.sort_values(by='Drug Regimen').reset_index(drop=True)

# Add subset for: 
# Capomulin 
capomulin_df = mouse_maxTime_df.loc[mouse_maxTime_df['Drug Regimen'] == 'Capomulin']
capomulin_tumorVol = pd.DataFrame(capomulin_df['Tumor Volume (mm3)'])
capomulin_tumorVol = capomulin_tumorVol.rename(columns={'Tumor Volume (mm3)':'Capomulin'}).sort_values(by='Capomulin').reset_index(drop=True)

# Ramicane
ramicane_df = mouse_maxTime_df.loc[mouse_maxTime_df['Drug Regimen'] == 'Ramicane']
ramicane_tumorVol = pd.DataFrame(ramicane_df['Tumor Volume (mm3)'])
ramicane_tumorVol = ramicane_tumorVol.rename(columns={'Tumor Volume (mm3)':'Ramicane'}).sort_values(by='Ramicane').reset_index(drop=True)

# Infubinol
infubinol_df = mouse_maxTime_df.loc[mouse_maxTime_df['Drug Regimen'] == 'Infubinol']
infubinol_tumorVol = pd.DataFrame(infubinol_df['Tumor Volume (mm3)'])
infubinol_tumorVol = infubinol_tumorVol.rename(columns={'Tumor Volume (mm3)':'Infubinol'}).sort_values(by='Infubinol').reset_index(drop=True)

# Ceftamin
ceftamin_df = mouse_maxTime_df.loc[mouse_maxTime_df['Drug Regimen'] == 'Ceftamin']
ceftamin_tumorVol = pd.DataFrame(ceftamin_df['Tumor Volume (mm3)'])
ceftamin_tumorVol = ceftamin_tumorVol.rename(columns={'Tumor Volume (mm3)':'Ceftamin'}).sort_values(by='Ceftamin').reset_index(drop=True)


# Summarizing Tumor Volumes across Drug Regimens
data = [capomulin_tumorVol['Capomulin'], 
        ramicane_tumorVol['Ramicane'], 
        infubinol_tumorVol['Infubinol'], 
        ceftamin_tumorVol['Ceftamin']]

# Grouping separate data frams to one
drug_Regimen_TumorVol = pd.concat(data, axis=1, keys=best_regimens)
# -

# ## The final tumor volume of each mouse across four of the most promising treatment regimens are:
#

drug_Regimen_TumorVol

# +
# Calculate the IQR and quantitatively determine if there are any potential outliers. 
# Capomulin
cap = capomulin_tumorVol['Capomulin']
quartiles = cap.quantile([.25,.5,.75]).round(2)
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = round(upperq-lowerq,2)

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

lower_bound = round(lowerq - (1.5*iqr),2)
upper_bound = round(upperq + (1.5*iqr),2)
print(f"Values below {lower_bound} for Capomulin could be outliers.")
print(f"Values above {upper_bound} for Capomulin could be outliers.")
print("----------------------------------------------------")
print("")

# Ramicane
ram = ramicane_tumorVol['Ramicane']
quartiles = ram.quantile([.25,.5,.75]).round(2)
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = round(upperq-lowerq,2)

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

lower_bound = round(lowerq - (1.5*iqr),2)
upper_bound = round(upperq + (1.5*iqr),2)
print(f"Values below {lower_bound} for Ramicane could be outliers.")
print(f"Values above {upper_bound} for Ramicane could be outliers.")
print("----------------------------------------------------")
print("")

# Infubinol
inf = infubinol_tumorVol['Infubinol']
quartiles = inf.quantile([.25,.5,.75]).round(2)
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = round(upperq-lowerq,2)

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

lower_bound = round(lowerq - (1.5*iqr),2)
upper_bound = round(upperq + (1.5*iqr),2)
print(f"Values below {lower_bound} for Infubinol could be outliers.")
print(f"Values above {upper_bound} for Infubinol could be outliers.")
print("----------------------------------------------------")
print("")

# Ceftamin
cef = ceftamin_tumorVol['Ceftamin']
quartiles = cef.quantile([.25,.5,.75]).round(2)
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = round(upperq-lowerq,2)

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

lower_bound = round(lowerq - (1.5*iqr),2)
upper_bound = round(upperq + (1.5*iqr),2)
print(f"Values below {lower_bound} for Ceftamin could be outliers.")
print(f"Values above {upper_bound} for Ceftamin could be outliers.")
print("----------------------------------------------------")
print("")

# Determine outliers using upper and lower bounds
    

# +
# Generate a box plot of the final tumor volume of each mouse across four regimens of interest
fig1, ax1 = plt.subplots(figsize=(7,5))
ax1.set_title('Final Tumor Volume by Drug Regimen')
ax1.set_xlabel('Drug Regimens')
ax1.set_ylabel('Tumor Volume (mm3)')
ax1.boxplot(data, sym='gD')
plt.xticks([1,2,3,4],best_regimens)

plt.show()
# -

# ## Line and Scatter Plots

# Generate a line plot of time point versus tumor volume for a mouse treated with Capomulin
# Create Capomulin data df
capomulin_data = clean_data_df.loc[clean_data_df['Drug Regimen'] == 'Capomulin']
capomulin_data

# +
# Group data by timepoint and get mean of tumor volume
tumor_vol_mean = capomulin_data.groupby('Timepoint')['Tumor Volume (mm3)'].mean().round(3)
cap_time_tumorVol = pd.DataFrame({'Avg Tumor Volume (Capomulin)':tumor_vol_mean})

# Create list of Timepoints and mean tumor volumes
timepoints = list(cap_time_tumorVol.index.values)
cap_tumorVol_mean = list(cap_time_tumorVol['Avg Tumor Volume (Capomulin)'].values)

# Lineplot of timeseries and mean tumor volume with configuration
v, = plt.plot(timepoints, cap_tumorVol_mean, marker="+", color="blue", linewidth="1", label ='Average')

plt.title('Capomulin Avg Tumor Volume Time Series')
plt.xlabel('Time (Days)')
plt.ylabel('Tumor Volume (mm3)')
plt.legend(handles= [v], loc="upper right")

# Set limits
plt.ylim(min(cap_tumorVol_mean)-1,max(cap_tumorVol_mean)+1)
plt.show()

# +
# Extract data for a single mouse ID: b128
b128_capomulin_data = capomulin_data.loc[merged_data_df['Mouse ID']=='b128'].reset_index()

# Create list of Timepoints and mean tumor volumes
b128_timepoints = b128_capomulin_data['Timepoint']
b128_cap_tumorVol = b128_capomulin_data['Tumor Volume (mm3)']

# Lineplot of timeseries and mean tumor volume with configuration
v, = plt.plot(b128_timepoints, b128_cap_tumorVol, marker="+", color="blue", linewidth="1", label ='Mouse ID: b128')

plt.title('Capomulin Tumor Volume Time Series')
plt.xlabel('Time (Days)')
plt.ylabel('Tumor Volume (mm3)')
plt.legend(handles= [v], loc="upper right")

# Set limits
plt.ylim(min(cap_tumorVol_mean)-1,max(cap_tumorVol_mean)+2)
plt.show()
# -

# Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
# Group data by timepoint and get mean of tumor volume
cap_mouse_weight = capomulin_data.groupby('Mouse ID')['Weight (g)'].mean().round(3)
cap_mouse_tumorVol_mean = capomulin_data.groupby('Mouse ID')['Tumor Volume (mm3)'].mean().round(3)
cap_weight_tumorVol = pd.DataFrame({'Mouse Weight (g)':cap_mouse_weight,
                                    'Capomulin Avg Tumor Volume (mm3)':cap_mouse_tumorVol_mean})
cap_weight_tumorVol

# +
# Create scatter plot of weight and mean tumor volume with configuration
plt.scatter(
    cap_weight_tumorVol['Mouse Weight (g)'], 
    cap_weight_tumorVol['Capomulin Avg Tumor Volume (mm3)'],
    marker="o", 
    facecolor="red", 
    edgecolors="black",
    alpha = 0.75)

plt.title('Mouse weight vs. Avg. Tumor Volume (Capomulin)')
plt.xlabel('Mouse Weight (g)')
plt.ylabel('Avg Tumor Volume (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
correlation = st.pearsonr(cap_weight_tumorVol['Mouse Weight (g)'], cap_weight_tumorVol['Capomulin Avg Tumor Volume (mm3)'])

print(f'The correlation between Mouse Weight and Tumor Volume is {round(correlation[0],2)}')

# +
# Calculate and add linear regression equation to previous scatter plot
x_values = cap_weight_tumorVol['Mouse Weight (g)']
y_values = cap_weight_tumorVol['Capomulin Avg Tumor Volume (mm3)']

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

regress_values = x_values * slope + intercept
line_eqn = f'Y = {str(round(slope,2))}x + {str(round(intercept,2))}'

plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,'r-')
plt.annotate(line_eqn,(18,37),fontsize=15,color='black')
plt.title('Mouse weight vs. Avg. Tumor Volume (Capomulin)')
plt.xlabel('Mouse Weight (g)')
plt.ylabel('Avg Tumor Volume (mm3)')

print(f'The r-squared is: {rvalue**2}')
print(f'The equation of the regression line is: {line_eqn}')
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered