-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadingData.py
94 lines (75 loc) · 2.36 KB
/
readingData.py
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
import numpy as np
"""
This method reads entire file and returns as a list
Params:
souce_file = file path
total_images = dataset size
length = length of pixel
width = length of pixel
Returns:
Entire file in list
"""
def load_data(source_file, total_images, length, width):
datasetFile = open(source_file)
data_line = datasetFile.readlines()
image_data= []
for i in range(total_images):
temp_data = []
for j in range(length*i, length*(i+1)):
temp_data.append(data_line[j])
image_data.append(temp_data)
return image_data
"""
This method returns the labels of training data.
Params:
Takes path of the input file
returns:
list of all the labels
"""
def load_label(source_file):
label_file = open(source_file)
label_lines = label_file.readlines()
labels = []
for i in range(len(label_lines)):
labels.append(label_lines[i].strip())
return labels
"""
This method requires entire data passed in list, and will return list of numpy array
Params:
image_data = list
length = length of pixel
width = length of pixel
Returns:
List of Numpy array
And array is representation of given data
"""
def matrix_transformation(image_data, length, width):
total_data = len(image_data)
final_data = []
for i in range(total_data):
mat = np.zeros((length, width))
single_image = image_data[i]
single_image_length = len(single_image)
for j in range(single_image_length):
single_line = single_image[j]
single_line_length = len(single_line)
for k in range(single_line_length):
if(single_line[k] == '+'):
mat[j][k] = 1
if(single_line[k] == '#'):
mat[j][k] = 2
final_data.append(mat)
return final_data
def matrix_transformation_test(image_data, length, width):
mat = np.zeros((length, width))
single_image = image_data
single_image_length = len(single_image)
for j in range(single_image_length):
single_line = single_image[j]
single_line_length = len(single_line)
for k in range(single_line_length):
if(single_line[k] == '+'):
mat[j][k] = 1
if(single_line[k] == '#'):
mat[j][k] = 2
return mat