Skip to content

Commit e71d97c

Browse files
authored
Update README.md with MATLAB calling function
1 parent 0eee094 commit e71d97c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ Save an "ops.npy" file with the parameters and a "spks.npy" file with a matrix o
166166
python -m rastermap --S spks.npy --ops ops.npy
167167
~~~
168168

169+
## From MATLAB
170+
If you have an existing MATLAB analysis pipeline (and are using MATLAB version R2021b or newer), you can use MATLAB's Python interface to call Rastermap. First you need to tell MATLAB where your Python enviroment with Rastermap is (more details: https://www.mathworks.com/help/matlab/matlab_external/install-supported-python-implementation.html). Create or modify the "PYTHONHOME" environmental variable in your OS to point to the Rastermap environment root folder. Then in MATLAB run the following statement, modified for the specific path to your Rastermap environment pythonw executable:
171+
172+
```
173+
pyenv('Version','C:\Users\admin\.conda\envs\rastermap\pythonw.exe', 'ExecutionMode', 'OutOfProcess')
174+
```
175+
Then you should be able to see your environment details in MATLAB after typing "pyenv" (the intepreter will not actually load until you try to run a Python statement). An example function to call Rastermap and return the sort order back as a MATLAB array is:
176+
```
177+
function [sortIdx] = rastermapSort(shankDataToSort)
178+
% wrapper to convert to numpy array, call Rastermap, and then convert back to MATLAB array
179+
180+
data = shankDataToSort.zScoredFiringRates;
181+
dataNdArray = py.numpy.array(data);
182+
pyrun("from rastermap import Rastermap") %load interpreter, import main function
183+
rmModel = pyrun("model = Rastermap(locality=0.5, time_lag_window=50).fit(spks)", "model", spks=dataNdArray);
184+
sortIdx = int16(py.memoryview(rmModel.isort.data)) + 1; %back to MATLAB array, 1-indexing
185+
186+
end
187+
```
188+
169189
# Inputs
170190

171191
Most of the time you will input to `Rastermap().fit` a matrix of neurons by time. For more details, these are all the inputs to the function:

0 commit comments

Comments
 (0)