How to generate random numbers with uniform distribution in Java? - java

So, i'm having trouble generating random numbers with uniform distribution in java, given the maximum and the minimun value of some attributes in some data set (Iris from UCI for machine learning). What i have is iris dataset, in some 2-d-array called samples. I put the random values according to the maximun and the minimun value of each attribute in iris data set (without the class attribute) in a 2-d-array called gworms (which has some extra fields for some other values of the algorithm).
So far, the full algorithm is not working properly, and my thoughts are in the fact that maybe the gworms (the points in 4-d space) are not generating correctly or with a good randomness. I think that the points are to close to each other (this i think because of some results obtained later whose code is not shown here). So, i'm asking for your help to validate this code in which i implement "uniform distribution" for gworms (for de first 4 positions):
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package glowworms;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Random;
import weka.core.AttributeStats;
import weka.core.Instances;
/**
*
* #author oscareduardo937
*/
public class GSO {
/* ************ Initializing parameters of CGSO algorithm ******************** */
int swarmSize = 1000; // Swarm size m
int maxIte = 200;
double stepSize = 0.03; // Step size for the movements
double luciferin = 5.0; // Initial luciferin level
double rho = 0.4; // Luciferin decay parameter
double gamma = 0.6; // Luciferin reinforcement parameter
double rs = 0.38; // Initial radial sensor range. This parameter depends on the data set and needs to be found by running experiments
double gworms[][] = null; // Glowworms of the swarm.
/* ************ Initializing parameters of clustering problem and data set ******************** */
int numAtt; // Dimension of the position vector
int numClasses; // Number of classes
int total_data; //Number of instances
int threshold = 5;
int runtime = 1;
/*Algorithm can be run many times in order to see its robustness*/
double minValuesAtts[] = new double[this.numAtt]; // Minimum values for all attributes
double maxValuesAtts[] = new double[this.numAtt]; // Maximum values for all attributes
double samples[][] = new double[this.total_data][this.numAtt]; //Samples of the selected dataset.
ArrayList<Integer> candidateList;
double r;
/*a random number in the range [0,1)*/
/* *********** Method to put the instances in a matrix and get max and min values for attributes ******************* */
public void instancesToSamples(Instances data) {
this.numAtt = data.numAttributes();
System.out.println("********* NumAttributes: " + this.numAtt);
AttributeStats attStats = new AttributeStats();
if (data.classIndex() == -1) {
//System.out.println("reset index...");
data.setClassIndex(data.numAttributes() - 1);
}
this.numClasses = data.numClasses();
this.minValuesAtts = new double[this.numAtt];
this.maxValuesAtts = new double[this.numAtt];
System.out.println("********* NumClasses: " + this.numClasses);
this.total_data = data.numInstances();
samples = new double[this.total_data][this.numAtt];
double[] values = new double[this.total_data];
for (int j = 0; j < this.numAtt; j++) {
values = data.attributeToDoubleArray(j);
for (int i = 0; i < this.total_data; i++) {
samples[i][j] = values[i];
}
}
for(int j=0; j<this.numAtt-1; j++){
attStats = data.attributeStats(j);
this.maxValuesAtts[j] = attStats.numericStats.max;
this.minValuesAtts[j] = attStats.numericStats.min;
//System.out.println("** Min Value Attribute " + j + ": " + this.minValuesAtts[j]);
//System.out.println("** Max Value Attribute " + j + ": " + this.maxValuesAtts[j]);
}
//Checking
/*for(int i=0; i<this.total_data; i++){
for(int j=0; j<this.numAtt; j++){
System.out.print(samples[i][j] + "** ");
}
System.out.println();
}*/
} // End of method InstancesToSamples
public void initializeSwarm(Instances data) {
this.gworms = new double[this.swarmSize][this.numAtt + 2]; // D-dimensional vector plus luciferin, fitness and intradistance.
double intraDistance = 0;
Random r = new Random(); //Random r;
for (int i = 0; i < this.swarmSize; i++) {
for (int j = 0; j < this.numAtt - 1; j++) {
//Uniform randomization of d-dimensional position vector
this.gworms[i][j] = this.minValuesAtts[j] + (this.maxValuesAtts[j] - this.minValuesAtts[j]) * r.nextDouble();
}
this.gworms[i][this.numAtt - 1] = this.luciferin; // Initial luciferin level for all swarm
this.gworms[i][this.numAtt] = 0; // Initial fitness for all swarm
this.gworms[i][this.numAtt + 1] = intraDistance; // Intra-distance for gworm i
}
//Checking gworms
/*for(int i=0; i<this.swarmSize; i++){
for(int j=0; j<this.numAtt+2; j++){
System.out.print(gworms[i][j] + "** ");
}
System.out.println();
}*/
} // End of method initializeSwarm
}
The main class is this one:
package uniformrandomization;
/**
*
* #author oscareduardo937
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import weka.core.Instances;
import glowworms.GSO;
public class UniformRandomization {
public UniformRandomization(){
super();
}
//Loading the data from the filename file to the program. It can be .arff or .csv
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;
try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);
}
return inputReader;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
BufferedReader datafile1 = readDataFile("src/data/iris.arff");
Instances data = new Instances(datafile1);
GSO gso = new GSO();
gso.instancesToSamples(data);
gso.initializeSwarm(data);
System.out.println("Fin...");
}
}
So i want to know if with this code, the numbers of the position ij of the gworms are generating within the range of max value and min value for attribute j.
Thanks so much in advanced.

Related

Can I write this code without using serialization?

For my project I was wondering whether there is a way I can do this assignment without using serialization. Here are the guidelines to the project and the code I already have together:
The Canadian Forest Service wants to do a simple simulation of the growth and pruning of forests. Each forest has a name and exactly 10 trees. The trees are planted when they are 1' to 5' tall, and each tree has a individual growth rate of 50%-100% per year. For the simulation new trees are constructed randomly within these bounds. A forest is reaped (by lumberjacks) on demand - all trees above a specifed height are cut down and replaced with new trees.
The user interface to the simulation must allow the user to:
Display the current forest (with tree heights to 2 decimal places)
Discard the current forest and create a new forest
Simulate a year's growth in the current forest
Reap the current forest of trees over a user specified height, replacing the reaped trees with random new trees.
Save the information about the current forest to file (named after the forest)
Discard the current forest and load the information about a forest from a file.
Class1
import java.io.*;
import java.util.*;
public class Forest{
//constants
private static final int MAX_NUM_TREES = 10;
//variables
int index;
private String name;
private Tree[] arrayOfTrees;
public Forest(String forestName){
//Constructor class that takes a name and creates an array of trees().
index = 0;
name = forestName;
arrayOfTrees = new Tree[MAX_NUM_TREES];
for(index = 0; index < arrayOfTrees.length; index++){
arrayOfTrees[index] = new Tree();
}
}
public void display(){
// displays the array of trees and the index
index = 0;
if(name != null){
System.out.println(name);
for(index = 0; index < arrayOfTrees.length; index ++){
System.out.printf("%2d : %s\n", (index + 1), arrayOfTrees[index]);
}
}else{
System.out.println("No forest.");
}
}
public void yearGrowth(){
//grows each tree in the array
index = 0;
for(index = 0; index < arrayOfTrees.length ; index ++){
arrayOfTrees[index].grow();
}
}
public void reap(int reapHeight){
//reaps the trees and prints out the old and new information
index = 0;
for(index = 0; index < arrayOfTrees.length; index++){
if(arrayOfTrees[index].getHeight() >= reapHeight){
System.out.println("Cut " + (index+1) + " : " + arrayOfTrees[index] );
arrayOfTrees[index] = new Tree();
System.out.println("New " + (index+1) + " : " + arrayOfTrees[index] );
}
}
}
public static void saveForest(Forest forest) throws IOException {
//saves the forest
String name = forest.getName();
ObjectOutputStream toStream;
toStream = new ObjectOutputStream(new FileOutputStream(name));
toStream.writeObject(forest);
toStream.close();
}
public static Forest loadForest(String fileName) throws IOException {
//loads the forest
ObjectInputStream fromStream = null;
Forest local;
fromStream = new ObjectInputStream(new FileInputStream(fileName));
try {
local = (Forest)fromStream.readObject();
}catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
return(null);
}finally{
try {
if (fromStream != null) {
fromStream.close();
}
} catch (IOException e) {
System.out.println(e.getMessage());
return(null);
}
}
return(local);
}
public String getName(){
return (name);
}
}
Class2
import java.util.Random;
import java.util.*;
import java.io.*;
public class Tree{
//creates the variables as the
private double height;
private double growthRate;
private static Random rand = new Random();
final double MIN_HEIGHT = 1;
final double MIN_GROWTH_RATE = 0.5;
final double MAX_HEIGHT = 5;
final double MAX_GROWTH_RATE = 1.0;
public Tree() {
//creates tree with a height and a growth rate
Random rand = new Random();
height = (MIN_HEIGHT + ((Math.random() * (MAX_HEIGHT - MIN_HEIGHT))));
growthRate = (MIN_GROWTH_RATE + (Math.random() * (MAX_GROWTH_RATE - MIN_GROWTH_RATE)));
}
public double grow(){
//tree grows and returns height
height = height * (1 + growthRate);
return height;
}
public double getHeight(){
return (height);
}
public double getGrowthRate(){
return (growthRate);
}
public String toString(){
//toString formats the output with height and growthrate
return (String.format("%7.2f (%2d%% pa)", height, ((int)(growthRate * 100))));
}
}
If by serialization you understand standard java serialization with ObjectXXXStream, then yes, you can avoid it.
If you mean serialization in more broad way, then no. Files cant directly store java objects you have to convert them to bytes (which is serialization by definition).
PS: If you actually ask "How?" you should include it in your question.

How to compile a .java file, which tools and datas need?

I have this .java datafile. The data file is a part of an imagej plugin.
The whole data structure is here:
enter link description here
package mosaic.plugins;
import ij.IJ;
import ij.ImagePlus;
import ij.macro.Interpreter;
import ij.measure.ResultsTable;
import ij.process.ByteProcessor;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import mosaic.plugins.utils.PlugIn8bitBase;
import net.imglib2.Cursor;
import net.imglib2.IterableInterval;
import net.imglib2.RandomAccess;
import net.imglib2.img.ImagePlusAdapter;
import net.imglib2.img.Img;
import net.imglib2.img.ImgFactory;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.NumericType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;
public class Naturalization extends PlugIn8bitBase
{
// Precision in finding your best T
private static final float EPS = 0.0001f;
// Prior parameter for first oder
// In this case is for all channels
// Fixed parameter
private static final float T1_pr = 0.3754f;
// Number of bins for the Laplacian Histogram
// In general is 4 * N_Grad
// max of laplacian value is 4 * 255
private static final int N_Lap = 2041;
// Offset shift in the histogram bins
// Has to be N_Lap / 2;
private static final int Lap_Offset = 1020;
// Number of bins for the Gradient
private static final int N_Grad = 512;
// Offset for the gradient histogram shift
private static final int Grad_Offset = 256;
// Prior parameter for second order (Parameters learned from trained data set)
// For different color R G B
// For one channel image use an average of them
private final float T2_pr[] = {0.2421f ,0.2550f, 0.2474f, 0.24816666f};
// Keeps values of PSNR for all images and channels in case of RGB. Maps: imageNumber -> map (channel, PSNR value)
private final Map<Integer, Map<Integer, Float>> iPsnrOutput = new TreeMap<Integer, Map<Integer, Float>>();
private synchronized void addPsnr(int aSlice, int aChannel, float aValue) {
Map<Integer, Float> map = iPsnrOutput.get(aSlice);
boolean isNewMap = false;
if (map == null) {
map = new TreeMap<Integer, Float>();
isNewMap = true;
}
map.put(aChannel, aValue);
if (isNewMap) {
iPsnrOutput.put(aSlice, map);
}
}
#Override
protected void processImg(ByteProcessor aOutputImg, ByteProcessor aOrigImg, int aChannelNumber) {
// perform naturalization
final ImagePlus naturalizedImg = naturalize8bitImage(aOrigImg, aChannelNumber);
// set processed pixels to output image
aOutputImg.setPixels(naturalizedImg.getProcessor().getPixels());
}
#Override
protected void postprocessBeforeShow() {
// Create result table with all stored PSNRs.
final ResultsTable rs = new ResultsTable();
for (final Entry<Integer, Map<Integer, Float>> e : iPsnrOutput.entrySet()) {
rs.incrementCounter();
for (final Entry<Integer, Float> m : e.getValue().entrySet()) {
switch(m.getKey()) {
case CHANNEL_R: rs.addValue("Naturalization R", m.getValue()); rs.addValue("Estimated R PSNR", calculate_PSNR(m.getValue())); break;
case CHANNEL_G: rs.addValue("Naturalization G", m.getValue()); rs.addValue("Estimated G PSNR", calculate_PSNR(m.getValue())); break;
case CHANNEL_B: rs.addValue("Naturalization B", m.getValue()); rs.addValue("Estimated B PSNR", calculate_PSNR(m.getValue())); break;
case CHANNEL_8G: rs.addValue("Naturalization", m.getValue()); rs.addValue("Estimated PSNR", calculate_PSNR(m.getValue())); break;
default: break;
}
}
}
if (!Interpreter.isBatchMode()) {
rs.show("Naturalization and PSNR");
showMessage();
}
}
private ImagePlus naturalize8bitImage(ByteProcessor imp, int aChannelNumber) {
Img<UnsignedByteType> TChannel = ImagePlusAdapter.wrap(new ImagePlus("", imp));
final float T2_prior = T2_pr[(aChannelNumber <= CHANNEL_B) ? 2-aChannelNumber : CHANNEL_8G];
final float[] result = {0.0f}; // ugly but one of ways to get result back via parameters;
// Perform naturalization and store PSNR result. Finally return image in ImageJ format.
TChannel = performNaturalization(TChannel, T2_prior, result);
addPsnr(imp.getSliceNumber(), aChannelNumber, result[0]);
return ImageJFunctions.wrap(TChannel,"temporaryName");
}
/**
* Naturalize the image
* #param Img original image
* #param Theta parameter
* #param Class<T> Original image
* #param Class<S> Calculation Type
* #param T2_prior Prior to use
* #param result One element array to store nautralization factor
*/
private <T extends NumericType<T> & NativeType<T> & RealType<T>, S extends RealType<S>> Img<T> doNaturalization(Img<T> image_orig, S Theta,Class<T> cls_t, float T2_prior, float[] result) throws InstantiationException, IllegalAccessException
{
if (image_orig == null) {return null;}
// Check that the image data set is 8 bit
// Otherwise return an error or hint to scale down
final T image_check = cls_t.newInstance();
final Object obj = image_check;
if (!(obj instanceof UnsignedByteType)) {
IJ.error("Error it work only with 8-bit type");
return null;
}
final float Nf = findNaturalizationFactor(image_orig, Theta, T2_prior);
result[0] = Nf;
final Img<T> image_result = naturalizeImage(image_orig, Nf, cls_t);
return image_result;
}
private <S extends RealType<S>, T extends NumericType<T> & NativeType<T> & RealType<T>>
Img<T> naturalizeImage(Img<T> image_orig, float Nf, Class<T> cls_t)
throws InstantiationException, IllegalAccessException
{
// Mean of the original image
// S mean_original = cls_s.newInstance();
// Mean<T,S> m = new Mean<T,S>();
// m.compute(image_orig.cursor(), mean_original);
// TODO: quick fix for deprecated code above. Is new 'mean' utility introduced in imglib2?
float mean_original = 0.0f;
final Cursor<T> c2 = image_orig.cursor();
float count = 0.0f;
while (c2.hasNext()) {
c2.next();
mean_original += c2.get().getRealFloat();
count += 1.0f;
}
mean_original /= count;
// Create result image
final long[] origImgDimensions = new long[2];
image_orig.dimensions(origImgDimensions);
final Img<T> image_result = image_orig.factory().create(origImgDimensions, cls_t.newInstance());
// for each pixel naturalize
final Cursor<T> cur_orig = image_orig.cursor();
final Cursor<T> cur_ir = image_result.cursor();
while (cur_orig.hasNext()) {
cur_orig.next();
cur_ir.next();
final float tmp = cur_orig.get().getRealFloat();
// Naturalize
float Nat = (int) ((tmp - mean_original)*Nf + mean_original + 0.5);
if (Nat < 0)
{Nat = 0;}
else if (Nat > 255)
{Nat = 255;}
cur_ir.get().setReal(Nat);
}
return image_result;
}
private <S extends RealType<S>, T extends NumericType<T> & NativeType<T> & RealType<T>> float findNaturalizationFactor(Img<T> image_orig, S Theta, float T2prior) {
final ImgFactory<FloatType> imgFactoryF = new ArrayImgFactory<FloatType>();
// Create one dimensional image (Histogram)
final Img<FloatType> LapCDF = imgFactoryF.create(new long[] {N_Lap}, new FloatType());
// Two dimensional image for Gradient
final Img<FloatType> GradCDF = imgFactoryF.create(new long[] {N_Grad, 2}, new FloatType());
// GradientCDF = Integral of the histogram of the of the Gradient field
// LaplacianCDF = Integral of the Histogram of the Laplacian field
final Img<FloatType> GradD = create2DGradientField();
calculateLaplaceFieldAndGradient(image_orig, LapCDF, GradD);
convertGrad2dToCDF(GradD);
calculateGradCDF(GradCDF, GradD);
calculateLapCDF(LapCDF);
// For each channel find the best T1
// EPS=precision
// for X component
float T_tmp = (float)FindT(Views.iterable(Views.hyperSlice(GradCDF, GradCDF.numDimensions()-1 , 0)), N_Grad, Grad_Offset, EPS);
// for Y component
T_tmp += FindT(Views.iterable(Views.hyperSlice(GradCDF, GradCDF.numDimensions()-1 , 1)), N_Grad, Grad_Offset, EPS);
// Average them and divide by the prior parameter
final float T1 = T_tmp/(2*T1_pr);
// Find the best parameter and divide by the T2 prior
final float T2 = (float)FindT(LapCDF, N_Lap, Lap_Offset, EPS)/T2prior;
// Calculate naturalization factor!
final float Nf = (float) ((1.0-Theta.getRealDouble())*T1 + Theta.getRealDouble()*T2);
return Nf;
}
/**
* Calculate the peak SNR from the Naturalization factor
*
* #param Nf naturalization factor
* #return the PSNR
*/
String calculate_PSNR(double x)
{
if (x >= 0 && x <= 0.934)
{
return String.format("%.2f", new Float(23.65 * Math.exp(0.6 * x) - 20.0 * Math.exp(-7.508 * x)));
}
else if (x > 0.934 && x < 1.07)
{
return new String("> 40");
}
else if (x >= 1.07 && x < 1.9)
{
return String.format("%.2f", new Float(-11.566 * x + 52.776));
}
else
{
return String.format("%.2f",new Float(13.06*x*x*x*x - 121.4 * x*x*x + 408.5 * x*x -595.5*x + 349));
}
}
private Img<UnsignedByteType> performNaturalization(Img<UnsignedByteType> channel, float T2_prior, float[] result) {
// Parameters balance between first order and second order
final FloatType Theta = new FloatType(0.5f);
try {
channel = doNaturalization(channel, Theta, UnsignedByteType.class, T2_prior, result);
} catch (final InstantiationException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
}
return channel;
}
// Original data
// N = nuber of bins
// offset of the histogram
// T current
private double FindT_Evalue(float[] p_d, int N, int offset, float T)
{
double error = 0;
for (int i=-offset; i<N-offset; ++i) {
final double tmp = Math.atan(T*(i)) - p_d[i+offset];
error += (tmp*tmp);
}
return error;
}
// Find the T
// data CDF Histogram
// N number of bins
// Offset of the histogram
// eps precision
private double FindT(IterableInterval<FloatType> data, int N, int OffSet, float eps)
{
//find the best parameter between data and model atan(Tx)/pi+0.5
// Search between 0 and 1.0
float left = 0;
float right = 1.0f;
float m1 = 0.0f;
float m2 = 0.0f;
// Crate p_t to save computation (shift and rescale the original CDF)
final float p_t[] = new float[N];
// Copy the data
final Cursor<FloatType> cur_data = data.cursor();
for (int i = 0; i < N; ++i)
{
cur_data.next();
p_t[i] = (float) ((cur_data.get().getRealFloat() - 0.5)*Math.PI);
}
// While the precision is bigger than eps
while (right-left>=eps)
{
// move left and right of 1/3 (m1 and m2)
m1=left+(right-left)/3;
m2=right-(right-left)/3;
// Evaluate on m1 and m2, ane move the extreme point
if (FindT_Evalue(p_t, N, OffSet, m1) <=FindT_Evalue(p_t, N, OffSet, m2)) {
right=m2;
}
else {
left=m1;
}
}
// return the average
return (m1+m2)/2;
}
private Img<FloatType> create2DGradientField() {
final long dims[] = new long[2];
dims[0] = N_Grad;
dims[1] = N_Grad;
final Img<FloatType> GradD = new ArrayImgFactory<FloatType>().create(dims, new FloatType());
return GradD;
}
private void calculateLapCDF(Img<FloatType> LapCDF) {
final RandomAccess<FloatType> Lap_hist2 = LapCDF.randomAccess();
//convert Lap to CDF
for (int i = 1; i < N_Lap; ++i)
{
Lap_hist2.setPosition(i-1,0);
final float prec = Lap_hist2.get().getRealFloat();
Lap_hist2.move(1,0);
Lap_hist2.get().set(Lap_hist2.get().getRealFloat() + prec);
}
}
private void calculateGradCDF(Img<FloatType> GradCDF, Img<FloatType> GradD) {
final RandomAccess<FloatType> Grad_dist = GradD.randomAccess();
// Gradient on x pointer
final IntervalView<FloatType> Gradx = Views.hyperSlice(GradCDF, GradCDF.numDimensions()-1 , 0);
// Gradient on y pointer
final IntervalView<FloatType> Grady = Views.hyperSlice(GradCDF, GradCDF.numDimensions()-1 , 1);
integrateOverRowAndCol(Grad_dist, Gradx, Grady);
scaleGradiens(Gradx, Grady);
}
private void scaleGradiens(IntervalView<FloatType> Gradx, IntervalView<FloatType> Grady) {
final RandomAccess<FloatType> Gradx_r2 = Gradx.randomAccess();
final RandomAccess<FloatType> Grady_r2 = Grady.randomAccess();
//scale, divide the number of integrated bins
for (int i = 0; i < N_Grad; ++i)
{
Gradx_r2.setPosition(i,0);
Grady_r2.setPosition(i,0);
Gradx_r2.get().set((float) (Gradx_r2.get().getRealFloat() / 255.0));
Grady_r2.get().set((float) (Grady_r2.get().getRealFloat() / 255.0));
}
}
private void integrateOverRowAndCol(RandomAccess<FloatType> Grad_dist, IntervalView<FloatType> Gradx, IntervalView<FloatType> Grady) {
final int[] loc = new int[2];
// pGrad2D has 2D CDF
final RandomAccess<FloatType> Gradx_r = Gradx.randomAccess();
// Integrate over the row
for (int i = 0; i < N_Grad; ++i)
{
loc[1] = i;
Gradx_r.setPosition(i,0);
// get the row
for (int j = 0; j < N_Grad; ++j)
{
loc[0] = j;
// Set the position
Grad_dist.setPosition(loc);
// integrate over the row to get 1D vector
Gradx_r.get().set(Gradx_r.get().getRealFloat() + Grad_dist.get().getRealFloat());
}
}
final RandomAccess<FloatType> Grady_r = Grady.randomAccess();
// Integrate over the column
for (int i = 0; i < N_Grad; ++i)
{
loc[1] = i;
Grady_r.setPosition(0,0);
for (int j = 0; j < N_Grad; ++j)
{
loc[0] = j;
Grad_dist.setPosition(loc);
Grady_r.get().set(Grady_r.get().getRealFloat() + Grad_dist.get().getRealFloat());
Grady_r.move(1,0);
}
}
}
private <T extends RealType<T>> void calculateLaplaceFieldAndGradient(Img<T> image, Img<FloatType> LapCDF, Img<FloatType> GradD) {
final RandomAccess<FloatType> Grad_dist = GradD.randomAccess();
final long[] origImgDimensions = new long[2];
image.dimensions(origImgDimensions);
final Img<FloatType> laplaceField = new ArrayImgFactory<FloatType>().create(origImgDimensions, new FloatType());
// Cursor localization
final int[] indexD = new int[2];
final int[] loc_p = new int[2];
final RandomAccess<T> img_cur = image.randomAccess();
final RandomAccess<FloatType> Lap_f = laplaceField.randomAccess();
final RandomAccess<FloatType> Lap_hist = LapCDF.randomAccess();
// Normalization 1/(Number of pixel of the original image)
long n_pixel = 1;
for (int i = 0 ; i < laplaceField.numDimensions() ; i++)
{n_pixel *= laplaceField.dimension(i)-2;}
// unit to sum
final double f = 1.0/(n_pixel);
// Inside the image for Y
final Cursor<FloatType> cur = laplaceField.cursor();
// For each point of the Laplacian field
while (cur.hasNext())
{
cur.next();
// Localize cursors
cur.localize(loc_p);
// Exclude the border
boolean border = false;
for (int i = 0 ; i < image.numDimensions() ; i++)
{
if (loc_p[i] == 0)
{border = true;}
else if (loc_p[i] == image.dimension(i)-1)
{border = true;}
}
if (border == true) {
continue;
}
// get the stencil value;
img_cur.setPosition(loc_p);
float L = -4*img_cur.get().getRealFloat();
// Laplacian
for (int i = 0 ; i < 2 ; i++)
{
img_cur.move(1, i);
final float G_p = img_cur.get().getRealFloat();
img_cur.move(-1,i);
final float G_m = img_cur.get().getRealFloat();
img_cur.move(-1, i);
final float L_m = img_cur.get().getRealFloat();
img_cur.setPosition(loc_p);
L += G_p + L_m;
// Calculate the gradient + convert into bin
indexD[1-i] = (int) (Grad_Offset + G_p - G_m);
}
Lap_f.setPosition(loc_p);
// Set the Laplacian field
Lap_f.get().setReal(L);
// Histogram bin conversion
L += Lap_Offset;
Lap_hist.setPosition((int)(L),0);
Lap_hist.get().setReal(Lap_hist.get().getRealFloat() + f);
Grad_dist.setPosition(indexD);
Grad_dist.get().setReal(Grad_dist.get().getRealFloat() + f);
}
}
private void convertGrad2dToCDF(Img<FloatType> GradD) {
final RandomAccess<FloatType> Grad_dist = GradD.randomAccess();
final int[] loc = new int[GradD.numDimensions()];
// for each row
for (int j = 0; j < GradD.dimension(1); ++j)
{
loc[1] = j;
for (int i = 1; i < GradD.dimension(0) ; ++i)
{
loc[0] = i-1;
Grad_dist.setPosition(loc);
// Precedent float
final float prec = Grad_dist.get().getRealFloat();
// Move to the actual position
Grad_dist.move(1, 0);
// integration up to the current position
Grad_dist.get().set(Grad_dist.get().getRealFloat() + prec);
}
}
//col integration
for (int j = 1; j < GradD.dimension(1); ++j)
{
// Move to the actual position
loc[1] = j-1;
for (int i = 0; i < GradD.dimension(0); ++i)
{
loc[0] = i;
Grad_dist.setPosition(loc);
// Precedent float
final float prec = Grad_dist.get().getRealFloat();
// Move to the actual position
Grad_dist.move(1, 1);
Grad_dist.get().set(Grad_dist.get().getRealFloat() + prec);
}
}
}
/**
* Show information about authors and paper.
*/
private void showMessage()
{
// Create main window with panel to store gui components
final JDialog win = new JDialog((JDialog)null, "Naturalization", true);
final JPanel msg = new JPanel();
msg.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Create message not editable but still focusable for copying
final JTextPane text = new JTextPane();
text.setContentType("text/html");
text.setText("<html>Y. Gong and I. F. Sbalzarini. Image enhancement by gradient distribution specification. In Proc. ACCV, <br>"
+ "12th Asian Conference on Computer Vision, Workshop on Emerging Topics in Image Enhancement and Restoration,<br>"
+ "pages w7–p3, Singapore, November 2014.<br><br>"
+ "Y. Gong and I. F. Sbalzarini, Gradient Distributions Priors for Biomedical Image Processing, 2014<br>http://arxiv.org/abs/1408.3300<br><br>"
+ "Y. Gong and I. F. Sbalzarini. A Natural-Scene Gradient Distribution Prior and its Application in Light-Microscopy Image Processing.<br>"
+ "IEEE Journal of Selected Topics in Signal Processing, Vol.10, No.1, February 2016, pages 99-114<br>"
+ "ISSN: 1932-4553, DOI: 10.1109/JSTSP.2015.2506122<br><br>"
+ "</html>");
text.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
text.setEditable(false);
msg.add(text);
// Add button "Close" for closing window easily
final JButton button = new JButton("Close");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
win.dispose();
}
});
msg.add(button);
// Finally show window with message
win.add(msg);
win.pack();
win.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
win.setVisible(true);
}
#Override
protected boolean showDialog() {
return true;
}
#Override
protected boolean setup(String aArgs) {
setFilePrefix("naturalized_");
return true;
}
}
I want it to compile it again and get a .class file or a whole .jar file of this plugin.
Which sturcuture and datas I need for get a .class data?
What are with the import files, where i can get the ij, java, javax and net files? In which structure must it be.
I am a novice in Java and only know, that the compiled command is javac.
on linux there is a command to do it which is javac
just : javac HelloWorld.java
it might be the same thing on windows but i am not sure (install a virtual linux box if there is no other way)
If something goes wrong google the error
If you want to compile a Java program from command line you should use the javac command and to invoke it just write java and then the name of your program.
Compiling a file you will have the .class file that you are looking for.

