source: main/trunk/model-sites-dev/mars/src/java/org/greenstone/mars/WekaApplyArousalModel.java@ 35207

Last change on this file since 35207 was 34802, checked in by davidb, 3 years ago

Saving instances changed to a DataSink, and located in WekaUtil

File size: 2.7 KB
Line 
1package org.greenstone.mars;
2
3/*
4import java.io.BufferedInputStream;
5import java.io.FileInputStream;
6
7import java.io.BufferedReader;
8import java.io.BufferedWriter;
9import java.io.FileReader;
10import java.io.FileWriter;
11
12import weka.core.converters.ConverterUtils.DataSource;
13import weka.core.Attribute;
14import weka.core.Instance;
15*/
16
17import weka.core.Instances;
18/*
19import weka.core.SerializationHelper;
20
21import weka.filters.Filter;
22import weka.filters.unsupervised.attribute.Remove;
23*/
24
25import weka.classifiers.Classifier;
26
27
28// Based on:
29// https://waikato.github.io/weka-wiki/use_weka_in_your_java_code/
30
31class WekaApplyArousalModel
32{
33 public static void main(String[] args)
34 {
35 WekaUtil.checkUsageApplyModel(args);
36
37 String classifier_input_filename = args[0];
38 String unclassified_data_input_filename = args[1];
39 String classified_data_output_filename = args[2];
40
41 Classifier classifier = WekaUtil.loadClassifierModel(classifier_input_filename);
42
43 Instances unlabeled_instances= WekaUtil.loadInstancesForClassification(unclassified_data_input_filename);
44
45 // It is permissible to run this code and supply it with a data file that includes groundtruth in it.
46 // In this situation, the 'unlabeled' instances:
47 // (i) need to be massaged to be in the same form as truly unlabeled data
48 // (ii) we also set up 'groundtruth_instances' as an alias (reference) to 'filtered_unlabeled_instanced'
49 // to trigger calculating the error on the predicted vaues
50
51 boolean has_groundtruth_data = WekaUtil.instancesHavePredictAttribute(unlabeled_instances,WekaUtil.AROUSAL_ATTRIBUTE_NAME);
52
53 // The following deals with (i) internally, ensuring that what is returned is suitable for making predictions on
54 Instances filtered_unlabeled_instances
55 = WekaUtil.filterInstancesForApplying(unlabeled_instances,has_groundtruth_data,
56 WekaUtil.AROUSAL_ATTRIBUTE_NAME,"472");
57
58 WekaUtil.checkDatasetInstancesCompatible(filtered_unlabeled_instances,"472");
59
60 // The following deals with (ii)
61 Instances groundtruth_instances = (has_groundtruth_data) ? filtered_unlabeled_instances : null;
62
63 System.out.println("Predicting arousal:");
64 Instances labeled_instances = WekaUtil.makePredictions(classifier, filtered_unlabeled_instances, groundtruth_instances);
65
66 /*
67 try {
68 // Save labeled data
69
70 System.out.println("Saving labeled instances: " + classified_data_output_filename);
71 FileWriter fw = new FileWriter(classified_data_output_filename);
72 BufferedWriter bw = new BufferedWriter(fw);
73
74 bw.write(labeled_instances.toString());
75 bw.newLine();
76 bw.flush();
77 bw.close();
78
79 }
80 catch (Exception e) {
81 e.printStackTrace();
82 }
83 */
84
85 WekaUtil.saveInstancesAsDataSink(labeled_instances,classified_data_output_filename);
86 }
87}
Note: See TracBrowser for help on using the repository browser.