Skip to content

Actigraphy

joshghimire edited this page May 19, 2023 · 44 revisions
Plots Generated by Actigraphy:
1. Number of Changed Pixels, Binned Hourly Using Mean (see below for difference between Mean and Sum):

2. Number of Changed Pixels, Binned Hourly using Sum:

3. Number of Changed Pixels by Hour of Day, Binned Hourly using Mean:

4. Number of Changed Pixels by Hour of Day, Binned Hourly using Sum:

5. Sum Total Sum Changed Pixel Values by Hour of Day, Binned Hourly using Sum:

...

The actigraphy code can be found here. As of 5/14/2023, use the master branch.

Actigraphy requires Matlab and the develop branch of PhoWatsonCommonMatlab to be downloaded as well.


How to run Actigraphy:

(as of 5.14.23)

  1. Open Matlab. Navigate to ActigraphyRun.m

  2. Change FoundVideoFilesPath to the where FoundVideoFiles.mat should be saved.
    (More details about FoundVideoFiles.mat and the other variables and functions used in actigraphy can be found below)

    Example:
    If you want FoundVideoFiles.mat to be saved in C:\Documents\Experiment01\Cohort01:
    FoundVideoFilesPath = 'C:\Documents\Experiment01\Cohort01';
  1. Change BBParenthPath such that it is the folder which contains all other BB folders.

    Example:

    If C:\Documents\Experiment01\Cohort01\Videos contains videos for each Behavioral Box/Digital Homecage in individual folders like so:

    BB01

    BB02

    BB03

    GeneralOutputsPath = 'C:\Documents\Experiment01\Cohort01\Videos';
  1. Change GeneralOutputsPath to where the tables, plots, and merged_actigraphy folders should be saved.

    Example:
    If you want tables and plots C:\Documents\Experiment01\Cohort01\Actigraphy Results:
    GeneralOutputsPath = 'C:\Documents\Experiment01\Cohort01\Actigraphy Results';
  1. TODO: Doublecheck if there is hardcoded video folder location in BatchAnalyzeVideos.m

  2. In the Command Window at the bottom of the screen, type in:

     ActigraphyRun
  1. Wait. It will take approx 1hr to generate actigraphy for 4hrs of video.

Summary of Actigraphy Steps:

Note: Running ActigraphyRun.m will perform these steps without any other user input. The explanations below are documentation/troubleshooting.

1) BatchProcessVideoFileAnalyzer.m

BatchProcessVideoFileAnalyzer.m iterates over .mp4 video files produced by the Digital Homecages, and creates an output FoundVideoFiles.mat data file containing a list of video files and metadata about them. Next, it uses this list of found video files to loop through them and compute actigraphy data for each of them. This data is saved out to a video-specific actigraphy file (meaning 1 per video) in the actigraphy directory/bbID folder. It tries not to over-write previous results, and it stores a list of the videos that have been processed in a variable named all_videos_output_data stored within FoundVideoFiles.mat.
Estimated Runtime: ~1hr per 4hr video

2) CombineActigraphyOutputResults.m

CombineActigraphyOutputResults.m loops over the per-video actigraphy produced in the previous stage to produce a concatenated actigraphy file named "MergedBoxActigraphyData_BB00.mat", where 00 is the BBID. The result will be one actigraphy file per box/Digital Homecage. The structure of the output is similar to the form of the FoundVideoFiles.mat in terms of its variables, which it was inspired by. The current boxes included in processing can be changed by setting the corresponding entry in "current_included_bbIDs" to true or false.
Estimated Runtime: <10 minutes per box.

3) ProduceActigraphyFinalOutputTable.m

Loads the raw actigraphy data vectors from the concatenated file produced in the previous step, and builts a MATLAB "timetable" structure from them to allow for efficient binning and processing of data. It optionally saves out a MergedBoxActigraphyData_BB00_Timetables.mat file just in case you don't want to repeat this step for multiple analysis. The runtime of this step is fairly low compared to the others though. Finally, it bins and analyzes the timetable and produces the final plots for variables of interest.
Estimated Runtime: <5 minutes per box.

4) ProduceActigraphyFinalPutputPlots.m

Loads the binned timetable files produced in the previous step, and plots them.
Estimated Runtime: <1 minute per box.


Main Run of Actigraphy in BatchAnalyzeVideo.m:

Here is what the first frame of an example video would look like, as captured by the camera. (imshow(readFrame(v,'native')):

In the main run of BatchAnalyzeVideo.m, we invert the image:

curr_greyscale_frame = imcomplement(rgb2gray(readFrame(v, 'native')));


Next, we set this frame #1 as the previous frame so when we iterate to the next frame, we can calculate the number of pixels that change between frame #1 and frame #2

NOTE: MATLAB's readFrame reads the next available frame.

Next, we read frame #2 and get the inverted image.

Then we calculate the absolute value of the difference between the current greyscale frame and the previous greyscale frame.

inter_frame_change = abs(curr_greyscale_frame - prev_greyscale_frame);

To get the number of pixels that had a >1 change in pixel value between the previous frame and current frame (aka the number of pixels that changed >1 pixel value, regardless of the magnitude of those changes):

results.num_changed_pixels(i) = sum((inter_frame_change > 1),'all');

To get the total change in pixels between the previous frame and current frame (aka the mangitude in change in pixel values between the current frame and previous frame)

results.total_sum_changed_pixel_value(i) = sum(inter_frame_change,'all');
Clone this wiki locally