-
Notifications
You must be signed in to change notification settings - Fork 1
/
ca-raw.py
executable file
·131 lines (102 loc) · 3.34 KB
/
ca-raw.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
#!/usr/bin/env python
# coding=utf-8
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
import argparse
import sys
import os
import datetime
import nixio as nix
import numpy as np
import pandas as pd
from ca.nix import *
from ca.img import *
loops = {
"1": [3, 1, 10, 1],
"2": [3, 1, 25, 3],
"3": [1, 25, 3, 1, 10]
}
pulses = [1, 3, 10, 25]
ages = [10, 11, 13, 14, 15, 16, 17, 18, 60]
def items_of_type(lst, the_type):
return [x for x in lst if x.type == the_type]
def item_of_type(lst, the_type):
items = items_of_type(lst, the_type)
if len(items) != 1:
raise RuntimeError("Bleh %d" % len(items))
return items[0]
def plot(data, conds, channel):
import matplotlib.pyplot as plt
plt.figure()
plt.title(channel)
plt.plot(data)
plt.legend([str(x) for x in conds])
plt.figure()
plt.title(channel)
plt.imshow(data, interpolation="None")
plt.colorbar()
plt.show()
def main():
parser = argparse.ArgumentParser(description="")
parser.add_argument("path")
parser.add_argument("-o, --outfile", dest="outfile", action="store_true", default=False)
parser.add_argument("-p, --plot", dest="plot", action="store_true", default=False)
parser.add_argument("channel", choices=["red", "green"])
args = parser.parse_args()
nf = nix.File.open(args.path, nix.FileMode.ReadOnly)
neurons = [b for b in nf.blocks if b.type == 'neuron']
if len(neurons) > 1:
print("[W] More then one neuron in file, funny", file=sys.stderr)
sys.exit(1)
neuron = neurons[0]
images = sorted(items_of_type(neuron.groups, "image.ca"),
key=lambda x: x.metadata['creation_time'])
totalred = []
totalgreen = []
redconds = []
for img in images:
meta = img.metadata
condition = meta['condition']
loop = loops.get(condition, None)
if loop is None:
print("[W] %s: unknown loop. skipping" % condition, file=sys.stderr)
continue
das = img.data_arrays
channels = item_of_type(das, "channel")
red = list(np.nonzero(np.array(channels) == 1)[0])
kymo = item_of_type(das, 'kymo.fg')
data = kymo[:]
l, t = data.shape
allred = np.empty((l, len(red)))
allgreen = np.empty((l, len(red)))
cut = split_image(data)
for i, r in enumerate(red):
f0 = cut[i][0][:, 0:10].mean(axis=1)
allred[:, i] = data[:, r]
allgreen[:, i] = f0
totalred.append(allred)
totalgreen.append(allgreen)
[redconds.append(l) for l in loop]
#print(totalred)
allred = np.hstack(totalred)
allgreen = np.hstack(totalgreen)
if args.channel == "red":
alldata = allred
else:
alldata = allgreen
filename = sys.stdout
if args.outfile:
bn = os.path.basename(args.path)
name, ext = os.path.splitext(bn)
filename = "%s.%s.csv" % (name, args.channel)
#print(allred.shape)
print("images:", [i.name for i in images], file=sys.stderr)
print("conditions:", redconds, file=sys.stderr)
if args.plot:
plot(alldata, redconds, filename)
df = pd.DataFrame(alldata)
df.columns = [str(x) for x in redconds]
df.to_csv(filename, index=False)
if __name__ == '__main__':
main()