In [1]:
drive_path = 'c:/'
import numpy as np
import pandas as pd
import os
import sys
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.stats import ks_2samp
from scipy.stats import anderson_ksamp
from scipy.stats import kruskal
from scipy.stats import variation
from scipy import signal as sps
import seaborn as sns
import glob
import re
from scipy.optimize import curve_fit

In [12]:
names=['Plane','Time','Elapsed (ms)']
df=pd.read_table('C:\\Users\Annie\Desktop\\160621_1_Notepad\\20.txt',skiprows=4)
names.extend([col for col in df.columns if 'G PMT' in col])
df=df.loc[df.Plane != 0,df.columns.isin(names)]
# # odf is the original dataframe, only G PMT columns in this dataframe
odf=df[[col for col in df.columns if 'G PMT' in col]]

In [13]:
df


Out[13]:
Unnamed: 0 G PMT Bkgnd G PMT (1) G PMT (1) - Bkgnd G PMT (2) G PMT (2) - Bkgnd G PMT (3) G PMT (3) - Bkgnd G PMT (4) G PMT (4) - Bkgnd ... G PMT (9) G PMT (9) - Bkgnd G PMT (10) G PMT (10) - Bkgnd G PMT (11) G PMT (11) - Bkgnd G PMT (12) G PMT (12) - Bkgnd G PMT (13) G PMT (13) - Bkgnd
0 0 -0.173974 -0.037080 0.045111 -0.150424 -0.126861 -0.129626 -0.086705 -0.181419 -0.280876 ... -0.155870 -0.167104 -0.123943 -0.020733 -0.231920 -0.290227 -0.059062 0.150835 -0.155942 -0.100515
1 1 0.005308 -0.009453 -0.018315 0.033122 0.060951 -0.058840 -0.120925 -0.010911 -0.001588 ... 0.066517 0.097447 -0.037537 0.117418 -0.022525 -0.035455 0.044139 0.120244 0.011000 0.062883
2 2 0.044849 0.052838 0.057634 0.036866 0.028880 0.018180 -0.007630 0.025891 -0.122600 ... 0.023165 -0.002138 -0.018567 0.155103 0.035018 0.024184 0.045777 0.020358 -0.007325 -0.100967
3 3 0.015619 0.026905 0.033681 0.047168 0.078733 -0.007146 -0.029179 -0.008179 0.072622 ... -0.002991 -0.026678 0.010043 0.218834 0.049929 0.082618 0.057510 0.137992 0.020968 0.010058
4 4 0.005005 0.008016 0.009825 -0.011547 -0.028107 0.028714 0.051661 -0.000725 -0.113339 ... 0.011875 -0.003998 0.019237 0.075800 0.037489 0.068414 0.010597 0.007708 0.013365 0.036398
5 5 -0.021996 -0.002045 0.009933 -0.007589 0.006825 -0.019107 -0.016310 -0.036488 -0.083074 ... -0.054532 -0.053448 -0.033785 -0.208866 0.015775 0.051940 -0.002937 0.062616 -0.010715 0.008689
6 6 0.003327 -0.010274 -0.018440 -0.005729 -0.014790 0.063135 0.121017 -0.038915 -0.158522 ... 0.010681 -0.004437 0.025661 0.137764 0.018141 0.031721 0.027152 0.047541 0.009222 0.052809
7 7 0.006958 0.044901 0.067683 0.002893 -0.001175 -0.009121 -0.024682 0.062462 0.110164 ... -0.084654 -0.128123 0.018985 0.100910 0.009037 0.010104 0.034719 0.077103 0.031710 0.096603
8 8 -0.038056 -0.008066 0.009939 -0.030762 -0.023464 -0.025669 -0.013681 0.016654 -0.153866 ... -0.052675 -0.044551 0.024772 -0.023800 -0.039871 -0.042608 -0.044458 -0.068302 -0.014501 0.029622
9 9 -0.007638 -0.000618 0.003596 -0.039347 -0.071073 -0.015216 -0.022550 0.010431 0.102192 ... 0.064772 0.103150 -0.029338 -0.017011 -0.030015 -0.052980 0.002733 0.024250 -0.010168 -0.005966
10 10 -0.015759 -0.004194 0.002750 0.013743 0.043259 0.040787 0.095513 -0.030893 -0.166922 ... 0.013929 0.036463 0.018794 -0.048544 -0.038334 -0.061462 -0.020491 -0.000054 -0.013146 -0.017591
11 11 0.015350 0.006558 0.001279 0.023249 0.031153 0.040962 0.065750 -0.037707 -0.096313 ... -0.009281 -0.003504 0.022455 0.051603 0.008514 0.000924 0.013756 0.014962 0.003954 0.001568
12 12 0.003717 -0.012748 -0.022633 -0.042408 -0.088558 -0.005288 -0.014005 -0.002973 -0.119423 ... -0.086939 -0.101502 0.023385 -0.077246 0.009210 0.013795 -0.000473 0.028200 0.016278 0.038704
13 13 -0.029194 -0.048494 -0.060081 -0.002552 0.024103 -0.041120 -0.052662 -0.007002 0.212531 ... -0.022509 -0.005701 -0.020736 -0.012674 -0.015496 -0.002794 -0.033738 -0.024981 -0.021257 0.009457
14 14 -0.017190 0.011309 0.028419 -0.036602 -0.056025 -0.056509 -0.094563 -0.021563 -0.058651 ... 0.071305 0.082165 -0.035613 -0.081476 -0.058112 -0.099194 -0.020464 -0.008633 -0.039463 -0.113983
15 15 -0.016679 -0.022554 -0.026081 -0.044499 -0.072333 -0.001801 0.012598 0.015266 0.225660 ... -0.033357 -0.052619 -0.024146 -0.043086 -0.044930 -0.073498 -0.030050 -0.077243 -0.005937 0.016964
16 16 0.001076 -0.022670 -0.036927 0.012456 0.023842 -0.009105 -0.018958 0.004304 -0.048952 ... 0.045628 0.074936 0.023156 -0.054858 0.017121 0.032223 -0.028223 -0.080433 0.005343 -0.001113
17 17 0.015301 0.000098 -0.009029 0.011034 0.006765 0.002068 -0.010740 -0.002775 -0.065591 ... -0.008506 -0.000194 0.026742 -0.152796 -0.005614 -0.026895 -0.012731 -0.087945 0.002495 -0.056092
18 18 -0.010598 -0.035240 -0.050035 -0.011674 -0.012751 0.006741 0.023522 0.006576 0.414551 ... 0.049775 0.053464 -0.034498 -0.119845 -0.010557 -0.011164 -0.042165 -0.082514 -0.014555 -0.022107
19 19 0.024622 0.015165 0.009486 0.014193 0.003758 0.033981 0.043039 0.006928 0.171708 ... 0.048111 0.040887 -0.011824 -0.003030 0.004875 -0.015200 -0.009105 -0.082200 0.003440 -0.056647
20 20 0.010919 -0.008363 -0.019940 0.026210 0.041509 0.014772 0.018501 0.056970 -0.013010 ... -0.051607 -0.055866 0.035995 -0.134128 0.052915 0.093699 -0.032104 -0.134887 0.011588 -0.020531
21 21 0.007927 -0.020506 -0.037578 0.002002 -0.003927 0.031976 0.055252 0.014371 -0.084014 ... 0.053832 0.053829 0.007446 0.003844 0.024586 0.040436 -0.009365 -0.035804 0.010530 -0.003170
22 22 0.019818 0.014170 0.010779 0.012438 0.005054 -0.001001 -0.021148 -0.025374 -0.157958 ... -0.046319 -0.051093 0.036501 -0.004980 0.044029 0.067346 -0.055407 -0.219116 0.000289 -0.052959
23 23 0.001354 -0.038575 -0.062548 0.005286 0.009221 -0.046358 -0.092534 -0.012651 -0.030963 ... 0.000956 -0.011838 -0.010174 0.096189 0.040493 0.078546 -0.017543 -0.059097 -0.017002 -0.052098
24 24 0.025215 0.035629 0.041882 0.026098 0.026982 0.048663 0.071356 0.022345 -0.084688 ... -0.029749 -0.058412 0.021536 -0.079701 0.044239 0.062499 -0.020008 -0.126156 0.010111 0.008445
25 25 -0.007999 -0.010388 -0.011822 -0.000275 0.007455 0.008773 0.025006 0.035003 0.147540 ... -0.035412 -0.055395 0.022947 -0.021047 0.006408 0.020146 -0.066619 -0.185202 0.012170 0.071759
26 26 0.001831 0.000299 -0.000621 0.011251 0.020675 -0.003839 -0.009327 0.026019 0.155606 ... 0.092743 0.139826 -0.026627 -0.181524 0.032616 0.062517 -0.051663 -0.131394 0.001292 0.024702
27 27 0.060308 0.007420 -0.024333 0.037591 0.014864 0.009445 -0.039780 0.055519 0.091933 ... 0.023124 0.031242 0.008970 -0.213851 0.004154 -0.051576 -0.029263 -0.166450 0.002650 -0.133819
28 28 0.091353 0.030819 -0.005525 0.066008 0.040650 0.016883 -0.055188 0.133080 0.086060 ... -0.035079 -0.084418 0.100508 0.008379 0.081926 0.072260 0.062105 0.003386 0.045841 0.003843
29 29 0.136306 0.057814 0.010687 0.098388 0.060448 0.053080 -0.027467 0.170729 -0.026163 ... 0.025749 0.011336 0.183534 -0.048880 0.111233 0.086177 0.067684 -0.084084 0.028820 -0.168509
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
145 145 -0.016792 -0.031859 -0.040907 -0.030536 -0.044289 0.039404 0.093789 0.007976 -0.074742 ... -0.002081 0.025305 -0.057982 -0.030449 0.002577 0.021847 0.030396 0.118733 -0.026830 -0.036249
146 146 0.022144 0.009479 0.001875 0.030465 0.038791 0.010773 -0.000233 0.022222 0.015715 ... -0.008476 -0.022898 0.040742 -0.172971 0.038143 0.054075 0.039179 0.069263 0.009166 -0.043397
147 147 0.007942 0.023860 0.033419 0.030941 0.053954 0.037631 0.066366 0.025170 -0.145758 ... -0.089422 -0.123038 0.044134 -0.064646 0.040143 0.072025 0.023251 0.050372 0.026994 0.072734
148 148 -0.013483 -0.004045 0.001622 -0.019127 -0.024774 -0.006859 -0.000448 -0.010979 -0.040520 ... -0.018688 -0.025498 -0.010689 0.036050 -0.000395 0.012638 0.046469 0.159061 0.017822 0.062398
149 149 0.008888 0.026028 0.036318 -0.050941 -0.110801 -0.039608 -0.086543 0.061657 0.152993 ... 0.064263 0.074498 -0.000833 0.036786 -0.002820 -0.014234 0.033280 0.077679 -0.009957 -0.053492
150 150 -0.025682 -0.008458 0.001884 -0.011864 0.001962 -0.000888 0.023110 0.014332 -0.158434 ... -0.054292 -0.041651 0.022375 0.082693 -0.007506 0.010509 0.017100 0.097276 -0.004025 0.024317
151 151 -0.003381 -0.033940 -0.052287 0.029967 0.063332 -0.001413 0.000491 -0.041680 -0.082795 ... -0.007150 -0.014086 0.038328 0.149385 -0.006971 -0.010419 0.033193 0.101055 -0.001782 0.013909
152 152 -0.016299 -0.034010 -0.044644 0.002335 0.020979 0.002046 0.019800 0.006687 -0.152884 ... -0.053236 -0.069703 0.016461 0.100478 -0.012595 -0.008876 0.001254 0.033184 -0.017429 -0.037220
153 153 -0.043949 -0.054072 -0.060151 -0.055083 -0.066223 -0.058355 -0.072298 -0.046805 0.330444 ... 0.112813 0.152562 -0.070117 -0.220978 -0.008880 0.025724 -0.029688 -0.003422 -0.015996 0.066323
154 154 -0.003587 -0.019454 -0.028982 0.021960 0.047519 -0.053319 -0.101451 -0.030181 0.130525 ... 0.001245 0.024735 0.007834 0.012775 0.009216 0.021865 0.022577 0.070606 -0.024719 -0.082520
155 155 -0.011004 -0.013423 -0.014876 -0.014985 -0.018968 0.005366 0.021209 -0.037067 0.132905 ... -0.025656 -0.009887 0.044255 0.026146 0.015310 0.041251 -0.054945 -0.140234 0.020031 0.076650
156 156 0.012979 -0.003459 -0.013327 0.018625 0.024274 -0.024156 -0.060095 -0.025919 -0.293854 ... 0.009567 0.001778 -0.001879 0.107837 0.009047 0.005171 -0.046436 -0.161724 0.009768 0.013420
157 157 -0.009179 -0.012912 -0.015154 -0.017693 -0.026212 -0.031790 -0.053673 -0.023650 0.085960 ... -0.029988 -0.042632 -0.035028 0.067815 -0.012469 -0.015733 -0.075833 -0.188057 0.003402 0.014497
158 158 -0.012253 -0.008710 -0.006582 -0.014971 -0.017690 -0.021942 -0.031320 -0.025330 0.022144 ... -0.005871 -0.010771 -0.037213 -0.073943 0.012815 0.037472 -0.044817 -0.090738 -0.039730 -0.050169
159 159 0.016495 0.043380 0.059522 0.015405 0.014314 -0.020126 -0.055568 0.020311 0.239346 ... 0.066004 0.072986 0.026576 -0.015876 -0.000480 -0.017265 0.028215 0.048230 -0.009993 -0.040130
160 160 -0.003650 0.052076 0.085534 0.001220 0.006093 -0.023494 -0.042699 0.043901 0.183896 ... -0.034669 -0.050818 0.034606 -0.004693 0.040032 0.082987 0.000410 0.006265 0.004692 -0.018152
161 161 -0.004369 0.003459 0.008160 0.013295 0.030970 -0.034281 -0.063231 -0.046218 -0.266289 ... -0.010581 -0.019717 0.007213 0.068074 0.004793 0.013716 -0.026561 -0.070695 -0.009535 -0.002796
162 162 0.004289 0.006555 0.007916 -0.002141 -0.008574 0.012119 0.019698 -0.020537 -0.164256 ... -0.032050 -0.024283 -0.042418 -0.025359 -0.010722 -0.025636 -0.000584 -0.011961 -0.018970 -0.029395
163 163 0.003069 0.012634 0.018377 -0.001512 -0.006097 0.016664 0.029821 0.017241 -0.234137 ... 0.008869 0.030814 -0.026964 0.074290 -0.002646 -0.008423 -0.028222 -0.089899 -0.010864 -0.082141
164 164 0.024978 0.021453 0.019337 0.000147 -0.024698 0.044888 0.064156 0.042906 0.280919 ... 0.041713 0.038268 0.010812 0.013409 0.021179 0.017267 -0.005408 -0.065876 0.012720 -0.036699
165 165 -0.017722 -0.006588 0.000096 0.010241 0.038220 0.034192 0.084434 0.003445 0.253927 ... -0.067421 -0.089611 0.008990 0.122629 0.006517 0.030207 -0.063154 -0.151360 0.031018 0.137147
166 166 -0.016324 -0.007563 -0.002303 -0.053018 -0.089730 -0.019054 -0.021697 -0.030841 -0.032554 ... 0.033321 0.068870 0.001537 -0.099416 -0.007239 0.001500 -0.027332 -0.050064 0.002185 0.020891
167 167 0.001724 0.034373 0.053976 0.008426 0.015131 -0.020350 -0.041714 -0.020536 -0.073756 ... -0.024755 -0.040881 -0.019455 -0.183500 -0.025347 -0.052255 -0.002355 -0.012328 -0.023883 -0.059753
168 168 0.010626 -0.014039 -0.028848 -0.043286 -0.097226 0.021227 0.031487 0.048222 0.201109 ... -0.021698 -0.012562 -0.010643 -0.040122 -0.024728 -0.059817 -0.003578 -0.033061 0.002499 -0.054162
169 169 0.007482 0.014719 0.019064 -0.030699 -0.068900 0.018814 0.029781 -0.031212 -0.236305 ... 0.013478 0.034387 0.017746 -0.093731 0.016976 0.026047 0.035516 0.086476 0.016407 0.013167
170 170 -0.027055 -0.021422 -0.018039 0.003850 0.034772 0.015751 0.057179 -0.043812 0.154588 ... -0.003550 -0.005381 -0.008948 -0.115327 -0.033577 -0.040307 -0.023247 -0.017694 0.011202 0.106758
171 171 0.008119 -0.004955 -0.012804 -0.011971 -0.032073 -0.022149 -0.051444 0.024094 0.018989 ... 0.090990 0.105602 -0.034566 -0.109043 -0.029343 -0.066584 0.010340 0.012051 -0.005387 -0.075678
172 172 0.022441 0.024993 0.026526 0.061107 0.099794 0.040118 0.057226 0.045002 -0.029303 ... -0.042136 -0.070522 0.026013 -0.137642 -0.008096 -0.038536 0.039203 0.068235 -0.005429 -0.020201
173 173 0.010812 -0.032489 -0.058488 0.029173 0.047544 0.051452 0.090784 0.030930 0.076484 ... -0.000805 -0.013923 0.048075 0.124377 0.015511 0.019761 0.041693 0.097811 0.019929 0.030618
174 174 0.000383 -0.016895 -0.027269 0.007786 0.015193 -0.010053 -0.020154 0.013005 -0.185572 ... 0.021756 0.018118 0.008520 0.164870 -0.002746 -0.006234 0.009611 0.043228 -0.000731 0.002440

