Skip to content

Commit

Permalink
Extend linting with ruff (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 committed Jan 8, 2024
1 parent 9e89c22 commit d9a9da8
Show file tree
Hide file tree
Showing 49 changed files with 174 additions and 117 deletions.
24 changes: 14 additions & 10 deletions asreview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# deprecated in __init__.py, use asreview.models.feature_extraction instead
from asreview._deprecated import _deprecated_func
from asreview.data.base import ASReviewData
from asreview.data.base import load_data
from asreview.io.utils import list_readers
from asreview.io.utils import list_writers
from asreview.project import ASReviewProject
from asreview.project import open_state
from asreview.utils import asreview_path
from asreview.utils import get_data_home

# deprecated in __init__.py, use asreview.models.feature_extraction instead
from asreview._deprecated import _deprecated_func
from asreview.models.feature_extraction.embedding_lstm import (
load_embedding as _load_embedding,
) # NOQA
)

# NOQA
from asreview.models.feature_extraction.embedding_lstm import (
sample_embedding as _sample_embedding,
) # NOQA
)

# NOQA
from asreview.models.feature_extraction.embedding_lstm import (
text_to_features as _text_to_features,
) # NOQA
)
from asreview.project import ASReviewProject
from asreview.project import open_state
from asreview.utils import asreview_path
from asreview.utils import get_data_home

# NOQA
from ._version import get_versions

__version__ = get_versions()["version"]
Expand Down
2 changes: 1 addition & 1 deletion asreview/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def main():
"-V",
"--version",
action="version",
version="%(prog)s {version}".format(version=__version__),
version=f"%(prog)s {__version__}",
)

