-
Notifications
You must be signed in to change notification settings - Fork 0
/
three_variable_plot.py
139 lines (101 loc) · 4.97 KB
/
three_variable_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
"""
Created by Sharon Tam
Date: 10/12/2021
Github Name:
Email: [email protected]
Three variable plots with optimum values
-This code reads bsstats_RenalAnimals_BS100_EvRate_base.csv from the bootstrapping_coact_stats_graph.py to plot cofluctuation(exceedance), state threshold,
and CI widths to find the narrowest CI width.
-Created plots are in this google drive link: https://drive.google.com/drive/folders/1xAHxpKK0xQ_E1a3C4fvNuDUqiFxydGyD?usp=sharing
"""
# In[Import Libraries]:
from string import ascii_letters
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#import seaborn as sns
import os
# In[Set paths and format data]:
result_folder_path = 'C:/Users/SmartBox/Desktop/Lab/Results 10_20/Three Variable Plots'
# Read in bootstrapped event rate csv file
df = pd.read_csv("C:/Users/SmartBox/Desktop/Lab/Renal Results/bsstats_RenalAnimals_BS100_EvRate_base.csv")
# Delete rows with zero event rate
df = df[df['mean'].notnull()]
df.replace("0p6", 0.6, inplace = True)
df.replace("0p75", 0.75, inplace = True)
df.replace("0p9", 0.9, inplace = True)
print(df.head())
# Group data by animal
grouped = df.groupby(df.animal)
animal_types = ['VF', 'Normal']
color_codes = ['magenta', 'blue']
measures = ['rate', 'std']
num_cols = 1
total_animals = 11
# In[Plot figures]:
# Create figures (one for each measure)
fig_rate = plt.figure(figsize = (30, 150))
rate_title = "Optimum Cofluctuation and State Threshold for Minimum CI Width (MEAN) - Renal Animals"
fig_rate.suptitle(rate_title, fontsize = 30)
fig_std = plt.figure(figsize = (30, 150))
std_title = "Optimum Cofluctuation and State Threshold for Minimum CI Width (STD) - Renal Animals"
fig_std.suptitle(std_title, fontsize = 30)
fig_rate.subplots_adjust(top = 0.95)
fig_std.subplots_adjust(top = 0.95)
fig_rate.tight_layout()
fig_std.tight_layout()
figures = [fig_rate, fig_std]
count = 1
for animal_type, type_color in zip(animal_types, color_codes):
# Get animal names
animal_folder_path = 'D:/Renal Study/Results_RenalAnimals/' + animal_type + "Animals"
animal_names = os.listdir(animal_folder_path)
animal_names = sorted(animal_names)
print(animal_names)
for animal in animal_names:
current_animal = grouped.get_group(animal)
measure_group = current_animal.groupby('measure')
#print(current_animal)
for measure in measures:
if (measure == 'rate'):
measure_term = 'mean'
fig_count = 0
else:
measure_term = 'std'
fig_count = 1
# Get data for correct measure
current_animal_measure = measure_group.get_group(measure)
interval_width = current_animal_measure['ci_width']
state_thr = current_animal_measure['state_threshold']
cofluctuation = current_animal_measure['exceedance']
# Find narrowest confidence interval and the optimal thresholds at nonzero event rate
min_index = interval_width.idxmin()
min_width = interval_width[min_index]
best_state_thr = state_thr[min_index]
best_cofluc = cofluctuation[min_index]
# Remove minimum values from all series
state_thr = state_thr.drop(min_index)
cofluctuation = cofluctuation.drop(min_index)
interval_width = interval_width.drop(min_index)
ax_title = animal + "_" + measure_term + "_" + animal_type + "_" + " Optimum Cofluctuation and \nState Threshold for Minimum CI Width (C:" + str(best_cofluc) + ", T:" + str(best_state_thr) + ", CI:" + str(round(min_width, 4)) + ")"
# Create figure
ax = figures[fig_count].add_subplot(total_animals, num_cols, count, projection='3d')
# Plot data, different color for minimum value
ax.scatter(state_thr, cofluctuation, interval_width, linewidths=1, alpha=.7, edgecolor='k', s = 250, c = type_color)
ax.scatter(best_state_thr, best_cofluc, min_width, linewidths=1, alpha=.7, edgecolor='k', s = 250, c = 'red')
ax.set_yticks([0.6, 0.75 ,0.9])
ax.set_title(ax_title, fontsize = 20)
ax.set_xlabel('State Threshold', fontsize = 15)
ax.set_ylabel('Cofluctuation', fontsize = 15)
ax.set_zlabel('CI Width', fontsize = 15, labelpad = 10)
plt.show()
count = count + 1
# Save figures as pdf
savefig_pdf_str = "RenalStudy_Mean_three_var_plots.pdf"
save_path = result_folder_path + "/" + savefig_pdf_str
fig_rate.savefig(save_path)
savefig_pdf_str = "RenalStudy_std_three_var_plots.pdf"
save_path = result_folder_path + "/" + savefig_pdf_str
fig_std.savefig(save_path)