175 rows × 28 columns


In [3]:
os.chdir('C:\\Users\\Annie\\Documents\\Data\\Ca_Imaging\\GoodFiles\\160321_2')
for filename in glob.glob('*.txt'):
    df=pd.read_table(filename,skiprows=4)
    odf=df[[col for col in df.columns if 'G PMT' in col]]
    # dff is DF/F for every cell
    base=[];
    for col in odf.columns:
        value=odf.loc[2:23,col].mean();
        base.append(value)
    base=pd.DataFrame(base).T
    base.columns=odf.columns
    # base
    dff=[];
    for col in odf.columns:
        value=(odf[col]-np.asscalar(base[col]))/np.asscalar(base[col])
        dff.append(value)
    dff=pd.DataFrame(dff).T
    # detrend
    dt=[];
    firsta=1;
    firstb=20;
    lasta=130
    lastb=174
    for col in dff.columns:
        firstvalues=pd.DataFrame([np.arange(firsta,firstb+1,1),dff.loc[firsta:firstb,col]]).T
        lastvalues=pd.DataFrame([np.arange(lasta,lastb+1,1),dff.loc[lasta:lastb,col]]).T
        temp=firstvalues.append(lastvalues)
        fitline=np.polyfit(temp[0],temp[1],2)
        tmp=np.poly1d(fitline)
        fitvalues=tmp(np.arange(len(dff[col])))
        detrended=np.subtract(dff[col],fitvalues)
        dt.append(detrended)
    dt=pd.DataFrame(dt).T
