Skip to content

Commit

Permalink
Merge pull request #78 from MATLAB-Community-Toolboxes-at-INCF/wds_20…
Browse files Browse the repository at this point in the history
…24_wrap

readability improvements and bug fixes
  • Loading branch information
stevevanhooser authored Dec 21, 2024
2 parents b32cbf2 + 4be336b commit 667f836
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 27 deletions.
55 changes: 55 additions & 0 deletions +deepinterp/+internal/+test/neuropixelsPlay.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

% this script was made to explore the input/output requirements of the
% pretrained neuropixels network

E = [[1:192]' [1:192]'+0.5]; % number electrodes by Y (whole number) and X 2*(mod 1)

NP = repmat(E,1,1,500); % 500 frames of data
% now transpose to be the same as the expected neuropixels data
NP = permute(NP, [3 1 2]);

nb_probes = 384; % Number of probes

pre_post_omission = 3; % Number of frames omitted before and after the inferred frame

pre_frame = 30; % Number of frames provided before the inferred frame
post_frame = 30; % Number of frames provided after the inferred frame

batch_size = 100;

start_frame = 100;

end_frame = 200;


input_full = single(zeros(batch_size, nb_probes, 2, pre_frame + post_frame));


for frame_index = start_frame:end_frame

input_index = (frame_index - pre_frame - pre_post_omission) : (frame_index + post_frame + pre_post_omission );

input_index = input_index(input_index ~= frame_index); % drop the predicted frame

for index_padding = 1: pre_post_omission % drop pre_post_omission number of frames surrounding the predicted frame
input_index = input_index(input_index ~= frame_index - index_padding);
input_index = input_index(input_index ~= frame_index + index_padding);
end

data_img_input = ephys(input_index, :, :);

data_img_input = permute(data_img_input,[2,3,1]);

data_img_input = (single(data_img_input) - local_mean) / local_std;

% alternating filling with zeros padding
odd = 1: 2: nb_probes;
even = odd + 1;

input_full(frame_index-start_frame+1, odd, 1, :) = data_img_input(:, 1, :);
input_full(frame_index-start_frame+1, even, 2, :) = data_img_input(:, 2, :);


end


58 changes: 58 additions & 0 deletions +deepinterp/NPcheckerboard2frame.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function frame = checkerboard2NPframe(cb)
% CHECKERBOARD2NPFRAME - convert geometrically realistic matrix to raw Neuropixels Phase 3 format
%
% FRAME = CHECKERBOARD2NPFRAME(CB)
%
% Convert geometrically-realistic matrix data CB (384 x 2 x NSAMPLES)
% into a format that matches the raw data output of Neuropixels Phase 3
% probes. CB is expected to reflect the checkerboard layout of electrodes,
% where every even linear index is 0.
%
% FRAME will be a 192 x 2 x NSAMPLES matrix.
%
% For example, the following single-frame example input where each data point has
% been set, for illustration purposes, to the electrode number (incremented by 0.25)
% would be transformed as follows:
%
% 1.0000 0
% 0 1.2500
% 2.0000 0
% 0 2.2500
% 3.0000 0
% 0 3.2500
% ...
% 190.0000 0
% 0 190.2500
% 191.0000 0
% 0 191.2500
% 192.0000 0
% 0 192.2500
%
% to
%
% 1.0000 1.2500
% 2.0000 2.2500
% 3.0000 3.2500
% ...
% 189.0000 189.2500
% 190.0000 190.2500
% 191.0000 191.2500
% 192.0000 192.2500

% Check input dimensions
if size(cb, 1) ~= 384 || size(cb, 2) ~= 2
error('Input matrix CB must be 384 x 2 x NSAMPLES.');
end

frame = zeros(size(cb,1)/2, size(cb,2), size(cb,3));
odd = 1:2:size(cb,1);
even = odd + 1;
frame(:,1,:) = cb(odd,1,:);
frame(:,2,:) = cb(even,2,:);







