Skip to content

Commit e2ffac8

Browse files
amrit110fcogidi
andauthored
Use ruff instead of other linting tools (#408)
* Use ruff instead of other linting tools * Make isort compatible with black again, needed for notebooks * Add return type to default internal fn * fix mypy errors for MetricCollection + minor ruff errors * fix ruff errors for evaluate package * Fix ruff errors for query, utils and models packages * Remove pylint config from pyproject.toml * Add --fix arg to ruff check in pre-commit * Add additional ruff fixes * Apply ruff fixes again * Apply ruff fixes on jupyter notebooks * Ignore names that start with X * Add max-doc-length setting to ruff pycodestyle check * Add more fixes * Update deps * Add final round of fixes * Remove pylint disables --------- Co-authored-by: Franklin <[email protected]>
1 parent 64482ab commit e2ffac8

File tree

201 files changed

+3778
-2867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+3778
-2867
lines changed

.pre-commit-config.yaml

+5-46
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,12 @@ repos:
2020
hooks:
2121
- id: black
2222

23-
- repo: https://github.com/PyCQA/isort
24-
rev: 5.12.0
23+
- repo: https://github.com/charliermarsh/ruff-pre-commit
24+
rev: 'v0.0.286'
2525
hooks:
26-
- id: isort
27-
28-
- repo: https://github.com/myint/docformatter
29-
rev: v1.7.5
30-
hooks:
31-
- id: docformatter
32-
args: [--in-place, --wrap-summaries=88, --wrap-descriptions=88, --blank]
33-
files: ".*.py$"
34-
35-
- repo: local
36-
hooks:
37-
- id: flake8
38-
name: flake8
39-
language: python
40-
entry: pflake8
41-
files: ".*.py$"
42-
43-
- repo: https://github.com/PyCQA/pydocstyle
44-
rev: 6.3.0
45-
hooks:
46-
- id: pydocstyle
47-
args: [--convention=numpy]
48-
additional_dependencies: [toml, tomli]
26+
- id: ruff
27+
args: [--fix, --exit-non-zero-on-fix]
28+
types_or: [python, jupyter]
4929

5030
- repo: https://github.com/pre-commit/mirrors-mypy
5131
rev: v1.5.1
@@ -56,15 +36,6 @@ repos:
5636
types: [python]
5737
exclude: "apps|use_cases|tests|cyclops/(process|models|tasks|monitor|report/plot)"
5838

59-
- repo: local
60-
hooks:
61-
- id: pylint
62-
name: pylint
63-
language: python
64-
entry: pylint
65-
files: ".*.py$"
66-
exclude: fsd
67-
6839
- repo: local
6940
hooks:
7041
- id: pytest
@@ -74,18 +45,6 @@ repos:
7445
pass_filenames: false
7546
always_run: true
7647

77-
- repo: https://github.com/nbQA-dev/nbQA
78-
rev: 1.7.0
79-
hooks:
80-
- id: nbqa-black
81-
- id: nbqa-isort
82-
- id: nbqa-check-ast
83-
- id: nbqa-flake8
84-
# ignore E203/W503 to avoid conflict with black:
85-
# https://github.com/psf/black/issues/354
86-
args: ['--ignore=E203,W503']
87-
# - id: nbqa-mypy
88-
8948
- repo: local
9049
hooks:
9150
- id: nbstripout

apps/interface/app.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
visualizer_page_components,
4545
)
4646

47+
4748
ANALYZE_DATA = None
4849

4950
app = Dash(external_stylesheets=[dbc.themes.UNITED], suppress_callback_exceptions=True)
@@ -170,7 +171,7 @@ def toggle_advanced_options(n_clicks, is_open):
170171
State(f"{APP_DIAG}-substring", "value"),
171172
],
172173
)
173-
def run_query( # pylint: disable=too-many-arguments, too-many-locals, too-many-branches
174+
def run_query(
174175
n_clicks: int,
175176
display_limit: int,
176177
save_queries_checked: bool,
@@ -274,10 +275,13 @@ def run_query( # pylint: disable=too-many-arguments, too-many-locals, too-many-
274275
],
275276
)
276277
def upload_data(
277-
local_contents, server_upload_click_timestamp, server_filepath, display_limit
278+
local_contents,
279+
server_upload_click_timestamp,
280+
server_filepath,
281+
display_limit,
278282
):
279283
"""Upload data and display the relevant information."""
280-
global ANALYZE_DATA # pylint: disable=global-statement
284+
global ANALYZE_DATA
281285

282286
if display_limit is None:
283287
return None, *tuple([None] * 2)
@@ -325,7 +329,7 @@ def upload_data(
325329
)
326330
def analyze_column(col_name):
327331
"""Display relevant information given a column name of the data being analyzed."""
328-
global ANALYZE_DATA # pylint: disable=global-statement, W0602
332+
global ANALYZE_DATA
329333

330334
if ANALYZE_DATA is None:
331335
return (None,)
@@ -338,7 +342,8 @@ def analyze_column(col_name):
338342
ANALYZE_DATA[col_name].value_counts().iloc[:50].to_frame().reset_index()
339343
)
340344
value_counts_df = value_counts_df.rename(
341-
{"index": "value", col_name: "count"}, axis=1
345+
{"index": "value", col_name: "count"},
346+
axis=1,
342347
)
343348
value_count_components = generate_table_contents(value_counts_df)
344349
return (value_count_components,)
@@ -354,7 +359,7 @@ def analyze_column(col_name):
354359
)
355360
def update_column_plot(col_name):
356361
"""Update column analysis plot."""
357-
global ANALYZE_DATA # pylint: disable=global-statement, W0602
362+
global ANALYZE_DATA
358363

359364
return [plot_histogram(ANALYZE_DATA, names=col_name, return_fig=True)]
360365

apps/interface/app_query.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Querying functions used in the query page."""
22

3-
# pylint: disable=no-member
4-
53
from typing import Dict
64

75
import pandas as pd
@@ -13,6 +11,7 @@
1311

1412
from .consts import APP_DIAG, APP_ENC # , APP_EVENT
1513

14+
1615
db = MIMICIVQuerier()
1716

1817

@@ -42,7 +41,7 @@ def patient_diagnoses(kwargs):
4241
return db.patient_diagnoses(**kwargs)
4342

4443

45-
def query( # pylint: disable=too-many-arguments
44+
def query(
4645
encounter_checked,
4746
encounter_kwargs,
4847
age_min,
@@ -67,7 +66,9 @@ def query( # pylint: disable=too-many-arguments
6766
else:
6867
diagnoses = patient_diagnoses(diagnosis_kwargs).run()
6968
datas[APP_ENC] = pd.merge(
70-
encounters, diagnoses.drop(SUBJECT_ID, axis=1), on=ENCOUNTER_ID
69+
encounters,
70+
diagnoses.drop(SUBJECT_ID, axis=1),
71+
on=ENCOUNTER_ID,
7172
)
7273

7374
# datas[APP_EVENT] = ...

apps/interface/component_utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def flatten_2d_tuple(tuple_of_tuples):
1616

1717

1818
def generate_table_contents(
19-
data: pd.DataFrame, display_limit: Optional[int] = None
19+
data: pd.DataFrame,
20+
display_limit: Optional[int] = None,
2021
) -> List:
2122
"""Generate the table content objects."""
2223
if display_limit is not None:
@@ -25,8 +26,7 @@ def generate_table_contents(
2526
columns, values = data.columns, data.astype(str).values
2627
header = [html.Tr([html.Th(col) for col in columns])]
2728
rows = [html.Tr([html.Td(cell) for cell in row]) for row in values]
28-
table = [html.Thead(header), html.Tbody(rows)]
29-
return table
29+
return [html.Thead(header), html.Tbody(rows)]
3030

3131

3232
def table_result(title, id_):
@@ -70,7 +70,7 @@ def get_dataframe_info(data):
7070
"Column": data.columns,
7171
"Non-Null Count": len(data) - data.isnull().sum().values,
7272
"Dtype": data.dtypes.values,
73-
}
73+
},
7474
)
7575

7676

apps/interface/tabs/analyze_tab.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..component_utils import table_result
88
from ..consts import APP_PAGE_ANALYZE
99

10+
1011
upload_components = (
1112
html.Label("Specify filename from results"),
1213
dmc.Space(h=10),

apps/interface/tabs/query_tab.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..component_utils import flatten_2d_tuple, table_result
99
from ..consts import APP_DIAG, APP_ENC, APP_EVENT, APP_PAGE_QUERY, TABLE_IDS, TABLES
1010

11+
1112
encounter_components = (
1213
dmc.Checkbox(
1314
id=f"{APP_ENC}-checkbox",
@@ -30,8 +31,8 @@
3031
dcool.TagInput(
3132
id=f"{APP_ENC}-sex",
3233
placeholder="Specify sexes (Blank = All)",
33-
)
34-
]
34+
),
35+
],
3536
),
3637
dmc.Space(h=50),
3738
html.Label("Age"),
@@ -49,10 +50,10 @@
4950
placeholder="Max (Blank = No max)",
5051
min=0,
5152
),
52-
]
53+
],
5354
),
5455
dmc.Space(h=50),
55-
]
56+
],
5657
),
5758
id=f"{APP_ENC}-collapse",
5859
),
@@ -76,8 +77,8 @@
7677
dcool.TagInput(
7778
id=f"{APP_DIAG}-code",
7879
placeholder="Specify codes (Blank = All)",
79-
)
80-
]
80+
),
81+
],
8182
),
8283
dmc.Space(h=50),
8384
html.Label("Diagnosis substring"),
@@ -87,12 +88,12 @@
8788
dcool.TagInput(
8889
id=f"{APP_DIAG}-substring",
8990
placeholder="Specify substrings (Blank = All)",
90-
)
91-
]
91+
),
92+
],
9293
),
9394
dmc.Space(h=50),
9495
dmc.Space(h=20),
95-
]
96+
],
9697
),
9798
id=f"{APP_DIAG}-collapse",
9899
),

apps/interface/tabs/visualizer_tab.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from ..css import CONTENT_STYLE, SIDEBAR_LIST_STYLE, SIDEBAR_STYLE, TEXT_ALIGN_CENTER
1313

14+
1415
STATIC = "static"
1516
TEMPORAL = "temporal"
1617

@@ -27,7 +28,7 @@
2728
"cyclops/use_cases/mimiciv/mortality_decompensation",
2829
"data/1-cleaned",
2930
"batch_0017.parquet",
30-
)
31+
),
3132
)
3233
encounters_events = list(events[ENCOUNTER_ID].unique())[0:80]
3334

@@ -169,7 +170,7 @@
169170
title="Options",
170171
is_open=False,
171172
),
172-
]
173+
],
173174
)
174175

175176

cyclops/data/features/medical_image.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323

2424
from cyclops.utils.log import setup_logging
2525

26+
2627
# Logging.
2728
LOGGER = logging.getLogger(__name__)
2829
setup_logging(print_level="INFO", logger=LOGGER)
2930

30-
# pylint: disable=fixme
31-
3231

3332
@dataclass
3433
class MedicalImage(Image): # type: ignore
@@ -52,15 +51,16 @@ class MedicalImage(Image): # type: ignore
5251
[
5352
LoadImage(reader=reader, simple_keys=True, dtype=None, image_only=True),
5453
ToNumpy(),
55-
]
54+
],
5655
)
5756
# Automatically constructed
5857
dtype: ClassVar[str] = "dict"
5958
pa_type: ClassVar[Any] = pa.struct({"bytes": pa.binary(), "path": pa.string()})
6059
_type: str = field(default="MedicalImage", init=False, repr=False)
6160

6261
def encode_example(
63-
self, value: Union[str, Dict[str, Any], npt.NDArray[Any]]
62+
self,
63+
value: Union[str, Dict[str, Any], npt.NDArray[Any]],
6464
) -> Dict[str, Any]:
6565
"""Encode example into a format for Arrow.
6666
@@ -89,7 +89,9 @@ def encode_example(
8989
if filename is not None and filename != "":
9090
output_ext_ = os.path.splitext(filename)[1]
9191
return _encode_ndarray(
92-
value["array"], metadata=metadata_, image_format=output_ext_
92+
value["array"],
93+
metadata=metadata_,
94+
image_format=output_ext_,
9395
)
9496
if value.get("path") is not None and os.path.isfile(value["path"]):
9597
# we set "bytes": None to not duplicate the data
@@ -102,7 +104,7 @@ def encode_example(
102104

103105
raise ValueError(
104106
"An image sample should have one of 'path' or 'bytes' "
105-
f"but they are missing or None in {value}."
107+
f"but they are missing or None in {value}.",
106108
)
107109

108110
def decode_example(
@@ -130,7 +132,7 @@ def decode_example(
130132
if not self.decode:
131133
raise RuntimeError(
132134
"Decoding is disabled for this feature. "
133-
"Please use MedicalImage(decode=True) instead."
135+
"Please use MedicalImage(decode=True) instead.",
134136
)
135137

136138
if token_per_repo_id is None:
@@ -141,7 +143,7 @@ def decode_example(
141143
if path is None:
142144
raise ValueError(
143145
"An image should have one of 'path' or 'bytes' but both are "
144-
f"None in {value}."
146+
f"None in {value}.",
145147
)
146148

147149
if is_local_path(path):
@@ -170,7 +172,8 @@ def decode_example(
170172
return {"array": image, "metadata": metadata}
171173

172174
def _read_file_from_bytes(
173-
self, buffer: BytesIO
175+
self,
176+
buffer: BytesIO,
174177
) -> Tuple[npt.NDArray[Any], Dict[str, Any]]:
175178
"""Read an image from bytes.
176179
@@ -203,8 +206,8 @@ def _encode_ndarray(
203206
204207
Parameters
205208
----------
206-
array_or_tensor : NdarrayOrTensor
207-
Numpy array or torch tensor.
209+
array : numpy.ndarray
210+
Numpy array to encode.
208211
metadata : dict, optional, default=None
209212
Metadata dictionary.
210213
image_format : str, optional, default=".png"

cyclops/data/loader.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ def load_nihcxr(path: str) -> Dataset:
1818
"timestamp",
1919
pd.date_range(start="1/1/2019", end="12/25/2019", periods=nih_ds.num_rows),
2020
)
21-
nih_ds = nih_ds.cast_column("features", Image(decode=True))
22-
return nih_ds
21+
return nih_ds.cast_column("features", Image(decode=True))

0 commit comments

Comments
 (0)