Category Archives: C#

Using Weka’s Data-Mining and Machine-Learning JAVA API algorithms from within C#

Sometimes you dont realy want to create ML algorithms from scratch. you just want to use them! unfortunantly not all of them are written for specific languages, for example, you can get an Information Gain / ID3 algorithm written in C#. However, you cant use it with numeric features in a multi-class problem. so the next best thing is to use weka’s IG which is written in Java (like all of weka), and if you dig enough on the weka wiki you will find that it is possible to use weka.jar from with in C#.
I have recently updated the wiki to reflect the proper workflow, and i managed to use a meta-classfier with SVM (SMO) using IG as the evaluator and Ranker as the search algorithm, in a 10 cross fold validation scenario.

details are here: http://weka.wikispaces.com/IKVM+with+Weka+tutorial

C# support for the Shogun Toolbox (SVM)

Part of my brain research and machine learning endeavors is to use SVM for the purpose of training and/or testing in real time. given the huge amount of temporal brain data that we acquire in real-time,  there is a need for a large scale SVM algorithm. usually SVM can handle thousands of samples with thousands of features, and with a cutting edge PC  you may be able to scale it up to ~10  times as much. but using shogun you can process more data, the shogun team reports up to 10 millions training samples and up to 7 billion test samples.

A few months back shogun did not support C# . Daniel Korn and I made some advances to create something called an interface file for shogun, that will make it work with C#.  half way through the process one of the shogun participants in google summer of code took control of the interface programming and finished it. with this new release there is a C# dll that everyone can use, and in it there is support for many data types.  i still need to figure out if it will support sparse lists/dictionaries so that the memory footprint will be small enough for any algorithm. with the new dll there are many C# examples files that we have created to test out the new interface/dll.

 

shogun 1.1.0 can be downloaded here.

our credits appear in the NEWS section of the release and on the front page.

Features:
  • New dimensionality reduction algorithms: Diffusion Maps, Kernel Locally Linear Embedding, Kernel Local Tangent Space Alignment, Linear Local Tangent Space Alignment, Neighborhood Preserving embedding, Locality Preserving Projections.
  • Various performance improvements for dimensionality reduction methods (BLAS, alignment formulation of the LLE, ..)
  • Automatical k determination mode for Locally Linear Embedding dimension reduction method based on reconstruction error.
  • ARPACK and SUPERLU integration.
  • Introduce the concept of Converters that can embed (arbitrary) feature types into different feature types.
  • LibSVM is now pthread-parallelized.
  • Create modshogun.dll for csharp.
  • Various new c# examples (thanks Daniel Korn and Ori Cohen).
  • Dimensionality reduction examples application is introduced

 

 

 

 

Controlling an avatar by thought using fMRI

Cohen O., 1, 3 


Mendelsohn A., 2
Drai D., 1
Malach R., 2
Friedman D. 1
1 The Interdisciplinary Center Herzliya,
2 Weizmann Institute of Science
3 Bar Ilan University.

We are carrying out a number of studies whereby subjects control the movement of an avatar by thought alone, using real-time fMRI. At the first stage of our study five three subjects were able to control right- and left-hand movements of an avatar using motor imagery without difficulty and with high accuracy. Therefore, we are running a study involving three classes: right-hand imagery to turn right, left-hand imagery to turn left, and feet imagery to move forward.
In the first part of the experiment we instruct the subject to imagine either left-hand, right-hand, or walking movements upon a predefined cue, and manually define regions of interest (ROIs), using a GLM analysis, by contrasting hand-motor and primary leg-motor regions. In the second phase (baseline) we compute the mean and standard deviation for each ROI. Finally, the subjects are instructed to move the avatar, according to auditory cues, by using motor imagery. At this preliminary stage we use a simple classification scheme; at each TR the system calculates the z-score value of each ROI relative to the mean values obtained during the baseline, and chooses the highest z-score value among the ROIs. The classification result is fed into the virtual reality system to move the avatar accordingly.
Our preliminary results indicate that subjects can learn to control an avatar using motor imagery in better-than-chance levels with very little training. Future work will include support vector machine (SVM) classifiers, with methods for feature reduction, in real time. Eventually we aim at allowing subjects to perform simple tasks in a virtual environment.

Unity3D for research, how to compile-run-debug outside of Unity3D by using Visual C#

I use unity for research purposes, and 90% of my code is not unity. it became quite tedious to work with unity as the debugger. as some of you know unity compiles the code on its own and then runs it, debugging usually means putting console outputs to see if something works. i had to figure out how i can debug outside of unity preferably VS with but still be able to use unity as the rendering engine, but only when i needed it to. consider the following: every line you write has nothing to do with unity, except when you need a character to move. obviously you would like to debug the entire thing without going into unity, and only later to use it to move the character. The following is one method of bypassing Unity3D as a debugger by using Visual Studio.

