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

# The two drugs that were shown to reduce the tumor volume
# had the most datapoints on the bar graph. probably because 
# they had the mice that lived longest on average?

# In the top 4 most promising drugs, the gap between
# number 2 (Capomulin : 8.332432) and
# number 4 (Propriva	:-11.736964) is the bigest change
# in effectiveness between any 2 drugs at 20.07 average
# change in tumor volume

# There seems to be a weak correlation between the mice's 
# weight and the volume of their tumors. as the weight 
# incressed, the max tumor volume stayed relitivly stable
# while the minimum tumor volume rose from ~23 mm^3 to almost
# double that at ~40 mm^3


# +
import scipy.stats as st
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.stats import linregress
# Study data files
mouse_metadata = "data/Mouse_metadata.csv"
study_results = "data/Study_results.csv"

# Read the mouse data and the study results
mouse_metadata = pd.read_csv(mouse_metadata)
study_results = pd.read_csv(study_results)
raw_df = pd.merge(mouse_metadata, study_results, on='Mouse ID', how='outer')
raw_df

# +
summary_base_df = raw_df.copy()
summary_slim_df = summary_base_df.drop(['Mouse ID', 'Sex', 'Age_months',
                                       'Weight (g)', 'Metastatic Sites',
                                       'Timepoint'], axis=1).copy()
tumor_summary_base_df = summary_slim_df.copy()
tumor_summary_base_df['Mean TV'] = summary_slim_df['Tumor Volume (mm3)']
tumor_summary_base_df['Median TV'] = summary_slim_df['Tumor Volume (mm3)']
tumor_summary_base_df['Variance TV'] = summary_slim_df['Tumor Volume (mm3)']
tumor_summary_base_df['STD TV'] = summary_slim_df['Tumor Volume (mm3)']
tumor_summary_base_df['SEM TV'] = summary_slim_df['Tumor Volume (mm3)']

summary_df = tumor_summary_base_df.groupby(['Drug Regimen']).agg({'Mean TV' : 'mean',
                                                                 'Median TV' : 'median',
                                                                 'Variance TV' : np.var,
                                                                 'STD TV' : np.std,
                                                                 'SEM TV' : 'sem'})
summary_df

# +
# Pandas Bar Chart - bar plot showing number of data points for each treatment regimen
plt.close()
reg_vs_dPQ = raw_df[['Drug Regimen', 'Tumor Volume (mm3)']].copy()
RvQ_df = reg_vs_dPQ.groupby(['Drug Regimen']).agg({'Tumor Volume (mm3)' : 'count'}).copy()
RvQ_df = RvQ_df.rename(columns={"Tumor Volume (mm3)": "Data Point Count"})

RvQ_df.plot(kind='bar', title='Quantity of Data Points per Drug')
plt.ylabel('Num of Data Points')
plt.show()

# +
# plt bar chart - bar plot showing number of data points for each treatment regimen
plt.close()
x_axis = np.arange(len(raw_df['Drug Regimen'].unique()))
tick_locations = [value for value in x_axis]

plt.bar(x_axis, RvQ_df['Data Point Count'], color='g', alpha=0.5, align="center")
plt.xticks(tick_locations, RvQ_df.index.values, rotation="vertical")
plt.ylabel('Num of Data Points')
plt.title('Quantity of Data Points per Drug')
plt.legend(labels=['Quantity'], loc='upper center')

plt.show()

# +
# pandas pie chart
plt.close()
MvsF_df = raw_df[['Mouse ID', 'Sex']].copy()
MvsF_df = MvsF_df.groupby(['Sex']).agg({'Mouse ID' : 'count'})
MvsF_df = MvsF_df.rename(columns={"Mouse ID": "Count"})
MvsF_df.plot(kind='pie', y='Count' , title='Gender vs Number of Mice', labeldistance=.4, autopct='%1.1f%%')

plt.show()