args, _ = parser.parse_known_args()
Expand Down
7 changes: 5 additions & 2 deletions asreview/_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _deprecated_func(msg):
def dec(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(msg)
warnings.warn(msg, stacklevel=2)
return func(*args, **kwargs)

return wrapper
Expand All @@ -32,7 +32,10 @@ def wrapper(*args, **kwargs):

class DeprecateAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
logging.warning(f"Argument {self.option_strings} is deprecated and is ignored.")
logging.warning(
f"Argument {self.option_strings} is deprecated and is ignored.",
stacklevel=2,
)
delattr(namespace, self.dest)


Expand Down
9 changes: 4 additions & 5 deletions asreview/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def run_command(
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
print(f"unable to find command, tried {commands}")
return None, None
stdout = process.communicate()[0].strip().decode()
if process.returncode != 0:
Expand Down Expand Up @@ -163,8 +163,7 @@ def versions_from_parentdir(

if verbose:
print(
"Tried directories %s but none started with prefix %s"
% (str(rootdirs), parentdir_prefix)
f"Tried directories {str(rootdirs)} but none started with prefix {parentdir_prefix}"
)
raise NotThisMethod("rootdir doesn't start with parentdir_prefix")

Expand All @@ -178,7 +177,7 @@ def git_get_keywords(versionfile_abs: str) -> Dict[str, str]:
# _version.py.
keywords: Dict[str, str] = {}
try:
with open(versionfile_abs, "r") as fobj:
with open(versionfile_abs) as fobj:
for line in fobj:
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
Expand Down Expand Up @@ -386,7 +385,7 @@ def git_pieces_from_vcs(
if verbose:
fmt = "tag '%s' doesn't start with prefix '%s'"
print(fmt % (full_tag, tag_prefix))
pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % (
pieces["error"] = "tag '{}' doesn't start with prefix '{}'".format(
full_tag,
tag_prefix,
)
Expand Down
11 changes: 7 additions & 4 deletions asreview/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(
img_url=None,
license=None,
year=None,
aliases=[],
aliases=None,
**kwargs,
):
"""Base class for metadata of dataset.
Expand Down Expand Up @@ -136,6 +136,8 @@ def __init__(
"""

if aliases is None:
aliases = []
self.dataset_id = dataset_id
self.filepath = filepath
self.title = title
Expand Down Expand Up @@ -406,7 +408,7 @@ def __init__(self):
meta_file = "https://raw.githubusercontent.com/asreview/paper-asreview/master/index_v1.json" # noqa
datasets = _download_from_metadata(meta_file)

super(NaturePublicationDataGroup, self).__init__(*datasets)
super().__init__(*datasets)


class SynergyDataSet(BaseDataSet):
Expand Down Expand Up @@ -744,7 +746,7 @@ def __init__(self):

datasets = [SynergyDataSet(k, **v) for k, v in synergy_metadata.items()]

super(SynergyDataGroup, self).__init__(*datasets)
super().__init__(*datasets)


class BenchmarkDataGroup(BaseDataGroup):
Expand All @@ -761,9 +763,10 @@ def __init__(self):
"use SYNERGY dataset instead. For more information, see "
"https://github.com/asreview/synergy-dataset.",
category=UserWarning,
stacklevel=2,
)

meta_file = "https://raw.githubusercontent.com/asreview/systematic-review-datasets/master/index_v1.json" # noqa
datasets = _download_from_metadata(meta_file)

super(BenchmarkDataGroup, self).__init__(*datasets)
super().__init__(*datasets)
2 changes: 1 addition & 1 deletion asreview/entry_points/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _deprecated_dev_mode():
exit(1)


class LABEntryPoint(object):
class LABEntryPoint:
"""Entry point to start the ASReview LAB webapp."""

def execute(self, argv):
Expand Down
2 changes: 1 addition & 1 deletion asreview/entry_points/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def execute(self, argv): # noqa
prior_df = s.get_priors()

print("The following records are prior knowledge:\n")
for i, row in prior_df.iterrows():
for _i, row in prior_df.iterrows():
preview = as_data.record(row["record_id"])
print(preview)

Expand Down
2 changes: 1 addition & 1 deletion asreview/io/excel_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def read_data(cls, fp):
best_sheet = None
sheet_obj_val = -1
wanted_columns = []
for type_name, type_list in COLUMN_DEFINITIONS.items():
for _type_name, type_list in COLUMN_DEFINITIONS.items():
wanted_columns.extend(type_list)

for sheet_name in dfs:
Expand Down
4 changes: 3 additions & 1 deletion asreview/io/paper_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class PaperRecord:
Any extra keyword arguments will be put in self.extra_fields.
"""

def __init__(self, record_id, column_spec={}, **kwargs):
def __init__(self, record_id, column_spec=None, **kwargs):
if column_spec is None:
column_spec = {}
for attr in [
"title",
"abstract",
Expand Down
2 changes: 1 addition & 1 deletion asreview/io/ris.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _strip_zotero_p_tags(note_list):

@classmethod
def _read_from_file(cls, fp, encoding="utf8"):
with open(fp, "r", encoding=encoding) as bibliography_file:
with open(fp, encoding=encoding) as bibliography_file:
return list(rispy.load(bibliography_file, skip_unknown_tags=True))

@classmethod
Expand Down
4 changes: 3 additions & 1 deletion asreview/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _is_record_id_int(s):
raise ValueError("Column 'record_id' should contain integer values.")


def _standardize_dataframe(df, column_def={}):
def _standardize_dataframe(df, column_def=None):
"""Create a ASReview readable dataframe.
The main purpose is to rename columns with slightly different names;
Expand All @@ -95,6 +95,8 @@ def _standardize_dataframe(df, column_def={}):
pd.DataFrame:
Cleaned dataframe with proper column names.
"""
if column_def is None:
column_def = {}
all_column_spec = {}

# remove whitespace from colnames
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/balance/double.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DoubleBalance(BaseBalance):
label = "Dynamic resampling (Double)"

def __init__(self, a=2.155, alpha=0.94, b=0.789, beta=1.0, random_state=None):
super(DoubleBalance, self).__init__()
super().__init__()
self.a = a
self.alpha = alpha
self.b = b
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/balance/triple.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
):
"""Initialize the triple balance strategy."""

super(TripleBalance, self).__init__()
super().__init__()
self.a = a
self.alpha = alpha
self.b = b
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/balance/undersample.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class UndersampleBalance(BaseBalance):

def __init__(self, ratio=1.0, random_state=None):
"""Initialize the undersampling balance strategy."""
super(UndersampleBalance, self).__init__()
super().__init__()
self.ratio = ratio
self._random_state = get_random_state(random_state)

Expand Down
2 changes: 1 addition & 1 deletion asreview/models/classifiers/logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LogisticClassifier(BaseTrainClassifier):
label = "Logistic regression"

def __init__(self, C=1.0, class_weight=1.0, random_state=None, n_jobs=1):
super(LogisticClassifier, self).__init__()
super().__init__()
self.C = C
self.class_weight = class_weight
self.n_jobs = n_jobs
Expand Down
4 changes: 2 additions & 2 deletions asreview/models/classifiers/lstm_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(
class_weight=30.0,
):
"""Initialize the LSTM base model"""
super(LSTMBaseClassifier, self).__init__()
super().__init__()
self.embedding_matrix = embedding_matrix
self.backwards = backwards
self.dropout = dropout
Expand Down Expand Up @@ -175,7 +175,7 @@ def full_hyper_space(self):

@property
def default_param(self):
defaults = super(LSTMBaseClassifier, self).default_param
defaults = super().default_param
defaults.pop("embedding_matrix")
return defaults

Expand Down
4 changes: 2 additions & 2 deletions asreview/models/classifiers/lstm_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(
class_weight=30.0,
):
"""Initialize the LSTM pool model."""
super(LSTMPoolClassifier, self).__init__()
super().__init__()
self.embedding_matrix = embedding_matrix
self.backwards = backwards
self.dropout = dropout
Expand Down Expand Up @@ -178,7 +178,7 @@ def full_hyper_space(self):

@property
def default_param(self):
defaults = super(LSTMPoolClassifier, self).default_param
defaults = super().default_param
defaults.pop("embedding_matrix")
return defaults

Expand Down
2 changes: 1 addition & 1 deletion asreview/models/classifiers/nb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NaiveBayesClassifier(BaseTrainClassifier):
label = "Naive Bayes"

def __init__(self, alpha=3.822):
super(NaiveBayesClassifier, self).__init__()
super().__init__()
self.alpha = alpha
self._model = MultinomialNB(alpha=alpha)
logging.debug(self._model)
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/classifiers/nn_2_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(
class_weight=30.0,
):
"""Initialize the 2-layer neural network model."""
super(NN2LayerClassifier, self).__init__()
super().__init__()
self.dense_width = int(dense_width)
self.optimizer = optimizer
self.learn_rate = learn_rate
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/classifiers/rf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RandomForestClassifier(BaseTrainClassifier):
def __init__(
self, n_estimators=100, max_features=10, class_weight=1.0, random_state=None
):
super(RandomForestClassifier, self).__init__()
super().__init__()
self.n_estimators = int(n_estimators)
self.max_features = int(max_features)
self.class_weight = class_weight
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/classifiers/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(
kernel="linear",
random_state=None,
):
super(SVMClassifier, self).__init__()
super().__init__()
self.gamma = gamma
self.class_weight = class_weight
self.C = C
Expand Down
4 changes: 2 additions & 2 deletions asreview/models/feature_extraction/doc2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(
**kwargs,
):
"""Initialize the doc2vec model."""
super(Doc2Vec, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.vector_size = int(vector_size)
self.epochs = int(epochs)
self.min_count = int(min_count)
Expand Down Expand Up @@ -168,7 +168,7 @@ def full_hyper_space(self):

eps = 1e-7

hyper_space, hyper_choices = super(Doc2Vec, self).full_hyper_space()
hyper_space, hyper_choices = super().full_hyper_space()
hyper_space.update(
{
"fex_vector_size": hp.quniform("fex_vector_size", 31.5, 127.5 - eps, 8),
Expand Down
2 changes: 1 addition & 1 deletion asreview/models/feature_extraction/embedding_idf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EmbeddingIdf(BaseFeatureExtraction):

def __init__(self, *args, embedding_fp=None, random_state=None, **kwargs):
"""Initialize the Embedding-Idf model."""
super(EmbeddingIdf, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.embedding_fp = embedding_fp
self.embedding = None
self._random_state = get_random_state(random_state)
Expand Down
8 changes: 4 additions & 4 deletions asreview/models/feature_extraction/embedding_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def __init__(
**kwargs,
):
"""Initialize the embedding matrix feature extraction."""
super(EmbeddingLSTM, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.embedding = None
self.num_words = num_words
self.max_sequence_length = max_sequence_length
Expand Down Expand Up @@ -132,7 +132,7 @@ def get_embedding_matrix(self, texts, embedding_fp):
def full_hyper_space(self):
from hyperopt import hp

hyper_space, hyper_choices = super(EmbeddingLSTM, self).full_hyper_space()
hyper_space, hyper_choices = super().full_hyper_space()
hyper_space.update({"fex_loop_sequences": hp.randint("fex_loop_sequences", 2)})
return hyper_space, hyper_choices

Expand Down Expand Up @@ -222,7 +222,7 @@ def _embedding_reader(filename, input_queue, block_size=1000):
Number of lines for each job.
"""

with open(filename, "r", encoding="utf-8", newline="\n") as f:
with open(filename, encoding="utf-8", newline="\n") as f:
# Throw away the first line, since we don't care about the dimensions.
f.readline()

Expand Down Expand Up @@ -404,7 +404,7 @@ def load_embedding(fp, word_index=None, n_jobs=None):
input_queue = Queue(queue_size)
output_queue = Queue()

with open(fp, "r", encoding="utf-8", newline="\n") as f:
with open(fp, encoding="utf-8", newline="\n") as f:
n_words, emb_vec_dim = list(map(int, f.readline().split(" ")))

logging.debug(f"Reading {n_words} vectors with {emb_vec_dim} dimensions.")
Expand Down

0 comments on commit d9a9da8

Please sign in to comment.