n-gram similarity for the words in the file

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sim;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import static jdk.nashorn.internal.objects.NativeMath.max;
/**
*
* #author admin
*/
public class Sim {
public String[][] bigramizedWords = new String[500][100];
public String[] words = new String[500];
public File file1 = new File("file1.txt");
public File file2 = new File("file2.txt");
public int tracker = 0;
public double matches = 0;
public double denominator = 0; //This will hold the sum of the bigrams of the 2 words
public double res;
public double results;
public Scanner a;
public PrintWriter pw1;
public Sim(){
intialize();
// bigramize();
results = max(res);
System.out.println("\n\nThe Bigram Similarity value between " + words[0] + " and " + words[1] + " is " + res + ".");
pw1.close();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Sim si=new Sim();
// TODO code application logic here
}
public void intialize() {
int j[]=new int[35];
try {
File file1=new File("input.txt");
File file2=new File("out.txt");
Scanner a = new Scanner(file1);
PrintWriter pw1= new PrintWriter(file2);
int i=0,count = 0;
while (a.hasNext()) {
java.lang.String gram = a.next();
if(gram.startsWith("question")|| gram.endsWith("?"))
{
count=0;
count-=1;
}
if(gram.startsWith("[")||gram.startsWith("answer")||gram.endsWith(" ") )
{
//pw1.println(count);
j[i++]=count;
count=0;
//pw1.println(gram);
//System.out.println(count);
}
else
{
// System.out.println(count);
count+=1;
//System.out.println(count + " " + gram);
}
int line=gram.length();
int sa_length;
//int[] j = null;
int refans_length=j[1];
//System.out.println(refans_length);
for(int k=2;k<=35;k++)
// System.out.println(j[k]);
//System.out.println(refans_length);
for(int m=2;m<=33;m++)
{
sa_length=j[2];
//System.out.println(sa_length);
for(int s=0;s<=refans_length;s++)
{
for(int l=0;l<=sa_length;l++)
{
for (int x = 0; x <= line - 2; x++) {
int tracker = 0;
bigramizedWords[tracker][x] = gram.substring(x, x + 2);
System.out.println(gram.substring(x, x + 2) + "");
//bigramize();
}
// bigramize();
}
}
}
bigramize();
words[tracker] = gram;
tracker++;
}
//pw1.close();
}
catch (FileNotFoundException ex) {
Logger.getLogger(Sim.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void bigramize() {
//for(int p=0;p<=sa_length;p++)
denominator = (words[0].length() - 1) + (words[1].length() - 1);
for (int k = 0; k < bigramizedWords[0].length; k++) {
if (bigramizedWords[0][k] != null) {
for (int i = 0; i < bigramizedWords[1].length; i++) {
if (bigramizedWords[1][i] != null) {
if (bigramizedWords[0][k].equals(bigramizedWords[1][i])) {
matches++;
}
}
}
}
}
matches *= 2;
res = matches / denominator;
}
}
I have tried the above code for bigramizing the words in the file "input.txt" i have got the result of bigram but i didnt get the similarity value.
for e.g:
input file contains as
answer:
high
risk
simulate
behaviour
solution
set
rules
[2]
rules
outline
high
source
knowledge
[1]
set
rules
simulate
behaviour
in the above example I have to compare the words under answer with every word under [2] as {high,rules} {high,outline} {high,high} {high,source} {high,knowledge} and I have to store the maximum value of the above comparison and again the second word from answer is taken and then similar process is taken. At last, mean of maximum value of each iteration is taken.

Bollinger Bands Using Talib

I am trying to implement a simple BBands application using talib.
I do not have any expirience in Talib and technical Indicators.
Can someone have a look at say whether this is a good implementation of Bollinger Bands using Talib and give me advice how can it be improved. If anyone has expirience with Talib and Bollinger Bands it would be much appriciated to give me a hand. Here is the code.
import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;
import com.tictactec.ta.lib.*;
import com.tictactec.ta.lib.meta.helpers.*;
public class ExampleTALib
{
/**
* The total number of periods to generate data for.
*/
public static final int TOTAL_PERIODS = 100;
/**
* The number of periods to average together.
*/
public static final int PERIODS_AVERAGE = 20;
public static void main(String[] args)
{
double[] closePrice = new double[TOTAL_PERIODS];
double[] out = new double[TOTAL_PERIODS];
MInteger begin = new MInteger();
MInteger length = new MInteger();
double[] outRealUpperBand = new double[TOTAL_PERIODS];
double[] outRealMiddleBand = new double[TOTAL_PERIODS];
double[] outRealLowerBand = new double[TOTAL_PERIODS];
for (int i = 0; i < closePrice.length; i++) {
closePrice[i] = (double) i;
}
Core c = new Core();
// RetCode retCode = c.sma(0, closePrice.length - 1, closePrice, PERIODS_AVERAGE, begin, length, out);
// RetCode re = c.sma(startIdx, endIdx, inReal, optInTimePeriod, outBegIdx, outNBElement, outReal)
RetCode retCode = c.bbands(0, closePrice.length - 1, closePrice, PERIODS_AVERAGE,
1.0, 3.0, MAType.Ema, begin, length, outRealUpperBand, outRealMiddleBand, outRealLowerBand);
// RetCode re = c.bbands(startIdx, endIdx, inReal, optInTimePeriod, optInNbDevUp, optInNbDevDn, optInMAType, outBegIdx, outNBElement, outRealUpperBand, outRealMiddleBand, outRealLowerBand)
if (retCode == RetCode.Success) {
System.out.println("Output Begin:" + begin.value);
System.out.println("Output Begin:" + length.value);
for (int i = begin.value; i < closePrice.length; i++) {
StringBuilder line = new StringBuilder();
line.append("Period #");
line.append(i+1);
line.append(" close= ");
line.append(closePrice[i]);
line.append(" mov avg=");
line.append(out[i-begin.value]);
System.out.println(line.toString());
}
}
else {
System.out.println("Error");
}
}
}

Understanding multithreading with a for loop Java

I've got some code that I don't think is able to be multithreaded, perhaps I'm wrong. I'd like to make execute this code on a clustered system but I'm unsure of how to scale it for such a deployment.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Coord {
public int a,b,c,d,e,f;
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("/Users/evanlivingston/2b.txt");
PrintStream pout = new PrintStream(out);
Scanner sc = new Scanner(new File("/Users/evanlivingston/1.txt"));
List<Coord> coords = new ArrayList<Coord>();{
// for each line in the file
while(sc.hasNextLine()) {
String[] numstrs = sc.nextLine().split("\\s+");
Coord c = new Coord();
c.a = Integer.parseInt(numstrs[1]);
c.b = Integer.parseInt(numstrs[2]);
c.c = Integer.parseInt(numstrs[3]);
c.d = Integer.parseInt(numstrs[4]);
c.e = Integer.parseInt(numstrs[5]);
c.f = Integer.parseInt(numstrs[6]);
coords.add(c);
}
// now you have all coords in memory
{
for(int i=0; i<coords.size(); i++ )
for( int j=0; j<coords.size(); j++)
{
Coord c1 = coords.get(i);
Coord c2 = coords.get(j);
double foo = ((c1.a - c2.a) * (c1.a - c2.a)) *1 ;
double goo = ((c1.b - c2.b) * (c1.b - c2.b)) *1 ;
double hoo = ((c1.c - c2.c) * (c1.c - c2.c)) *2 ;
double joo = ((c1.d - c2.d) * (c1.d - c2.d)) *2 ;
double koo = ((c1.e - c2.e) * (c1.e - c2.e)) *4 ;
double loo = ((c1.f - c2.f) * (c1.f - c2.f)) *4 ;
double zoo = Math.sqrt(foo + goo + hoo + joo + koo + loo);
DecimalFormat df = new DecimalFormat("#.###");
pout.println(i + " " + j + " " + df.format(zoo));
System.out.println(i);
}
pout.flush();
pout.close();
}
}
}
}
I appreciate any help anyone can offer.
Splitting the inner for loop into separate tasks looks like a good candidate for where to make this process multithreaded. Here is one way this could be done with an ExecutorService and Futures
final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
final List<Future<String>> results = new LinkedList<Future<String>>();
// now you have all coords in memory
for (int i = 0; i < coords.size(); i++) {
final int index = i;
final Coord c1 = coords.get(index);
results.add(executor.submit(new Callable<String>() {
public String call() {
final StringBuilder stringBuilder = new StringBuilder();
for (int j = 0; j < coords.size(); j++) {
final Coord c2 = coords.get(j);
final double foo = ((c1.a - c2.a) * (c1.a - c2.a)) * 1;
final double goo = ((c1.b - c2.b) * (c1.b - c2.b)) * 1;
final double hoo = ((c1.c - c2.c) * (c1.c - c2.c)) * 2;
final double joo = ((c1.d - c2.d) * (c1.d - c2.d)) * 2;
final double koo = ((c1.e - c2.e) * (c1.e - c2.e)) * 4;
final double loo = ((c1.f - c2.f) * (c1.f - c2.f)) * 4;
final double zoo = Math.sqrt(foo + goo + hoo + joo + koo + loo);
final DecimalFormat df = new DecimalFormat("#.###");
stringBuilder.append(index + " " + j + " " + df.format(zoo));
System.out.println(index);
}
return stringBuilder.toString();
}
}));
}
for (Future<String> result : results) {
pout.print(result.get());
}
pout.flush();
pout.close();
executor.shutdown();
For clustering, I think Hazelcast offers a good solution that will allow you to define a shared ExecutorService and shared Collections. You would need two flavors of nodes, the single node responsible for all I/O and creating the list of Coords as well as submitting the tasks. And a processing node which simply executes the tasks. That is all my opinion of how I might do it. However, if your dataset is small enough to fit in memory it is likely not worth the effort to split up the processing this much.
It looks very parallelizable to me. Why don't you have threads process one row of data at a time? You could use an AtomicInteger to keep a count of how many rows have been claimed by worker threads. Each thread would do a counter.getAndIncrement to get a row to work on (if it returns coords.size() or higher, the thread should terminate), then do all the math for that row, and repeat.
The printing would be out of order, but you could instead fill some buffers with the results, then quickly print everything at the end.

Categories