<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.unizh.ini.jaer.projects.cochsoundloc;

import ch.unizh.ini.jaer.chip.cochlea.BinauralCochleaEvent;
import ch.unizh.ini.jaer.chip.cochlea.BinauralCochleaEvent.Ear;
import ch.unizh.ini.jaer.chip.cochlea.CochleaAMSEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jaer.chip.*;
import net.sf.jaer.event.EventPacket;
import net.sf.jaer.eventprocessing.EventFilter2D;
import java.util.*;
import com.sun.opengl.util.GLUT;
import java.awt.Graphics2D;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import net.sf.jaer.graphics.FrameAnnotater;
import net.sf.jaer.util.EngineeringFormat;
import java.io.*;
import java.util.concurrent.ArrayBlockingQueue;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import net.sf.jaer.util.RemoteControlCommand;
import net.sf.jaer.util.RemoteControlled;

/**
 * Extracts interaural time difference (ITD) from a binaural cochlea input.
 * 
 * @author Holger
 */
public class ITDFilter extends EventFilter2D implements Observer, FrameAnnotater, RemoteControlled {

    public static String getDescription() {
        return "Measures ITD (Interaural time difference) using a variety of methods";
    }
    private ITDCalibrationGaussians calibration = null;
    private float averagingDecay = getPrefs().getFloat("ITDFilter.averagingDecay", 1);
    private int maxITD = getPrefs().getInt("ITDFilter.maxITD", 800);
    private int numOfBins = getPrefs().getInt("ITDFilter.numOfBins", 16);
    private int maxWeight = getPrefs().getInt("ITDFilter.maxWeight", 5);
    private int dimLastTs = getPrefs().getInt("ITDFilter.dimLastTs", 4);
    private int maxWeightTime = getPrefs().getInt("ITDFilter.maxWeightTime", 500000);
    private boolean display = getPrefs().getBoolean("ITDFilter.display", false);
    private boolean displayNormalize = getPrefs().getBoolean("ITDFilter.displayNormalize", true);
    private boolean displaySoundDetected = getPrefs().getBoolean("ITDFilter.displaySoundDetected", true);
    private boolean useLaterSpikeForWeight = getPrefs().getBoolean("ITDFilter.useLaterSpikeForWeight", true);
    private boolean usePriorSpikeForWeight = getPrefs().getBoolean("ITDFilter.usePriorSpikeForWeight", true);
    private boolean computeMeanInLoop = getPrefs().getBoolean("ITDFilter.computeMeanInLoop", true);
    private boolean writeAvgITD2File = getPrefs().getBoolean("ITDFilter.writeAvgITD2File", false);
    private boolean writeITD2File = getPrefs().getBoolean("ITDFilter.writeITD2File", false);
    private boolean sendITDsToOtherThread = getPrefs().getBoolean("ITDFilter.sendITDsToOtherThread", false);
    private int itdEventQueueSize = getPrefs().getInt("ITDFilter.itdEventQueueSize", 1000);
    private int timeLocalExtremaDetection = getPrefs().getInt("ITDFilter.timeLocalExtremaDetection", 200000);
    private boolean writeBin2File = getPrefs().getBoolean("ITDFilter.writeBin2File", false);
    private boolean saveFrequenciesSeperately = getPrefs().getBoolean("ITDFilter.saveFrequenciesSeperately", false);
    private boolean invert = getPrefs().getBoolean("ITDFilter.invert", false);
    private boolean write2FileForEverySpike = getPrefs().getBoolean("ITDFilter.write2FileForEverySpike", false);
    private boolean weightFrequencies = getPrefs().getBoolean("ITDFilter.weightFrequencies", false);
    private boolean useRidgeRegression = getPrefs().getBoolean("ITDFilter.useRidgeRegression", false);
    private boolean use1DRegression = getPrefs().getBoolean("ITDFilter.use1DRegression", false);
    private boolean normToConfThresh = getPrefs().getBoolean("ITDFilter.normToConfThresh", false);
    private boolean showAnnotations = getPrefs().getBoolean("ITDFilter.showAnnotations", false);
    private int confidenceThreshold = getPrefs().getInt("ITDFilter.confidenceThreshold", 30);
    private int activityThreshold = getPrefs().getInt("ITDFilter.activityThreshold", 30);
    private int numLoopMean = getPrefs().getInt("ITDFilter.numLoopMean", 2);
    private int numOfCochleaChannels = getPrefs().getInt("ITDFilter.numOfCochleaChannels", 32);
    private boolean useCalibration = getPrefs().getBoolean("ITDFilter.useCalibration", false);
    private String calibrationFilePath = getPrefs().get("ITDFilter.calibrationFilePath", null);
    private double[] frequencyWeights;
    private double[] ridgeWeights;
    ITDFrame frame;
    private ITDBins myBins;
    private boolean connectToPanTiltThread = false;
    //private LinkedList[][] lastTimestamps;
    //private ArrayList<LinkedList<Integer>> lastTimestamps0;
    //private ArrayList<LinkedList<Integer>> lastTimestamps1;
    private int[][][][] lastTs;
    private int[][][] lastTsCursor;
    //private int[][] AbsoluteLastTimestamp;
    Iterator iterator;
    private float lastWeight = 1f;
    private int avgITD;
    private float avgITDConfidence = 0;
    private float ILD;
    EngineeringFormat fmt = new EngineeringFormat();
    FileWriter fstream;
    FileWriter fstreamBins;
    FileWriter freqfstreamBins;
    BufferedWriter ITDFile;
    BufferedWriter AvgITDFile;
    BufferedWriter BinFile;
    BufferedWriter freqBinFile;
    private boolean wasMoving = false;
    private int numNeuronTypes = 1;
    private static ArrayBlockingQueue ITDEventQueue = null;
    private boolean ITDEventQueueFull = false;
    public PanTilt panTilt = null;
    private ITDBins[] freqBins;
    private double ConfidenceRecentMax = 0;
    private int ConfidenceRecentMaxTime = 0;
    private double ConfidenceRecentMin = 1e6;
    private int ConfidenceRecentMinTime = 0;
    private boolean ConfidenceRising = true;

    public String processRemoteControlCommand(RemoteControlCommand command, String input) {
        String[] tok = input.split("\\s");
        if (tok.length < 2) {
            return "not enough arguments\n";
        }

        try {
            if (tok[1].equals("saveitd")) {
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    startAvgITD2File(tok[2]);
                    return "starting to save itds\n";
                }
            }
            if (tok[1].equals("stopsaveitd")) {
                setWriteAvgITD2File(false);
                return "stop saving itds\n";
            }
            if (tok[1].equals("savefreqbins")) {
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    startSaveFreq(tok[2]);
                    return "starting to save freqbins\n";
                }
            }
            if (tok[1].equals("stopsavefreqbins")) {
                setSaveFrequenciesSeperately(false);
                return "stop saving freqbins\n";
            }
            if (tok[1].equals("savebin")) {
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    startWriteBin2File(tok[2]);
                    return "starting to save bins\n";
                }
            }
            if (tok[1].equals("stopsavebin")) {
                setWriteBin2File(false);
                return "stop saving bins\n";
            }
            if (tok[1].equals("resetbins")) {
                createBins();
                return "bins reseted.\n";
            }
            if (tok[1].equals("savebinnow")) {
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    writeBin2FileNow(tok[2]);
                    return "writing bins now\n";
                }
            }
            log.info("Received Command:" + input);
        } catch (IOException e) {
            return "IOExeption in remotecontrol\n";<commit_msg>changed remoteControl commands for the ITDFilter<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.unizh.ini.jaer.projects.cochsoundloc;

import ch.unizh.ini.jaer.chip.cochlea.BinauralCochleaEvent;
import ch.unizh.ini.jaer.chip.cochlea.BinauralCochleaEvent.Ear;
import ch.unizh.ini.jaer.chip.cochlea.CochleaAMSEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jaer.chip.*;
import net.sf.jaer.event.EventPacket;
import net.sf.jaer.eventprocessing.EventFilter2D;
import java.util.*;
import com.sun.opengl.util.GLUT;
import java.awt.Graphics2D;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import net.sf.jaer.graphics.FrameAnnotater;
import net.sf.jaer.util.EngineeringFormat;
import java.io.*;
import java.util.concurrent.ArrayBlockingQueue;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import net.sf.jaer.util.RemoteControlCommand;
import net.sf.jaer.util.RemoteControlled;

/**
 * Extracts interaural time difference (ITD) from a binaural cochlea input.
 * 
 * @author Holger
 */
public class ITDFilter extends EventFilter2D implements Observer, FrameAnnotater, RemoteControlled {

    public static String getDescription() {
        return "Measures ITD (Interaural time difference) using a variety of methods";
    }
    private ITDCalibrationGaussians calibration = null;
    private float averagingDecay = getPrefs().getFloat("ITDFilter.averagingDecay", 1);
    private int maxITD = getPrefs().getInt("ITDFilter.maxITD", 800);
    private int numOfBins = getPrefs().getInt("ITDFilter.numOfBins", 16);
    private int maxWeight = getPrefs().getInt("ITDFilter.maxWeight", 5);
    private int dimLastTs = getPrefs().getInt("ITDFilter.dimLastTs", 4);
    private int maxWeightTime = getPrefs().getInt("ITDFilter.maxWeightTime", 500000);
    private boolean display = getPrefs().getBoolean("ITDFilter.display", false);
    private boolean displayNormalize = getPrefs().getBoolean("ITDFilter.displayNormalize", true);
    private boolean displaySoundDetected = getPrefs().getBoolean("ITDFilter.displaySoundDetected", true);
    private boolean useLaterSpikeForWeight = getPrefs().getBoolean("ITDFilter.useLaterSpikeForWeight", true);
    private boolean usePriorSpikeForWeight = getPrefs().getBoolean("ITDFilter.usePriorSpikeForWeight", true);
    private boolean computeMeanInLoop = getPrefs().getBoolean("ITDFilter.computeMeanInLoop", true);
    private boolean writeAvgITD2File = getPrefs().getBoolean("ITDFilter.writeAvgITD2File", false);
    private boolean writeITD2File = getPrefs().getBoolean("ITDFilter.writeITD2File", false);
    private boolean sendITDsToOtherThread = getPrefs().getBoolean("ITDFilter.sendITDsToOtherThread", false);
    private int itdEventQueueSize = getPrefs().getInt("ITDFilter.itdEventQueueSize", 1000);
    private int timeLocalExtremaDetection = getPrefs().getInt("ITDFilter.timeLocalExtremaDetection", 200000);
    private boolean writeBin2File = getPrefs().getBoolean("ITDFilter.writeBin2File", false);
    private boolean saveFrequenciesSeperately = getPrefs().getBoolean("ITDFilter.saveFrequenciesSeperately", false);
    private boolean invert = getPrefs().getBoolean("ITDFilter.invert", false);
    private boolean write2FileForEverySpike = getPrefs().getBoolean("ITDFilter.write2FileForEverySpike", false);
    private boolean weightFrequencies = getPrefs().getBoolean("ITDFilter.weightFrequencies", false);
    private boolean useRidgeRegression = getPrefs().getBoolean("ITDFilter.useRidgeRegression", false);
    private boolean use1DRegression = getPrefs().getBoolean("ITDFilter.use1DRegression", false);
    private boolean normToConfThresh = getPrefs().getBoolean("ITDFilter.normToConfThresh", false);
    private boolean showAnnotations = getPrefs().getBoolean("ITDFilter.showAnnotations", false);
    private int confidenceThreshold = getPrefs().getInt("ITDFilter.confidenceThreshold", 30);
    private int activityThreshold = getPrefs().getInt("ITDFilter.activityThreshold", 30);
    private int numLoopMean = getPrefs().getInt("ITDFilter.numLoopMean", 2);
    private int numOfCochleaChannels = getPrefs().getInt("ITDFilter.numOfCochleaChannels", 32);
    private boolean useCalibration = getPrefs().getBoolean("ITDFilter.useCalibration", false);
    private String calibrationFilePath = getPrefs().get("ITDFilter.calibrationFilePath", null);
    private double[] frequencyWeights;
    private double[] ridgeWeights;
    ITDFrame frame;
    private ITDBins myBins;
    private boolean connectToPanTiltThread = false;
    //private LinkedList[][] lastTimestamps;
    //private ArrayList<LinkedList<Integer>> lastTimestamps0;
    //private ArrayList<LinkedList<Integer>> lastTimestamps1;
    private int[][][][] lastTs;
    private int[][][] lastTsCursor;
    //private int[][] AbsoluteLastTimestamp;
    Iterator iterator;
    private float lastWeight = 1f;
    private int avgITD;
    private float avgITDConfidence = 0;
    private float ILD;
    EngineeringFormat fmt = new EngineeringFormat();
    FileWriter fstream;
    FileWriter fstreamBins;
    FileWriter freqfstreamBins;
    BufferedWriter ITDFile;
    BufferedWriter AvgITDFile;
    BufferedWriter BinFile;
    BufferedWriter freqBinFile;
    private boolean wasMoving = false;
    private int numNeuronTypes = 1;
    private static ArrayBlockingQueue ITDEventQueue = null;
    private boolean ITDEventQueueFull = false;
    public PanTilt panTilt = null;
    private ITDBins[] freqBins;
    private double ConfidenceRecentMax = 0;
    private int ConfidenceRecentMaxTime = 0;
    private double ConfidenceRecentMin = 1e6;
    private int ConfidenceRecentMinTime = 0;
    private boolean ConfidenceRising = true;

    public String processRemoteControlCommand(RemoteControlCommand command, String input) {
        String[] tok = input.split("\\s",3);
        if (tok.length < 2) {
            return "not enough arguments\n";
        }

        try {
            if (tok[1].equals("saveitd")) {
                if (tok.length < 2) {
                    return "not enough arguments\n";
                } else {
                    startAvgITD2File(tok[2]);
                    return "starting to save itds\n";
                }
            }
            if (tok[1].equals("saveitdandreset")) {
                if (tok.length < 2) {
                    return "not enough arguments\n";
                } else {
                    chip.getAeViewer().zeroTimestamps();
                    createBins();
                    startAvgITD2File(tok[2]);
                    return "starting to save itds\n";
                }
            }
            if (tok[1].equals("stopsaveitd")) {
                setWriteAvgITD2File(false);
                return "stop saving itds\n";
            }
            if (tok[1].equals("savefreqbins")) {
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    startSaveFreq(tok[2]);
                    return "starting to save freqbins\n";
                }
            }
            if (tok[1].equals("stopsavefreqbins")) {
                setSaveFrequenciesSeperately(false);
                return "stop saving freqbins\n";
            }
            if (tok[1].equals("savebin")) {
                String filename = tok[2];
                log.info("save bins to: "+filename);
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    startWriteBin2File(filename);
                    return "starting to save bins\n";
                }
            }
            if (tok[1].equals("stopsavebin")) {
                setWriteBin2File(false);
                return "stop saving bins\n";
            }
            if (tok[1].equals("resetbins")) {
                createBins();
                return "bins reseted.\n";
            }
            if (tok[1].equals("savebinnow")) {
                if (tok.length < 3) {
                    return "not enough arguments\n";
                } else {
                    writeBin2FileNow(tok[2]);
                    return "writing bins now\n";
                }
            }
            if(tok[1].equals("zerotimestamps"))
            {
                chip.getAeViewer().zeroTimestamps();
                return "zeroed time\n";
            }
            log.info("Received Command:" + input);
        } catch (IOException e) {
            return "IOExeption in remotecontrol\n";
{'fraction_non_alphanumeric': 0.10577039641168627, 'fraction_numerical': 0.009576918414353255, 'mean_word_length': 2.9670593892762684, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140054', 'n_tokens_mistral': 4955, 'n_tokens_neox': 4699, 'n_words': 1008}