# +
# plt pie chart
plt.close()
plt.pie(MvsF_df['Count'], labels=MvsF_df.index.values,  autopct='%1.1f%%', labeldistance=.4)
plt.title('Gender vs Number of Mice')
plt.legend()

plt.show()
# -

base_promising_df = raw_df[['Mouse ID', 'Drug Regimen', 'Timepoint', 'Tumor Volume (mm3)']].copy()
mice_max_time = base_promising_df.groupby('Mouse ID', as_index=False).agg({'Timepoint' : 'max',
                                                                          'Tumor Volume (mm3)' : 'last',
                                                                          'Drug Regimen' : 'first'})
mice_max_time = mice_max_time.rename(columns={'Timepoint' : 'Max Timepoint'})
mice_min_time = base_promising_df.groupby('Mouse ID', as_index=False).agg({'Timepoint' : 'min',
                                                                          'Tumor Volume (mm3)' : 'first',
                                                                          'Drug Regimen' : 'first'})
mice_min_time = mice_min_time.rename(columns={'Timepoint' : 'Min Timepoint'})
grup_promis_df = mice_min_time.copy()
grup_promis_df['Tumor Volume (mm3)'] = grup_promis_df['Tumor Volume (mm3)'] - mice_max_time['Tumor Volume (mm3)']
grup_promis_df['Avg Change in TV'] = grup_promis_df['Tumor Volume (mm3)']
promis_df = grup_promis_df.groupby('Drug Regimen').agg({'Avg Change in TV' : 'mean'})
promis_df = promis_df.sort_values('Avg Change in TV', ascending=False).head(4)
promisingDrugs = promis_df.index.values
promis_df
# the higher the number, the less turmor was left at the end of the trial
# negitive values shows an INCREASE in Tumor vol

# +
prom_drug_all_df = base_promising_df.loc[(raw_df['Drug Regimen'] == promisingDrugs[0]) | 
                              (raw_df['Drug Regimen'] == promisingDrugs[1]) | 
                              (raw_df['Drug Regimen'] == promisingDrugs[2]) | 
                              (raw_df['Drug Regimen'] == promisingDrugs[3]), :].copy()

prom_drug_Ram_df = base_promising_df.loc[(raw_df['Drug Regimen'] == promisingDrugs[0]), :].copy()
prom_drug_Cap_df = base_promising_df.loc[(raw_df['Drug Regimen'] == promisingDrugs[1]), :].copy()
prom_drug_Prop_df = base_promising_df.loc[(raw_df['Drug Regimen'] == promisingDrugs[2]), :].copy()
prom_drug_Ceft_df = base_promising_df.loc[(raw_df['Drug Regimen'] == promisingDrugs[3]), :].copy()

prom_drug_dfs = [prom_drug_Ram_df,
                prom_drug_Cap_df,
                prom_drug_Prop_df,
                prom_drug_Ceft_df]

prom_drug_all_df
# -

for df in prom_drug_dfs:    
    drugName = df['Drug Regimen'].unique()
    drugName[0]

    data = df['Tumor Volume (mm3)']

    quartiles = data.quantile([.25,.5,.75])
    lowerq = quartiles[0.25]
    upperq = quartiles[0.75]
    iqr = upperq-lowerq
    lower_bound = lowerq - (1.5*iqr)
    upper_bound = upperq + (1.5*iqr)
    print(f'Drug : {drugName[0]}\n')
    print(f"The lower quartile is: {lowerq}")
    print(f"The upper quartile is: {upperq}")
    print(f"The interquartile range is: {iqr}")
    print(f"The the median of is: {quartiles[0.5]} ")
    print(f"Values below {lower_bound} could be outliers.")
    print(f"Values above {upper_bound} could be outliers.")
    
    print('\n-------------------------------------------------\n')

