Skip to content

Commit 0b037e6

Browse files
committed
python 3 fixes, initial commit
1 parent be9a234 commit 0b037e6

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

tools/randomize.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#####################################################################
2+
3+
# Example : randomize loaded CVS file and write out to file
4+
# basic illustrative python script
5+
6+
# Author : Toby Breckon, [email protected]
7+
8+
# Copyright (c) 2014 / 2016 School of Engineering & Computing Science,
9+
# Durham University, UK
10+
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
11+
12+
#####################################################################
13+
14+
import csv
15+
import cv2
16+
import numpy as np
17+
18+
from random import shuffle
19+
20+
########### Load Data Set - Example
21+
22+
# load full data set (unsplit)
23+
24+
reader=csv.reader(open("input.data","rt", encoding='ascii'),delimiter=',')
25+
26+
27+
entry_list = []
28+
29+
for row in reader:
30+
entry_list.append(row)
31+
32+
########### randomize (different order for every file loaded)
33+
# N.B. to randomize attributes / labels together - append into single np array
34+
# with one {attribute/label} pair together, then shuffle
35+
36+
shuffle(entry_list)
37+
38+
########### Write Data Set - Example
39+
40+
writer = csv.writer(open("output.data", "wt", encoding='ascii'), delimiter=',')
41+
writer.writerows(entry_list)
42+
43+
#####################################################################

tools/reader.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#####################################################################
2+
3+
# Example : load HAPT data set only
4+
# basic illustrative python script
5+
6+
# For use with test / training datasets : HAPT-data-set-DU
7+
8+
# Author : Toby Breckon, [email protected]
9+
10+
# Copyright (c) 2014 School of Engineering & Computing Science,
11+
# Durham University, UK
12+
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
13+
14+
#####################################################################
15+
16+
import csv
17+
import cv2
18+
import os
19+
import numpy as np
20+
21+
########### Define classes
22+
23+
classes = {} # define mapping of cla
24+
inv_classes = {v: k for k, v in classes.items()}
25+
26+
########### Load Data Set
27+
28+
path_to_data = "../../assignment/2016-17/HAPT-data-set-DU" # edit this
29+
30+
# Training data - as currenrtly split
31+
32+
attribute_list = []
33+
label_list = []
34+
35+
reader=csv.reader(open(os.path.join(path_to_data, "Train/x_train.txt"),"rt", encoding='ascii'),delimiter=' ')
36+
for row in reader:
37+
# attributes in columns 0-561
38+
attribute_list.append(list(row[i] for i in (range(0,561))))
39+
40+
reader=csv.reader(open(os.path.join(path_to_data, "Train/y_train.txt"),"rt", encoding='ascii'),delimiter=' ')
41+
for row in reader:
42+
# attributes in column 1
43+
label_list.append(row[0])
44+
45+
training_attributes=np.array(attribute_list).astype(np.float32)
46+
training_labels=np.array(label_list).astype(np.float32)
47+
48+
# Testing data - as currently split
49+
50+
attribute_list = []
51+
label_list = []
52+
53+
reader=csv.reader(open(os.path.join(path_to_data, "Test/x_test.txt"),"rt", encoding='ascii'),delimiter=' ')
54+
for row in reader:
55+
# attributes in columns 0-561
56+
attribute_list.append(list(row[i] for i in (range(0,561))))
57+
58+
reader=csv.reader(open(os.path.join(path_to_data, "Test/y_test.txt"),"rt", encoding='ascii'),delimiter=' ')
59+
for row in reader:
60+
# attributes in column 1
61+
label_list.append(row[0])
62+
63+
testing_attributes=np.array(attribute_list).astype(np.float32)
64+
testing_labels=np.array(label_list).astype(np.float32)
65+
66+
########### test output for sanity
67+
68+
print(training_attributes)
69+
print(len(training_attributes))
70+
print(training_labels)
71+
print(len(training_labels))
72+
73+
print(testing_attributes)
74+
print(len(testing_attributes))
75+
print(testing_labels)
76+
print(len(testing_labels))
77+
78+
#####################################################################

tools/selectlines.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#####################################################################
2+
3+
# Example : select subset of lines from CVS file and write to files
4+
# basic illustrative python script
5+
6+
# Author : Toby Breckon, [email protected]
7+
8+
# Copyright (c) 2014 / 2016 School of Engineering & Computing Science,
9+
# Durham University, UK
10+
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
11+
12+
#####################################################################
13+
14+
import csv
15+
import cv2
16+
import numpy as np
17+
import math
18+
19+
from random import shuffle
20+
21+
########### Load Data Set - Example
22+
23+
# load full data set (unsplit)
24+
25+
reader=csv.reader(open("input.data","rt", encoding='ascii'),delimiter=',')
26+
27+
entry_list = []
28+
29+
for row in reader:
30+
entry_list.append(row)
31+
32+
########### Write Data Set - Example
33+
34+
# write first N% of the entries to first file
35+
36+
N = 30.0
37+
38+
writerA = csv.writer(open("outputA.data", "wt", encoding='ascii'), delimiter=',')
39+
writerA.writerows(entry_list[0:int(math.floor(len(entry_list)* (N/100.0)))])
40+
41+
# write the remaining (100-N)% of the entries of the second file
42+
43+
writerB = csv.writer(open("outputB.data", "wt", encoding='ascii'), delimiter=',')
44+
writerB.writerows(entry_list[int(math.floor(len(entry_list)* (N/100.0))):len(entry_list)])
45+
46+
#####################################################################

0 commit comments

Comments
 (0)