Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ipd_extended/run_classification.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (68 sloc)
2.64 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import constants as cst | |
import subprocess as sp | |
import csv | |
import numpy as np | |
import re | |
import os | |
import signal | |
import sys | |
import util | |
def run_random_forest1(base_dir_name, experiment_name): | |
file_path = cst.BASE + base_dir_name + "/" + experiment_name + "/out.arff" | |
if not os.path.exists(file_path): | |
print('no file', file_path) | |
return None | |
try: | |
output = str(sp.check_output(["java", "-cp", cst.WEKA_BIN, | |
"weka.classifiers.trees.RandomForest", '-P', '100', '-I', | |
'100', '-num-slots', '1', '-K', '0', '-M', '1.0', '-V', '0.001', '-S', '1', | |
"-t", file_path], timeout=30)) | |
match = re.search('Correctly Classified Instances\s+\d+\s+(\d+\.?\d+)\s+%', output) | |
if match: | |
return experiment_name + "," + match.group(1) | |
return experiment_name + ",?" | |
except sp.TimeoutExpired: | |
print("timeout exceeded", experiment_name) | |
return experiment_name + ",?" | |
def classify_experiments(base_dir_name, class_arr): | |
global results | |
results = [] | |
listdir = os.listdir(cst.BASE + base_dir_name) | |
# listdir = None | |
# with open(cst.BASE + base_dir_name + "/experiments_list.txt", 'r') as f: | |
# listdir = f.readlines() | |
# # listdir = os.listdir(cst.BASE + base_dir_name) | |
# if listdir is None or len(listdir) == 0: | |
# return results | |
total = len(listdir) | |
counter = 0 | |
for experiment in listdir: | |
counter += 1 | |
if class_arr is not None and experiment in class_arr: | |
print(experiment, "has already been processed", counter, "/", total) | |
continue | |
# if experiment.startswith("xor"): | |
# continue | |
print('running random forest on', experiment, counter, "/", total) | |
classification = run_random_forest1(base_dir_name, experiment.strip()) | |
if not classification: | |
continue | |
results.append(classification) | |
results.append("\n") | |
return results | |
def signal_handler(signal, frame): | |
global stop_signal, results | |
stop_signal = True | |
print('Writing down Classification.csv') | |
with open(cst.BASE + base_dir_name + "/Classification.csv", "a") as f: | |
f.writelines(results) | |
sys.exit(0) | |
global stop_signal | |
stop_signal = False | |
if __name__ == '__main__': | |
base_dir_name = sys.argv[1] | |
signal.signal(signal.SIGINT, signal_handler) | |
class_arr = util.read_csv(cst.BASE + base_dir_name + "/Classification.csv") | |
res = classify_experiments(base_dir_name, class_arr) | |
with open(cst.BASE + base_dir_name + "/Classification.csv", "a") as f: | |
f.writelines(res) |