-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathARFFBoxLabel.py
More file actions
99 lines (88 loc) · 3.48 KB
/
Copy pathARFFBoxLabel.py
File metadata and controls
99 lines (88 loc) · 3.48 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
# -*- coding: utf-8 -*-
import arff
import numpy as np
class ARFFBoxLabel:
"""ARFF Box Label Exchange class"""
def __init__(self):
# attributes = {u'FileName':0, u'BoxNumber':1, u'BoxCenterX':2, u'BoxCenterY':3, u'BoxWidth':4, u'BoxHeight':5}
return
def toARFF(self, data, Type=u'MBMC'):
obj = self.create(Type)
for i in range(len(data)):
obj['data'].append(data[i])
return obj
def create(self, Type=u'MBMC'):
if Type == u'CenterOnly':
obj = {u'description':u'',
u'relation':Type,
u'attributes': [
(u'FileName', u'STRING'),(u'BoxNumber', u'INTEGER'),
(u'BoxCenterX', u'INTEGER'),(u'BoxCenterY', u'INTEGER'),
],
u'data': []
}
elif Type == u'MBMC':
obj = {u'description':u'',
u'relation':Type,
u'attributes': [
(u'FileName', u'STRING'),(u'BoxNumber', u'INTEGER'),(u'Class', u'INTEGER'),
(u'BoxCenterX', u'INTEGER'),(u'BoxCenterY', u'INTEGER'),
(u'BoxWidth', u'INTEGER'),(u'BoxHeight', u'INTEGER')
],
u'data': []
}
elif Type == u'MBMCwGrav':
obj = {u'description':u'',
u'relation':Type,
u'attributes': [
(u'FileName', u'STRING'),(u'BoxNumber', u'INTEGER'),(u'Class', u'INTEGER'),
(u'BoxCenterX', u'INTEGER'),(u'BoxCenterY', u'INTEGER'),
(u'BoxWidth', u'INTEGER'),(u'BoxHeight', u'INTEGER'),
(u'ObjGravityX', u'INTEGER'),(u'ObjGravityY', u'INTEGER')
],
u'data': []
}
return obj
def concat(self, obj1, obj2):
obj = self.create(obj1['relation'])
for i in range(len(obj1['data'])): obj['data'].append(obj1['data'][i])
for i in range(len(obj2['data'])): obj['data'].append(obj2['data'][i])
return obj
def read(self, filename):
obj = arff.load(open(filename, 'rb'))
return obj
def readCSV(self, filename, attributes, sort):
f = open(filename, 'r')
csv = []
for line in f:
if line != "\n" and line[0] != '#':
line = line.split(',')
# Extend data length
while(len(line) < len(sort)):
line.append(0)
data = []
for i in sort:
if attributes[i] == u'INTEGER':
try:
data.append(int(line[i]))
except:
data.append(int(0))
elif attributes[i] == u'REAL':
try:
data.append(float(line[i]))
except:
data.append(float(0))
elif attributes[i] == u'STRING':
try:
data.append(unicode(line[i]))
except:
data.append('')
csv.append(data)
f.close()
return csv
def write(self, obj, filename):
encoder = arff.ArffEncoder()
f = open(filename, 'wb')
f.write(encoder.encode(obj))
f.close()
return