innolitics / freesurfer

The official FreeSurfer repository maintained by LCN
Other
1 stars 2 forks source link

Port segmentation #24

Open yujanshrestha opened 6 years ago

yujanshrestha commented 6 years ago

Port segmentation script line by line and dump out fixtures along the way for testing.

yujanshrestha commented 6 years ago

We are splitting this into 3 parts, each can be worked on independently provided you take a snapshot of the variables from MATLAB and load them into Python like this:

in MATLAB

% code before the split
save('part2.mat`)  % this saves all workspace variables
% code after these split

in Python

fixture = scipy.io.loadmat(os.path.join(MATLAB_FIXTURE_PATH, 'part2.mat'), struct_as_record=False, squeeze_me=True)
locals().update(fixture) # this dumps all variables from matlab into Python locals.
yujanshrestha commented 6 years ago

As a result please do not rename any variables so that we can just stitch together the three parts at the end and variable names will match up.

yujanshrestha commented 6 years ago

should you run into situations where you simply do not have data in the fixture because it is inside C++ land, for example:

totalTransformationMatrix = downSamplingTransformMatrix * double( kvlGetTransformMatrix( transform ) );

There is no way transform has anything useful because it is just a reference.

or if something just has not been implemented yet

initialNodeDeformationInTemplateSpace = kvlWarpMesh( ...

A solution is to just drop a TODO and skip the problematic section and write out the relevant variables as .mat fixtures:

in MATLAB

  totalTransformationMatrix = downSamplingTransformMatrix * double( kvlGetTransformMatrix( transform ) );
  save('/Users/ys/work/freesurfer/GEMS2/Testing/matlab_data/totalTransformationMatrix.mat', 'totalTransformationMatrix')

and read them back in Python

    # totalTransformationMatrix = downSamplingTransformMatrix @ kvlGetTransformMatrix( transform )
    # TODO: remove this guy once we port part 1
    totalTransformationMatrix = load_mat_file('/Users/ys/work/freesurfer/GEMS2/Testing/matlab_data/totalTransformationMatrix.mat')['totalTransformationMatrix']
yujanshrestha commented 6 years ago

so regarding the where vs find issue. I decided to not try to flatten numpy arrays to match the exact indexing scheme used in MATLAB. This means the index arrays will be slightly different shape but still used in the same way

in MATLAB

downSampledMaskIndices = find( downSampledMask ); 

will yield a Nx1 vector because MATLAB allows you to index into a multidimensional array with just one scalar and uses Fortran style indexing to calculate the actual indices.

in Python

downSampledMaskIndices = np.where( downSampledMask )

will yield a tuple containing three vectors of size N because Numpy does not allow single dimension index.

However, the usage in code will still be very similar in MATLAB:

priors( : ) = double( tmp( downSampledMaskIndices, : ) ) / 65535;

in Python:

priors = tmp[downSampledMaskIndices] / 65535
yujanshrestha commented 6 years ago

@orwonthe Just be aware of the design choice above so we are consistent and the three parts can be merged together with all variable names and shapes matching up.

yujanshrestha commented 6 years ago

Also note that find vs where does not return indices in the same order