Skip to content

Commit

Permalink
Refactor to latest black code style (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
denCalderon authored Aug 17, 2022
1 parent 213bd9a commit 0d5f099
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
Expand Down
86 changes: 43 additions & 43 deletions he-samples/examples/logistic-regression/datasets/generate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)]
Expand All @@ -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=",")
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion kit/commands/check_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion kit/commands/docker_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions kit/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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])
Expand Down
6 changes: 3 additions & 3 deletions kit/commands/list_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}

Expand Down
18 changes: 9 additions & 9 deletions kit/tools/healg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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] != ":":
Expand All @@ -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:
Expand Down Expand Up @@ -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())

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions kit/utils/component_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions kit/utils/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}"
Expand Down

0 comments on commit 0d5f099

Please sign in to comment.