Using VC# we create another solution outside of the unity project directory, this solution/project will be the one that we are working on and debugging from.

So basically:

  1. unity3d_directory\game
  2. unity3d_directory\testGame <= this is our project

Then we need to add these to our solution references (find them in the unity installation dir):

  1. the actual “game” solution file
  2. UnityEngine.dll
  3. UnityEditor.dll
  4. UnityScript.dll

Then add the Dlls to the references of actual unity game solution (they may already be there).
This takes care of a bunch of errors that relate to the code not finding the unity API.

Then for every class that inherits from MonoBehaviour, we want to add this preprocessor directive, it means that inside (DEBUG) VS we do not use the MonoBehaviour and outside (Unity) we do:

instead of:

public class MyClass: MonoBehaviour
{//class code}

this:

public class MyClass
   #if !DEBUG
      :MonoBehaviour
   #endif
{//class code}

Before we continue we need to understand a little bit about how unity interfaces with a class

  1. It first runs the “Awake()” function, we can consider this awake function to be the constructor, note that a real constructor will always be run when Unity loads a class (even before it runs it). so its better to use Awake inside Unity.
  2. After all awake methods are run, Unity runs “Start()” function before “Update()” but only once. this is also a type of constructor, but it runs after the Awake one.
  3. Then Unity calls “Update()”

so basically what we want to do in VC# is to call the constructor instead of Awake() or Start() (i use either one of them). but make sure that we can still use this code inside unity (remember that we dont want constructors inside unity).

so here is a typical method of enabling only the constructor inside VC# and only the Awake/Start functions inside Unity. what you see here is a preprocessor directive telling visual studio to ignore the “start()” line if we are in debugmode, which means we get the constructor functionality, and it also tells unity to ignore the constructor line and think of this function as awake(). cool isnt it ?

public class MyClass
   #if !DEBUG
      : MonoBehaviour
   #endif
{
   //  this function behaves as Start() functionality inside Unity as a constructor inside VC#
   #if !DEBUG
      public void Start()
   #else
      public MyClass()
   #endif
      {//code goes here}
}

So we handled our constructor/awake/start problem. now we address another problem of running actual code . your class should have an Update() function that runs your code, and this will be the main functionality function in each class.

public class MyClass
   #if !DEBUG
      : MonoBehaviour
   #endif
{
   //  this function behaves as Start() functionality inside Unity as a constructor inside VC#
   #if !DEBUG
      public void Start()
   #else
      public MyClass()
   #endif
      {//code goes here}

   public Update()
   {
      // here goes the main code of our class functionality
   }
}

Now lets say that we want a function that outputs to the console in unity when inside unity and output text to the dos window when inside VC#.

public class MyClass
   #if !DEBUG
      : MonoBehaviour
   #endif
{
   //  this function behaves as Start() functionality inside Unity as a constructor inside VC#
   #if !DEBUG
      public void Start()
   #else
      public MyClass()
   #endif
      {//code goes here}

   public Update()
   {
      // here goes the main code of our class functionality
   }

   public static void print(string s)
   {
      #if !DEBUG
         Debug.Log(s); //Unity
      #else
         Console.WriteLine(s); //C#
      #endif
   }
}

And what about a function that prints to Dos or Unity Gui?

public class MyClass
   #if !DEBUG
      : MonoBehaviour
   #endif
{
   //  this function behaves as Start() functionality inside Unity as a constructor inside VC#
   #if !DEBUG
      public void Start()
   #else
      public MyClass()
   #endif
      {//code goes here}

   public Update()
   {
      // here goes the main code of our class functionality
   }

   public static void print(string s)
   {
      #if !DEBUG
         Debug.Log(s); //Unity
      #else
         Console.WriteLine(s); //C#
      #endif
   }

   private void PrintToGUI()
   {
      #if !DEBUG
         GUI.guiText.text = "Unity";
      #else
         Console.WriteLine("Visual C#");
      #endif
   }
}

typically you would assign this script to a unity object, but because we are using VC#, we need to emulate how unity runs these classes, from our testGame project we create instances for these classes, our constructors will initialize it. if you are inside Unity, Unity will run awake() and then Start() when instanced. then we create a loop that runs all the Update() functions.

namespace testGame
{
   class Test
   {
      static void Main(string[] args)
      {
          MyClass mc = new MyClass();
          while (true)
          {
             mc.Update();
          }
       }
   }   
}