source: trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/CSVMockusDataLoader.java

Last change on this file was 135, checked in by sherbold, 8 years ago
  • code documentation and formatting
File size: 2.5 KB
Line 
1// Copyright 2015 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.cpdp.loader;
16
17import java.io.File;
18import java.io.IOException;
19import java.util.ArrayList;
20
21import weka.core.Attribute;
22import weka.core.DenseInstance;
23import weka.core.Instances;
24import de.ugoe.cs.util.FileTools;
25
26/**
27 * <p>
28 * Reads data from the data set provided by Mockus (and Zhang) for universal defect prediction.
29 * </p>
30 *
31 * @author Steffen Herbold
32 */
33class CSVMockusDataLoader implements SingleVersionLoader {
34
35    @Override
36    public Instances load(File file) {
37        final String[] lines;
38        try {
39
40            lines = FileTools.getLinesFromFile(file.getAbsolutePath());
41        }
42        catch (IOException e) {
43            throw new RuntimeException(e);
44        }
45
46        // configure Instances
47        final ArrayList<Attribute> atts = new ArrayList<Attribute>();
48
49        String[] lineSplit = lines[0].split(",");
50        for (int j = 0; j < lineSplit.length - 3; j++) {
51            atts.add(new Attribute(lineSplit[j + 2]));
52        }
53
54        final ArrayList<String> classAttVals = new ArrayList<String>();
55        classAttVals.add("0");
56        classAttVals.add("1");
57        final Attribute classAtt = new Attribute("bug", classAttVals);
58        atts.add(classAtt);
59
60        final Instances data = new Instances(file.getName(), atts, 0);
61        data.setClass(classAtt);
62
63        // fetch data
64        for (int i = 1; i < lines.length; i++) {
65            lineSplit = lines[i].split(",");
66            double[] values = new double[lineSplit.length - 2];
67            for (int j = 0; j < values.length - 1; j++) {
68                values[j] = Double.parseDouble(lineSplit[j + 2].trim());
69            }
70            values[values.length - 1] = lineSplit[lineSplit.length - 1].trim().equals("0") ? 0 : 1;
71            data.add(new DenseInstance(1.0, values));
72        }
73
74        return data;
75    }
76
77    @Override
78    public boolean filenameFilter(String filename) {
79        return filename.endsWith(".csv");
80    }
81
82}
Note: See TracBrowser for help on using the repository browser.