Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csharp typemap and examples #238

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 96 additions & 0 deletions examples/undocumented/csharp_modular/Load.cs
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;

using org.shogun;
using org.jblas;

public class Load
{
public static DoubleMatrix load_numbers(string filename)
{
DoubleMatrix result = null;
try
{
DoubleMatrix temp = DoubleMatrix.loadAsciiFile(filename);
result = temp.reshape(2, temp.Length / 2);
}
catch(java.io.IOException e)
{
Console.WriteLine("Unable to create matrix from " + filename + ": " + e.Message);
Environment.Exit(-1);
}
return result;
}
public static DoubleMatrix load_labels(string filename)
{
DoubleMatrix result = null;
try
{
DoubleMatrix temp = DoubleMatrix.loadAsciiFile(filename);
result = temp.reshape(1, temp.Length);
}
catch(java.io.IOException e)
{
Console.WriteLine("Unable to create matrix from " + filename + ": " + e.Message);
Environment.Exit(-1);
}
return result;
}
public static string[] load_dna(string filename)
{
List<string> list = new List<string>();
string[] result = null;
try
{
FileInputStream fstream = new FileInputStream(filename);
DataInputStream @in = new DataInputStream(fstream);
BufferedReader buffer = new BufferedReader(new InputStreamReader(@in));
string line;
while((line = buffer.readLine()) != null)
{
list.Add(line);
}
@in.close();
result = new string[list.Count];
for (int i = 0; i < list.Count; i++)
{
result[i] = (string)list[i];
}
}
catch(java.io.IOException e)
{
Console.WriteLine("Unable to create matrix from " + filename + ": " + e.Message);
Environment.Exit(-1);
}
return result;
}

public static string[] load_cubes(string filename)
{
List<string> list = new List<string>();
string[] result = null;
try
{
FileInputStream fstream = new FileInputStream(filename);
DataInputStream @in = new DataInputStream(fstream);
BufferedReader buffer = new BufferedReader(new InputStreamReader(@in));
string line;
while((line = buffer.readLine()) != null)
{
list.Add(line);
}
@in.close();
result = new string[list.Count];
for (int i = 0; i < list.Count; i++)
{
result[i] = (string)list[i];
}
}
catch(java.io.IOException e)
{
Console.WriteLine("Unable to create matrix from " + filename + ": " + e.Message);
Environment.Exit(-1);
}
return result;
}
}
26 changes: 26 additions & 0 deletions examples/undocumented/csharp_modular/MatrixTest.cs
@@ -0,0 +1,26 @@
using System;

using org.shogun;
using org.jblas;

public class MatrixTest
{
static MatrixTest()
{
// The library is specified in the 'DllImport' attribute for .NET:
// System.loadLibrary("modshogun");
}

static void Main(string[] argv)
{
modshogun.init_shogun();
Console.WriteLine("Test DoubleMatrix(jblas):");
RealFeatures x = new RealFeatures();
double[][] y = { new double[] { 1, 2 }, new double[] { 3, 4 }, new double[] { 5, 6 } };
DoubleMatrix A = new DoubleMatrix(y);
x.set_feature_matrix(A);
DoubleMatrix B = x.get_feature_matrix();
Console.WriteLine(B.ToString());
modshogun.exit_shogun();
}
}
25 changes: 25 additions & 0 deletions examples/undocumented/csharp_modular/VectorTest.cs
@@ -0,0 +1,25 @@
using System;

using org.shogun;
using org.jblas;

public class VectorTest
{
static VectorTest()
{
// The library is specified in the 'DllImport' attribute for .NET:
// System.loadLibrary("modshogun");
}

static void Main(string[] argv)
{
modshogun.init_shogun_with_defaults();

double[][] y = { new double[] { 1, 2, 3, 4 } };
DoubleMatrix A = new DoubleMatrix(y);
Labels x = new Labels(A);
DoubleMatrix B = x.get_labels();
Console.WriteLine(B.ToString());
modshogun.exit_shogun();
}
}
36 changes: 36 additions & 0 deletions examples/undocumented/csharp_modular/check.sh
@@ -0,0 +1,36 @@
#!/bin/bash
if [ -z "${JAVA}" ]
then
JAVAC=javac
JAVA=java
fi

args=$#
if [ $args -gt 1 ]; then
echo "Usage: ./check.sh check all examples.
./check.sh name.cs check one example."
exit 0
fi

export CLASSPATH=/usr/share/java/jblas.jar:../../../src/interfaces/java_modular/shogun.jar:.
export LD_LIBRARY_PATH=../../../src/shogun:../../../src/interfaces/java_modular

if [ $args -eq 0 ]; then
${JAVAC} Load.cs
${JAVAC} $(ls *.cs | sed 's/Load.cs//')

for filename in *.class
do
[ "$filename" = "Load.class" ] && continue
echo "running ${filename%.class} .."
${JAVA} ${filename%.class} >/dev/null
done
fi

if [ $args -eq 1 ]; then
arg=("$@")
${JAVAC} Load.cs
${JAVAC} ${arg[0]}
${JAVA} ${arg[0]%.cs} >/dev/null
fi

@@ -0,0 +1,54 @@
using System.Collections;

using org.shogun;
using org.jblas;