#     dt.to_csv(filename.split('.')[0]+'dt.txt')

In [4]:
test=pd.read_csv('C:\\Users\\Annie\\Documents\\Data\\Ca_Imaging\\GoodFiles\\160321_1\\21dt.txt')
test=test[[col for col in test.columns if 'G PMT' in col]]
plt.plot(test);



In [ ]:


In [ ]:


In [ ]:


In [ ]:

Do this fit on all of the cells from one trial


In [4]:
# dff is DF/F for every cell
base=[];
for col in odf.columns:
    value=odf.loc[2:23,col].mean();
    base.append(value)
base=pd.DataFrame(base).T
base.columns=odf.columns
# base
dff=[];
for col in odf.columns:
    value=(odf[col]-np.asscalar(base[col]))/np.asscalar(base[col])
    dff.append(value)
dff=pd.DataFrame(dff).T
# detrend
dt=[];
firsta=1;
firstb=20;
lasta=130
lastb=174
for col in dff.columns:
    firstvalues=pd.DataFrame([np.arange(firsta,firstb+1,1),dff.loc[firsta:firstb,col]]).T
    lastvalues=pd.DataFrame([np.arange(lasta,lastb+1,1),dff.loc[lasta:lastb,col]]).T
    temp=firstvalues.append(lastvalues)
    fitline=np.polyfit(temp[0],temp[1],2)
    tmp=np.poly1d(fitline)
    fitvalues=tmp(np.arange(len(dff[col])))
    detrended=np.subtract(dff[col],fitvalues)
    dt.append(detrended)
