-
Notifications
You must be signed in to change notification settings - Fork 1
/
features.py
116 lines (88 loc) · 2.68 KB
/
features.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import os
from datetime import datetime
ipDict = {}
allDevices = {}
devices = []
def ipFeature(line):
try:
start, end, ip, device, identity = line.split(";")
except ValueError:
return None
ip = ip.strip()
ip1, ip2, ip3, ip4 = [int(x) for x in ip.split(".")]
vec = "0"* 32
first8 = '{0:08b}'.format(ip1)
second8 = '{0:08b}'.format(ip2)
third8 = '{0:08b}'.format(ip3)
fourth8 = '{0:08b}'.format(ip4)
vec = first8 + second8 + third8 + fourth8
# print vec
# print list(vec)
return [int(x) for x in list(vec)]
def timeStartFeature(line):
try:
start, end, ip, device, identity = line.split(";")
hour, minute, sec = [int(x) for x in start.strip().split(":")]
except ValueError:
return None
first5 = '{0:05b}'.format(hour)
second6 = '{0:06b}'.format(minute)
third6 = '{0:06b}'.format(sec)
vect = first5 + second6 + third6
# print vect
return [int(x) for x in list(vect)]
def timeEndFeature(line):
start, end, ip, device, identity = line.split(";")
hour, minute, sec = [int(x) for x in end.strip().split(":")]
first5 = '{0:05b}'.format(hour)
second6 = '{0:06b}'.format(minute)
third6 = '{0:06b}'.format(sec)
vect = "0" * 17
vect[0:5] = first5
vect[5:11] = second6
vect[11:17] = third6
print vect
return list(vect)
def device(line):
start, end, ip, device, identity = line.split(";")
device = device.strip()
devices.append(device)
# This is the index of device
if device in allDevices:
return allDevices[device]
else:
allDevices[device] = len(devices) - 1
return allDevices[device]
# clean data
def duration(line):
try:
start, end, ip, device, identity = line.split(";")
FMT = '%H:%M:%S'
tdelta = datetime.strptime(end.strip(), FMT) - datetime.strptime(start.strip(), FMT)
except ValueError:
return None
return tdelta.seconds
# What the start hour is
def timeOfDay(line):
try:
start, end, ip, device, identity = line.split(";")
hour, minute, sec = [int(x) for x in start.strip().split(":")]
except ValueError:
return None
# first5 = '{0:05b}'.format(hour)
# second6 = '{0:06b}'.format(minute)
# third6 = '{0:06b}'.format(sec)
return hour
def durationLessThanMinute(line):
return duration(line)/60 < 1
def durationOneToFive(line):
minutes = duration(line)/60
return minutes > 0 and minutes < 5
def durationFiveOrMore(line):
minutes = duration(line)/60
return minutes >= 5
# midnight cases
# construct more data for time