Skip to content

[ENH] update from bids-matlab #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:

- name: Run tests
run: |
octave $OCTFLAGS --eval "runTests"
octave $OCTFLAGS --eval "run_tests"
cat test_report.log | grep 0
bash <(curl -s https://codecov.io/bash)

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ envs/*
# visual studio code stuff
.vscode


demos/*/*.json
demos/*/*/*.json
demos/*/derivatives



2 changes: 2 additions & 0 deletions demos/atlas/create_roi_from_atlas.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
% (C) Copyright 2021 CPP ROI developers

opt.roi.atlas = 'wang';
opt.roi.name = {'V1v', 'V1d'};
opt.roi.dir = fullfile(pwd, 'derivatives', 'cpp_roi', 'group');
Expand Down
10 changes: 6 additions & 4 deletions demos/roi/other_demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

% change the label entity and remove the hs one
leftRoiImage = renameFile(leftRoiImage, ...
struct('label', 'ns left motion', ...
'hs', ''));
struct('entities', struct( ...
'label', 'ns left motion', ...
'hs', '')));
rightRoiImage = renameFile(rightRoiImage, ...
struct('label', 'ns right motion', ...
'hs', ''));
struct('entities', struct( ...
'label', 'ns right motion', ...
'hs', '')));
4 changes: 4 additions & 0 deletions miss_hit.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# styly guide (https://florianschanda.github.io/miss_hit/style_checker.html)
line_length: 100

regex_function_name: "[a-z]+(_*([a-zA-Z0-9]){1}[A-Za-z]+)*" # almost anything goes in the root folder
regex_script_name: "[a-z0-9]+(_[a-z0-9]+)*"

exclude_dir: "lib"

copyright_entity: "CPP ROI developers"

tab_width: 2
Expand Down
File renamed without changes.
9 changes: 7 additions & 2 deletions src/atlas/extractRoiByLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

p = bids.internal.parse_filename(sourceImage);
p.label = labelStruct.ROI;
p.type = 'mask';
newName = createFilename(p);
p.suffix = 'mask';
p.use_schema = false;

newName = bids.create_filename(p);
hdr.fname = spm_file(hdr.fname, 'filename', newName);

% Cluster labels as their size.
spm_write_vol(hdr, outputVol);
outputImage = hdr.fname;

json = bids.derivatives_json(outputImage);
bids.util.jsonencode(json.filename, json.content);

end
24 changes: 15 additions & 9 deletions src/atlas/extractRoiFromAtlas.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% (C) Copyright 2021 CPP ROI developers

function roiImage = extractRoiFromAtlas(roiDir, atlas, roiName, hemisphere)
%
% (C) Copyright 2021 CPP ROI developers

if strcmp(atlas, 'wang')

Expand All @@ -22,16 +22,22 @@

roiImage = extractRoiByLabel(sourceImage, labelStruct);

nameStructure = struct( ...
'space', 'MNI', ...
'hemi', hemisphere, ...
'desc', atlas, ...
'label', roiName, ...
'type', 'mask', ...
entities = struct('space', 'MNI', ...
'hemi', hemisphere, ...
'desc', atlas, ...
'label', roiName);
nameStructure = struct('entities', entities, ...
'suffix', 'mask', ...
'ext', '.nii');
newName = createFilename(nameStructure);

nameStructure.use_schema = false;

newName = bids.create_filename(nameStructure);

movefile(roiImage, fullfile(roiDir, newName));

roiImage = fullfile(roiDir, newName);

json = bids.derivatives_json(roiImage);
bids.util.jsonencode(json.filename, json.content);
end
3 changes: 1 addition & 2 deletions src/atlas/getRetinoProbaAtlas.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
% (C) Copyright 2021 CPP ROI developers

function [maxProbaFiles, roiLabels] = getRetinoProbaAtlas()
%
% Loads the volumetric data from the
Expand All @@ -15,6 +13,7 @@
% PMID: 25452571
% Probabilistic Maps of Visual Topography in Human Cortex
%
% (C) Copyright 2021 CPP ROI developers

unzipAtlas('wang');

Expand Down
4 changes: 2 additions & 2 deletions src/atlas/getRoiLabelLookUpTable.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% (C) Copyright 2021 CPP ROI developers

function roiLabelLUT = getRoiLabelLookUpTable(atlas)
%
% (C) Copyright 2021 CPP ROI developers

if exist(atlas, 'file')
roiLabelLUT = spm_load(atlas);
Expand Down
15 changes: 10 additions & 5 deletions src/atlas/labelClusters.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
% (C) Copyright 2021 CPP ROI developers

function outputImage = labelClusters(sourceImage, peakThreshold, extendThreshold)

%
% Adapted from:
% https://en.wikibooks.org/wiki/SPM/How-to#How_to_remove_clusters_under_a_certain_size_in_a_binary_mask?
%
% (C) Copyright 2021 CPP ROI developers

hdr = spm_vol(sourceImage);

Expand All @@ -18,14 +18,19 @@

% Write new image with cluster laebelled with their voxel size
p = bids.internal.parse_filename(sourceImage);
p.type = 'dseg'; % discrete segmentation
newName = createFilename(p);
p.suffix = 'dseg'; % discrete segmentation
p.use_schema = false;

newName = bids.create_filename(p);
hdr.fname = spm_file(hdr.fname, 'filename', newName);

% Cluster labels as their size.
spm_write_vol(hdr, vol);
outputImage = hdr.fname;

json = bids.derivatives_json(outputImage);
bids.util.jsonencode(json.filename, json.content);

end

function [l2, num] = getClusters(hdr, peakThreshold)
Expand Down
4 changes: 2 additions & 2 deletions src/atlas/returnAtlasDir.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% (C) Copyright 2021 CPP ROI developers

function atlasDir = returnAtlasDir(atlas)
%
% (C) Copyright 2021 CPP ROI developers

atlasDir = fullfile(fileparts(mfilename('fullpath')), '..', '..', 'atlas');

Expand Down
4 changes: 2 additions & 2 deletions src/atlas/unzipAtlas.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% (C) Copyright 2021 CPP ROI developers

function unzipAtlas(atlas)
%
% (C) Copyright 2021 CPP ROI developers

atlasDir = returnAtlasDir();

Expand Down
18 changes: 11 additions & 7 deletions src/roi/createRoi.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
% (C) Copyright 2021 CPP ROI developers

function [mask, outputFile] = createRoi(type, specification, volumeDefiningImage, outputDir, saveImg)
%
% Returns a mask to be used as a ROI by ``spm_summarize``.
Expand Down Expand Up @@ -66,6 +64,7 @@
% mask.global.XYZmm
%
%
% (C) Copyright 2021 CPP ROI developers

if nargin < 5
saveImg = false;
Expand Down Expand Up @@ -319,6 +318,9 @@
% delete label files
delete(fullfile(outputDir, '*_mask_labels.mat'));

json = bids.derivatives_json(outputFile);
bids.util.jsonencode(json.filename, json.content);

end

function roiName = createRoiName(mask, volumeDefiningImage)
Expand All @@ -327,14 +329,15 @@

p.filename = '';
p.ext = '.nii';
p.type = 'mask';
p.suffix = 'mask';
p.use_schema = false;

if ~isempty(volumeDefiningImage)
tmp = bids.internal.parse_filename(volumeDefiningImage);

% if the volume defining image has a space entity we reuse it
if isfield(p, 'space')
p.space = tmp.space;
p.entities.space = tmp.space;
end

end
Expand All @@ -347,11 +350,12 @@

label = '';
if isfield(p, 'label')
label = p.label;
label = p.entities.label;
end

p.label = [label ' ' mask.label];
p.entities.label = [label ' ' mask.label];
p.use_schema = false;

roiName = createFilename(p);
roiName = bids.create_filename(p);

end
9 changes: 5 additions & 4 deletions src/roi/keepHemisphere.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
% (C) Copyright 2021 CPP ROI developers

function outputImage = keepHemisphere(inputImage, hemisphere)
%
% Only keep the values from one hemisphere. Sets the other half to NaN.
Expand All @@ -14,6 +12,8 @@
% :param hemisphere: ``'lh'`` or ``'rh'``
% :type hemisphere: string
%
%
% (C) Copyright 2021 CPP ROI developers

hdr = spm_vol(inputImage);
vol = spm_read_vols(hdr);
Expand All @@ -33,8 +33,9 @@
vol(discard, :, :) = NaN;

p = bids.internal.parse_filename(inputImage);
p.hs = lower(hemisphere);
newName = createFilename(p);
p.entities.hs = lower(hemisphere);
p.use_schema = false;
newName = bids.create_filename(p);

hdr.fname = spm_file(inputImage, 'filename', newName);

Expand Down
14 changes: 8 additions & 6 deletions src/roi/renameNeuroSynth.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
% (C) Copyright 2021 CPP ROI developers

function outputImage = renameNeuroSynth(inputImage)
% give the neurosynth map a name that is more bids friendly
%
% space-MNI_label-neurosynthKeyWordsUsed_probseg.nii
%
%
% (C) Copyright 2021 CPP ROI developers

p.filename = spm_file(inputImage, 'filename');
p.type = 'probseg';
p.suffix = 'probseg';
p.ext = '.nii';
p.space = 'MNI';
p.entities.space = 'MNI';

basename = spm_file(inputImage, 'basename');
parts = strsplit(basename, '_');
p.label = ['neurosynth ' parts{1}];
p.entities.label = ['neurosynth ' parts{1}];

p.use_schema = false;

newName = createFilename(p);
newName = bids.create_filename(p);

outputImage = spm_file(inputImage, 'filename', newName);

Expand Down
19 changes: 12 additions & 7 deletions src/roi/thresholdToMask.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
% (C) Copyright 2021 CPP ROI developers

function outputImage = thresholdToMask(inputImage, threshold)

%
% TODO
% allow threshold to be inferior than, greater than or both
%
%
%
% (C) Copyright 2021 CPP ROI developers

hdr = spm_vol(inputImage);
img = spm_read_vols(hdr);

p = bids.internal.parse_filename(inputImage);
p.type = 'mask';
newName = createFilename(p);
hdr.fname = spm_file(hdr.fname, 'filename', newName);
p.suffix = 'mask';
p.use_schema = false;
newName = bids.create_filename(p);

hdr.fname = spm_file(hdr.fname, 'filename', newName);
img = img > threshold;

spm_write_vol(hdr, img);

outputImage = hdr.fname;

json = bids.derivatives_json(outputImage);
bids.util.jsonencode(json.filename, json.content);

end
30 changes: 0 additions & 30 deletions src/utils/convertToValidCamelCase.m

This file was deleted.

27 changes: 0 additions & 27 deletions src/utils/createFilename.m

This file was deleted.

Loading