-
Notifications
You must be signed in to change notification settings - Fork 0
/
explore_conn.py
221 lines (155 loc) · 6.34 KB
/
explore_conn.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
#%%
import numpy as np
import pandas as pd
from pathlib import Path
import pymaid
import json
from matplotlib import pyplot as plt
import os
HERE = Path(__file__).resolve().parent
OUT_DIR = HERE / "output"
creds_path = os.environ.get("PYMAID_CREDENTIALS_PATH", HERE / "seymour.json")
with open(creds_path) as f:
creds = json.load(f)
rm = pymaid.CatmaidInstance(**creds)
### Load data ###
#%%
with open(OUT_DIR / 'merged_cosine_th1_input.json') as json_file:
input = json.load(json_file)
with open(OUT_DIR / 'merged_cosine_th1_output.json') as json_file:
output = json.load(json_file)
with open(OUT_DIR / 'merged_cosine_th2_input.json') as json_file:
input2 = json.load(json_file)
with open(OUT_DIR / 'merged_cosine_th2_output.json') as json_file:
output2 = json.load(json_file)
#%%
canalysis_input = pd.read_csv(HERE / 'canalysis_cosine_th2_input.csv')
canalysis_output = pd.read_csv(HERE / 'canalysis_cosine_th2_output.csv')
#%%
'''
input_vals = {key:val for key, val in input.items() if val != None}
sorted_IV=[] # IV means input values
# w is connectivity metric
for w in sorted(input_vals, key=input_vals.get, reverse = False):
sorted_IV.append([w, input[w]])
output_vals = {key:val for key, val in output.items() if val != None}
sorted_OV=[] # Output values
for w in sorted(output_vals, key=output_vals.get, reverse = False):
sorted_OV.append([w, output[w]])
#%%
with open("sorted_conn_inputs.json", 'w') as outfile:
json.dump(sorted_IV, outfile)
with open("sorted_conn_outputs.json", 'w') as outfile:
json.dump(sorted_OV, outfile)
'''
### Explore data ###
#%%
def gen_xy(data, normalise = False):
""" Generate x and y values for plotting from connectivity analysis data, comparing similarity across L-R pairs
cosine similarity values on x and # of L-R pairs on y
Args:
data: either json nested dict, with connectivity values stored within 'cosine similarity')
or pandas df, with connectivity values stored in column 'cosine_similarity'
normalise (bool): option to normalise y (cumulative sum) values, if plotting for different amounts of values (x). Defaults to False.
"""
if type(data) == dict:
vals = data.get('cosine_similarity', {})
vals = list(vals.values())
vals = [np.nan if x is None else x for x in vals]
x = sorted(v for v in vals if not np.isnan(v))
y = np.arange(len(x))
if normalise:
y /= len(x)
if type(data) == pd.DataFrame:
vals = list(data['cosine_similarity'] )
vals = [np.nan if x is None else x for x in vals]
x = sorted(v for v in vals if not np.isnan(v))
y = np.arange(len(x))
if normalise:
y /= len(x)
else:
print('data not of correct format (json dict or pandas df)')
return x, y
## Plots for whole data ##
#%%
fig = plt.figure()
ax = fig.add_subplot()
out_x, out_y = gen_xy(output, False)
ax.plot(out_x, out_y, label="output similarity")
in_x, in_y = gen_xy(input, False)
ax.plot(in_x, in_y, label="input similarity")
ax.legend()
ax.set_xlabel("Cosine similarity value")
ax.set_label("Cumulative frequency")
fig.savefig("canalysis_cumulative_th1.pdf", format="pdf")
#%%
fig2 = plt.figure()
ax = fig2.add_subplot()
out2_x, out2_y = gen_xy(output2, False)
ax.plot(out2_x, out2_y, label="output similarity")
in2_x, in2_y = gen_xy(input2, False)
ax.plot(in2_x, in2_y, label="input similarity")
ax.legend()
ax.set_xlabel("Cosine similarity value")
ax.set_label("Cumulative frequency")
fig2.savefig("canalysis_cumulative_th2.pdf", format="pdf")
#%%
fig3 = plt.figure()
ax = fig3.add_subplot()
out_x, out_y = gen_xy(output, False)
ax.plot(out_x, out_y, label="output, th=1")
in_x, in_y = gen_xy(input, False)
ax.plot(in_x, in_y, label="input, th=1")
out2_x, out2_y = gen_xy(output2, False)
ax.plot(out2_x, out2_y, label="output, th=2")
in2_x, in2_y = gen_xy(input2, False)
ax.plot(in2_x, in2_y, label="input, th=2")
ax.legend()
ax.set_xlabel("Cosine similarity value")
ax.set_label("Cumulative frequency")
fig3.savefig("canalysis_cumulative_th1+2.pdf", format="pdf")
## Lineage analysis ##
#%%
bp = pd.read_csv(HERE / "brain-pairs.csv")
bp.drop('region', axis=1, inplace=True)
brain_pairs = bp.to_numpy().astype(int).tolist()
pair_dict = {}
for skids in brain_pairs:
pair_dict[skids[0]] = skids[1]
pair_dict[skids[1]] = skids[0]
#%%
def lineage_subsetter(inputs = True):
# Iterate through all json files (containing skids of each lineage) in directory, subset inputs or outputs results table and export as CSV
directory = os.fsencode(HERE / 'lineages')
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".json"):
lineage = filename.replace('.json', '')
# Extract all skids in each lineage from CATMAID downloaded JSONs
neurons = pd.read_json(HERE / f"lineages/{filename}")
neurons = pymaid.get_names(neurons.loc[:]['skeleton_id'])
# neurons contains both left and right neurons within lineage annotation,
# as CATMAID pairs may not completely correspond to those in brain_pairs (and thus canalysis_{input/output} csv)
# Convert neurons from lineage into integer list of skids
skids = [int(i) for i in list(neurons.keys())]
# Subset inputs or outputs CSV with all scores by skids in lineage
if inputs == True:
lineage_df = canalysis_input[(canalysis_input['left_skid'].isin(skids)) | (canalysis_input['right_skid'].isin(skids))]
else:
lineage_df = canalysis_output[(canalysis_output['left_skid'].isin(skids)) | (canalysis_output['right_skid'].isin(skids))]
# Output to CSV
lineage_df.to_csv(OUT_DIR / "lineage_csvs"/f"canalysis2_{'in' if inputs else 'out'}put_{lineage}.csv", index=False)
lineage_subsetter(inputs = True)
lineage_subsetter(inputs = False)
## Plots for lineage subsets ##
#%%
fig = plt.figure()
ax = fig.add_subplot()
out_x, out_y = gen_xy(output, False)
ax.plot(out_x, out_y, label="output similarity")
in_x, in_y = gen_xy(input, False)
ax.plot(in_x, in_y, label="input similarity")
ax.legend()
ax.set_xlabel("Cosine similarity value")
ax.set_label("Cumulative frequency")
fig.savefig("canalysis_cumulative_th1.pdf", format="pdf")