source: trunk/CrossPare/src/de/ugoe/cs/cpdp/loader/CSVDataLoader.java @ 41

Last change on this file since 41 was 41, checked in by sherbold, 9 years ago
  • formatted code and added copyrights
  • Property svn:mime-type set to text/plain
File size: 2.9 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 * Loads the instances for a software version from a CSV file of the PROMISE data set mined by
28 * Jurezko and Madeyski.
29 *
30 * @author Steffen Herbold
31 */
32class CSVDataLoader implements SingleVersionLoader {
33
34    /*
35     * (non-Javadoc)
36     *
37     * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader#load( java.io.File)
38     */
39    @Override
40    public Instances load(File file) {
41        final String[] lines;
42        try {
43            lines = FileTools.getLinesFromFile(file.getAbsolutePath());
44        }
45        catch (IOException e) {
46            throw new RuntimeException(e);
47        }
48
49        // configure Instances
50        final ArrayList<Attribute> atts = new ArrayList<Attribute>();
51
52        String[] lineSplit = lines[0].split(",");
53        for (int j = 0; j < lineSplit.length - 4; j++) {
54            atts.add(new Attribute(lineSplit[j + 3]));
55        }
56        final ArrayList<String> classAttVals = new ArrayList<String>();
57        classAttVals.add("0");
58        classAttVals.add("1");
59        final Attribute classAtt = new Attribute("bug", classAttVals);
60        atts.add(classAtt);
61
62        final Instances data = new Instances(file.getName(), atts, 0);
63        data.setClass(classAtt);
64
65        // fetch data
66        for (int i = 1; i < lines.length; i++) {
67            lineSplit = lines[i].split(",");
68            double[] values = new double[lineSplit.length - 3];
69            for (int j = 0; j < values.length - 1; j++) {
70                values[j] = Double.parseDouble(lineSplit[j + 3].trim());
71            }
72            values[values.length - 1] = lineSplit[lineSplit.length - 1].trim().equals("0") ? 0 : 1;
73            data.add(new DenseInstance(1.0, values));
74        }
75
76        return data;
77    }
78
79    /*
80     * (non-Javadoc)
81     *
82     * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader#
83     * filenameFilter(java.lang.String)
84     */
85    @Override
86    public boolean filenameFilter(String filename) {
87        return filename.endsWith(".csv");
88    }
89
90}
Note: See TracBrowser for help on using the repository browser.