Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
pro
CRDiS
Commits
94dec73d
Commit
94dec73d
authored
7 years ago
by
Kamil Jurek
Browse files
Options
Download
Email Patches
Plain Diff
generating all possible rules improved"
parent
e44205fb
master
alternative_rules
No related merge requests found
Changes
11
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
.vscode/settings.json
+3
-0
.vscode/settings.json
detectors/online_simulator.py
+1
-1
detectors/online_simulator.py
detectors/rule.py
+2
-2
detectors/rule.py
detectors/rules_detector.py
+37
-18
detectors/rules_detector.py
other/rill.py
+0
-0
other/rill.py
other/stack_zscore_detector.py
+0
-0
other/stack_zscore_detector.py
other/zscore_rules.py
+0
-0
other/zscore_rules.py
plots/seq_with_random.png
+0
-0
plots/seq_with_random.png
test_output.txt
+6583
-0
test_output.txt
test_rules_detecting_2.py
+5
-2
test_rules_detecting_2.py
utils.py
+5
-4
utils.py
with
6636 additions
and
27 deletions
+6636
-27
.vscode/settings.json
0 → 100644
View file @
94dec73d
{
"python.pythonPath"
:
"C:
\\
Users
\\
EKAMJUR
\\
AppData
\\
Local
\\
Continuum
\\
anaconda3
\\
python.exe"
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
detectors/online_simulator.py
View file @
94dec73d
...
...
@@ -17,7 +17,7 @@ class OnlineSimulator(object):
self
.
rules_sets
=
[
set
()
for
i
in
range
(
len
(
self
.
sequences
))]
self
.
parameters_history
=
[
defaultdict
(
list
)
for
i
in
range
(
len
(
self
.
sequences
))]
self
.
rules_detector
=
rules_detector
self
.
combined_rules
=
[]
self
.
combined_rules
=
set
()
if
rules_detector
!=
None
:
self
.
rules_detector
.
set_online_simulator
(
self
)
...
...
This diff is collapsed.
Click to expand it.
detectors/rule.py
View file @
94dec73d
...
...
@@ -10,8 +10,8 @@ class Rule(object):
def
__repr__
(
self
):
generalized
=
" Generalized"
if
self
.
generalized
else
""
return
(
str
(
self
.
lhs
)
+
" ==> "
+
str
(
self
.
rhs
)
+
"
| nr_of_occurences:"
+
str
(
self
.
number_of_occurrences
)
+
" last_occurence:"
+
str
(
self
.
last_occurrence
)
+
" occurences:"
+
str
(
self
.
occurrences
)
+
generalized
)
return
(
str
(
self
.
lhs
)
+
" ==> "
+
str
(
self
.
rhs
)
+
"
\n\t
| nr_of_occurences:"
+
str
(
self
.
number_of_occurrences
)
+
"
|
last_occurence:"
+
str
(
self
.
last_occurrence
)
+
"
|
occurences:"
+
str
(
self
.
occurrences
)
+
generalized
)
# + "nr:" + str(self.number_of_occurrences) + " lastOcc:" + str(self.last_occurrence)
def
__eq__
(
self
,
other
):
...
...
This diff is collapsed.
Click to expand it.
detectors/rules_detector.py
View file @
94dec73d
...
...
@@ -5,10 +5,11 @@ from rule import Rule
from
online_simulator
import
OnlineSimulator
class
RulesDetector
(
object
):
def
__init__
(
self
,
target_seq_index
,
window_size
=
0
,
round_to
=
100
):
def
__init__
(
self
,
target_seq_index
,
window_size
=
0
,
round_to
=
100
,
type
=
"closed"
):
self
.
target_seq_index
=
target_seq_index
self
.
round_to
=
round_to
self
.
window_size
=
window_size
self
.
type
=
type
def
set_online_simulator
(
self
,
sim
):
self
.
simulator
=
sim
...
...
@@ -26,8 +27,10 @@ class RulesDetector(object):
window_begin
=
round_to
(
prev_prev_change_point_target
.
at_
,
self
.
round_to
)
if
prev_prev_change_point_target
!=
None
else
0
window_end
=
round_to
(
prev_change_point_target
.
at_
,
self
.
round_to
)
#self.generate_closed_rules(window_begin, window_end, current_index)
self
.
generate_all_rules
(
window_begin
,
window_end
,
current_index
)
if
self
.
type
==
"closed"
:
self
.
generate_closed_rules
(
window_begin
,
window_end
,
current_index
)
else
:
self
.
generate_all_rules
(
window_begin
,
window_end
,
current_index
)
def
generate_closed_rules
(
self
,
window_begin
,
window_end
,
current_index
):
combined_rule
=
[]
...
...
@@ -91,15 +94,15 @@ class RulesDetector(object):
r
.
set_last_occurence
(
current_index
)
r
.
increment_occurrences
()
r
.
occurrences
.
append
(
current_index
)
combined_rule
.
append
(
r
ule
)
combined_rule
.
append
(
r
)
print
(
"Rule already in set:"
,
r
)
if
is_new_rule
:
rule
.
set_last_occurence
(
current_index
)
rule
.
increment_occurrences
()
gen_rule
=
self
.
generalize_rule
(
seq_index
,
rule
)
if
gen_rule
!=
None
:
gen_rule
.
set_last_occurence
(
current_index
)
#
gen_rule = self.generalize_rule(seq_index, rule)
#
if gen_rule != None:
#
gen_rule.set_last_occurence(current_index)
# self.simulator.rules_sets[m].add(gen_rule)
self
.
simulator
.
rules_sets
[
seq_index
].
add
(
rule
)
...
...
@@ -108,11 +111,11 @@ class RulesDetector(object):
if
len
(
combined_rule
)
>
0
:
print
(
"Adding to combined rules"
)
self
.
simulator
.
combined_rules
.
a
ppend
(
combined_rule
)
self
.
simulator
.
combined_rules
.
a
dd
(
tuple
(
combined_rule
)
)
def
generate_all_rules
(
self
,
window_begin
,
window_end
,
current_index
):
generated_rules
=
[[]
for
i
in
range
(
len
(
self
.
simulator
.
detected_change_points
))]
combined_rule
=
[]
for
seq_index
,
change_point_list
in
enumerate
(
self
.
simulator
.
detected_change_points
):
if
seq_index
==
self
.
target_seq_index
:
continue
...
...
@@ -143,7 +146,7 @@ class RulesDetector(object):
last_point
.
attr_name
)
lhss
.
append
([
lhs_elem
])
for
point_index
in
range
(
1
,
len
(
points_in_window
)
+
1
):
for
point_index
in
range
(
1
,
len
(
points_in_window
)):
# print("inside window case")
prefix
=
lhss
[
-
1
]
# print("prefix:", prefix)
...
...
@@ -155,7 +158,19 @@ class RulesDetector(object):
point
.
prev_value
,
point
.
attr_name
)
lhss
.
append
([
lhs_elem
]
+
prefix
)
first_point
=
points_in_window
[
0
]
if
round_to
(
first_point
.
at_
-
first_point
.
prev_value_len
,
self
.
round_to
)
<=
window_begin
:
# print("before window case")
if
round_to
(
first_point
.
at_
-
window_begin
,
self
.
round_to
)
>=
0
:
lhs_elem_len
=
round_to
(
first_point
.
at_
-
window_begin
,
self
.
round_to
)
prefix
=
lhss
[
-
1
]
for
lhs_len
in
range
(
self
.
round_to
,
lhs_elem_len
+
1
,
self
.
round_to
):
lhs_elem
=
LHS_element
(
lhs_len
,
first_point
.
prev_value
,
first_point
.
attr_name
)
lhss
.
append
([
lhs_elem
]
+
prefix
)
last_target_change_point
=
self
.
simulator
.
detected_change_points
[
self
.
target_seq_index
][
-
1
]
rhs_elem_len
=
round_to
(
last_target_change_point
.
prev_value_len
,
self
.
round_to
)
for
rhs_len
in
range
(
self
.
round_to
,
rhs_elem_len
+
1
,
self
.
round_to
):
...
...
@@ -167,8 +182,7 @@ class RulesDetector(object):
if
len
(
lhss
)
>
0
:
for
rhs
in
rhss
:
for
lhs
in
lhss
:
rule
=
Rule
(
lhs
,
rhs
)
rule
.
occurrences
.
append
(
current_index
)
rule
=
Rule
(
lhs
,
rhs
)
is_new_rule
=
True
for
r
in
self
.
simulator
.
rules_sets
[
seq_index
]:
...
...
@@ -178,26 +192,31 @@ class RulesDetector(object):
r
.
increment_occurrences
()
r
.
occurrences
.
append
(
current_index
)
#print("Rule already in set:", r)
generated_rules
[
seq_index
].
append
(
r
)
if
is_new_rule
:
rule
.
set_last_occurence
(
current_index
)
rule
.
increment_occurrences
()
rule
.
occurrences
.
append
(
current_index
)
self
.
simulator
.
rules_sets
[
seq_index
].
add
(
rule
)
#print("New rule:", rule)
generated_rules
[
seq_index
].
append
(
rule
)
generated_rules
[
seq_index
].
append
(
rule
)
print
(
"=============================================="
)
combined_rule
=
[]
#[self.simulator.combined_rules.add((x,y,z)) if x.rhs == y.rhs and x.rhs == z.rhs else None for x in generated_rules[0] for y in generated_rules[1] for z in generated_rules[2]]
for
seq_rules
in
generated_rules
:
if
seq_rules
:
print
(
seq_rules
)
combined_rule
.
append
(
seq_rules
[
-
1
])
print
(
seq_rules
[
-
1
])
for
gr
in
seq_rules
:
print
(
gr
)
print
(
"=============================================="
)
if
len
(
combined_rule
)
>
0
:
print
(
"Adding to combined rules"
)
self
.
simulator
.
combined_rules
.
a
ppend
(
combined_rule
)
self
.
simulator
.
combined_rules
.
a
dd
(
tuple
(
combined_rule
)
)
def
generalize_rule
(
self
,
seq_index
,
new_rule
):
for
rule
in
self
.
simulator
.
rules_sets
[
seq_index
]:
...
...
@@ -231,7 +250,7 @@ class RulesDetector(object):
points_in_window
=
[]
points_before_window
=
[]
points_after_window
=
[]
for
n
,
change_point
in
enumerate
(
self
.
simulator
.
detected_change_points
[
seq_index
]
)
:
for
change_point
in
self
.
simulator
.
detected_change_points
[
seq_index
]:
if
round_to
(
change_point
.
at_
,
self
.
round_to
)
>
window_begin
:
if
round_to
(
change_point
.
at_
,
self
.
round_to
)
<
window_end
:
points_in_window
.
append
(
change_point
)
...
...
This diff is collapsed.
Click to expand it.
detectors
/rill.py
→
other
/rill.py
View file @
94dec73d
File moved
This diff is collapsed.
Click to expand it.
detectors
/stack_zscore_detector.py
→
other
/stack_zscore_detector.py
View file @
94dec73d
File moved
This diff is collapsed.
Click to expand it.
detectors
/zscore_rules.py
→
other
/zscore_rules.py
View file @
94dec73d
File moved
This diff is collapsed.
Click to expand it.
plots/seq_with_random.png
0 → 100644
View file @
94dec73d
61.4 KB
This diff is collapsed.
Click to expand it.
test_output.txt
0 → 100644
View file @
94dec73d
This diff is collapsed.
Click to expand it.
test_rules_detecting_
occupancy
.py
→
test_rules_detecting_
2
.py
View file @
94dec73d
...
...
@@ -50,7 +50,10 @@ detector4 = ZScoreDetector(window_size = win_size, threshold=4)
# detector3 = AdwinDetector()
# detector4 = AdwinDetector()
rules_detector
=
RulesDetector
(
target_seq_index
=
3
,
window_size
=
0
,
round_to
=
100
)
rules_detector
=
RulesDetector
(
target_seq_index
=
3
,
window_size
=
0
,
round_to
=
100
,
type
=
"all"
)
simulator
=
OnlineSimulator
(
rules_detector
,
[
detector1
,
detector2
,
detector3
,
detector4
],
...
...
@@ -64,7 +67,7 @@ start_time = time.time()
simulator
.
run
(
plot
=
False
,
detect_rules
=
True
)
print_detected_change_points
(
simulator
.
get_detected_changes
())
print_rules
(
simulator
.
get_rules_sets
(),
0
)
print_combined_rules
(
simulator
.
get_combined_rules
(),
1
)
print_combined_rules
(
simulator
.
get_combined_rules
(),
0
)
end_time
=
time
.
time
()
print
(
end_time
-
start_time
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
utils.py
View file @
94dec73d
...
...
@@ -3,24 +3,25 @@ import math
def
print_rules
(
rules_sets
,
support
):
print
(
"---------------------------------------------------------------------------------------"
)
for
rules_set
in
rules_sets
:
for
rule
in
sorted
(
rules_set
,
key
=
lambda
x
:
x
.
number_of_occurrences
,
reverse
=
True
):
for
rule
in
sorted
(
rules_set
,
key
=
lambda
x
:
(
x
.
number_of_occurrences
,
len
(
x
.
lhs
),
x
.
rhs
.
len
,
x
.
lhs
[
0
].
len
),
reverse
=
True
):
if
rule
.
number_of_occurrences
>=
support
:
print
(
rule
)
print
(
"----------------------------------------------------------------------------------------"
)
print
(
"----------------------------------------------------------------------------------------"
)
print
()
def
print_combined_rules
(
combined_rules
,
support
):
print
(
"---------------------------------------------------------------------------------------"
)
for
combined_rule
in
sorted
(
combined_rules
,
key
=
lambda
x
:
x
[
0
].
number_of_occurrences
,
reverse
=
True
):
for
combined_rule
in
sorted
(
combined_rules
,
key
=
lambda
x
:
min
(
x
[
i
].
number_of_occurrences
for
i
in
range
(
len
(
x
)))
,
reverse
=
True
):
# print(combined_rule)
nr_of_occu
=
min
([
combined_rule
[
i
].
number_of_occurrences
for
i
in
range
(
len
(
combined_rule
))])
if
nr_of_occu
>=
support
:
for
i
,
rule
in
enumerate
(
combined_rule
):
print
(
rule
.
lhs
,
rule
.
number_of_occurrences
,
rule
.
last_occurrence
,
"AND"
if
i
<
len
(
combined_rule
)
-
1
else
""
)
print
(
rule
.
lhs
,
rule
.
number_of_occurrences
,
rule
.
last_occurrence
,
rule
.
occurrences
,
"AND"
if
i
<
len
(
combined_rule
)
-
1
else
""
)
print
(
" ==>"
,
)
print
(
combined_rule
[
0
].
rhs
)
print
(
"number of occurenes:"
,
nr_of_occu
)
print
()
print
(
"---------------------------------------------------------------------------------------"
)
def
print_detected_change_points
(
detected_changes
):
print
(
"---------------------------------------------------------------------------------------"
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help