Skip to content

Commit ac08170

Browse files
committed
Fix pre-existing flake8 errors (unused imports, whitespace, bug)
- Remove unused imports: LazyLoader, numpy, typing.List, random - Fix missing whitespace around arithmetic operators (E226) - Fix whitespace before ']' (E202) - Fix bug in DifferentialEvolution: best_score was never updated, causing all candidates to be accepted instead of only improvements
1 parent d68f1fd commit ac08170

8 files changed

Lines changed: 9 additions & 14 deletions

File tree

tests/test_word_embedding.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def test_embedding_paragramcf():
1919
@pytest.mark.skipif(not _gensim_available, reason="gensim is not installed")
2020
def test_embedding_gensim():
2121
# download a trained word2vec model
22-
from textattack.shared.utils import LazyLoader
2322
from textattack.shared.utils.install import TEXTATTACK_CACHE_DIR
2423

2524
path = os.path.join(TEXTATTACK_CACHE_DIR, "test_gensim_embedding.txt")

textattack/attacker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __init__(self, attack, dataset, attack_args=None):
9292
def _get_worklist(self, start, end, num_examples, shuffle):
9393
if end - start < num_examples:
9494
logger.warn(
95-
f"Attempting to attack {num_examples} samples when only {end-start} are available."
95+
f"Attempting to attack {num_examples} samples when only {end - start} are available."
9696
)
9797
candidates = list(range(start, end))
9898
if shuffle:

textattack/goal_functions/custom/named_entity_recognition.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import json
88

9-
import numpy as np
10-
119
from textattack.goal_function_results import NamedEntityRecognitionGoalFunctionResult
1210
from textattack.goal_functions import GoalFunction
1311

textattack/search_methods/differential_evolution.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import List
2-
31
import numpy as np
42
from scipy.optimize import differential_evolution
53

@@ -60,6 +58,7 @@ def obj(perturbation_vector):
6058
result = self.get_goal_results([cand])[0][0]
6159
cur_score = -result.score
6260
if cur_score <= best_score:
61+
best_score = cur_score
6362
best_result_found = result
6463
return cur_score
6564

textattack/trainer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def train(self):
753753

754754
if self._global_step > 0:
755755
prog_bar.set_description(
756-
f"Loss {self._total_loss/self._global_step:.5f}"
756+
f"Loss {self._total_loss / self._global_step:.5f}"
757757
)
758758

759759
# TODO: Better way to handle TB and Wandb logging
@@ -804,7 +804,7 @@ def train(self):
804804
correct_predictions = (preds == targets).sum().item()
805805
accuracy = correct_predictions / len(targets)
806806
metric_log = {"train/train_accuracy": accuracy}
807-
logger.info(f"Train accuracy: {accuracy*100:.2f}%")
807+
logger.info(f"Train accuracy: {accuracy * 100:.2f}%")
808808
else:
809809
pearson_correlation, pearson_pvalue = scipy.stats.pearsonr(
810810
preds, targets
@@ -920,7 +920,7 @@ def evaluate(self):
920920
eval_score = accuracy
921921

922922
if self._metric_name == "accuracy":
923-
logger.info(f"Eval {self._metric_name}: {eval_score*100:.2f}%")
923+
logger.info(f"Eval {self._metric_name}: {eval_score * 100:.2f}%")
924924
else:
925925
logger.info(f"Eval {self._metric_name}: {eval_score:.4f}%")
926926

textattack/training_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def _create_dataset_from_args(cls, args):
557557
label >= 0
558558
for label in train_dataset_labels_set
559559
if isinstance(label, int)
560-
), f"Train dataset has negative label/s {[label for label in train_dataset_labels_set if isinstance(label,int) and label < 0 ]} which is/are not supported by pytorch.Use --filter-train-by-labels to keep suitable labels"
560+
), f"Train dataset has negative label/s {[label for label in train_dataset_labels_set if isinstance(label, int) and label < 0]} which is/are not supported by pytorch. Use --filter-train-by-labels to keep suitable labels"
561561

562562
assert num_labels >= len(
563563
train_dataset_labels_set
@@ -569,7 +569,7 @@ def _create_dataset_from_args(cls, args):
569569
label >= 0
570570
for label in eval_dataset_labels_set
571571
if isinstance(label, int)
572-
), f"Eval dataset has negative label/s {[label for label in eval_dataset_labels_set if isinstance(label,int) and label < 0 ]} which is/are not supported by pytorch.Use --filter-eval-by-labels to keep suitable labels"
572+
), f"Eval dataset has negative label/s {[label for label in eval_dataset_labels_set if isinstance(label, int) and label < 0]} which is/are not supported by pytorch. Use --filter-eval-by-labels to keep suitable labels"
573573

574574
assert num_labels >= len(
575575
set(eval_dataset_labels_set)

textattack/transformations/word_swaps/word_swap_differential_evolution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Word Swap for Differential Evolution
33
-------------------------------------
4-
Extends WordSwap.
4+
Extends WordSwap.
55
6-
If a Transformation wants to be compatible with
6+
If a Transformation wants to be compatible with
77
textattack.search_methods.DifferentialEvolution,
88
then it must extend from this class.
99
"""

textattack/transformations/word_swaps/word_swap_invisible_characters.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
-----------------------------------
44
"""
55

6-
import random
76
from typing import List, Tuple
87

98
import numpy as np

0 commit comments

Comments
 (0)