Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs598mp-fall2021-proj
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
chsieh16
cs598mp-fall2021-proj
Commits
d84105f4
Commit
d84105f4
authored
3 years ago
by
chsieh16
Browse files
Options
Downloads
Patches
Plain Diff
Refactor file creation and feature names
parent
c9830a80
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dtree_learner.py
+19
-22
19 additions, 22 deletions
dtree_learner.py
with
19 additions
and
22 deletions
dtree_learner.py
+
19
−
22
View file @
d84105f4
...
...
@@ -6,31 +6,32 @@ import csv
import
grammar_generation_utils
import
json
class
DTreeLearner
(
LearnerBase
):
def
__init__
(
self
,
state_dim
:
int
,
perc_dim
:
int
,
timeout
:
int
=
10000
)
->
None
:
super
().
__init__
()
self
.
_state_dim
:
int
=
state_dim
self
.
_perc_dim
:
int
=
perc_dim
self
.
_s2f_func_list
:
List
=
[]
#check tempLocation dir exists, if not create it.
self
.
dir_name
=
"
tempLocation
"
check_temp_dir
:
bool
=
os
.
path
.
isdir
(
self
.
dir_name
)
if
not
check_temp_dir
:
# check directory name exists, if not create it.
self
.
dir_name
=
"
out
"
if
not
os
.
path
.
isdir
(
self
.
dir_name
):
os
.
makedirs
(
self
.
dir_name
)
# create empty files
self
.
data_file
=
self
.
dir_name
+
"
/pre.data
"
self
.
names_file
=
self
.
dir_name
+
"
/pre.names
"
open
(
self
.
data_file
,
'
w
'
).
close
()
open
(
self
.
names_file
,
'
w
'
).
close
()
path_prefix
=
self
.
dir_name
+
"
/pre
"
self
.
data_file
=
path_prefix
+
"
.data
"
self
.
names_file
=
path_prefix
+
"
.names
"
self
.
tree_out
=
path_prefix
+
"
.json
"
self
.
exec
=
'
c50exact/c5.0dbg -I 1 -m 1 -f tempLocation/pre
'
# create empty files or truncate existing contents in files
open
(
self
.
data_file
,
'
w
'
).
close
()
open
(
self
.
names_file
,
'
w
'
).
close
()
self
.
tree_out
=
"
tempLocation/pre.json
"
self
.
exec
=
f
'
c50exact/c5.0dbg -I 1 -m 1 -f
{
path_prefix
}
'
@property
def
state_dim
(
self
)
->
int
:
...
...
@@ -41,16 +42,13 @@ class DTreeLearner(LearnerBase):
return
self
.
_perc_dim
def
set_grammar
(
self
,
grammar
)
->
None
:
count
=
1
numerical_vars
=
[]
for
a_mat
,
b_vec
in
grammar
:
numerical_vars
:
List
[
str
]
=
[]
for
count
,
(
a_mat
,
b_vec
)
in
enumerate
(
grammar
):
self
.
_s2f_func_list
.
append
(
construct_sample_to_feature_func
(
a_mat
,
b_vec
))
numerical_vars
.
extend
(
f
"
fvar
{
i
}
_A
{
count
}
B
{
count
}
"
for
i
in
range
(
self
.
perc_dim
))
numerical_vars
.
append
(
"
dbar
"
+
f
'
A
{
count
}
B
{
count
}
'
)
numerical_vars
.
append
(
"
psibar
"
+
f
'
A
{
count
}
B
{
count
}
'
)
count
+=
1
grammar_generation_utils
.
generateInputLanguageFile
(
self
.
names_file
,
numerical_vars
,
[])
def
add_implication_examples
(
self
,
*
args
)
->
None
:
...
...
@@ -65,7 +63,7 @@ class DTreeLearner(LearnerBase):
feature_vec_list
.
append
(
feature_vec
)
print
(
"
Positive feature vectors (dbar, psibar):
"
,
feature_vec_list
)
self
.
write_to_file
(
self
.
data_file
,
feature_vec_list
,
"
true
"
)
self
.
write_to_file
(
self
.
data_file
,
feature_vec_list
,
"
true
"
)
def
add_negative_examples
(
self
,
*
args
)
->
None
:
feature_vec_list
=
[]
...
...
@@ -78,14 +76,13 @@ class DTreeLearner(LearnerBase):
print
(
"
Negative feature vectors (dbar, psibar):
"
,
feature_vec_list
)
self
.
write_to_file
(
self
.
data_file
,
feature_vec_list
,
"
false
"
)
def
write_to_file
(
self
,
file
:
str
,
feature_vec_list
,
label
:
str
):
def
write_to_file
(
self
,
file
:
str
,
feature_vec_list
,
label
:
str
):
with
open
(
file
,
'
a
'
)
as
d_file
:
data_out
=
csv
.
writer
(
d_file
)
for
f
in
feature_vec_list
:
print
(
f
)
data_out
.
writerow
(
f
+
[
label
])
def
learn
(
self
)
->
Tuple
:
res
=
os
.
popen
(
self
.
exec
).
read
()
print
(
res
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment