-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocessingFunctions.py
More file actions
66 lines (53 loc) · 1.89 KB
/
PreprocessingFunctions.py
File metadata and controls
66 lines (53 loc) · 1.89 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 19 11:21:31 2023
@author: kenta.hagihara
"""
import os
import csv
import numpy as np
import pylab as plt
from scipy.signal import medfilt, butter, filtfilt
from scipy.stats import linregress
from scipy.optimize import curve_fit, minimize
import glob
#Preprocessing functions
def tc_crop(tc, nFrame2cut):
tc_cropped = tc[nFrame2cut:]
return tc_cropped
def tc_medfilt(tc, kernelSize):
tc_filtered = medfilt(tc, kernel_size=kernelSize)
return tc_filtered
def tc_lowcut(tc, sampling_rate):
b,a = butter(2, 9, btype='low', fs=sampling_rate)
tc_filtered = filtfilt(b,a, tc)
return tc_filtered
def tc_polyfit(tc, sampling_rate, degree):
time_seconds = np.arange(len(tc)) /sampling_rate
coefs = np.polyfit(time_seconds, tc, deg=degree)
tc_poly = np.polyval(coefs, time_seconds)
return tc_poly
def tc_slidingbase(tc, sampling_rate):
b,a = butter(2, 0.0001, btype='low', fs=sampling_rate)
tc_base = filtfilt(b,a, tc, padtype='even')
return tc_base
def tc_dFF(tc, tc_base, b_percentile):
tc_dFoF = tc/tc_base
sort = np.sort(tc_dFoF)
b_median = np.median(sort[0:round(len(sort) * b_percentile)])
tc_dFoF = tc_dFoF - b_median
return tc_dFoF
def tc_filling(tc, nFrame2cut):
tc_filled = np.append(np.ones([nFrame2cut,1])*tc[0], tc)
return tc_filled
#Preprocessing total function
def tc_preprocess(tc, nFrame2cut, kernelSize, sampling_rate, degree, b_percentile):
tc_cropped = tc_crop(tc, nFrame2cut)
tc_filtered = medfilt(tc_cropped, kernel_size=kernelSize)
tc_filtered = tc_lowcut(tc_filtered, sampling_rate)
tc_poly = tc_polyfit(tc_filtered, sampling_rate, degree)
tc_estim = tc_filtered - tc_poly
tc_base = tc_slidingbase(tc_filtered, sampling_rate)
tc_dFoF = tc_dFF(tc_filtered, tc_base, b_percentile)
tc_dFoF = tc_filling(tc_dFoF, nFrame2cut)
return tc_dFoF