public class classifier_averaged_perceptron_modular
{
static classifier_averaged_perceptron_modular()
{
// The library is specified in the 'DllImport' attribute for .NET:
// System.loadLibrary("modshogun");
}

public ArrayList parameter_list = new ArrayList(2);
public classifier_averaged_perceptron_modular()
{
parameter_list.Add(Arrays.asList(new double?(10), new int?(1000)));
parameter_list.Add(Arrays.asList(new double?(10), new int?(10)));
}
public virtual Serializable run(IList para)
{
modshogun.init_shogun_with_defaults();
double learn_rate = (double)((double?)para[0]);
int max_iter = (int)((int?)para[1]);

DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix testdata_real = Load.load_numbers("../data/fm_test_real.dat");

DoubleMatrix trainlab = Load.load_labels("../data/label_train_twoclass.dat");
RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
Labels labels = new Labels(trainlab);
AveragedPerceptron perceptron = new AveragedPerceptron(feats_train, labels);
perceptron.set_learn_rate(learn_rate);
perceptron.set_max_iter(max_iter);
perceptron.train();

perceptron.set_features(feats_test);
DoubleMatrix out_labels = perceptron.apply().get_labels();
ArrayList result = new ArrayList();
result.Add(perceptron);
result.Add(out_labels);

modshogun.exit_shogun();
return result;
}
static void Main(string[] argv)
{
classifier_averaged_perceptron_modular x = new classifier_averaged_perceptron_modular();
x.run((IList)x.parameter_list[0]);
}
}
@@ -0,0 +1,39 @@
using org.shogun;
using org.jblas;
// This Java 'import static' statement cannot be converted to .NET:
import static org.shogun.EAlphabet.DNA;

public class classifier_domainadaptationsvm_modular
{
static classifier_domainadaptationsvm_modular()
{
// The library is specified in the 'DllImport' attribute for .NET:
// System.loadLibrary("modshogun");
}

static void Main(string[] argv)
{
modshogun.init_shogun_with_defaults();
int degree = 3;
int C = 1;

string[] fm_train_dna = {"CGCACGTACGTAGCTCGAT", "CGACGTAGTCGTAGTCGTA", "CGACGGGGGGGGGGTCGTA", "CGACCTAGTCGTAGTCGTA", "CGACCACAGTTATATAGTA", "CGACGTAGTCGTAGTCGTA", "CGACGTAGTTTTTTTCGTA", "CGACGTAGTCGTAGCCCCA", "CAAAAAAAAAAAAAAAATA", "CGACGGGGGGGGGGGCGTA"};
string[] fm_test_dna = {"AGCACGTACGTAGCTCGAT", "AGACGTAGTCGTAGTCGTA", "CAACGGGGGGGGGGTCGTA", "CGACCTAGTCGTAGTCGTA", "CGAACACAGTTATATAGTA", "CGACCTAGTCGTAGTCGTA", "CGACGTGGGGTTTTTCGTA", "CGACGTAGTCCCAGCCCCA", "CAAAAAAAAAAAACCAATA", "CGACGGCCGGGGGGGCGTA"};

StringCharFeatures feats_train = new StringCharFeatures(fm_train_dna, DNA);
StringCharFeatures feats_test = new StringCharFeatures(fm_test_dna, DNA);

WeightedDegreeStringKernel kernel = new WeightedDegreeStringKernel(feats_train, feats_train, degree);
double[][] label_train_dna = { new double[] { -1, -1, -1, -1, -1, 1, 1, 1, 1, 1 } };
Labels labels = new Labels(new DoubleMatrix(label_train_dna));

SVMLight svm = new SVMLight(C, kernel, labels);
svm.train();

DomainAdaptationSVM dasvm = new DomainAdaptationSVM(C, kernel, labels, svm, 1.0);
dasvm.train();

DoubleMatrix @out = dasvm.apply(feats_test).get_labels();
modshogun.exit_shogun();
}
}
@@ -0,0 +1,36 @@
using System;

using org.shogun;
using org.jblas;

public class classifier_gaussiannaivebayes_modular
{
static classifier_gaussiannaivebayes_modular()
{
// The library is specified in the 'DllImport' attribute for .NET:
// System.loadLibrary("modshogun");
}

static void Main(string[] argv)
{
modshogun.init_shogun_with_defaults();

DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix testdata_real = Load.load_numbers("../data/fm_test_real.dat");

DoubleMatrix trainlab = Load.load_labels("../data/label_train_multiclass.dat");

RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);
Labels labels = new Labels(trainlab);

GaussianNaiveBayes gnb = new GaussianNaiveBayes(feats_train, labels);
gnb.train();
DoubleMatrix out_labels = gnb.apply(feats_test).get_labels();
Console.WriteLine(out_labels.ToString());

modshogun.exit_shogun();
}
}
44 changes: 44 additions & 0 deletions examples/undocumented/csharp_modular/classifier_gmnpsvm_modular.cs
@@ -0,0 +1,44 @@
using System;

using org.shogun;
using org.jblas;

public class classifier_gmnpsvm_modular
{
static classifier_gmnpsvm_modular()
{
// The library is specified in the 'DllImport' attribute for .NET:
// System.loadLibrary("modshogun");
}

static void Main(string[] argv)
{
modshogun.init_shogun_with_defaults();
double width = 2.1;
double epsilon = 1e-5;
double C = 1.0;

DoubleMatrix traindata_real = Load.load_numbers("../data/fm_train_real.dat");
DoubleMatrix testdata_real = Load.load_numbers("../data/fm_test_real.dat");

DoubleMatrix trainlab = Load.load_labels("../data/label_train_multiclass.dat");

RealFeatures feats_train = new RealFeatures();
feats_train.set_feature_matrix(traindata_real);
RealFeatures feats_test = new RealFeatures();
feats_test.set_feature_matrix(testdata_real);

GaussianKernel kernel = new GaussianKernel(feats_train, feats_train, width);

Labels labels = new Labels(trainlab);

GMNPSVM svm = new GMNPSVM(C, kernel, labels);
svm.set_epsilon(epsilon);
svm.train();
kernel.init(feats_train, feats_test);
DoubleMatrix out_labels = svm.apply(feats_test).get_labels();
Console.WriteLine(out_labels.ToString());

modshogun.exit_shogun();
}
}