public static void main(String[] agrs) throws IOException, ClassNotFoundException { //We first load the WasteIncinerator bayesian network which has multinomial and Gaussian variables. BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn"); //We simply create an BayesianNetworkSampler object, passing to the constructor the BN model. BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.setSeed(0); //The method sampleToDataStream returns a DataStream with ten DataInstance objects. DataStream<DataInstance> dataStream = sampler.sampleToDataStream(10); //We finally save the sampled data set to a arff file. DataStreamWriter.writeDataToFile(dataStream, "datasets/simulated/sample-WasteIncinerator.arff"); } }
/** * Sets a given {@link Variable} object as noisy. * @param var a given {@link Variable} object. * @param noiseProb a double that represents the noise probability. */ public void setMARVar(Variable var, double noiseProb){ this.localSampler.setMARVar(var, noiseProb);}
/** * Sets a given {@link Variable} object as hidden. * @param var a given {@link Variable} object. */ public void setHiddenVar(Variable var) { this.localSampler.setHiddenVar(var); }
@Override public Iterator<DataInstance> call(Integer ind, Iterator<Integer> iterator) throws Exception { localSampler.setSeed(seed+ind); return localSampler.sampleToDataStream(localNSamples).iterator(); } };
public static void generateData(int seed, double tempMean, String outputFile) throws Exception { BayesianNetwork network = createFireDetectorModel(tempMean); BayesianNetworkSampler sampler = new BayesianNetworkSampler(network); sampler.setSeed(seed); sampler.setLatentVar(network.getVariables().getVariableByName("Temperature")); sampler.setLatentVar(network.getVariables().getVariableByName("Smoke")); DataStream<DataInstance> dataStream = sampler.sampleToDataStream(1000); DataStreamWriter.writeDataToFile(dataStream, outputFile); } public static void main(String[] args) throws Exception {
BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize);
BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.loadOptions(); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize);
BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.setMARVar(bn.getVariables().getVariableById(0),0.2); sampler.setMARVar(bn.getVariables().getVariableById(3),0.2); sampler.setMARVar(bn.getVariables().getVariableById(8),0.2); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize);
/** * Creates a new BayesianNetworkSampler given an input {@link BayesianNetwork} object. * @param network1 an input {@link BayesianNetwork} object. */ public BayesianNetworkSampler(BayesianNetwork network1){ this.network = network1; localSampler = new eu.amidst.core.utils.BayesianNetworkSampler(network1); }
public DataSpark sampleToDataSpark(JavaSparkContext sc, int nSamples, int parallelism) { int localNSamples = nSamples/parallelism; JavaRDD<Integer> partitions = sc.parallelize(Arrays.asList(new Integer[parallelism]), parallelism); Function2 getPartitionSample = new Function2<Integer, Iterator<Integer>, Iterator<DataInstance>>(){ @Override public Iterator<DataInstance> call(Integer ind, Iterator<Integer> iterator) throws Exception { localSampler.setSeed(seed+ind); return localSampler.sampleToDataStream(localNSamples).iterator(); } }; JavaRDD<DataInstance> sampleRDD = partitions.mapPartitionsWithIndex(getPartitionSample, false); // Get the attributes from a local instance Attributes attributes = this.localSampler.sampleToDataStream(1).getAttributes(); return new DataSparkFromRDD(sampleRDD, attributes); }
/** * Sets a given {@link Variable} object as latent. A latent variable doesn't contain an attribute and therefore * doesn't generate a sampling value. * @param var a given {@link Variable} object. */ public void setLatentVar(Variable var){ this.localSampler.setLatentVar(var); }
public static void main(String[] args) throws Exception { int sampleSize = 10000; BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/simulated/asia.bn"); System.out.println(bn); BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize); ParallelTAN tan = new ParallelTAN(); tan.setNumCores(4); tan.setNumSamplesOnMemory(1000); tan.setNameRoot("X"); tan.setNameTarget("E"); BayesianNetwork model = tan.learn(data); System.out.println(model.toString()); } }
public static void generateData() throws Exception { BayesianNetwork network = createFireDetectorModel(); BayesianNetworkSampler sampler = new BayesianNetworkSampler(network); sampler.setSeed(0); sampler.setLatentVar(network.getVariables().getVariableByName("Temperature")); sampler.setLatentVar(network.getVariables().getVariableByName("Smoke")); DataStream<DataInstance> dataStream = sampler.sampleToDataStream(1000); DataStreamWriter.writeDataToFile(dataStream, "./datasets/sensorReadings.arff"); } public static void main(String[] args) throws Exception {
BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.loadOptions(); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize);
@Override public void flatMap(Long value, Collector<DataInstance> out) throws Exception { this.localSampler.setSeed(seed + value.intValue()); this.localSampler.sampleToDataStream(batchSize).stream().forEach(d -> out.collect(d)); } }
/** * Creates a new BayesianNetworkSampler given an input {@link BayesianNetwork} object. * @param bn an input {@link BayesianNetwork} object. */ public BayesianNetworkSampler(BayesianNetwork bn){ this.network = bn; localSampler = new eu.amidst.core.utils.BayesianNetworkSampler(bn); }
public DataFlink<DataInstance> sampleToDataFlink(ExecutionEnvironment env, int nSamples) { try{ int nBatches = nSamples/this.batchSize; DataSet<DataInstance> data = env.generateSequence(0,nBatches-1) .flatMap(new SampleMap(this.localSampler,batchSize,seed)); Attributes attributes = this.localSampler.sampleToDataStream(1).getAttributes(); return new DataFlinkWrapper<>(network.getName(), attributes, data); }catch(Exception ex){ throw new UndeclaredThrowableException(ex); } }
private static void sampleBayesianNetwork() throws IOException { BayesianNetwork bn = new BayesianNetwork(dag); BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); sampler.setSeed(0); //The method sampleToDataStream returns a DataStream with ten DataInstance objects. DataStream<DataInstance> dataStream = sampler.sampleToDataStream(getSampleSize()); //We finally save the sampled data set to an arff file. DataStreamWriter.writeDataToFile(dataStream, "datasets/sampleBatchSize.arff"); }
public static void demoPigs() throws IOException, ClassNotFoundException { //It needs GBs, so avoid putting this file in a Dropbox folder!! //String dataFile = new String("/Users/afa/Pigs.arff"); BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("networks/dataWeka/Pigs.bn"); int sampleSize = 10000; BayesianNetworkSampler sampler = new BayesianNetworkSampler(bn); ArrayList<Integer> vSamplesOnMemory = new ArrayList(Arrays.asList(5000)); ArrayList<Integer> vNumCores = new ArrayList(Arrays.asList(1, 2, 3, 4)); for (Integer samplesOnMemory : vSamplesOnMemory) { for (Integer numCores : vNumCores) { System.out.println("Learning TAN: " + samplesOnMemory + " samples on memory, " + numCores + " core/s ..."); DataStream<DataInstance> data = sampler.sampleToDataStream(sampleSize); ParallelTAN tan = new ParallelTAN(); tan.setNumCores(numCores); tan.setNumSamplesOnMemory(samplesOnMemory); tan.setNameRoot(bn.getVariables().getListOfVariables().get(0).getName()); tan.setNameTarget(bn.getVariables().getListOfVariables().get(1).getName()); Stopwatch watch = Stopwatch.createStarted(); BayesianNetwork model = tan.learn(data); System.out.println(watch.stop()); } } }
/** * Sets a given {@link Variable} object as noisy. * @param var a given {@link Variable} object. * @param noiseProb a double that represents the noise probability. */ public void setMARVar(Variable var, double noiseProb){ this.localSampler.setMARVar(var, noiseProb);}