Help for changing the colormap of .tsf file in command line

Hello Mrtrix Expert

I have a minor issue. I wish to add an option in mrview in order change the colormap of my streamline in command line. I’m looking for a bit of help.

Looking at this post : Adding command line option to mrview to set tractography colors , It was straightfoward to modify mrview to add the option:

 -tractography.tsf_colourmap index  (multiple uses permitted)
     Sets the colourmap of the .tsf file as indexed in the tsfcolourmap dropdown
     menu. Requires -tractography.tsf_load already

using this:

+ Option ("tractography.tsf_colourmap", "Sets the colourmap of the .tsf file as indexed in the tsf colourmap dropdown menu. Requires -tractography.tsf_load already ").allow_multiple()
            +   Argument ("index").type_integer();

Now I need to call a Class or a Function that actually do the change.

 if (opt.opt->is ("tractography.tsf_colourmap"))
          {
            try {
              int n = opt[0];
              if (n < 0 || !ColourMap::maps[n].name)
                throw Exception ("invalid overlay colourmap index \"" + std::string (opt[0]) + "\" for -tractography.tsf_colourmap option");
             
		// help needed here !
		// scalar_file_options->set_track_colormap(n) ?
            }
	    catch (Exception& e) { e.display(); }

          }

My knowledge of the MRtrix code is close to zero (it is actually zero). In case, it is simple, could you indicate me the command or the class to look for changing the colourmap of the .tsf file. I have been able to it in hard coding the file Tractogram.cpp , line 379

{
          set_allowed_features (true, true, true);
          colourmap = 3;  //// <- here
          connect (&window(), SIGNAL (fieldOfViewChanged()), this, SLOT (on_FOV_changed()));
          on_FOV_changed ();
        }

But a cleaner way would be welcomed. And it will prevent me from editing the file and recompiling of the software.

Thanks in advance,
Valéry

Hi Valéry,

Looks like you’ve done the bulk of the work! Any chance you can commit your changes to a GitHub repo and create a (draft) pull request?

In the meantime, I think doing this properly would probably require a minor refactor of the code. But what you’re looking for is basically the same as for the -tractography.tsf_load option. It’ll involve something like:

          if (opt.opt->is ("tractography.tsf_colourmap"))
          {
            try {
              // check whether a tractogram is loaded:
              if (process_commandline_option_tsf_check_tracto_loaded()) {
                // get list of selected tractograms:
                QModelIndexList indices = tractogram_list_view->selectionModel()->selectedIndexes();
                if (indices.size() != 1)
                  throw Exception ("-tractography.tsf_colourmap option requires one tractogram to be selected");
                // get pointer to tractogram:
                Tractogram* tractogram = tractogram_list_model->get_tractogram (indices[0]);
                // check tractogram has a scalar file attached and prepare the scalar_file_options object:
                if (tractogram->get_color_type() == TrackColourType::ScalarFile){
                  scalar_file_options->set_tractogram (tractogram);
                  // invoke member function (which needs to be added - see below):
                  track_scalar_file_options->set_colourmap (opt[0]);
               }
            }
            catch (Exception& e) { e.display(); }
            return true;
          }

For this to work, you’ll need to add a method to the TrackScalarFileOptions class to set the colourmap – currently there is no public method to do this. You could add a public method, called void set_colourmap (int colourmap_index);, which does something like:

        void TrackScalarFileOptions::set_colourmap (int colourmap_index)
        {
          if (tractogram) {
            tractogram->colourmap = colourmap_index;
            update_UI();
            window().updateGL();
          }
        }

Note none of this is tested. Hopefully it’ll get you close enough though. I recommend you create a pull request so we can comment and help out more effectively.

Thanks for the solution, it looks like it is perfectly working. I will try to create a pull request.