From 0a0fbcc46d875530a9d2a9e9ff779bf062e05612 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 8 Feb 2019 13:03:15 +0100 Subject: [PATCH] cleaned up generateRandomKsatDataSet_db.py / fixed type casting bug in ArgumentParser --- .gitignore | 4 ++ generateRandomKsatDataSet_db.py | 68 +++++++++++++++++---------------- util/script.py | 6 ++- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 7316d1f..4f33460 100644 --- a/.gitignore +++ b/.gitignore @@ -98,3 +98,7 @@ ENV/ #### config files # database.config + +### vim +# +*.swp diff --git a/generateRandomKsatDataSet_db.py b/generateRandomKsatDataSet_db.py index 626d3d2..67c5e5a 100755 --- a/generateRandomKsatDataSet_db.py +++ b/generateRandomKsatDataSet_db.py @@ -3,19 +3,35 @@ from util import randomSAT from util import kSAT import util.script as scriptUtils -import h5py -import numpy as np import pymongo import ssl def main(): args = __parseArguments() - - dbClient = __getDBClient(args["dbConfigPath"]); - db = dbClient["experiments"] - dbInstances = db["instances"] - dbExperimentScopes = db["experiment_scopes"] + dbContext = __getDBContext(args["dbConfigPath"]) + + __generateExperiment(args, dbContext) + + dbContext["client"].close() + +def __generateExperiment(args, dbContext): + experimentScope = __prepareExperimentScope(args) + + for i in range(args["instances"]): + sat = randomSAT.generateRandomKSAT(args["clauses"], + args["variables"], + args["variablesPerClause"]) + + instanceId = dbContext["instances"].insert_one(sat.writeJSONLike()).inserted_id + + experimentScope["instances"].append(instanceId) + + dbContext["experimentScopes"].insert_one(experimentScope) + + + +def __prepareExperimentScope(args): experimentScope = {} experimentScope["instances"] = [] @@ -28,21 +44,8 @@ def main(): experimentScope["description"] = description + return experimentScope - for i in range(args["instances"]): - print("loop", i) - - sat = randomSAT.generateRandomKSAT(args["clauses"], - args["variables"], - args["variablesPerClause"]) - - instanceId = dbInstances.insert_one(sat.writeJSONLike()).inserted_id - - experimentScope["instances"].append(instanceId) - - dbExperimentScopes.insert_one(experimentScope) - - dbClient.close() def __getDBClient(dbConfigPath): dbConf = scriptUtils.readConfig(dbConfigPath) @@ -58,6 +61,17 @@ def __getDBClient(dbConfigPath): ssl=True, ssl_cert_reqs=ssl.CERT_NONE) + +def __getDBContext(dbConfigPath): + dbContext = {} + + dbContext["client"] = __getDBClient(dbConfigPath) + dbContext["db"] = dbContext["client"]["experiments"] + dbContext["instances"] = dbContext["db"]["instances"] + dbContext["experimentScopes"] = dbContext["db"]["experiment_scopes"] + + return dbContext + def __parseArguments(): parser = scriptUtils.ArgParser() @@ -84,17 +98,5 @@ def __parseArguments(): return parser.parse() -def __initFile(args): - f = h5py.File(args["fileName"], "w") - - __SAT_TYPE__ = np.dtype([('bindings', (np.int32, (args["clauses"], - args["variablesPerClause"])))]) - - instances = f.create_dataset(name="instances", - shape=(args["instances"],), - dtype=__SAT_TYPE__) - - return f, __SAT_TYPE__ - if __name__ == "__main__": main() diff --git a/util/script.py b/util/script.py index 51d4159..18f80b7 100644 --- a/util/script.py +++ b/util/script.py @@ -28,7 +28,8 @@ class ArgParser: self.__flags[alias] = {"longFlag": longFlag, "hasDefault": False, - "ignoreDatabaseConfig": ignoreDatabaseConfig} + "ignoreDatabaseConfig": ignoreDatabaseConfig, + "type": type} if default != None: self.__flags[alias]["hasDefault"] = True @@ -79,7 +80,8 @@ class ArgParser: tmpValue = args[longFlag] if tmpValue == None: - tmpValue == input("pass arguement %s: " % longFlag) + tmpValue = flag["type"](input("pass arguement %s: " % longFlag)) + return tmpValue