-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixIO.java
More file actions
174 lines (170 loc) · 5.37 KB
/
MatrixIO.java
File metadata and controls
174 lines (170 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Scanner;
/*
By Brendan C. Reidy
Created 12/10/2019
Last Modified 12/17/2019
MatrixIO
Offloads array/matrix input & output to a separate class
*/
public class MatrixIO {
public static int getLength(String aFileName)
{
if(aFileName==null)
{
System.out.println("[FATAL] Input file name is null");
return -1;
}
try {
System.out.println("Counting file: " + aFileName);
Scanner fileScanner = new Scanner(new File(aFileName));
int count = 0;
while(fileScanner.hasNext())
{
fileScanner.nextLine();
count++;
}
fileScanner.close();
return count;
}catch(FileNotFoundException e){
System.out.println("File '" + aFileName + "' does not exist");
}catch (Exception e){
System.out.println(e.getMessage());
}
return -1;
}
static float[][] readTrainingData(String aFileName, int length)
{
if(aFileName==null)
{
System.out.println("[FATAL] Input file name is null");
return null;
}
try {
float[][] dataArray = new float[length][];
System.out.println("Reading file: " + aFileName);
Scanner fileScanner = new Scanner(new File(aFileName));
int count = 0;
while(fileScanner.hasNext())
{
String fileLine = fileScanner.nextLine();
String[] splitLines = fileLine.split(",");
float[] values = new float[splitLines.length];
for(int i=0; i<splitLines.length; i++){
float current = Float.parseFloat(splitLines[i]);
values[i] = current;
}
if(count>=dataArray.length) {
System.out.println("[WARNING] Reading stopped early. File is larger than specified");
break;
}
dataArray[count] = values;
count++;
}
fileScanner.close();
return dataArray;
}catch(FileNotFoundException e){
System.out.print("File '" + aFileName + "' does not exit");
}catch (Exception e){
System.out.println(e.getMessage());
}
return null;
}
public static float[] flattenArray(float[][] array)
{
float[] returnVal = new float[array.length*array[0].length];
int k=0;
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[0].length; j++)
{
returnVal[k] = array[i][j];
k++;
}
}
return returnVal;
}
public static int[] flattenArray(int[][] array)
{
int[] returnVal = new int[array.length*array[0].length];
int k=0;
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[0].length; j++)
{
returnVal[k] = array[i][j];
k++;
}
}
return returnVal;
}
public static void saveToFile(LinkedList<Float> list, String aFileName){
String[] copy = new String[list.size()];
for(int i=0; i<list.size(); i++)
{
copy[i] = Float.toString(list.get(i));
}
saveToFile(copy, aFileName);
}
public static void saveToFile(double[] array, String aFileName){
String[] copy = new String[array.length];
for(int i=0; i<array.length; i++)
{
copy[i] = Double.toString(array[i]);
}
saveToFile(copy, aFileName);
}
public static void saveToFile(float[] array, String aFileName){
String[] copy = new String[array.length];
for(int i=0; i<array.length; i++)
{
copy[i] = Float.toString(array[i]);
}
saveToFile(copy, aFileName);
}
public static void saveToFile(int[] array, String aFileName){
String[] copy = new String[array.length];
for(int i=0; i<array.length; i++)
{
copy[i] = Integer.toString(array[i]);
}
saveToFile(copy, aFileName);
}
static void saveToFile(String[] aInput, String aFileName)
{
if(aFileName==null)
return;
try{
PrintWriter writer = new PrintWriter(aFileName, "UTF-8");
String str = "";
for(int index = 0; index<aInput.length; index++)
str+=aInput[index] + "\n";
writer.println(str);
writer.close();
}catch(FileNotFoundException e){
System.out.println("File does not exist");
}catch(Exception e){
System.out.println(e.getMessage());
}
}
static void print2D(float[] input, int dimensionSize)
{
int k=0;
for(int x=0; x<input.length/dimensionSize; x++)
{
for(int y=0; y<dimensionSize; y++)
{
int pixel = 0;
int pixelRepresentation = (int) (input[k] + 0.5);
if(pixelRepresentation>=1)
pixel = 1;
System.out.print(pixel + " ");
k++;
}
System.out.println();
}
}
}