dt=pd.DataFrame(dt).T
plt.plot(dt);


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-01a85792bba0> in <module>()
      1 # dff is DF/F for every cell
      2 base=[];
----> 3 for col in odf.columns:
      4     value=odf.loc[2:23,col].mean();
      5     base.append(value)

NameError: name 'odf' is not defined

In [6]:
plt.plot(dff);


Testing


In [24]:
# Divided is DF/F for first column
baseline=pd.DataFrame(odf.iloc[5:26,11]).mean();
example=pd.DataFrame(odf.iloc[:,11]);
temp=example.subtract(baseline);
divided=temp.divide(baseline);
plt.figure();
plt.plot(divided);



In [25]:
#Get the values of the first and last part
dt=divided.iloc[:,0];
# plt.plot(dt);
first=[np.arange(1,20,1),divided.iloc[1:20,0]];
# plt.plot(first[1]);
last=[np.arange(130,174,1),divided.iloc[130:174,0]];
# plt.plot(last[1]);
firstdf=pd.DataFrame(first).T
lastdf=pd.DataFrame(last).T

In [26]:
#Make a fit line
temp=firstdf.append(lastdf)
fitline=np.polyfit(temp[0],temp[1],2);
tmp=np.poly1d(fitline)
# plt.plot(tmp(np.arange(len(dt))));
# plt.plot(temp[0],temp[1]);

