-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplyPLSModelToNovelSessionScript.py
More file actions
182 lines (146 loc) · 7.49 KB
/
ApplyPLSModelToNovelSessionScript.py
File metadata and controls
182 lines (146 loc) · 7.49 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 5 16:52:07 2020
@author: thugwithyoyo
"""
import numpy as np
import PeriEventTraceFuncLib as PETFL
import LongRegFuncLib as LRFL
from collections import defaultdict
import os
import shelve
from tkinter import *
from tkinter import filedialog
# Specify the number of latent factors to include in PLS regression model.
nLatents = 3
############## Acquire the training and testing data sets #####################
# Acquire path of workspace to load.
#
CellFluorTraces_Frame_train, BehavDict_train, SessionName_train = LRFL.BuildSessionDatasets()
#
CellFluorTraces_Frame_test, BehavDict_test, SessionName_test = LRFL.BuildSessionDatasets()
## Set defualt parent directories
#DataRootDir = '/home/thugwithyoyo/CaTransDecoding/CalciumImagingData'
#SaveRootDir = '/home/thugwithyoyo/CaTransDecoding/Output'
#
## Start file dialogs
#root = tk.Tk()
#
### Begin Training block ##
## Prompt user to navigate to, and select, the session behavior file
#PathToBehavFile = askopenfilename(title='Select TRAINING session behavior file',
# filetypes=[("json files","*.json"),
# ("csv files", "*.csv")],
# initialdir=DataRootDir)
#
## Prompt user to navigate to, and select, the session imaging file
#PathToFluorFile = askopenfilename(title='Select TRAINING session fluorescence record file',
# filetypes=[("json files","*.json"),
# ("csv files", "*.csv")],
# initialdir=DataRootDir)
#
## Assemble the dataframe of cell fluorescence traces.
#CellFluorTraces_Frame_train = CellFluorTraces_FrameGen(PathToFluorFile)
#
## Assemble the dictionary of event occurence timestamps
#BehavDict_train = BehavDictGen(PathToBehavFile)
### End Training block ##
#
### Begin Testing block ##
## Prompt user to navigate to, and select, the session behavior file
#PathToBehavFile = askopenfilename(title='Select TESTING session behavior file',
# filetypes=[("json files","*.json"),
# ("csv files", "*.csv")],
# initialdir=DataRootDir)
#
## Prompt user to navigate to, and select, the session imaging file
#PathToFluorFile = askopenfilename(title='Select TESTING session fluorescence record file',
# filetypes=[("json files","*.json"),
# ("csv files", "*.csv")],
# initialdir=DataRootDir)
#
## Assemble the dataframe of cell fluorescence traces.
#CellFluorTraces_Frame_test = CellFluorTraces_FrameGen(PathToFluorFile)
#
## Assemble the dictionary of event occurence timestamps
#BehavDict_test = BehavDictGen(PathToBehavFile)
### End Testing block ##
#
## End file dialogs
#root.withdraw()
######### Arrange the fluorescence dataframe into global arrangement ##########
lr_frame = LRFL.BuildLongRegDataframeFromCSV()
#SessionName_test = '2018-12-27-11-04-59'
CommonCellsDict = LRFL.CommonCellsExtractor(lr_frame,
SessionName_train,
SessionName_test)
(NumCells,) = CommonCellsDict['S1_local_cellset'].shape
CellFluorTraces_Frame_train = CellFluorTraces_Frame_train[
np.hstack([np.array(['Timestamps']),
CommonCellsDict['S1_local_cellset']])]
CellFluorTraces_Frame_train_shuf = CellFluorTraces_Frame_train[
np.hstack([np.array(['Timestamps']),
np.random.choice(CommonCellsDict['S1_local_cellset'],
size=NumCells, replace=False)])]
CellFluorTraces_Frame_test = CellFluorTraces_Frame_test[
np.hstack([np.array(['Timestamps']),
CommonCellsDict['S2_local_cellset']])]
CellFluorTraces_Frame_test_shuf = CellFluorTraces_Frame_test[
np.hstack([np.array(['Timestamps']),
np.random.choice(CommonCellsDict['S2_local_cellset'],
size=NumCells, replace=False)])]
#"ColumnsSortMap_train" = pd.
# CellFlourTraces_Frame_train = CellFlourTraces_Frame_train["ColumnsSortMap_train"]
#"ColumnsSortMap_test" = pd.
# CellFlourTraces_Frame_test = CellFlourTraces_Frame_test["ColumnsSortMap_test"]
###############################################################################
# Run the peri event extractor using the (arranged) fluorescence dataframe and
# corresponding behavior dictionary to assemble the training and testing datasets.
# Define a dictionary to contain keyed access to sliding window analysis parameters
ParamsDict = defaultdict(dict)
# Right arm to both right and left targets
ParamsDict['RefEventsList'] = ['M6T0_Entry_ts', 'M6T1_Entry_ts']
# Scalar values assigned to event types listed above.
ParamsDict['AssignedEventVals'] = [-1, 1]
# Set peri-event extraction window (seconds)
ParamsDict['BoundaryWindow'] = [-1., 1.]
# Pack into a dict the two lists that specify the names and assigned
# numerical values of the behavioral event types listed in ParamsDict
RefEventsDict = {'RefEventsList' : ParamsDict['RefEventsList'],
'AssignedEventVals' : ParamsDict['AssignedEventVals']}
# Run peri-event extraction and assemble the dictionary to be used for
# PLS-regression training.
PeriEventExtractorDict_train = PETFL.PeriEventExtractor_Trace(BehavDict_train,
CellFluorTraces_Frame_train,
RefEventsDict,
ParamsDict['BoundaryWindow'])
PeriEventExtractorDict_train_shuf = PETFL.PeriEventExtractor_Trace(BehavDict_train,
CellFluorTraces_Frame_train_shuf,
RefEventsDict,
ParamsDict['BoundaryWindow'])
# Run peri-event extraction and assemble the dictionary to be used to test the
# PLS-regression model above.
PeriEventExtractorDict_test = PETFL.PeriEventExtractor_Trace(BehavDict_test,
CellFluorTraces_Frame_test,
RefEventsDict,
ParamsDict['BoundaryWindow'])
PeriEventExtractorDict_test_shuf = PETFL.PeriEventExtractor_Trace(BehavDict_test,
CellFluorTraces_Frame_test_shuf,
RefEventsDict,
ParamsDict['BoundaryWindow'])
###############################################################################
# Compute the PLS regression matrices from the training dataset.
Performance_train = PETFL.PLS_DecoderPerformance(PeriEventExtractorDict_train,
nLatents)
###############################################################################
##
Performance_test = PETFL.ApplyPLSModel(Performance_train['B'],
Performance_train['B_0'],
PeriEventExtractorDict_test)
Performance_test_shuf = PETFL.ApplyPLSModel(Performance_train['B'],
Performance_train['B_0'],
PeriEventExtractorDict_test_shuf)
Performance_train_shuf = PETFL.ApplyPLSModel(Performance_train['B'],
Performance_train['B_0'],
PeriEventExtractorDict_train_shuf)