Skip to content

Commit 6558e07

Browse files
authored
Merge pull request #40 from EliahKagan/strings
Improve code style for strings
2 parents d6824bb + 3a06b8c commit 6558e07

File tree

7 files changed

+56
-43
lines changed

7 files changed

+56
-43
lines changed

examples/ag_news.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ def fit_model(
7070

7171

7272
def main() -> None:
73-
print(f"Fetching data...")
73+
print("Fetching data...")
7474
((train_text, train_labels), (test_text, test_labels)) = get_data()
7575

76-
print(f"Fitting model...")
76+
print("Fitting model...")
7777
model = fit_model(train_text, train_labels)
7878
random_indicies = np.random.choice(test_text.shape[0], 1000, replace=False)
7979

8080
sample_test_text = test_text[random_indicies]
8181
sample_test_labels = test_labels[random_indicies]
8282

83-
print(f"Generating predictions...")
83+
print("Generating predictions...")
8484
top_k = 1
8585

8686
# Here we use the `sampling_percentage` to save time

examples/imdb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def fit_model(
7070

7171

7272
def main() -> None:
73-
print(f"Fetching data...")
73+
print("Fetching data...")
7474
((train_text, train_labels), (test_text, test_labels)) = get_data()
7575

76-
print(f"Fitting model...")
76+
print("Fitting model...")
7777
model = fit_model(train_text, train_labels)
7878

7979
# Randomly sampling from the test set.
@@ -87,7 +87,7 @@ def main() -> None:
8787
sample_test_text = test_text[random_indicies]
8888
sample_test_labels = test_labels[random_indicies]
8989

90-
print(f"Generating predictions...")
90+
print("Generating predictions...")
9191
top_k = 1
9292

9393
# Here we use the `sampling_percentage` to save time

npc_gzip/exceptions.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class InvalidCompressorException(Exception):
1111

1212
def __init__(self, compression_library: str) -> None:
1313
self.message = f"""
14-
Compression Library ({compression_library})
14+
Compression Library ({compression_library})
1515
is not currently supported.
1616
"""
1717
super().__init__(self.message)
@@ -25,10 +25,10 @@ class MissingDependencyException(Exception):
2525

2626
def __init__(self, compression_library: str) -> None:
2727
self.message = f"""
28-
Compression Library ({compression_library})
29-
is missing an underlying dependency. Try
30-
installing those missing dependencies and
31-
load this again.
28+
Compression Library ({compression_library})
29+
is missing an underlying dependency. Try
30+
installing those missing dependencies and
31+
load this again.
3232
3333
Common missing dependencies for:
3434
@@ -50,7 +50,7 @@ def __init__(
5050
self.message = f"""
5151
Unable to aggregate ({stringa}) and ({stringb}).
5252
One or both of the two strings are too short to concatenate.
53-
53+
5454
"""
5555

5656
if function_name is not None:
@@ -66,11 +66,11 @@ def __init__(
6666
compressed_value_b: Optional[float] = None,
6767
function_name: Optional[str] = None,
6868
) -> None:
69-
self.message = f"""
70-
The combination of compressed values passed equal zero.
69+
self.message = """
70+
The combination of compressed values passed equal zero.
7171
This will result in a divide by zero error.
7272
73-
73+
7474
"""
7575

7676
if function_name is not None:
@@ -91,7 +91,7 @@ def __init__(
9191
arg1: {type(a)}
9292
arg2: {type(b)}
9393
arg3: {type(c)}
94-
94+
9595
"""
9696

9797
if function_name is not None:
@@ -112,7 +112,7 @@ def __init__(
112112
arg1: {array_a.shape}
113113
arg2: {array_b.shape}
114114
arg3: {array_c.shape}
115-
115+
116116
"""
117117

118118
if function_name is not None:
@@ -128,11 +128,11 @@ def __init__(
128128
function_name: Optional[str] = None,
129129
) -> None:
130130
self.message = f"""
131-
The `distance_metric` ({distance_metric}) provided is not
131+
The `distance_metric` ({distance_metric}) provided is not
132132
currently supported. Please submit an Issue and/or
133133
Pull Request here to add support:
134134
https://github.com/bazingagin/npc_gzip
135-
135+
136136
"""
137137

138138
if supported_distance_metrics is not None:
@@ -153,9 +153,9 @@ def __init__(
153153
function_name: Optional[str] = None,
154154
) -> None:
155155
self.message = f"""
156-
The type passed ({passed_type}) provided is not
157-
currently supported.
158-
156+
The type passed ({passed_type}) provided is not
157+
currently supported.
158+
159159
"""
160160

161161
if supported_types is not None:
@@ -174,13 +174,13 @@ def __init__(
174174
function_name: Optional[str] = None,
175175
) -> None:
176176
self.message = f"""
177-
If training labels are passed, the number
178-
of training data samples must equal the
177+
If training labels are passed, the number
178+
of training data samples must equal the
179179
number of training label samples
180-
180+
181181
training_samples: {training_samples}
182182
label_samples: {label_samples}
183-
183+
184184
"""
185185

186186
if function_name is not None:

npc_gzip/knn_classifier.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ class KnnClassifier:
2727
>>> training_labels = [random.randint(0, 1) for _ in range(len(training_data))]
2828
>>> assert len(training_data) == len(training_labels)
2929
30-
>>> model = KnnClassifier(compressor=GZipCompressor(), training_inputs=training_data, training_labels=training_labels, distance_metric="ncd")
30+
>>> model = KnnClassifier(
31+
... compressor=GZipCompressor(),
32+
... training_inputs=training_data,
33+
... training_labels=training_labels,
34+
... distance_metric="ncd",
35+
... )
3136
3237
>>> test = np.array(["hey", "you are a real pain in my ass", "go away please"])
3338

npc_gzip/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def generate_sentence(number_of_words: int = 10) -> str:
2727
str: Sentence of random numbers and letters.
2828
"""
2929

30-
assert number_of_words > 0, f"`number_of_words` must be greater than zero."
30+
assert number_of_words > 0, "`number_of_words` must be greater than zero."
3131

3232
words = []
3333
for word in range(number_of_words):
@@ -58,7 +58,7 @@ def generate_dataset(number_of_sentences: int) -> list:
5858
list: List of sentences (str).
5959
"""
6060

61-
assert number_of_sentences > 0, f"`number_of_sentences` must be greater than zero."
61+
assert number_of_sentences > 0, "`number_of_sentences` must be greater than zero."
6262

6363
dataset = []
6464
for sentence in range(number_of_sentences):

original_codebase/experiments.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ def calc_dis(
2525
self, data: list, train_data: Optional[list] = None, fast: bool = False
2626
) -> None:
2727
"""
28-
Calculates the distance between either `data` and itself or `data` and `train_data`
29-
and appends the distance to `self.distance_matrix`.
28+
Calculates the distance between either `data` and itself or `data` and
29+
`train_data` and appends the distance to `self.distance_matrix`.
3030
3131
Arguments:
3232
data (list): Data to compute distance between.
3333
train_data (list): [Optional] Training data to compute distance from `data`.
34-
fast (bool): [Optional] Uses the _fast compression length function of `self.compressor`.
34+
fast (bool): [Optional] Uses the _fast compression length function
35+
of `self.compressor`.
3536
3637
Returns:
3738
None: None
@@ -68,13 +69,14 @@ def calc_dis_with_single_compressed_given(
6869
self, data: list, data_len: list = None, train_data: Optional[list] = None
6970
) -> None:
7071
"""
71-
Calculates the distance between either `data`, `data_len`, or `train_data`
72-
and appends the distance to `self.distance_matrix`.
72+
Calculates the distance between either `data`, `data_len`, or
73+
`train_data` and appends the distance to `self.distance_matrix`.
7374
7475
Arguments:
7576
data (list): Data to compute distance between.
7677
train_data (list): [Optional] Training data to compute distance from `data`.
77-
fast (bool): [Optional] Uses the _fast compression length function of `self.compressor`.
78+
fast (bool): [Optional] Uses the _fast compression length function
79+
of `self.compressor`.
7880
7981
Returns:
8082
None: None
@@ -186,7 +188,8 @@ def calc_acc(
186188
k (int?): TODO
187189
label (list): Predicted Labels.
188190
train_label (list): Correct Labels.
189-
provided_distance_matrix (list): Calculated Distance Matrix to use instead of `self.distance_matrix`.
191+
provided_distance_matrix (list): Calculated Distance Matrix to use
192+
instead of `self.distance_matrix`.
190193
rand (bool): TODO
191194
192195
Returns:
@@ -245,7 +248,8 @@ def combine_dis_acc(
245248
train_label: Optional[list] = None,
246249
) -> tuple:
247250
"""
248-
Calculates the distance and the accuracy of the algorithm for data with training.
251+
Calculates the distance and the accuracy of the algorithm for data with
252+
training.
249253
250254
Arguments:
251255
k (int?): TODO
@@ -304,7 +308,8 @@ def combine_dis_acc_single(
304308
label: Any, # int, as used in this application
305309
) -> tuple:
306310
"""
307-
Calculates the distance and the accuracy of the algorithm for a single datum with training.
311+
Calculates the distance and the accuracy of the algorithm for a single
312+
datum with training.
308313
309314
Arguments:
310315
k (int?): TODO

original_codebase/utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def NCD(c1: float, c2: float, c12: float) -> float:
1212
Arguments:
1313
c1 (float): The compressed length of the first object.
1414
c2 (float): The compressed length of the second object.
15-
c12 (float): The compressed length of the concatenation of the first and second objects.
15+
c12 (float): The compressed length of the concatenation of the first
16+
and second objects.
1617
1718
Returns:
1819
float: The Normalized Compression Distance c1 and c2.
@@ -51,7 +52,8 @@ def CDM(c1: float, c2: float, c12: float) -> float:
5152
Arguments:
5253
c1 (float): The compressed length of the first object.
5354
c2 (float): The compressed length of the second object.
54-
c12 (float): The compressed length of the concatenation of the first and second objects.
55+
c12 (float): The compressed length of the concatenation of the first
56+
and second objects.
5557
5658
Returns:
5759
float: The Compound Dissimilarity Measure value between c1 and c2.
@@ -72,7 +74,8 @@ def MSE(v1: np.ndarray, v2: np.ndarray) -> float:
7274
v2 (np.ndarray): The second array.
7375
7476
Returns:
75-
float: The Mean Squared Error value, representing the average squared difference between v1 and v2.
77+
float: The Mean Squared Error value, representing the average squared
78+
difference between v1 and v2.
7679
7780
Formula:
7881
MSE(v1, v2) = Σ((v1 - v2) ** 2) / len(v1)
@@ -200,8 +203,8 @@ def agg_by_min_or_max(
200203
Arguments:
201204
i1 (torch.Tensor): First series of numbers.
202205
i2 (torch.Tensor): Second series of numbers.
203-
aggregate_by_minimum (bool): True if you want to take the minimum of the two series.
204-
False if you want to take the maximum instead.
206+
aggregate_by_minimum (bool): True to take the minimum of the two series.
207+
False to take the maximum instead.
205208
206209
Returns:
207210
torch.Tensor: Average of the two series.

0 commit comments

Comments
 (0)