In [27]:
#Get dtrended values
fitvalues=tmp(np.arange(len(dt)))
detrended=np.subtract(dt,fitvalues)
plt.plot(detrended);
# plt.plot(dt);



In [ ]:


In [ ]:


In [16]:
temp=firstdf.append(lastdf)
def model_func(x,a,k,b):
    return a*np.exp(-k*x)+b

y=np.array(temp.iloc[:,1])
x=np.array(temp.iloc[:,0])

#curve fit
p0=(1.,1.e-5,1.)
opt,pcov=curve_fit(model_func,x,y,p0)
a,k,b=opt

#plot
x2=np.linspace(0,180,2)
y2=model_func(x2,a,k,b)
# plt.plot(x2,y2);
# plt.plot(x,y);


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-16-de298212a424> in <module>()
      8 #curve fit
      9 p0=(1.,1.e-5,1.)
---> 10 opt,pcov=curve_fit(model_func,x,y,p0)
     11 a,k,b=opt
     12 

C:\Users\Annie\Anaconda2\lib\site-packages\scipy\optimize\minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, **kwargs)
    653         cost = np.sum(infodict['fvec'] ** 2)
    654         if ier not in [1, 2, 3, 4]:
--> 655             raise RuntimeError("Optimal parameters not found: " + errmsg)
    656     else:
    657         res = least_squares(func, p0, args=args, bounds=bounds, method=method,

RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.

In [98]:


In [ ]:


In [ ]:


In [ ]: