From 094bad97e763a090fa74ca9b317a727457d120da Mon Sep 17 00:00:00 2001 From: minhuanli Date: Thu, 20 Nov 2025 10:28:15 -0500 Subject: [PATCH 1/5] Feature, mordernize init file, lazy import to accelerate the import --- SFC_Torch/__init__.py | 53 +++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/SFC_Torch/__init__.py b/SFC_Torch/__init__.py index 64febd7..abc9e04 100644 --- a/SFC_Torch/__init__.py +++ b/SFC_Torch/__init__.py @@ -1,20 +1,49 @@ def getVersionNumber(): - import pkg_resources - version = pkg_resources.require("SFcalculator_torch")[0].version - return version + try: + from importlib.metadata import version + except ImportError: + # Fallback for Python < 3.8 + from importlib_metadata import version + return version("SFcalculator_torch") __author__ = "Minhuan Li" -__email__ = "minhuanli@g.harvard.edu" +__email__ = "minhuanli41@gmail.com" __version__ = getVersionNumber() -# Top level API -from .Fmodel import SFcalculator -from .io import PDBParser, fetch_pdb, fetch_pdbredo -from .symmetry import get_polar_axis +# Lazy loading to speed up imports while maintaining API compatibility +_LAZY_IMPORTS = { + 'SFcalculator': 'Fmodel', + 'PDBParser': 'io', + 'fetch_pdb': 'io', + 'fetch_pdbredo': 'io', + 'get_polar_axis': 'symmetry', + 'utils': 'utils', + 'patterson': 'patterson', + 'packingscore': 'packingscore', +} -# Submodules -from . import utils -from . import patterson -from . import packingscore +def __getattr__(name): + """Lazy import mechanism for top-level API""" + if name in _LAZY_IMPORTS: + module_name = _LAZY_IMPORTS[name] + import importlib + + # Import the module + if name in ['utils', 'patterson', 'packingscore']: + # These are submodule imports + module = importlib.import_module(f'.{module_name}', __name__) + globals()[name] = module + return module + else: + # These are specific objects from modules + module = importlib.import_module(f'.{module_name}', __name__) + obj = getattr(module, name) + globals()[name] = obj + return obj + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + +def __dir__(): + """Support for dir() to show available lazy imports""" + return list(globals().keys()) + list(_LAZY_IMPORTS.keys()) From 7ab6298a3ad1873794eee0ccff9286248e20a0a4 Mon Sep 17 00:00:00 2001 From: minhuanli Date: Thu, 20 Nov 2025 10:42:45 -0500 Subject: [PATCH 2/5] remove old config files --- .coveragerc | 2 -- MANIFEST.in | 2 -- setup.cfg | 5 ----- setup.py | 31 ------------------------------- 4 files changed, 40 deletions(-) delete mode 100644 .coveragerc delete mode 100644 MANIFEST.in delete mode 100644 setup.cfg delete mode 100644 setup.py diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index a4e9df3..0000000 --- a/.coveragerc +++ /dev/null @@ -1,2 +0,0 @@ -[run] -source = SFC_Torch diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 7566540..0000000 --- a/MANIFEST.in +++ /dev/null @@ -1,2 +0,0 @@ -include license -include SFC_Torch/VERSION \ No newline at end of file diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 6cf0351..0000000 --- a/setup.cfg +++ /dev/null @@ -1,5 +0,0 @@ -[aliases] -test=pytest - -[tool:pytest] -addopts = --cov=SFC_Torch --cov-config=.coveragerc --cov-report xml diff --git a/setup.py b/setup.py deleted file mode 100644 index 1b094d2..0000000 --- a/setup.py +++ /dev/null @@ -1,31 +0,0 @@ -from setuptools import setup, find_packages - -# Get version number -def getVersionNumber(): - with open("SFC_Torch/VERSION", "r") as vfile: - version = vfile.read().strip() - return version - -__version__ = getVersionNumber() - -setup(name="sfcalculator_torch", - version=__version__, - author="Minhaun Li", - license="MIT", - description="A differentiable pipeline connecting molecule models and crystallpgraphy data", - url="https://github.com/Hekstra-Lab/SFcalculator", - author_email='minhuanli@g.harvard.edu', - packages=find_packages(), - include_package_data=True, - python_requires=">=3.9", - install_requires=[ - "gemmi>=0.5.6, <=0.6.7", - "reciprocalspaceship>=0.9.18, <=1.0.3", - "numpy<2.0.0", - "tqdm", - "loguru", - ], - extras_require={ - "test": ["pytest", "pytest-cov"] - }, -) \ No newline at end of file From 9cdcd4ed96f9dca4da2fa939b57f32f42cb73d3c Mon Sep 17 00:00:00 2001 From: minhuanli Date: Thu, 20 Nov 2025 10:43:16 -0500 Subject: [PATCH 3/5] pyproject toml config, now I am up to date --- pyproject.toml | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d430c1f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,70 @@ +[build-system] +requires = ["setuptools>=61.0.0", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "sfcalculator_torch" +dynamic = ["version"] +description = "A differentiable pipeline connecting molecule models and scattering data" +readme = "README.md" +requires-python = ">=3.9" +license = {text = "MIT"} +authors = [ + {name = "Minhuan Li", email = "minhuanli@g.harvard.edu"} +] +keywords = ["pytorch", "structure-factors", "differentiable", "crystallography", "cryoEM"] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Science/Research", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", +] + +dependencies = [ + "gemmi>=0.5.6,<=0.6.7", + "reciprocalspaceship>=0.9.18,<=1.0.3", + "numpy<2.0.0", + "tqdm", + "loguru", +] + +[project.optional-dependencies] +test = ["pytest", "pytest-cov"] +dev = ["pytest", "pytest-cov", "build", "twine"] + +[project.urls] +Homepage = "https://github.com/Hekstra-Lab/SFcalculator_torch" +Documentation = "https://hekstra-lab.github.io/SFcalculator_torch/" +Repository = "https://github.com/Hekstra-Lab/SFcalculator_torch" +"Bug Tracker" = "https://github.com/Hekstra-Lab/SFcalculator_torch/issues" + +[tool.setuptools] +packages = ["SFC_Torch"] +include-package-data = true + +[tool.setuptools.package-data] +SFC_Torch = ["VERSION"] + +[tool.setuptools.dynamic] +version = {file = "SFC_Torch/VERSION"} + +[tool.pytest.ini_options] +addopts = "--cov=SFC_Torch --cov-report xml --cov-report term-missing" +testpaths = ["tests"] + +[tool.coverage.run] +source = ["SFC_Torch"] + +[tool.coverage.report] +exclude_lines = [ + "pragma: no cover", + "def __repr__", + "raise AssertionError", + "raise NotImplementedError", + "if __name__ == .__main__.:", +] + From 597aedbc17f63d6d0f43d25aa9122d00323e2796 Mon Sep 17 00:00:00 2001 From: minhuanli Date: Thu, 20 Nov 2025 10:44:01 -0500 Subject: [PATCH 4/5] update CI files to be compatible --- .github/workflows/Build.yml | 4 ++-- .github/workflows/Publish.yml | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index fc4d921..072409c 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.9", "3.10", "3.11"] + python-version: ["3.9", "3.10", "3.11", "3.12"] # Skip CI if 'skip ci' is contained in latest commit message if: "!contains(github.event.head_commit.message, 'skip ci')" @@ -34,7 +34,7 @@ jobs: - name: Test with pytest run: | - pytest --cov=SFC_Torch --cov-report=xml + pytest - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 diff --git a/.github/workflows/Publish.yml b/.github/workflows/Publish.yml index 2e94234..3ee7777 100644 --- a/.github/workflows/Publish.yml +++ b/.github/workflows/Publish.yml @@ -8,15 +8,15 @@ jobs: deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Install Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v5 with: - python-version: '3.9' + python-version: '3.11' - name: Install dependencies run: | python -m pip install --upgrade pip - pip install build packaging setuptools twine wheel + pip install build twine - name: Build package run: | python -m build From dbd92741e083feb6e1d720dc4ebd495629618419 Mon Sep 17 00:00:00 2001 From: minhuanli Date: Thu, 20 Nov 2025 10:44:42 -0500 Subject: [PATCH 5/5] bump to 0.3.0 --- SFC_Torch/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SFC_Torch/VERSION b/SFC_Torch/VERSION index 3a4036f..0d91a54 100644 --- a/SFC_Torch/VERSION +++ b/SFC_Torch/VERSION @@ -1 +1 @@ -0.2.5 +0.3.0