-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
235 lines (161 loc) · 6.6 KB
/
stats.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pathlib
import statsmodels.api as sm
from statsmodels.multivariate.manova import MANOVA
from sklearn.preprocessing import LabelEncoder
from analysis import read_df, syllable_sort, sum_groups, get_headers
# # Generate some example data
# data = np.random.normal(size=1000)
# # Calculate occupancy and total counts
# occupancy, bins = np.histogram(data, bins=50)
# total_counts = np.sum(occupancy)
# # Calculate PDF
# pdf = occupancy / total_counts
# # Plot normalized occupancy
# plt.bar(bins[:-1], pdf, width=np.diff(bins), color='blue', alpha=0.5)
# plt.xlabel('Data')
# plt.ylabel('Normalized Occupancy')
# plt.title('Normalized Occupancy Plot')
# # plt.show()
# # Define the PDF
# x = np.linspace(-5, 5, 100)
# y = np.linspace(-5, 5, 100)
# X, Y = np.meshgrid(x, y)
# Z = np.exp(-0.5*(X**2 + Y**2)) / (2*np.pi)
# # Plot the PDF as a contour plot
# fig, ax = plt.subplots(figsize=(6, 6))
# ax.contour(X, Y, Z)
# print(X)
# # Add a heatmap of the PDF
# sns.heatmap(Z, cmap="YlGnBu", square=True, ax=ax)
# # Set the title and axis labels
# ax.set_title('Normalized 2D Gaussian')
# ax.set_xlabel('x')
# ax.set_ylabel('y')
# # Define x and y coordinates of the probabilities
# x = np.linspace(-5, 5, 100)
# y = np.linspace(-5, 5, 100)
# # Create a meshgrid of the x and y coordinates
# X, Y = np.meshgrid(x, y)
# # Define the probability density values
# pdf_data = np.random.rand(len(x), len(y))
# # Normalize the probabilities
# pdf_data = pdf_data / np.sum(pdf_data)
# # Create a heatmap of the probability density
# plt.pcolormesh(X, Y, pdf_data, cmap='viridis')
# plt.colorbar()
# plt.show()
moseq_dir = pathlib.Path.cwd().parent
filename = moseq_dir/'data/rTMS/mean_df2.csv'
# df = syllable_sort(read_df(filename), "usage")
# sum_df = sum_groups(syllable_sort(read_df(filename), "usage"))
# print(len(df))
# for i in df:
# print('---')
# print(i)
# print('---')
# print("control")
# print(len(df[i]['sham - stim']))
# print(df[i]['sham - stim'])
# print("rTMS")
# print(len(df[i]['rTMS - stim']))
# print(df[i]['rTMS - stim'])
def organise(df):
# print(df[0])
new_df = df
# for i in range(len(df)):
# if df[i][df[0].index("uuid")][:-1] not in new_df:
# print(df[i][df[0].index("uuid")][:-1])
# new_df[df[i][df[0].index("uuid")][:-1]] = ''
# print(df[i][df[0].index("group")], df[i][df[0].index("uuid")], df[i][df[0].index("uuid")][:-1])
df[0].extend(['treatment', 'timepoint'])
for row in df[1:]:
row.extend(row[0].split(" - "))
# print(new_df)
# print(df[0].index("uuid"))
organise(read_df(filename))
def read_csv(filename):
df = pd.read_csv(filename)
return df
print(read_csv(filename))
def add_time_treat(df):
for idx, key in enumerate(df[0]):
if len(key.split(" ")) > 1:
df[0][idx] = key.replace(" ", "_")
print(df[0])
df[0].extend(['treatment', 'timepoint'])
for row in df[1:]:
row.extend(row[0].split(" - "))
df = pd.DataFrame(df[1:], columns=df[0])
return df
dataframe = add_time_treat(read_df(filename))
# dataframe = syllable_sort(add_time_treat(read_df(filename)), "usage")
print(dataframe)
### SYLLABLE ANALYSIS ###
## MAP SYLLABLE NUM TO LABEL
# SYLLABLE_MAP = get_syllable_map(syllable_info_path, info=['label', 'desc'])
# ## PLOT OVERALL SYLLABLE USAGE
# plot_syllable_sums(sum_groups(syllable_sort(read_df(mean_df_path_usage_nums), 'usage')))
# ## PLOT OVERALL VELOCITY
# plot_syllable_sums(sum_groups(syllable_sort(read_df(mean_df_path_usage_nums), 'velocity_2d_mm_mean')), titles=['Average Syllable 2d Velocity', 'Syllable', 'Velocity'])
# plot_syllable_sums(sum_groups(syllable_sort(read_df(mean_df_path_usage_nums), 'centroid_x_px_mean')), titles=['Spots', 'Syllable', 'Trace'], plot_type='scatter')
# SYLLABLE LOCATIONS
# arrays, headers = get_location_arrays(sum_groups(syllable_sort(read_df(mean_df_path_usage_nums), 'centroid_x_px_mean')), sum_groups(syllable_sort(read_df(mean_df_path_usage_nums), 'centroid_y_px_mean')), return_headers=True)
# plot_syllable_locations(arrays, headers)
# # ## PRINT SYLLABLE OVERVIEW
# syllable_dict = syllable_overview(read_df(mean_df_path_usage_nums), ["usage", "duration", "velocity_2d_mm_mean", "velocity_3d_mm_mean", "height_ave_mm_mean", "dist_to_center_px_mean"])
# print_syllable_overview(syllable_dict, names=True)
# ## PLOT SYLLABLE OVERVIEW
# rapid_plot(mean_df_path_usage_prob, 'usage', title='label')
# # rapid_plot(mean_df_path_usage_prob, 'velocity_2d_mm_mean') # velocity goes down
# # rapid_plot(mean_df_path_usage_prob, 'height_ave_mm_mean') # ephrin usually higher than WT
# ## PLOT BEHAVIOUR CLASSIFICATION
# plot_syllable_count(syllable_count(get_syllable_map(syllable_info_path)))
# ## COVARIENCE AND CORRELATIONAL MATRIX
# transition_matrix(transition_matrix_dir, mtype='corr')
# transition_matrix(transition_matrix_dir, mtype='cov')
# def manova(df):
# # Define dependent variables
# dependent_vars = ['Var1', 'Var2', 'Var3']
# # Define independent variable
# independent_var = 'group'
# # Encode groups as numeric values
# encoder = LabelEncoder()
# groups = encoder.fit_transform(df[independent_var])
# # Set up data
# y = df[dependent_vars].to_numpy()
# # Perform MANOVA
# manova = MANOVA(endog=y, exog=sm.add_constant(groups))
# # Print results
# print(manova.mv_test())
def manova2(df):
# Load data into a pandas DataFrame
data = df
# Separate the independent variables (predictors) and dependent variables (response variables)
X = data[['usage', 'duration', 'velocity_2d_mm_mean']]
y = data['syllable']
# Add intercept column to X
X = sm.add_constant(X)
# Fit the MANOVA model
manova = MANOVA.from_formula('usage + duration + velocity_2d_mm_mean ~ C(group)', data=data)
result = manova.mv_test()
# Print the MANOVA results
print(result.summary())
# manova2(add_time_treat(read_csv(filename)))
def manova_analysis(data, independent_vars, dependent_vars=['usage, velocity_2d_mm_mean']):
if dependent_vars == "all":
formula = ' + '.join(data.columns[2:]) + f" ~ {'*'.join(independent_vars)}"
else:
formula = ' + '.join(dependent_vars) + f" ~ {'*'.join(independent_vars)}"
print(formula)
print("[RUNNING]: MANOVA")
manova = MANOVA.from_formula(formula, data=data)
print("[FINISHED]: MANOVA")
result = manova.mv_test()
print(result)
print(result.summary())
return manova.mv_test()
manova_analysis(add_time_treat(read_df(filename)), ['timepoint', 'treatment'])