final_vol_Ram_df = mice_max_time.loc[(mice_max_time['Drug Regimen'] == promisingDrugs[0]), :].copy()
final_vol_Cap_df = mice_max_time.loc[(mice_max_time['Drug Regimen'] == promisingDrugs[1]), :].copy()
final_vol_Prop_df = mice_max_time.loc[(mice_max_time['Drug Regimen'] == promisingDrugs[2]), :].copy()
final_vol_Ceft_df = mice_max_time.loc[(mice_max_time['Drug Regimen'] == promisingDrugs[3]), :].copy()

# +
plt.close()
red_square = dict(markerfacecolor='r', marker='s')

data = final_vol_Ram_df['Tumor Volume (mm3)']
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1,4, sharey=True, gridspec_kw=dict(wspace=.4))
ax1.set_title(promisingDrugs[0])
ax1.set_ylabel('Final Tumor Vol')
ax1.boxplot(data, showfliers=True, flierprops=red_square)

data = final_vol_Cap_df['Tumor Volume (mm3)']
ax2.set_title(promisingDrugs[1])
ax2.set_ylabel('Final Tumor Vol')
ax2.boxplot(data, showfliers=True, flierprops=red_square)

data = final_vol_Prop_df['Tumor Volume (mm3)']
ax3.set_title(promisingDrugs[2])
ax3.set_ylabel('Final Tumor Vol')
ax3.boxplot(data, showfliers=True, flierprops=red_square)

data = final_vol_Ceft_df['Tumor Volume (mm3)']
ax4.set_title(promisingDrugs[3])
ax4.set_ylabel('Final Tumor Vol')
ax4.boxplot(data, showfliers=True, flierprops=red_square)

plt.show()
# -

cap_df = prom_drug_Cap_df.copy()
cap_mice_base = cap_df.drop_duplicates(['Mouse ID']).copy()
cap_mice = cap_mice_base["Mouse ID"].reset_index(drop=True)
lucky_mouse = cap_mice[13]

# +
# Line and scatter plots
plt.close()
mouse_data = cap_df.loc[cap_df['Mouse ID'] == lucky_mouse, :]
mouse_data
x_vals = mouse_data['Timepoint']
y_vals = mouse_data['Tumor Volume (mm3)']

plt.plot(x_vals, y_vals, color='red')
plt.title('Time point versus tumor volume for a mouse treated with Capomulin')
plt.xlabel('Time')
plt.ylabel('Tumor Vol')
plt.legend(labels=[f'Mouse ID : {lucky_mouse}'], loc='upper right')

plt.show()
# -

cap_temp_df = raw_df.loc[(raw_df['Drug Regimen'] == promisingDrugs[1]), :].copy()
cap_weight_df = cap_df
cap_weight_df['Weight (g)'] = cap_temp_df['Weight (g)']

# +
plt.close()
x_vals = cap_weight_df['Weight (g)']
y_vals = cap_weight_df['Tumor Volume (mm3)']
plt.scatter(x_vals, y_vals)
plt.xlabel('Weight in grams')
plt.ylabel('Tumor Volume in mm^3')
plt.title('Weight vs Tumor Vol For Capomulin')
plt.legend(labels=['A mouse'], loc='lower right')

plt.show()
# -

correlation = st.pearsonr(x_vals, y_vals)
print(f"The correlation between both factors is {round(correlation[0],2)}")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_vals, y_vals)
regress_values = x_vals * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
print(line_eq)
print(f"The r-squared is: {rvalue}")

# +
plt.close()
x_vals = cap_weight_df['Weight (g)']
y_vals = cap_weight_df['Tumor Volume (mm3)']
plt.scatter(x_vals, y_vals)
plt.xlabel('Weight in grams')
plt.ylabel('Tumor Volume in mm^3')
plt.title('Weight vs Tumor Vol For Capomulin')
plt.plot(x_vals, regress_values, "r-")
plt.legend(labels=['Correlation line', 'A mouse'], loc='lower right')

plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered