<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ine.telluride.jaer.cuda;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import javax.media.opengl.GLAutoDrawable;
import javax.swing.JOptionPane;
import net.sf.jaer.aemonitor.AEPacketRaw;
import net.sf.jaer.chip.AEChip;
import net.sf.jaer.event.EventPacket;
import net.sf.jaer.eventio.AEUnicastInput;
import net.sf.jaer.eventprocessing.EventFilter2D;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.media.opengl.GL;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import net.sf.jaer.eventio.AEUnicastOutput;
import net.sf.jaer.eventprocessing.FilterChain;
import net.sf.jaer.eventprocessing.filter.RefractoryFilter;
import net.sf.jaer.graphics.AEViewer;
import net.sf.jaer.graphics.FrameAnnotater;
/**
 * Allows control of remote CUDA process for filtering jAER data.
 *<p>
 * This filter does not process events at all. Rather it opens a TCP stream socket connection to a remote (maybe on the same machine)
 * process which is a CUDA GPGPU program which accepts AEs from jAER and which sends its output back to jAER for visualization.
 * <p>
 * The stream socket works with text command lines as though it were a terminal.  
 *
 * 
 * @author tobi/yingxue/jay
 */
public class CUDAObjectTrackerControl extends EventFilter2D implements FrameAnnotater {
    public final int CONTROL_PORT_DEFAULT=9998;
    AEViewer outputViewer=null;
    private int controlPort=getPrefs().getInt("CUDAObjectTrackerControl.controlPort", CONTROL_PORT_DEFAULT);
    private int recvOnPort=getPrefs().getInt("CUDAObjectTrackerControl.inputPort", 10012);
    private int sendToPort=getPrefs().getInt("CUDAObjectTrackerControl.outputPort", 10000);
    private String hostname=getPrefs().get("CUDAObjectTrackerControl.hostname", "localhost");
    private String cudaExecutablePath=getPrefs().get("CUDAObjectTrackerControl.cudaExecutablePath", null);
    private String cudaEnvironmentPath=getPrefs().get("CUDAObjectTrackerControl.cudaEnvironmentPath", null);
    private DatagramSocket controlSocket=null;
    InetAddress cudaInetAddress=null;
    private boolean cudaEnabled=getPrefs().getBoolean("CUDAObjectTrackerControl.cudaEnabled", true);
    Process cudaProcess=null;
    ProcessBuilder cudaProcessBuilder=null;
    AEUnicastOutput unicastOutput;
    AEUnicastInput unicastInput;
    CUDAChip cudaChip = null;
    CUDAChip.CUDAExtractor cudaExtractor=null;
    volatile private int inhibSpikeCount=0; // to annotate output

    /*
     *
    // from config.h in CUDA code
    #define MEMBRANE_TAU			10000.0F	// membrane time constant
    #define MEMBRANE_THRESHOLD		100.0F		// membrane threshold
    #define MEMBRANE_POTENTIAL_MIN	-50.0F		// membrane equilibrium potential

    #define MIN_FIRING_TIME_DIFF	15000		// low pass filter the events from retina

    #define E_I_NEURON_POTENTIAL	10.0		// excitatory to inhibitory synaptic weight
    #define I_E_NEURON_POTENTIAL	10.0		// inhibitory to excitatory synaptic weight

     */
    // TODO these cuda parameters better handled by indexed properties, but indexed properties not handled in FilterPanel yet
    final String CMD_THRESHOLD = "threshold";
    private float threshold = getPrefs().getFloat("CUDAObjectTrackerControl.threshold",100);
    final String CMD_MEMBRANE_TAU = "membraneTau";
    private float membraneTauUs = getPrefs().getFloat("CUDAObjectTrackerControl.membraneTauUs",10000);
    final String CMD_MEMBRANE_POTENTIAL_MIN = "membranePotentialMin";
    private float membranePotentialMin = getPrefs().getFloat("CUDAObjectTrackerControl.membranePotentialMin",-50);
    final String CMD_MIN_FIRING_TIME_DIFF = "minFiringTimeDiff";
    private float minFiringTimeDiff = getPrefs().getFloat("CUDAObjectTrackerControl.minFiringTimeDiff",15000);
    final String CMD_E_I_NEURON_POTENTIAL = "eISynWeight";
    private float eISynWeight = getPrefs().getFloat("CUDAObjectTrackerControl.eISynWeight",10);
    final String CMD_I_E_NEURON_POTENTIAL = "iESynWeight";
    private float iESynWeight = getPrefs().getFloat("CUDAObjectTrackerControl.iESynWeight",10);
    final String CMD_EXIT = "exit";
    private final String CMD_DEBUG_LEVEL = "debugLevel";
    private int debugLevel = getPrefs().getInt("CUDAObjectTrackerControl.debugLevel",1);
    private final String CMD_MAX_XMIT_INTERVAL_MS = "maxXmitIntervalMs";
    private int maxXmitIntervalMs = getPrefs().getInt("CUDAObjectTrackerControl.maxXmitIntervalMs",20);
    final String CMD_CUDA_ENABLED = "cudaEnabled";
    final String CMD_DELTA_TIME_US = "deltaTimeUs";
    private int deltaTimeUs = getPrefs().getInt("CUDAObjectTrackerControl.deltaTimeUs",1000);
    private final String CMD_NUM_OBJECTS = "numObject";
    private int numObject = getPrefs().getInt("CUDAObjectTrackerControl.numObject",5);
//     final String CMD_TERMINATE_IMMEDIATELY="terminate";
    final String CMD_KERNEL_SHAPE = "kernelShape";
//     final String CMD_SPIKE_PARTITIONING_METHOD="spikePartitioningMethod";
    private String CMD_CUDAS_RECVON_PORT="inputPort"; // swapped here because these are CUDAs
    private String CMD_CUDAS_SENDTO_PORT="outputPort";
    private boolean loopbackTestEnabled=getPrefs().getBoolean("CUDAObjectTrackerControl.loopbackTestEnabled", false);
    final String CMD_LOOPBACK_TEST_ENABLED="loopbackEnabled";

    public void annotate(float[][][] frame) {
    }

    public void annotate(Graphics2D g) {
    }

    public void annotate(GLAutoDrawable drawable) {<commit_msg>added check for null reference in annotate method to avoid exception on loading preferences.<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ine.telluride.jaer.cuda;
import java.awt.Graphics2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import javax.media.opengl.GLAutoDrawable;
import javax.swing.JOptionPane;
import net.sf.jaer.aemonitor.AEPacketRaw;
import net.sf.jaer.chip.AEChip;
import net.sf.jaer.event.EventPacket;
import net.sf.jaer.eventio.AEUnicastInput;
import net.sf.jaer.eventprocessing.EventFilter2D;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.media.opengl.GL;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import net.sf.jaer.eventio.AEUnicastOutput;
import net.sf.jaer.eventprocessing.FilterChain;
import net.sf.jaer.eventprocessing.filter.RefractoryFilter;
import net.sf.jaer.graphics.AEViewer;
import net.sf.jaer.graphics.FrameAnnotater;
/**
 * Allows control of remote CUDA process for filtering jAER data.
 *<p>
 * This filter does not process events at all. Rather it opens a TCP stream socket connection to a remote (maybe on the same machine)
 * process which is a CUDA GPGPU program which accepts AEs from jAER and which sends its output back to jAER for visualization.
 * <p>
 * The stream socket works with text command lines as though it were a terminal.  
 *
 * 
 * @author tobi/yingxue/jay
 */
public class CUDAObjectTrackerControl extends EventFilter2D implements FrameAnnotater {
    public final int CONTROL_PORT_DEFAULT=9998;
    AEViewer outputViewer=null;
    private int controlPort=getPrefs().getInt("CUDAObjectTrackerControl.controlPort", CONTROL_PORT_DEFAULT);
    private int recvOnPort=getPrefs().getInt("CUDAObjectTrackerControl.inputPort", 10012);
    private int sendToPort=getPrefs().getInt("CUDAObjectTrackerControl.outputPort", 10000);
    private String hostname=getPrefs().get("CUDAObjectTrackerControl.hostname", "localhost");
    private String cudaExecutablePath=getPrefs().get("CUDAObjectTrackerControl.cudaExecutablePath", null);
    private String cudaEnvironmentPath=getPrefs().get("CUDAObjectTrackerControl.cudaEnvironmentPath", null);
    private DatagramSocket controlSocket=null;
    InetAddress cudaInetAddress=null;
    private boolean cudaEnabled=getPrefs().getBoolean("CUDAObjectTrackerControl.cudaEnabled", true);
    Process cudaProcess=null;
    ProcessBuilder cudaProcessBuilder=null;
    AEUnicastOutput unicastOutput;
    AEUnicastInput unicastInput;
    CUDAChip cudaChip = null;
    CUDAChip.CUDAExtractor cudaExtractor=null;
    volatile private int inhibSpikeCount=0; // to annotate output

    /*
     *
    // from config.h in CUDA code
    #define MEMBRANE_TAU			10000.0F	// membrane time constant
    #define MEMBRANE_THRESHOLD		100.0F		// membrane threshold
    #define MEMBRANE_POTENTIAL_MIN	-50.0F		// membrane equilibrium potential

    #define MIN_FIRING_TIME_DIFF	15000		// low pass filter the events from retina

    #define E_I_NEURON_POTENTIAL	10.0		// excitatory to inhibitory synaptic weight
    #define I_E_NEURON_POTENTIAL	10.0		// inhibitory to excitatory synaptic weight

     */
    // TODO these cuda parameters better handled by indexed properties, but indexed properties not handled in FilterPanel yet
    final String CMD_THRESHOLD = "threshold";
    private float threshold = getPrefs().getFloat("CUDAObjectTrackerControl.threshold",100);
    final String CMD_MEMBRANE_TAU = "membraneTau";
    private float membraneTauUs = getPrefs().getFloat("CUDAObjectTrackerControl.membraneTauUs",10000);
    final String CMD_MEMBRANE_POTENTIAL_MIN = "membranePotentialMin";
    private float membranePotentialMin = getPrefs().getFloat("CUDAObjectTrackerControl.membranePotentialMin",-50);
    final String CMD_MIN_FIRING_TIME_DIFF = "minFiringTimeDiff";
    private float minFiringTimeDiff = getPrefs().getFloat("CUDAObjectTrackerControl.minFiringTimeDiff",15000);
    final String CMD_E_I_NEURON_POTENTIAL = "eISynWeight";
    private float eISynWeight = getPrefs().getFloat("CUDAObjectTrackerControl.eISynWeight",10);
    final String CMD_I_E_NEURON_POTENTIAL = "iESynWeight";
    private float iESynWeight = getPrefs().getFloat("CUDAObjectTrackerControl.iESynWeight",10);
    final String CMD_EXIT = "exit";
    private final String CMD_DEBUG_LEVEL = "debugLevel";
    private int debugLevel = getPrefs().getInt("CUDAObjectTrackerControl.debugLevel",1);
    private final String CMD_MAX_XMIT_INTERVAL_MS = "maxXmitIntervalMs";
    private int maxXmitIntervalMs = getPrefs().getInt("CUDAObjectTrackerControl.maxXmitIntervalMs",20);
    final String CMD_CUDA_ENABLED = "cudaEnabled";
    final String CMD_DELTA_TIME_US = "deltaTimeUs";
    private int deltaTimeUs = getPrefs().getInt("CUDAObjectTrackerControl.deltaTimeUs",1000);
    private final String CMD_NUM_OBJECTS = "numObject";
    private int numObject = getPrefs().getInt("CUDAObjectTrackerControl.numObject",5);
//     final String CMD_TERMINATE_IMMEDIATELY="terminate";
    final String CMD_KERNEL_SHAPE = "kernelShape";
//     final String CMD_SPIKE_PARTITIONING_METHOD="spikePartitioningMethod";
    private String CMD_CUDAS_RECVON_PORT="inputPort"; // swapped here because these are CUDAs
    private String CMD_CUDAS_SENDTO_PORT="outputPort";
    private boolean loopbackTestEnabled=getPrefs().getBoolean("CUDAObjectTrackerControl.loopbackTestEnabled", false);
    final String CMD_LOOPBACK_TEST_ENABLED="loopbackEnabled";

    public void annotate(float[][][] frame) {
    }

    public void annotate(Graphics2D g) {
    }

    public void annotate(GLAutoDrawable drawable) {
        if(cudaExtractor==null) return;
{'fraction_non_alphanumeric': 0.08600293584319144, 'fraction_numerical': 0.012088766082376306, 'mean_word_length': 5.325505188421627, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4503656', 'n_tokens_mistral': 3729, 'n_tokens_neox': 3519, 'n_words': 819}