48 changes: 48 additions & 0 deletions +deepinterp/NPframe2checkerboard.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function cb = NPframe2checkerboard(frame)
% NPFRAME2CHECKERBOARD - convert raw Neuropixels Phase 3 to geometrically realistic matrix
%
% CB = NPframe2checkerboard(FRAME)
%
% Convert Neuropixels data frame data FRAME (192 x 2 x NSAMPLES)
% into a geometrically-realistic matrix that reflects the checkerboad
% layout of electrodes such as the Phase 3 version.
%
% CB will be a 384 x 2 x NSAMPLES matrix where every even linear index
% will be 0.
%
% For example, the following single-frame example input where each data point has
% been set, for illustration purposes, to the electrode number (incremented by 0.5)
% would be transformed as follows:
%
% 1.0000 1.5000
% 2.0000 2.5000
% 3.0000 3.5000
% ...
% 189.0000 189.5000
% 190.0000 190.5000
% 191.0000 191.5000
%
% to
%
% 1.0000 0
% 0 1.5000
% 2.0000 0
% 0 2.5000
% 3.0000 0
% 0 3.5000
%...
% 190.0000 0
% 0 190.5000
% 191.0000 0
% 0 191.5000
% 192.0000 0
% 0 192.5000

cb = zeros(size(frame,1)*2,size(frame,2),size(frame,3));

odd = 1:2:size(frame,1)*2;
even = odd + 1;

cb(odd,1,:) = frame(:,1,:);
cb(even,2,:) = frame(:,2,:);

27 changes: 19 additions & 8 deletions +deepinterp/Net.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
options.Npost (1,1) double {mustBeInteger} = 30
options.Nomit (1,1) double {mustBeInteger} = 1
options.modelParameters (1,1) struct = struct('none',[])
options.varname (1,:) char = "net"
end

networkType = options.type;
Expand All @@ -77,13 +78,17 @@
obj.network=deepinterp.internal.importKerasMAE(options.file);
case 'tensorflowzip',
obj.network=deepinterp.internal.openTensorFlowNetwork(options.file,matlab.lang.makeValidName(options.model));
case 'matlab',
matdata = load(options.file);
obj.network = getfield(matdata,options.varname);
case 'pretrained',
[modelfilename, modelparams] = deepinterp.internal.getPretrainedModelFilename(options.model);
obj = deepinterp.Net(modelparams.format,'file',modelfilename,...
'N',modelparams.dim(1),'M',modelparams.dim(2),...
'Npre',modelparams.pre,'Npost',modelparams.post,'Nomit',modelparams.omit,...
'type',modelparams.type,...
'modelParameters',modelparams.parameters);
'modelParameters',modelparams.parameters,'model',options.model,...
"varname",options.varname);
return;
otherwise,
error(['Unknown command: ' command]);
Expand Down Expand Up @@ -131,22 +136,26 @@
options.progbar (1,1) logical = true;
end

if obj.networkType=="fMRI",
error(['fMRI not yet supported by deepinterp.Net class.']);
end;

output = input;
Nomit_pre = (obj.Nomit+1)/2;
Nomit_post = (obj.Nomit+1)/2;
offsets = [ fliplr(-[Nomit_pre:obj.Npre+Nomit_pre-1]) Nomit_post:obj.Npost+Nomit_post-1];
if options.progbar,
deepinterp.internal.progressbar();
end;
totalWork = size(input,3)-(obj.Npost+obj.Npre);
for t = obj.Npre+1 : size(input,3) - obj.Npost,
totalWork = size(input,3)-(obj.Npost+obj.Npre+Nomit_pre-1+Nomit_post-1);
for t = obj.Npre+1+Nomit_pre-1 : size(input,3) - obj.Npost-Nomit_post+1,
if options.progbar,
deepinterp.internal.progressbar((t-(obj.Npre+1))/totalWork);
end;
output(:,:,t) = predict(obj.network,input(:,:,offsets+t));
end;
if options.progbar,
deepinterp.internal.progressbar(0.9999999999);
deepinterp.internal.progressbar(1);
end;
end; % interp()

