Skip to content

Commit 541ba48

Browse files
authored
Merge pull request #143 from nilchia/lint_py
fix flake8 linting errors and add lint check in CI
2 parents c4bc2b9 + 4007e35 commit 541ba48

20 files changed

Lines changed: 4768 additions & 2427 deletions

.github/workflows/lint.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Lint Python scripts
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.py'
7+
pull_request:
8+
paths:
9+
- '**.py'
10+
11+
jobs:
12+
lint:
13+
name: Lint Python scripts
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: ['3.11', '3.12']
19+
steps:
20+
- uses: actions/checkout@v4
21+
with:
22+
persist-credentials: false
23+
24+
- uses: actions/setup-python@v5
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Cache pip
29+
uses: actions/cache@v4
30+
id: cache-pip
31+
with:
32+
path: ~/.cache/pip
33+
key: pip_cache_py_${{ matrix.python-version }}_${{ hashFiles('**/pyproject.toml') }}
34+
35+
- name: Install linters
36+
run: |
37+
python -m pip install --upgrade pip
38+
pip install isort flake8
39+
40+
- name: Run isort
41+
run: isort --check-only --diff .
42+
43+
- name: Run flake8
44+
run: flake8 . --max-line-length=120

examples/tutorials/cbioportal.ipynb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"import numpy as np\n",
1414
"import pandas as pd\n",
1515
"\n",
16-
"STUDY_ID = 'pancan_pcawg_2020'"
16+
"STUDY_ID = 'coad_cptac_2019'"
1717
]
1818
},
1919
{
@@ -37,6 +37,24 @@
3737
"cbio.get_cbioportal_data(cbio.study_id)"
3838
]
3939
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"id": "b08670ec",
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"# Check the specific files the notebook needs are present\n",
48+
"required_files = [\"data_cna.txt\", \"data_mutations.txt\", \"data_clinical_sample.txt\"]\n",
49+
"missing = [f for f in required_files if not os.path.exists(os.path.join(STUDY_ID, f))]\n",
50+
"assert not missing, f\"Missing required files in {STUDY_ID}: {missing}\"\n",
51+
"\n",
52+
"print(f\"All required files present in ./{STUDY_ID}/:\")\n",
53+
"for f in required_files:\n",
54+
" size = os.path.getsize(os.path.join(STUDY_ID, f))\n",
55+
" print(f\" {f} ({size:,} bytes)\")"
56+
]
57+
},
4058
{
4159
"cell_type": "code",
4260
"execution_count": null,
@@ -144,7 +162,7 @@
144162
"metadata": {},
145163
"outputs": [],
146164
"source": [
147-
"counts = np.unique(list(data['clin'].CANCER_TYPE), return_counts=True) "
165+
"counts = np.unique(list(data['clin'].CANCER_TYPE), return_counts=True)"
148166
]
149167
},
150168
{
@@ -274,8 +292,8 @@
274292
"metadata": {},
275293
"outputs": [],
276294
"source": [
277-
"data_importer = flexynesis.data.DataImporter(path=f'{STUDY_ID}/', \n",
278-
" data_types = ['mut', 'cna'], \n",
295+
"data_importer = flexynesis.data.DataImporter(path=f'{STUDY_ID}/',\n",
296+
" data_types = ['mut', 'cna'],\n",
279297
" concatenate=False, top_percentile=10, variance_threshold=0.8,\n",
280298
" min_features=500)\n",
281299
"train_dataset, test_dataset = data_importer.import_data()"
@@ -324,7 +342,7 @@
324342
],
325343
"metadata": {
326344
"kernelspec": {
327-
"display_name": "Python 3",
345+
"display_name": "flexy",
328346
"language": "python",
329347
"name": "python3"
330348
},
@@ -338,7 +356,7 @@
338356
"name": "python",
339357
"nbconvert_exporter": "python",
340358
"pygments_lexer": "ipython3",
341-
"version": "3.11.0"
359+
"version": "3.14.4"
342360
}
343361
},
344362
"nbformat": 4,

flexynesis/__init__.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Lazy imports to avoid slow startup
22
# Only import essential components that are needed for basic functionality
33

4+
from importlib.metadata import PackageNotFoundError, version
5+
46
# Import core modules without heavy dependencies
57
from .config import search_spaces
6-
from importlib.metadata import PackageNotFoundError, version
78

89
# Use the distribution name as published on PyPI (may differ from import name in some projects)
910
_DISTRIBUTION_NAME = "flexynesis"
@@ -13,72 +14,81 @@
1314
except PackageNotFoundError:
1415
# Happens in some dev scenarios before the package is installed
1516
__version__ = "0+unknown"
16-
17+
18+
1719
class LazyModule:
1820
"""Lazy module that only imports when accessed."""
19-
21+
2022
def __init__(self, module_name):
2123
self._module_name = module_name
2224
self._module = None
2325
self._import_error = None
24-
26+
2527
def _import_module(self):
2628
if self._module is None and self._import_error is None:
2729
try:
2830
import importlib
29-
self._module = importlib.import_module(f'.{self._module_name}', package=__name__)
31+
32+
self._module = importlib.import_module(
33+
f".{self._module_name}", package=__name__
34+
)
3035
except ImportError as e:
3136
self._import_error = e
32-
raise ImportError(f"Failed to import {self._module_name} module: {e}. "
33-
f"This usually means some dependencies are missing. "
34-
f"Try installing required packages or check your environment.")
37+
raise ImportError(
38+
f"Failed to import {self._module_name} module: {e}. "
39+
f"This usually means some dependencies are missing. "
40+
f"Try installing required packages or check your environment."
41+
)
3542
elif self._import_error is not None:
3643
raise self._import_error
3744
return self._module
38-
45+
3946
def __getattr__(self, name):
4047
module = self._import_module()
4148
return getattr(module, name)
42-
49+
4350
def __dir__(self):
4451
if self._module is None:
4552
return []
4653
module = self._import_module()
4754
return dir(module)
48-
55+
4956
def __repr__(self):
5057
if self._module is None:
5158
return f"<LazyModule '{self._module_name}' (not yet imported)>"
5259
else:
5360
return f"<LazyModule '{self._module_name}' (imported)>"
5461

62+
5563
# Create lazy module proxies - these are NOT imported yet
56-
modules = LazyModule('modules')
57-
data = LazyModule('data')
58-
main = LazyModule('main')
59-
models = LazyModule('models')
60-
feature_selection = LazyModule('feature_selection')
61-
utils = LazyModule('utils')
64+
modules = LazyModule("modules")
65+
data = LazyModule("data")
66+
main = LazyModule("main")
67+
models = LazyModule("models")
68+
feature_selection = LazyModule("feature_selection")
69+
utils = LazyModule("utils")
70+
6271

6372
# Import commonly used classes directly for easy access
6473
# These will be imported lazily when first accessed
6574
def _get_data_importer():
6675
"""Lazy getter for DataImporter class."""
6776
return data.DataImporter
6877

78+
6979
def _get_models():
7080
"""Lazy getter for model classes."""
7181
return models
7282

83+
7384
# Export all modules and commonly used classes
7485
__all__ = [
7586
"search_spaces",
7687
"modules",
77-
"data",
88+
"data",
7889
"main",
7990
"models",
8091
"feature_selection",
8192
"utils",
82-
"DataImporter"
93+
"DataImporter",
8394
]
84-

0 commit comments

Comments
 (0)