Analysing brain blood flow

In a previous post I described how to use ultrasound to measure brain blood flow. So, the data has been gathered. To recap, this is what it looks like:

tcd

Now what? How do we analyse this? Some ultrasound machines have their own analysis software that can be used to determine whether the blood flow is normal. Usually, it requires training and experience to interpret ultrasound results for diagnostic or healthcare purposes, even with custom software. For research purposes, however, things are a bit different. The custom software may not always be what we are looking for, and diagnosis may not always be the end-game. In my research, I analyse beat-by-beat blood flow velocity in healthy participants, looking how this changes over time. I have used custom Matlab scripts to analyse these, using quite simple steps:

  1. Export the data in .dat or .txt format. This will depend on the software, and I have found it easiest to work with machines that allow direct (during data collection) connection with a computer, feeding into Spike, LabChart or similar programs, as I know how to export data from such software without any fuss.
  2. Read the data file in MATLAB (or similar), then follow these processing steps
    1. Visualise the data. I always look at my data before I start working on it. This helps figuring out the quality, and making sure I’ve measured what I think I have measured.
    2. Smooth the data. No data is perfect, and TCD data can be quite noisy for a number of reasons. While smoothing helps getting rid of some of these issues, I prefer to smooth as little as possible.
    3. Remove any gaps. There may be gaps in the data file where the probe had to be shifted for gel application or similar. These can be removed manually or you can use a threshold (lower and higher, outside of the physiological range). I prefer to automate as much as possible, both because I don’t want the hassle and I want to avoid the potential bias that comes with manual changes. I tend to visualise the datafile whenever using thresholds, to make sure the data looks fine, and I use the same threshold for all subjects.
    4. Remove extra noise? If the data is particularly noisy, a second threshold excluding values that are greater/smaller than e.g. 1 standard deviation from the rest of the window should be helpful in removing noise. This should get rid of peaks arising from shifting the probe or applying more gel.
    5. Find the min and max values. I typically run a for loop for this, tagging all the min and max values (creating vectors for tags, values and time). While it may not be the most elegant method, I prefer to find the values by creating a window moving through the datafile, using an if statement to tag only values that are higher (max) or lower (min) than the other values in the window. I tend to use +/- 40 data points if the acquisition rate is about 50 Hz. I visualise the min/max values superimposed on the raw data to make sure I have correctly identified the values. This is where it is important not to have smoothed too much, because that flattens the wave and you might end up with several similar values and shifted/non-existing peaks/troughs.
    6. Identify the waveforms. I run a for loop to identify full waveforms, using the tags created in the step above. If the min values are tagged ‘0’ and the max values ‘1’, then a waveform would be identified by a 0-1-0 sequence (with an appropriate if statement excluding datapoints that are too far apart in terms of time to avoid any misclassifications across several waveforms).
    7. Calculate what you are looking for. With these vectors, means can be calculated (or systolic values, diastolic values, R-R intervals and so on). Means can be calculated as follows: Mean CBFV = (PSV + [EDV × 2])/3, where CBFV is cerebral blood flow velocity, PSV is peak systolic velocity (the max value of the waveform), and EDV is end-diastolic velocity (the second min value in the waveform). These are highlighted in the figure above. Just for reference, average adult mean flow velocity is 46-86 cm/sec.
  3. Rinse and repeat for all participants

 

Further reading:

Advances in Transcranial Doppler US: Imaging Ahead Jonathan D. Kirsch, et. al. RadioGraphics 2013; 33:E1–E14. Link.

Purkayastha, S. & Sorond, F. (2012) Transcranial Doppler Ultrasound: Technique and Application. Semin Neurol. 32(4): 411–420. Link.

161px-Matlab_Logo

MATLAB for physiological analysis

I use MATLAB for my number crunching. While data acquisition and analysis programs such as Spike and LabChart have analysis functions that are good, they are not always appropriate for the type of analysis required. Also, I like to know all the calculations made on my raw data, and so my analysis tends to gravitate towards custom-made code. Dealing with time series for the most part, I’ve found MATLAB very useful, but it’s also handy for other types of data.

I therefore thought I’d compile a small list of things that has worked for me for the MATLAB novice looking to start using the program.

Learning MATLAB:

  1. Start small. Create a small matrix (e.g.20×20). Write code for processing your small matrix and make sure that it does what you expect it to do. Learn the basics on this matrix before touching real data.
  2. Break it. Error messages are great. Each message will lead to a better understanding of how to use MATLAB, so make sure you understand what they mean.
  3. Use the forum. MATLAB central will have many (if not all) the answers you can wish for (including: “what does my error message mean”). It’s a great resource! Link: http://uk.mathworks.com/matlabcentral/

Using MATLAB:

  1. Remember that MATLAB is only as good as its user. In many cases, you’ll get a number which might appear reasonable, but errors in your code can still be present. Getting an output does not mean that your code works the way you want.
  2. Learn to plot your data. Plot raw data and superimpose your calculations along the way. Mistakes should be easy to visualise, at least when working with time series. This is an easy quality check and has spared me some embarrassing mistakes in the past. Also, the figures you get can be made quite attractive for publication purposes. Certainly prettier than MS Excel, at least.
  3. Run the code on a known data set first. Make sure that the code produces the results you would expect. Again, this is a simple thing that can spare you a lot of grief. Compare with in-built programs if possible (such as spike detection in LabChart versus custom-written spike detection in MATLAB).
  4. Understand which computations are better done manually. Small, one-off computations could require more work coding up than just processing manually. The point of the exercise is to make things easier, not harder.
  5. Learn how to run a series of analyses with one click. Sometimes you’ll need to run many subjects in quick succession. Writing code that reads a list of subject numbers (one click) and automatically loops through the analysis process for each line on the list is one way of doing this that has saved me some time in the past.

Automated analysis can be very useful as it is less time consuming in the long run and you reduce the risk of silly, manual mistakes (typos etc.). With a few clicks, you could run all your number crunching in the background whilst you have your morning coffee and try to wake up your brain. Sounds good, eh? It also helps you understand the data better. For example, if you want to calculate a waveform mean, you have to know the mathematical formula for doing so. In an in-built program, this would not be needed. In short, it can be very helpful, when done right.

A few other things:

  1. Text files are good. Text files (.txt or .dat) are quite easy to work with in MATLAB. Save raw data in one of these simple formats as well as the custom format.
  2. Company is better. If there is someone else around that knows or wants to learn, join forces. It’s much more fun that way.
  3. Annotation is the best. Annotate and annotate well. Your future self thanks you! Trust me on this. A few simple %this-is-what-this-line-does will make your life infinitely easier.

If you’re not certain about using MATLAB, there’s a 30-day trial version available that you can play with to figure out if it is for you. It’ll take a little while to get to grips with if you’re not used to this type of program, but it’s worth the time.

161px-Matlab_Logo