Skip to content

Commit

Permalink
Update formatting to PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
ronald-jaepel committed Mar 18, 2024
1 parent 540cef6 commit bf7f26d
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions cadet/cadet.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
from addict import Dict

import copy
import json
import os
import platform
import pprint
import shutil
import subprocess
import warnings
from pathlib import Path

with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=FutureWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
import h5py
import numpy
import subprocess
import pprint
import copy
import json
from addict import Dict

import filelock
import contextlib

from pathlib import Path

from cadet.cadet_dll import CadetDLL


class H5():
pp = pprint.PrettyPrinter(indent=4)

Expand Down Expand Up @@ -117,10 +120,12 @@ def __setitem__(self, key, value):
obj = obj[i]
obj[parts[-1]] = value


def is_dll(value):
suffix = Path(value).suffix
return suffix in {'.so', '.dll'}


class CadetMeta(type):
_cadet_runner_class = None
_is_file_class = None
Expand All @@ -144,7 +149,7 @@ def __init__(cls):
del cls._cadet_runner_class

if is_dll(value):
cls._cadet_runner_class = CadetDLL(value)
cls._cadet_runner_class = CadetDLL(value)
cls._is_file_class = False
else:
cls._cadet_runner_class = CadetFile(value)
Expand All @@ -154,13 +159,18 @@ def __init__(cls):
def cadet_path(cls):
del cls._cadet_runner_class


class Cadet(H5, metaclass=CadetMeta):
#cadet_path must be set in order for simulations to run
# cadet_path must be set in order for simulations to run
def __init__(self, *data):
super().__init__(*data)
self._cadet_runner = None
self.return_information = None
self._is_file = None
self.cadet_dll_path = None
self.cadet_create_lwe_path = None
self.cadet_cli_path = None
self._install_path = None

@property
def is_file(self):
Expand All @@ -173,8 +183,9 @@ def is_file(self):
def cadet_runner(self):
if self._cadet_runner is not None:
return self._cadet_runner
if self._cadet_runner_class is not None:
if hasattr(self, "_cadet_runner_class") and self._cadet_runner_class is not None:
return self._cadet_runner_class
self.autodetect_cadet()

@property
def cadet_path(self):
Expand All @@ -188,7 +199,7 @@ def cadet_path(self, value):
del self._cadet_runner

if is_dll(value):
self._cadet_runner = CadetDLL(value)
self._cadet_runner = CadetDLL(value)
self._is_file = False
else:
self._cadet_runner = CadetFile(value)
Expand All @@ -209,14 +220,14 @@ def load_results(self):
if runner is not None:
runner.load_results(self)

def run(self, timeout = None, check=None):
def run(self, timeout=None, check=None):
data = self.cadet_runner.run(simulation=self.root.input, filename=self.filename, timeout=timeout, check=check)
#self.return_information = data
# self.return_information = data
return data

def run_load(self, timeout = None, check=None, clear=True):
def run_load(self, timeout=None, check=None, clear=True):
data = self.cadet_runner.run(simulation=self.root.input, filename=self.filename, timeout=timeout, check=check)
#self.return_information = data
# self.return_information = data
self.load_results()
if clear:
self.clear()
Expand All @@ -227,14 +238,15 @@ def clear(self):
if runner is not None:
runner.clear()


class CadetFile:

def __init__(self, cadet_path):
self.cadet_path = cadet_path

def run(self, filename = None, simulation=None, timeout = None, check=None):
def run(self, filename=None, simulation=None, timeout=None, check=None):
if filename is not None:
data = subprocess.run([self.cadet_path, filename], timeout = timeout, check=check, capture_output=True)
data = subprocess.run([self.cadet_path, filename], timeout=timeout, check=check, capture_output=True)
return data
else:
print("Filename must be set before run can be used")
Expand All @@ -245,9 +257,10 @@ def clear(self):
def load_results(self, sim):
sim.load(paths=["/meta", "/output"], update=True)


def convert_from_numpy(data, func):
ans = Dict()
for key_original,item in data.items():
for key_original, item in data.items():
key = func(key_original)
if isinstance(item, numpy.ndarray):
item = item.tolist()
Expand All @@ -264,16 +277,18 @@ def convert_from_numpy(data, func):
ans[key] = item
return ans

def recursively_load_dict( data, func):

def recursively_load_dict(data, func):
ans = Dict()
for key_original,item in data.items():
for key_original, item in data.items():
key = func(key_original)
if isinstance(item, dict):
ans[key] = recursively_load_dict(item, func)
else:
ans[key] = item
return ans


def set_path(obj, path, value):
"paths need to be broken up so that subobjects are correctly made"
path = path.split('/')
Expand All @@ -285,7 +300,8 @@ def set_path(obj, path, value):

temp[path[-1]] = value

def recursively_load( h5file, path, func, paths):

def recursively_load(h5file, path, func, paths):
ans = Dict()
if paths is not None:
for path in paths:
Expand All @@ -306,8 +322,8 @@ def recursively_load( h5file, path, func, paths):
ans[key] = recursively_load(h5file, local_path + '/', func, None)
return ans

def recursively_save(h5file, path, dic, func):

def recursively_save(h5file, path, dic, func):
if not isinstance(path, str):
raise ValueError("path must be a string")
if not isinstance(h5file, h5py._hl.files.File):
Expand Down

0 comments on commit bf7f26d

Please sign in to comment.