User Tools

Site Tools


analysis:course-w16:week3long

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
analysis:course-w16:week3long [2016/01/17 16:55]
mvdm [MultiRaster]
analysis:course-w16:week3long [2018/07/07 10:19] (current)
Line 1: Line 1:
 ~~DISCUSSION~~ ~~DISCUSSION~~
  
-:!: UNDER CONSTRUCTION -- DO NOT USE YET! +===== Module 3: Visualizing neural data in MATLAB =====
- +
-===== Module 3: Visualizing neural data in MATLAB ​(long version) ​=====+
  
 Goals: Goals:
Line 9: Line 7:
   * Master the basics of plotting data in MATLAB using the versatile ''​plot()''​ function   * Master the basics of plotting data in MATLAB using the versatile ''​plot()''​ function
   * Understand how to use //handles// to fine-tune formatting   * Understand how to use //handles// to fine-tune formatting
-  * Meet some existing visualization tools: ''​MultiRaster()'',​ ''​PlotTSDfromIV()''​ and ''​ft_dataviewer()''​ +  * Meet some existing visualization tools: ''​MultiRaster()'',​ ''​PlotTSDfromIV()''​ and ''​ft_databrowser()''​ 
-  * Use interactive figure properties for dynamic data viewing+  * Use interactive figure properties ​with callback functions ​for dynamic data viewing
   * Learn to use functions with variable numbers of input arguments (using ''​varargin''​ and ''​cfg''​ inputs) effectively   * Learn to use functions with variable numbers of input arguments (using ''​varargin''​ and ''​cfg''​ inputs) effectively
   * Understand which file format to export figures to   * Understand which file format to export figures to
Line 17: Line 15:
 Resources: Resources:
  
-  * (reference) MATLAB documentation for ''​plot'',​ [[ http://​www.mathworks.com/​help/​matlab/​ref/​figure_props.html | Figure properties ]], [[ http://​www.mathworks.com/​help/​matlab/​ref/​axes_props.html | Axis properties ]] and [[ http://​www.mathworks.com/​help/​matlab/​ref/​line_props.html | Line Properties ]]+  * (reference) MATLAB documentation for ''​plot'',​ [[ http://​www.mathworks.com/​help/​matlab/​ref/​figure_props.html | Figure properties]],​ [[ http://​www.mathworks.com/​help/​matlab/​ref/​axes_props.html | Axis properties]] and [[ http://​www.mathworks.com/​help/​matlab/​ref/​line_props.html | Line Properties]]
   * (reference) [[http://​www.mathworks.com/​help/​matlab/​creating_plots/​figures-plots-and-graphs.html | MATLAB plotting gallery ]]   * (reference) [[http://​www.mathworks.com/​help/​matlab/​creating_plots/​figures-plots-and-graphs.html | MATLAB plotting gallery ]]
   * (reference) [[http://​www.vandermeerlab.org/​BIOL377_assignment3_2012.pdf | Place cell assignment]] from %%MvdM%%'​s BIOL377 course at Waterloo from a few years ago (if you want a gentle intro to what is going on in the data you are looking at; can be done in 30 min)   * (reference) [[http://​www.vandermeerlab.org/​BIOL377_assignment3_2012.pdf | Place cell assignment]] from %%MvdM%%'​s BIOL377 course at Waterloo from a few years ago (if you want a gentle intro to what is going on in the data you are looking at; can be done in 30 min)
Line 152: Line 150:
 </​code>​ </​code>​
  
-This way, you have precise control over the formatting of command line output -- the cryptic ''​%5.3f''​ tag specifies that the contents of ''​this_spk''​ should be formatted as a floating-point number with digits before, and 3 digits following, the decimal point; ''​\n''​ specifies a newline. (For reference: ''​fprintf()''​ has [[http://​www.mathworks.com/​help/​matlab/​ref/​fprintf.html#​input_argument_formatspec | many other formatting options]]).+This way, you have precise control over the formatting of command line output -- the cryptic ''​%4.3f''​ tag specifies that the contents of ''​this_spk''​ should be formatted as a floating-point number with digits before, and 3 digits following, the decimal point; ''​\n''​ specifies a newline. (For reference: ''​fprintf()''​ has [[http://​www.mathworks.com/​help/​matlab/​ref/​fprintf.html#​input_argument_formatspec | many other formatting options]]).
  
 Now that we know what we are dealing with, let's meet the plot() command: Now that we know what we are dealing with, let's meet the plot() command:
Line 309: Line 307:
 The topics that follow in this section are optional, in the sense that later modules do not assume you know how to do these things. Feel free to use this section as you see fit, but do make sure you go through the next section ("​Using existing visualization tools"​)! The topics that follow in this section are optional, in the sense that later modules do not assume you know how to do these things. Feel free to use this section as you see fit, but do make sure you go through the next section ("​Using existing visualization tools"​)!
  
-==== Interactive figures ====+==== Interactive figures ​and callback functions ​====
  
 Figure windows in MATLAB have many properties (here is the complete [[http://​www.mathworks.com/​help/​matlab/​ref/​figure-properties.html | list]]). A particularly useful one is the ''​KeyPressFcn''​ property, which specifies a function to be called when a key is pressed while the figure is active. Figure windows in MATLAB have many properties (here is the complete [[http://​www.mathworks.com/​help/​matlab/​ref/​figure-properties.html | list]]). A particularly useful one is the ''​KeyPressFcn''​ property, which specifies a function to be called when a key is pressed while the figure is active.
Line 465: Line 463:
 </​code>​ </​code>​
  
-If you can't get this to work, the resulting file is here; Windows Media Player synthesizes it with a plain piano instrument, which is effective at communicating the sudden, violently synchronous activity during SWRs!+If you can't get this to work, the resulting file can be made to sound like this {{:​analysis:​course-w16:​r042.mp3|here}} (with Ableton Live); Windows Media Player synthesizes it with a plain piano instrument, which sounds a bit different but is also effective at communicating the sudden, violently synchronous activity during SWRs!
 ===== Using existing visualization tools ===== ===== Using existing visualization tools =====
  
Line 477: Line 475:
 please = []; please.fc = {'​R042-2013-08-18-CSC03a.ncs'​};​ please = []; please.fc = {'​R042-2013-08-18-CSC03a.ncs'​};​
 csc = LoadCSC(please);​ csc = LoadCSC(please);​
-LoadMetadata;​+ 
 +LoadMetadata; ​% load some experiment metadata such as trial start and end times
  
 % %
 cfg_plot = []; cfg_plot = [];
-cfg_plot.lfp(1) = csc; +cfg_plot.lfp = csc; 
- +cfg_plot.spkColor = '​jet';​ 
-cfg_plot.evt(1) = metadata.taskvars.trial_iv_L;​+cfg_plot.evt = metadata.taskvars.trial_iv_L; ​% "​left"​ trials on the T-maze
  
 h = MultiRaster(cfg_plot,​S);​ h = MultiRaster(cfg_plot,​S);​
 </​code>​ </​code>​
 +
 +You should get:
 +
 +{{ :​analysis:​course-w16:​plot_tutorial6.png?​nolink&​600 |}}
  
 Now, press the '​h'​ key to bring up a window explaining the various keyboard shortcuts available. These work through the ''​KeyPressFcn''​ method introduced above; you can examine MultiRaster'​s ''​KeyPressFcn''​ by typing ''​edit navigate''​ (it's called ''​navigate.m''​). Now, press the '​h'​ key to bring up a window explaining the various keyboard shortcuts available. These work through the ''​KeyPressFcn''​ method introduced above; you can examine MultiRaster'​s ''​KeyPressFcn''​ by typing ''​edit navigate''​ (it's called ''​navigate.m''​).
  
 MultiRaster also uses a trick to speed up the plotting of spikes; it's many times faster than the method we used in this module so far, but it is more difficult to read the code -- take a look if you are interested in finding out how it works! MultiRaster also uses a trick to speed up the plotting of spikes; it's many times faster than the method we used in this module so far, but it is more difficult to read the code -- take a look if you are interested in finding out how it works!
 +
 +Notice the output argument ''​h'',​ this is actually a struct with handles to the various plot objects. MultiRaster'​s help explains what they are.
  
 Plotting time series data, such as the LFP used here, results in large numbers of data points which slow down MATLAB. A good way of further speeding up plotting is to decimate (i.e. downsample) time series data. So here, instead of plotting the full 2kHz signal, plotting it at 500Hz would be just as good and result in significant speedup, especially when scrolling/​zooming a lot. Plotting time series data, such as the LFP used here, results in large numbers of data points which slow down MATLAB. A good way of further speeding up plotting is to decimate (i.e. downsample) time series data. So here, instead of plotting the full 2kHz signal, plotting it at 500Hz would be just as good and result in significant speedup, especially when scrolling/​zooming a lot.
 +
 +=== Diversion: handling variable numbers of function inputs ===
 +
 +A standard function definition specifies the exact number of input and output arguments the function expects: ''​function y = sqrt(x)''​ has one input and one output argument. However, it is often useful to have a variable number of input arguments, so that you can override defaults or specify additional options as needed without complicating simple function calls. This section shows you two ways of doing that.
 +
 +== Method 1: cfg input ==
 +
 +As you can see in the above example, and in MultiRaster'​s help, using a ''​cfg''​ struct as an input is one way to handle variable numbers of inputs to a function. Inside the function, you can do things like, "if cfg has a field called lfp, then plot it". It is also a good way to deal with defaults which may be optionally overwritten. For instance, MultiRaster by default plots all spikes in black, but this can be overridden, as we did in the above example. Here is how that works (taken from inside MultiRaster,​ do not run this):
 +
 +<code matlab>
 +cfg_def.spkColor = '​k';​
 +
 +cfg = ProcessConfig(cfg_def,​cfg_in);​
 +</​code>​
 +
 +What happens is that inside MultiRaster,​ a default config struct (''​cfg_def''​) is defined. The function ''​ProcessConfig()''​ compares the default to the incoming config (''​cfg_in''​);​ if anything in ''​cfg_in''​ is different, then the default is overridden. That way, the function can be called simply without specifying each option, because it will just use the defaults. But, if you want, you can specify something else in the input config.
 +
 +== Method 2: varargin ==
 +
 +An alternative approach is to use the special input argument ''​varargin''​. Here is the idea:
 +
 +<code matlab>
 +function test_fun(a,​varargin)
 +
 +b = 2; % set defaults for b and c
 +c = 3;
 +
 +extract_varargin;​ % override b and c if specified
 +
 +fprintf('​a is %d, b is %d, c is %d\n',​a,​b,​c);​
 +</​code>​
 +
 +Test it:
 +
 +<code matlab>
 +>> test_fun(1)
 +a is 1, b is 2, c is 3
 +>> test_fun(1,'​b',​1)
 +a is 1, b is 1, c is 3
 +>> ​
 +</​code>​
 +
 +Note how specifying a "​key-value"​ pair ('''​b',​1''​) overwrote the default for ''​b''​. ''​extract_varargin''​ basically goes through the varargins and assigns each value to the corresponding key, similar to ''​ProcessConfig()''​.
 +
 +☛ Verify your understanding by calling ''​test_fun''​ such that ''​b''​ and ''​c''​ are both set to ''​0''​. Also note what happens when you do ''​test_fun(1,'​B',​1)''​!
 +
 ==== PlotTSDFromIV ==== ==== PlotTSDFromIV ====
  
-==== ft_dataviewer ​====+''​PlotTSDFromIV()''​ is a simple, lightweight function designed to show time series data (TSD) for specific intervals (IV, hence the name). It has two different display modes, the first one ('''​iv'''​) looks like this: 
 + 
 +<code matlab>​ 
 +cfg []; 
 +cfg.display ​'​iv';​ 
 +cfg.width ​0.1; 
 +PlotTSDfromIV(cfg,​metadata.SWRtimes,​csc);​ % need to LoadMetadata to make this work! 
 +</​code>​ 
 + 
 +{{ :​analysis:​course-w16:​plot_tutorial7.png?​nolink&​600 |}} 
 + 
 +(The intervals highlighted in red are manually selected, putative SWR events.) 
 + 
 +☛ Try the other display mode, '''​tsd'''​. 
 +==== FieldTrip'​s databrowser ​==== 
 + 
 +As implied by the name, ''​MultiRaster()''​ is primarily intended for plotting spike data, although it can accept multiple tsd's for plotting as well.  
 + 
 +The %%FieldTrip%% toolbox, which will be covered in more detail in later modules, has a number of data visualization tools that are better suited for plotting time series data such as LFPs, EEG/MEG and single-trial fMRI data.  
 + 
 +To get our current data into the %%FieldTrip%% format, we first have to do a few steps: 
 + 
 +<code matlab>​ 
 +% convert to ft format 
 +cfg = []; cfg.mode = '​resample';​ 
 +csc_ft = TSDtoFT(cfg,​csc);​ 
 +t0 = csc_ft.time{1}(1);​ csc_ft.time{1} = csc_ft.time{1}-t0;​ 
 + 
 +% create ft trials from iv 
 +trl_cfg = []; 
 +trl_cfg.t = IVcenters(metadata.SWRtimes)-t0;​ 
 +trl_cfg.mode = '​neuralynx';​ 
 +trl_cfg.hdr = csc_ft.hdr;​ 
 +trl_cfg.twin = [-1 1]; 
 + 
 +trl = ft_maketrl(trl_cfg);​ 
 + 
 +% use trials to create trialified data structure 
 +temp_cfg = []; temp_cfg.trl = trl; 
 + 
 +ft_in = ft_redefinetrial(temp_cfg,​csc_ft);​ 
 +</​code>​  
 + 
 +Now we can run the data browser by typing ''​ft_databrowser([],​ft_in);'',​ to get 
 + 
 +{{ :​analysis:​course-w16:​plot_tutorial8.png?​nolink&​600 |}}
  
 +Notice the various control buttons for moving between trials, channels (here we have only one loaded), and other functions, documented [[http://​www.fieldtriptoolbox.org/​faq/​how_can_i_use_the_databrowser | here]]. %%FieldTrip%% is able to load many types of data files, see [[http://​www.fieldtriptoolbox.org/​reading_data | here]] for the list and how to use its data loaders.
 ===== Challenges ====== ===== Challenges ======
  
analysis/course-w16/week3long.1453067701.txt.gz · Last modified: 2018/07/07 10:19 (external edit)