Expand All @@ -168,10 +177,12 @@
inSz = obj.network.Layers(1).InputSize;
obj.N = inSz(1);
obj.M = inSz(2);
assert(inSz(3)==obj.Npre+obj.Npost,...
['Npre + Npost must add up to the total ' ...
'number of image inputs to ' ...
'deepinterp.net.ImageTimeSeries.']);
if numel(inSz)<4,
assert(inSz(end)==obj.Npre+obj.Npost,...
['Npre + Npost must add up to the total ' ...
'number of image inputs to ' ...
'deepinterp.net.ImageTimeSeries.']);
end;
end;
end; % SetInputSize
end;
Expand Down
6 changes: 4 additions & 2 deletions +deepinterp/setup.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function setup()
function tbd = setup()
% SETUP - add DeepInterpolation Toolbox folders to the MATLAB path
%
% SETUP()
% TBD = SETUP()
%
% Adds DeepInterpolation Toolobx folders to the MATLAB path.
%
% Returns the toolbox directory TBD.
%

tbd = deepinterp.toolboxpath();

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Get started with inference examples which apply the DeepInterpolation principle

| Data Type | Pretrained<br />Model| Sample<br />Data | View <br />:eyes: | Run <br /> ▶️
| :--- | :---: | :---: | --- | --- |
|🔬"Ophys"<br /> (optical physiology<sup>2</sup>)| [🤖](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained.json)<br /> (AWS, 120 MB) | [💾](sampleData/ophys_tiny_761605196.tif) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_ophys_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ophys_inference.mlx)
|⚡"Ephys" (electrophysiology<sup>1</sup>)| [:robot:](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained.json) | [💾](sampleData/ephys_tiny_continuous.dat2) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_ephys_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ephys_inference.mlx)
|🔬"Ophys"<br /> (optical physiology<sup>2</sup>)| [🤖](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained.json#L3)<br /> (AWS, 120 MB) | [💾](sampleData/ophys_tiny_761605196.tif) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_ophys_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ophys_inference.mlx)
|⚡"Ephys" (electrophysiology<sup>1</sup>)| [:robot:](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained#L54.json) | [💾](sampleData/ephys_tiny_continuous.dat2) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_ephys_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ephys_inference.mlx)


<sub><sup>1</sup> via Neuropixels neural probes <sup>2</sup> via two-photon (2P) calcium imaging<sub>
Expand All @@ -28,9 +28,9 @@ First-time users on MATLAB Online will be prompted to install the [Deep Learning
Expected use cases involve training on a user's representative or actual dataset. The following examples illustrate the transfer learning workflow for (re)training DeepInterpolation pretrained models along with subsequent inference. Notably the DeepInterpolation principle **does not require separate ground truth data**; thus training and inference can be run using the same dataset.

| Data Type | Pretrained Model | Sample Data | View :eyes:| Run ▶️
|---|---|---|---|---|
| 🔬"Ophys" (optical physiology<sup>1</sup>)| [TODO](sample_data/pretrainedNetwork.mat) | [sample data](sampleData/ophys_tiny_761605196.tif) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2Fbfd58de9-1242-48ba-81bc-dfe9c37fae6b%2Ffiles%2Fexamples%2Fcustomdatastore_example.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ophys_training.mlx)
| ⚡ Ephys" (electrophysiology<sup>2</sup>) | ["Ephys-Neuropixels_Phase_3a_1050"](pretrainedModels/pretrained.json) | [sample data](sampleData/ephys_tiny_continuous.dat2) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_ephys_training.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ephys_training.mlx)
|---| :---: |---|---|---|
| 🔬"Ophys" (optical physiology<sup>1</sup>)| [:robot:](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained#L3.json) | [sample data](sampleData/ophys_tiny_761605196.tif) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2Fbfd58de9-1242-48ba-81bc-dfe9c37fae6b%2Ffiles%2Fexamples%2Fcustomdatastore_example.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ophys_training.mlx)
| ⚡ Ephys" (electrophysiology<sup>2</sup>) | [:robot:](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained#L54.json) | [sample data](sampleData/ephys_tiny_continuous.dat2) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_ephys_training.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/tiny_ephys_training.mlx)

<sub><sup>1</sup> via two-photon (2P) calcium imaging <sup>2</sup> via Neuropixels neural probes<sub>

Expand All @@ -40,9 +40,9 @@ Expected use cases involve training on a user's representative or actual dataset
Expected use cases will involve larger data sizes, for which remote data locations and/or big data handling become important considerations. The following examples utilize larger datasets from cloud-hosted public scientific data archives to illustrate these data acccess & handling workflows.

| Data Type | Pretrained Model | Public Data Archive | View :eyes: | Run ▶️
|---|---|---|---|---|
|🔬"Ophys" (optical physiology) | [model](pretrainedModels/pretrained.json) | [Allen Brain Observatory](http://allen-brain-observatory.s3.amazonaws.com/visual-coding-2p/ophys_movies/ophys_experiment_496908818.h5) (55.6 GB) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Fophys_AllenBrainObservatory.mlx&embed=web) | (\*) |
|🧠 fMRI (functional magnetic resonance imaging) | [TODO](pretrainedModels/pretrained.json) (AWS)| [Open Neuro](https://openneuro.org/datasets/ds001246/versions/1.2.1) (18.3 GB)| [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_fMRI_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/other/fMRI_OpenNeuro.mlx)
|---|:---:|---|---|---|
|🔬"Ophys" (optical physiology) | [:robot:](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained#L92.json) | [Allen Brain Observatory](http://allen-brain-observatory.s3.amazonaws.com/visual-coding-2p/ophys_movies/ophys_experiment_496908818.h5) (55.6 GB) | [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Fophys_AllenBrainObservatory.mlx&embed=web) | (\*) |
|🧠 fMRI (functional magnetic resonance imaging) | [:robot:](https://github.com/MATLAB-Community-Toolboxes-at-INCF/DeepInterpolation-MATLAB/blob/main/pretrainedModels/pretrained#L74.json) | [Open Neuro](https://openneuro.org/datasets/ds001246/versions/1.2.1) (18.3 GB)| [![View Deep-Interpolation-MATLAB on File Exchange](https://www.mathworks.com/matlabcentral/images/matlab-file-exchange.svg)](https://viewer.mathworks.com/?viewer=live_code&url=https%3A%2F%2Fwww.mathworks.com%2Fmatlabcentral%2Fmlc-downloads%2Fdownloads%2F84c22101-bffc-435a-910c-b0c7dcd5b386%2F29e7e92d-4639-4178-8e19-739580981e60%2Ffiles%2Fexamples%2Ftiny_fMRI_inference.mlx&embed=web) | [![Open in MATLAB Online](https://www.mathworks.com/images/responsive/global/open-in-matlab-online.svg)](https://matlab.mathworks.com/open/github/v1?repo=INCF/DeepInterpolation-MATLAB&file=examples/other/fMRI_OpenNeuro.mlx)

<sub>(\*) This data-intensive example is recommended for use on a local machine, not for MATLAB online.</sub>

Expand Down
Binary file modified examples/ophys_AllenBrainObservatory.mlx
Binary file not shown.
Binary file modified examples/other/fMRI_OpenNeuro.mlx
Binary file not shown.
Binary file modified examples/other/tiny_ophys_inference_detailed.mlx
Binary file not shown.
Binary file modified examples/tiny_ephys_inference.mlx
Binary file not shown.
Binary file modified examples/tiny_ephys_training.mlx
Binary file not shown.
Binary file modified examples/tiny_ophys_inference.mlx
Binary file not shown.
Binary file modified examples/tiny_ophys_training.mlx
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 667f836

Please sign in to comment.