PYTHON CODE

# Import the required modules.
import scipy.signal as si
import numpy as N


def extract_envelope(data,frequencyLow,frequencyHigh,Fs):
    '''Function to compute the envelope of data using Hilbert transform.
        Inputs:
        data: data array
        frequencyLow, frequencyHigh: frequency range for envelope extraction.
        Fs: sampling frequency'''
    


    # Filter the trace in the desired frequency range using a second order Butterworth filter applied in forward and reverse direction.
    nyq=Fs/float(2)
    b,a=si.butter(2,[frequencyLow/nyq,frequencyHigh/nyq],btype="bandpass",analog=False,output='ba')
    filtered_data=si.filtfilt(b,a,data)

    # Perform Hilbert transformation to extract the envelope.
    hil=si.hilbert(filtered_data)
    envelope=N.abs(hil)

    return envelope


IMAGEJ MACROS


//Macro for automatically detecting arm entries in the radial arm water maze.
//Requires to define target regions of interest (ROIs), in this case "6arm_maze1.zip". These must be adusted to match the maze architecture.
// Inputs: Recorded uncompressed .avi or .tiff videos
// Outputs: Result .txt files of all 6 arms

macro "Batch maze video analysis" {

dir1 = getDirectory("Choose source directory");
list= getFileList(dir1);
setBatchMode(false);


for (i=0; i<list.length; i++) {
filename=list[i];
showProgress(i+1, list.length);

if (endsWith(filename, ".tif")){
open(dir1+filename);
}

if (endsWith(filename, ".avi")){
run("AVI...", "select=["+dir1+list[i]+"] convert");
}


filename = substring (filename,0,lastIndexOf(filename,"."));

setAutoThreshold("Default");
//run("Threshold...");
setThreshold(0, 95);
setThreshold(0, 95);
run("Convert to Mask", " ");
run("Smooth", "stack");
setAutoThreshold("Default");
setThreshold(120, 255);
setThreshold(120, 255);
run("Convert to Mask", " ");

rename("Experiment");
run("MTrack2 ", "minimum=20 maximum=500 maximum_=1000 minimum_=100 save display show show show save=[D:\\data\\RAWM config files\\ROI results temp\\"+filename+"trackresults.xls] ");

selectWindow("Paths");
close();
selectWindow("Experiment labels");
run("Reduce...", "reduction=10");
save("/home/tempfiles/"+filename+"labels.tif");
close();

if(i<1){
run("ROI Manager...");
roiManager("Open", "/home/tempfiles/6arm_maze1.zip");
roiManager("Show All");
}
selectWindow("Experiment");
roiManager("Select", 0);
run("Set Measurements...", "  mean standard redirect=None decimal=5");
roiManager("Multi Measure");
run("Input/Output...", "jpeg=75 gif=-1 file=.txt");
selectWindow("Results");
saveAs("Results", "/home/tempfiles/"+filename+"north.txt");
roiManager("Select", 1);
roiManager("Multi Measure");
saveAs("Results", "/home/tempfiles/"+filename+"east.txt");
roiManager("Select", 2);
roiManager("Multi Measure");
saveAs("Results", "/home/tempfiles/"+filename+"southEast.txt");
roiManager("Select", 3);
roiManager("Multi Measure");
saveAs("Results", "/home/tempfiles/"+filename+"southWest.txt");
roiManager("Select", 4);
roiManager("Multi Measure");
saveAs("Results", "/home/tempfiles/"+filename+"northWest.txt");
roiManager("Select", 5);
roiManager("Multi Measure");
saveAs("Results", "/home/tempfiles/"+filename+"target.txt");
selectWindow("Experiment");
close();

if (isOpen("Results")) {
       selectWindow("Results");
       run("Close");
   } 


}
}



################################################################################################
//Batch macro to trace the movement of mice in a maze. Utilizes MTrack2 of FIJI.



