in prediction_generation/old-code/kcpa_ecp.py [0:0]
def main():
args = parse_args()
# Load dataset
data = load_dataset(args.input)
time_series = np.array(data["series"][0]["raw"]).reshape(-1, 1) # Convert to numpy array
dataset_md5 = get_md5(args.input)
# Set `maxcp` to a reasonable value if it's 'max'
if args.maxcp == 'max':
args.maxcp = min(len(time_series) // 2, 100) # Limit L to at most 100 or half the length of the series
start_time = time.time()
try:
# Run the appropriate algorithm
if args.algorithm == 'e.divisive':
result = e_divisive(time_series, alpha=args.alpha, runs=args.runs, minsize=args.minsize, siglvl=args.siglvl)
elif args.algorithm == 'kcpa':
result = kcpa(time_series, L=args.maxcp, cost=args.cost)
runtime = time.time() - start_time
# If result is None or empty, raise an error to handle it as a failure
if not result:
raise ValueError("No valid change points detected.")
# Construct success output
output_data = {
"error": None,
"command": f"python3.9 /TCPDBench/execs/python/kcpa_ecp.py -i {args.input} -a {args.algorithm} --alpha {args.alpha} --minsize {args.minsize} --runs {args.runs} --siglvl {args.siglvl}",
"script": "/TCPDBench/execs/python/kcpa_ecp.py",
"script_md5": get_md5('/TCPDBench/execs/python/kcpa_ecp.py'),
"hostname": socket.gethostname(),
"dataset": args.input.split('/')[-1].split('.')[0],
"dataset_md5": dataset_md5,
"status": "SUCCESS",
"parameters": {
"alpha": args.alpha,
"minsize": args.minsize,
"max_cp": args.maxcp,
"method": args.algorithm,
"runs": args.runs,
"siglvl": args.siglvl
},
"arguments": vars(args),
"result": {
"cplocations": list(map(int, result)),
"runtime": runtime
}
}
except Exception as e:
# Handle any errors and produce a failure output
runtime = time.time() - start_time
output_data = {
"error": str(e),
"command": f"python3.9 /TCPDBench/execs/python/kcpa_ecp.py -i {args.input} -a {args.algorithm} --alpha {args.alpha} --minsize {args.minsize} --runs {args.runs} --siglvl {args.siglvl}",
"script": "/TCPDBench/execs/python/kcpa_ecp.py",
"script_md5": get_md5('/TCPDBench/execs/python/kcpa_ecp.py'),
"hostname": socket.gethostname(),
"dataset": args.input.split('/')[-1].split('.')[0],
"dataset_md5": dataset_md5,
"status": "FAIL",
"parameters": {
"alpha": args.alpha,
"minsize": args.minsize,
"max_cp": args.maxcp,
"method": args.algorithm,
"runs": args.runs,
"siglvl": args.siglvl
},
"arguments": vars(args),
"result": {
"cplocations": None,
"runtime": None
}
}
# Write output to file or print to console
if args.output:
with open(args.output, 'w') as outfile:
json.dump(output_data, outfile, indent=4)
else:
print(json.dumps(output_data, indent=4))