diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index 2cbd029b..c3a1b951 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -26,9 +26,9 @@ jobs: steps: - uses: actions/checkout@v2 # Required for pre-commit - - run: pip3 install bandit cpplint docker mypy pylint types-toml + - run: pip3 install bandit black cpplint docker mypy pylint types-toml - run: sudo snap install shfmt - - run: sudo apt install black shellcheck + - run: sudo apt install shellcheck # NOTE: This is deprecated in favor of pre-commit.ci - uses: pre-commit/action@v2.0.3 with: diff --git a/he-samples/examples/logistic-regression/datasets/generate_data.py b/he-samples/examples/logistic-regression/datasets/generate_data.py index 0b346981..7109ccc4 100644 --- a/he-samples/examples/logistic-regression/datasets/generate_data.py +++ b/he-samples/examples/logistic-regression/datasets/generate_data.py @@ -21,21 +21,21 @@ def doTrain(Xtrain, ytrain, Xtest, ytest, epochs=10, verbose=False): """Efficient logistic regression training - Efficient logistic regression training by Bergamaschi et. al (https://eprint.iacr.org/2019/425) - Provides a fast/efficient logistic regression training with cleartext data - - Args: - Xtrain (numpy.array): Training data samples in numpy 2d array - ytrain (numpy.array): Target of training set - Xtest (numpy.array): Test data samples for validation in numpy 2d array - ytest (numpy.array): Target of test set for validation - epochs (int): Number of training epochs. Default = 10 - verbose (bool): Set to True for printing training progress. Default = False - - Returns: - bias (float): bias of logistic regression trained model - weights (numpy.array): weights of logistic regression trained model - """ + Efficient logistic regression training by Bergamaschi et. al (https://eprint.iacr.org/2019/425) + Provides a fast/efficient logistic regression training with cleartext data + + Args: + Xtrain (numpy.array): Training data samples in numpy 2d array + ytrain (numpy.array): Target of training set + Xtest (numpy.array): Test data samples for validation in numpy 2d array + ytest (numpy.array): Target of test set for validation + epochs (int): Number of training epochs. Default = 10 + verbose (bool): Set to True for printing training progress. Default = False + + Returns: + bias (float): bias of logistic regression trained model + weights (numpy.array): weights of logistic regression trained model + """ v = lrb.get_initweight(Xtrain, ytrain) w = v @@ -45,7 +45,7 @@ def doTrain(Xtrain, ytrain, Xtest, ytest, epochs=10, verbose=False): print("== Logistic Regression Training ==") for i in range(epochs): learning_rate = 10.0 / ((i + 1) + 1) - new_lmbda = (1.0 + np.sqrt(1 + 4 * lmbda ** 2)) / 2.0 + new_lmbda = (1.0 + np.sqrt(1 + 4 * lmbda**2)) / 2.0 smoothing = (1 - lmbda) / new_lmbda lmbda = new_lmbda @@ -76,14 +76,14 @@ class DataMode(Enum): def saveData(dataName, X, y, datamode: DataMode = DataMode.eval): """Save data samples to csv file - Stores the data samples to be used for the LRHE example. + Stores the data samples to be used for the LRHE example. - Args: - dataName (str): data name prefix - X (numpy.array): data samples to be stored in 2d numpy array - y (numpy.array): targets to be stored in a 1d numpy array - datamode (DataMode): Determines the suffix [train, test, eval]. Default = DataMode.eval - """ + Args: + dataName (str): data name prefix + X (numpy.array): data samples to be stored in 2d numpy array + y (numpy.array): targets to be stored in a 1d numpy array + datamode (DataMode): Determines the suffix [train, test, eval]. Default = DataMode.eval + """ nFeatures = X.shape[1] suffix = datamode.name features = [f"feature_{i}" for i in range(nFeatures)] @@ -101,13 +101,13 @@ def saveData(dataName, X, y, datamode: DataMode = DataMode.eval): def saveModel(dataName, b, w): """Save logistic regression model to csv file - Stores the model to be used for the LRHE example. + Stores the model to be used for the LRHE example. - Args: - dataName (str): data name prefix - b (float): bias of LR model - w (numpy.array): weights of LR model - """ + Args: + dataName (str): data name prefix + b (float): bias of LR model + w (numpy.array): weights of LR model + """ lr_model = np.concatenate(([b], w), axis=0).tolist() with open(f"{dataName}_lrmodel.csv", "w") as csvfile: writer = csv.writer(csvfile, delimiter=",") @@ -117,23 +117,23 @@ def saveModel(dataName, b, w): def generateSynData(nSamples, nFeatures): """Generate synthetic dataset - Generates synthetic datasets with the use of sklearn.datasets.make_classification. - Splits the entire dataset into train, test and eval with the ratio of 2:1:1. + Generates synthetic datasets with the use of sklearn.datasets.make_classification. + Splits the entire dataset into train, test and eval with the ratio of 2:1:1. - Note that this will generate purposely well-fitted data samples for LR training + Note that this will generate purposely well-fitted data samples for LR training - Args: - nSamples (int): number of data samples - nFeatures (int): number of features + Args: + nSamples (int): number of data samples + nFeatures (int): number of features - Returns: - Xtrain (numpy.array): train dataset. 1/4 of nSamples - ytrain (numpy.array): train target - Xtest (numpy.array): test dataset. 1/4 of nSamples - ytest (numpy.array): test target - Xeval (numpy.array): eval dataset. 1/4 of nSamples - yeval (numpy.array): eval target - """ + Returns: + Xtrain (numpy.array): train dataset. 1/4 of nSamples + ytrain (numpy.array): train target + Xtest (numpy.array): test dataset. 1/4 of nSamples + ytest (numpy.array): test target + Xeval (numpy.array): eval dataset. 1/4 of nSamples + yeval (numpy.array): eval target + """ data = sklearn.datasets.make_classification( n_samples=nSamples, diff --git a/he-samples/examples/logistic-regression/datasets/lr_base.py b/he-samples/examples/logistic-regression/datasets/lr_base.py index 90fe2ac2..b6d9dbf2 100644 --- a/he-samples/examples/logistic-regression/datasets/lr_base.py +++ b/he-samples/examples/logistic-regression/datasets/lr_base.py @@ -21,7 +21,7 @@ def sigmoid_poly3(x): # 4-degree polynomial representation of log(sigmoid(x)) function, effective in range of [-5, 5] def log_sig4(x): - return 0.000527 * x ** 4 - 0.0822 * x ** 2 + 0.5 * x - 0.78 + return 0.000527 * x**4 - 0.0822 * x**2 + 0.5 * x - 0.78 # Realign target to -1, 1 and calculate X@y' diff --git a/kit/commands/check_deps.py b/kit/commands/check_deps.py index 7bc214ca..7c16180c 100644 --- a/kit/commands/check_deps.py +++ b/kit/commands/check_deps.py @@ -53,7 +53,7 @@ def make_from_str(cls, dep_str: str) -> Dep: def version_string_to_tuple(ver_str: str) -> Tuple[int, ...]: - """version '10.11.12' -> (10, 11, 12) """ + """version '10.11.12' -> (10, 11, 12)""" try: return tuple(int(i) for i in ver_str.split(".")) except ValueError as e: diff --git a/kit/commands/docker_build.py b/kit/commands/docker_build.py index e49263ba..3d4a2411 100644 --- a/kit/commands/docker_build.py +++ b/kit/commands/docker_build.py @@ -24,7 +24,6 @@ def try_setup_docker(args): # pylint: disable=unused-argument print("This command is disabled. To enable it install the docker-py dependency") print(" pip install docker") - else: def try_setup_docker(args): diff --git a/kit/commands/init.py b/kit/commands/init.py index cf0abbe2..a90afc97 100644 --- a/kit/commands/init.py +++ b/kit/commands/init.py @@ -19,7 +19,7 @@ class Tags: def file_exists(file: Path) -> bool: - """ Wrapper to check if file exists because Path.exists() cannot be mocked + """Wrapper to check if file exists because Path.exists() cannot be mocked directly due to being used internally by pytest creating some clash""" return file.exists() @@ -85,7 +85,7 @@ def append_to_rc(path: Path, content: str) -> None: def get_rc_file() -> Path: - """ Return the correct file to add shell commands""" + """Return the correct file to add shell commands""" active_shell_path = Path(environment["SHELL"]).name if active_shell_path == "bash": @@ -138,7 +138,7 @@ def init_hekit(args) -> None: # 2-Register hekit link and hekit.py script to enable tab completion eval_lines = ( "if [ -n $(type -p register-python-argcomplete) ]; then\n" - ' eval "$(register-python-argcomplete hekit)"\n' + ' eval "$(register-python-argcomplete hekit)"\n' "fi\n" ) content = "".join([export_line, path_line, eval_lines]) diff --git a/kit/commands/list_cmd.py b/kit/commands/list_cmd.py index 8f8804b9..368413f7 100644 --- a/kit/commands/list_cmd.py +++ b/kit/commands/list_cmd.py @@ -17,8 +17,8 @@ class RepoProperties: - """ Contains a dictionary with the structure of the repo - and widths of the widest component and instance""" + """Contains a dictionary with the structure of the repo + and widths of the widest component and instance""" def __init__(self, repo_location: str, separation_spaces: int = _SEP_SPACES): # Get the components and instances @@ -50,7 +50,7 @@ def structure(self) -> Dict[str, List[str]]: @staticmethod def _repo_struct(path: PathType) -> Dict[str, List[str]]: """Return a dictionary with sorted keys as components and values as - sorted list of instances""" + sorted list of instances""" path = Path(path) return {component: list_dirs(path / component) for component in list_dirs(path)} diff --git a/kit/tools/healg.py b/kit/tools/healg.py index 67484f17..92cd80e0 100644 --- a/kit/tools/healg.py +++ b/kit/tools/healg.py @@ -43,15 +43,15 @@ def write_primes(start: int, stop: int, outfile=stdout): def powerset(iterable): """Returns a generator. - Note that we do not return the empty set. - https://docs.python.org/3/library/itertools.html""" + Note that we do not return the empty set. + https://docs.python.org/3/library/itertools.html""" s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(1, len(s) + 1)) def find_ms(ps, ds, factorize): """Generator returns the p, gen for max m's for p^d""" - prime_factors = factorize(p ** d - 1 for p in ps for d in ds) + prime_factors = factorize(p**d - 1 for p in ps for d in ds) if prime_factors: all_factors = tuple(powerset(primes) for primes in prime_factors) pd = ((p, d) for p in ps for d in ds) @@ -62,7 +62,7 @@ def find_ms(ps, ds, factorize): def str_to_range(s): """Parse a string and return a Python range object. - This function expects a positive integer.""" + This function expects a positive integer.""" if s.isdigit(): num = int(s) return range(num, num + 1) @@ -128,7 +128,7 @@ def is_prime(self, n): def parse_factor_line(line): - """ 'num: f1 f2 f3' -> (num, (f1, f2, f3))""" + """'num: f1 f2 f3' -> (num, (f1, f2, f3))""" split_line = line.split() # ['key:', 'v1', 'v2' , ...] key = split_line[0] if key[-1] != ":": @@ -140,7 +140,7 @@ def parse_factor_line(line): def compute_prime_factors(numbers, factor_util="factor"): """Return generator. Keys m, Value prime factors. - Process out to factor""" + Process out to factor""" numbers = list(numbers) if len(numbers) == 0: @@ -193,8 +193,8 @@ def set_gen_algebras_subparser(subparsers): def phi(prime_factors): - """"Euler's totient from prime factors. - This function assumes that only primes are passed in.""" + """ "Euler's totient from prime factors. + This function assumes that only primes are passed in.""" c = Counter(prime_factors) return math.prod((p - 1) * p ** (k - 1) for p, k in c.items()) @@ -206,7 +206,7 @@ def correct_for_d(p, d, m): This function expects that p is a prime number.""" for e in range(1, d + 1): - if (p ** e) % m == 1: + if (p**e) % m == 1: break return e, e != d diff --git a/kit/utils/component_builder.py b/kit/utils/component_builder.py index bda610c2..1723ccb7 100644 --- a/kit/utils/component_builder.py +++ b/kit/utils/component_builder.py @@ -22,7 +22,7 @@ def __init__(self, message, error): def chain_run(funcs: Iterable[Callable]): """Run functions sequentially. Fail at first function with failed - return value.""" + return value.""" for fn in funcs: success, return_code = fn() if not success: @@ -56,7 +56,7 @@ def run(cmd_and_args: Union[str, List[str]]) -> Tuple[bool, int]: def try_run(spec: dict, attrib: str): """Try to run the attrib in the spec. - Do nothing (pass success) if no key in dict. + Do nothing (pass success) if no key in dict. """ try: return run(spec[attrib]) @@ -118,7 +118,7 @@ def setup(self): def already_successful(self, stage): """Returns True if stage already recorded in info file - as successful""" + as successful""" return self._info_file["status"][stage] == "success" def update_info_file(self, stage, success): diff --git a/kit/utils/spec.py b/kit/utils/spec.py index 65899548..b89f9528 100644 --- a/kit/utils/spec.py +++ b/kit/utils/spec.py @@ -107,8 +107,8 @@ def fill_dep_str(s: str) -> str: def get_dependencies(instances_list: List) -> List[str]: - """ Returns a list of dependencies defined - and used in the recipe file """ + """Returns a list of dependencies defined + and used in the recipe file""" dependency_list: List[str] = [] def fill_dependencies_list(s: str, d: dict) -> None: @@ -135,7 +135,7 @@ def fill_dependencies_list(s: str, d: dict) -> None: def fill_rloc_paths(d: dict, repo_location: PathType) -> dict: """Create absolute path for the top-level attribs that begin - with 'init_' or '_export_' by prepending repo location""" + with 'init_' or '_export_' by prepending repo location""" for k, v in d.items(): if k.startswith("init_") or k.startswith("export_"): d[k] = f"{repo_location}/{v}"