macro "Batch tracing"{
source=getDirectory("Choose source directory");
target=getDirectory("Choose directory for renamed images");
list= getFileList(source);
setBatchMode(true);


// Loop over all files in the selected directory.//
for (i=0; i<list.length; i++) {
	showProgress(i+1, list.length);
	filename=list[i];
	currentFile = list[i];
	currentFile = substring (currentFile,0,lastIndexOf(currentFile,"."));
	open(source+filename);
	
	
	
	rename("Experiment");
	//run("Threshold...");
	setThreshold(0, 100);
	setThreshold(0, 100);
	run("Convert to Mask", "method=Default background=Light");
	run("Smooth", "stack");
	setThreshold(100, 255);
	setThreshold(100, 255);
	run("Convert to Mask", "method=Default background=Light");
	// Track the mouse path.//
	trackSaver = target+currentFile+"_trackresults.xls";
	run("MTrack2 ", "minimum=10 maximum=10000 maximum_=1000 minimum_=1000 save display show show show save=[trackSaver]");
	selectWindow("Paths");
	save(target+currentFile+"_paths.tif");
	close();
	selectWindow("Experiment labels");
	run("Reduce...", "reduction=10");
	save(target+currentFile+"_experiment_labels.tif");
	close();
	selectWindow("Experiment");
	close();
	
	// Close all windows.//
	if (isOpen("Results")) {
		selectWindow("Results");
		run("Close");
	}
}




################################################################################################
macro "Various thresholds colocalisation" {

// colocalization analysis of 2-channel images //
// Input: 2-channel .tif files; channel1 to be colocalized with channel2 //
// All files must be located in a source directory, results will be saved to a target directory //
// Measures colocalization of the 5, 10, 25, 50 and 70 brightest percent //
// Output: Threshold files, rotated files, and summary .txt files for all images //





// Define source and target directories with file dialog.
source=getDirectory("Choose source directory");
target=getDirectory("Choose target directory");
list= getFileList(source);
setBatchMode(true);

for (i=0; i<list.length; i++) {
        filename=list[i];
	open(source+filename);
	filename= substring(filename,0,lastIndexOf(filename,"."));
	result_file=File.open(target+filename+"_Results_summary.txt");
	print(result_file,"Results:");
	print(result_file,"top 5% colocalized area");
	print(result_file,"top 5%_rotated colocalized area");
	print(result_file,"top 10% colocalized area");
	print(result_file,"top 10%_rotated colocalized area");
	print(result_file,"top 25% colocalized area");
	print(result_file,"top 25%_rotated colocalized area");
	print(result_file,"top 50% colocalized area");
	print(result_file,"top 50%_rotated colocalized area");
	print(result_file,"top 70% colocalized area");
	print(result_file,"top 70%_rotated colocalized area");
	print(result_file,"");
	


	run("Stack to Images");
	run("Images to Stack", "name=Stack title=[]");
	run("Duplicate...", "title=5percent duplicate range=1-2");
	selectWindow("Stack");
	run("Duplicate...", "title=10percent duplicate range=1-2");
	selectWindow("Stack");
	run("Duplicate...", "title=25percent duplicate range=1-2");
	selectWindow("Stack");
	run("Duplicate...", "title=50percent duplicate range=1-2");
	selectWindow("Stack");
	run("Duplicate...", "title=70percent duplicate range=1-2");


	selectWindow("5percent");
	setAutoThreshold("Default");
	//run("Threshold...");
	setAutoThreshold("Yen");
	setAutoThreshold("Triangle");
	setThreshold(252, 255);
	setThreshold(252, 255);
	run("Convert to Mask", " ");
	selectWindow("10percent");
	setAutoThreshold("Triangle");
	setThreshold(230, 255);
	setThreshold(230, 255);
	run("Convert to Mask", " ");
	selectWindow("25percent");
	setAutoThreshold("Triangle");
	setThreshold(191, 255);
	setThreshold(191, 255);
	run("Convert to Mask", " ");
	selectWindow("50percent");
	setAutoThreshold("Triangle");
	setThreshold(128, 255);
	setThreshold(128, 255);
	run("Convert to Mask", " ");
	selectWindow("70percent");
	setAutoThreshold("Triangle");
	setThreshold(77, 255);
	setThreshold(77, 255);
	run("Convert to Mask", " ");
	selectWindow("Stack");
	close();


        selectWindow("5percent");
        run("Stack to Images");
        run("Duplicate...", "title=5percent-0002-rotated");
        run("Rotate 90 Degrees Right");
        imageCalculator("Multiply create", "5percent-0002","5percent-0001");
        imageCalculator("Multiply create", "5percent-0002-rotated","5percent-0001");
        selectWindow("Result of 5percent-0002");
        run("Set Measurements...", "  area_fraction redirect=None decimal=5");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_5percent.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }
        selectWindow("Result of 5percent-0002-rotated");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_5percent_rotated.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }
        selectWindow("5percent-0001");
        close();
        selectWindow("5percent-0002");
        close();
        selectWindow("5percent-0002-rotated");
        close();

        selectWindow("10percent");
        run("Stack to Images");
        run("Duplicate...", "title=10percent-0002-rotated");
        run("Rotate 90 Degrees Right");
        imageCalculator("Multiply create", "10percent-0002","10percent-0001");
        imageCalculator("Multiply create", "10percent-0002-rotated","10percent-0001");
        selectWindow("Result of 10percent-0002");
        run("Set Measurements...", "  area_fraction redirect=None decimal=5");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_10percent.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }
        
        selectWindow("Result of 10percent-0002-rotated");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_10percent_rotated.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }
        selectWindow("10percent-0001");
        close();
        selectWindow("10percent-0002");
        close();
        selectWindow("10percent-0002-rotated");
        close();


        selectWindow("25percent");
        run("Stack to Images");
        run("Duplicate...", "title=25percent-0002-rotated");
        run("Rotate 90 Degrees Right");
        imageCalculator("Multiply create", "25percent-0002","25percent-0001");
        imageCalculator("Multiply create", "25percent-0002-rotated","25percent-0001");
        selectWindow("Result of 25percent-0002");
        run("Set Measurements...", "  area_fraction redirect=None decimal=5");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_25percent.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }

        selectWindow("Result of 25percent-0002-rotated");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_25percent_rotated.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }
        selectWindow("25percent-0001");
        close();
        selectWindow("25percent-0002");
        close();
        selectWindow("25percent-0002-rotated");
        close();

        selectWindow("50percent");
        run("Stack to Images");
        run("Duplicate...", "title=50percent-0002-rotated");
        run("Rotate 90 Degrees Right");
        imageCalculator("Multiply create", "50percent-0002","50percent-0001");
        imageCalculator("Multiply create", "50percent-0002-rotated","50percent-0001");
        selectWindow("Result of 50percent-0002");
        run("Set Measurements...", "  area_fraction redirect=None decimal=5");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_50percent.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }

        
        selectWindow("Result of 50percent-0002-rotated");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_50percent_rotated.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }

        selectWindow("50percent-0001");
        close();
        selectWindow("50percent-0002");
        close();
        selectWindow("50percent-0002-rotated");
        close();

        selectWindow("70percent");
        run("Stack to Images");
        run("Duplicate...", "title=70percent-0002-rotated");
        run("Rotate 90 Degrees Right");
        imageCalculator("Multiply create", "70percent-0002","70percent-0001");
        imageCalculator("Multiply create", "70percent-0002-rotated","70percent-0001");
        selectWindow("Result of 70percent-0002");
        run("Set Measurements...", "  area_fraction redirect=None decimal=5");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_70percent.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }

        
        selectWindow("Result of 70percent-0002-rotated");
        run("Measure");
        area=getResult("%Area");
        print(result_file,area);
        save(target+filename+"Result_of_70percent_rotated.tif");
        close();
        if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }


        selectWindow("70percent-0001");
        close();
        selectWindow("70percent-0002");
        close();
        selectWindow("70percent-0002-rotated");
        close();
        File.close(result_file);

        }
if (isOpen("Results")) {
	    selectWindow("Results");
	    run("Close");
	    }
}
