forked from katemartian/Photometry_data_processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_zdFF.py
64 lines (51 loc) · 2.52 KB
/
get_zdFF.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
'''
get_zdFF.py calculates standardized dF/F signal based on calcium-idependent
and calcium-dependent signals commonly recorded using fiber photometry calcium imaging
Ocober 2019 Ekaterina Martianova [email protected]
Reference:
(1) Martianova, E., Aronson, S., Proulx, C.D. Multi-Fiber Photometry
to Record Neural Activity in Freely Moving Animal. J. Vis. Exp.
(152), e60278, doi:10.3791/60278 (2019)
https://www.jove.com/video/60278/multi-fiber-photometry-to-record-neural-activity-freely-moving
'''
def get_zdFF(reference,signal,smooth_win=10,remove=200,lambd=5e4,porder=1,itermax=50):
'''
Calculates z-score dF/F signal based on fiber photometry calcium-idependent
and calcium-dependent signals
Input
reference: calcium-independent signal (usually 405-420 nm excitation), 1D array
signal: calcium-dependent signal (usually 465-490 nm excitation for
green fluorescent proteins, or ~560 nm for red), 1D array
smooth_win: window for moving average smooth, integer
remove: the beginning of the traces with a big slope one would like to remove, integer
Inputs for airPLS:
lambd: parameter that can be adjusted by user. The larger lambda is,
the smoother the resulting background, z
porder: adaptive iteratively reweighted penalized least squares for baseline fitting
itermax: maximum iteration times
Output
zdFF - z-score dF/F, 1D numpy array
'''
import numpy as np
from sklearn.linear_model import Lasso
# Smooth signal
reference = smooth_signal(reference, smooth_win)
signal = smooth_signal(signal, smooth_win)
# Remove slope using airPLS algorithm
r_base=airPLS(reference,lambda_=lambd,porder=porder,itermax=itermax)
s_base=airPLS(signal,lambda_=lambd,porder=porder,itermax=itermax)
# Remove baseline and the begining of recording
reference = (reference[remove:] - r_base[remove:])
signal = (signal[remove:] - s_base[remove:])
# Standardize signals
reference = (reference - np.median(reference)) / np.std(reference)
signal = (signal - np.median(signal)) / np.std(signal)
# Align reference signal to calcium signal using non-negative robust linear regression
lin = Lasso(alpha=0.0001,precompute=True,max_iter=1000,
positive=True, random_state=9999, selection='random')
n = len(reference)
lin.fit(reference.reshape(n,1), signal.reshape(n,1))
reference = lin.predict(reference.reshape(n,1)).reshape(n,)
# z dFF
zdFF = (signal - reference)
return zdFF