Skip to content

Commit

Permalink
Fix #51, better handling cache path
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskoenig committed Jun 13, 2024
1 parent 649f9d6 commit 78c196f
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 46,163 deletions.
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "König, Matthias"
}
],
"description": "<p><code>pymetadata</code> is a collection of python utilities for working with metadata in the context of COMBINE standards with source code available from available from <a href=\"https://github.com/matthiaskoenig/pymetadata\">https://github.com/matthiaskoenig/pymetadata</a></p>\n<p>Features include among others<ul><li>annotation classes and helpers</li></ul></p>\n<p>If you have any questions or issues please <a href=\"https://github.com/matthiaskoenig/pymetadata/issues\">open an issue</a></p>\n<h2>Funding</h2><p>Matthias König is supported by the Federal Ministry of Education and Research (BMBF, Germany) within the research network Systems Medicine of the Liver (<strong>LiSyM</strong>, grant number 031L0054) and by the German Research Foundation (DFG) within the Research Unit Programme FOR 5151 <strong><a href=\"https://qualiperf.de\">QuaLiPerF</a></strong> (Quantifying Liver Perfusion-Function Relationship in Complex Resection - A Systems Medicine Approach)\" by grant number 436883643 and by grant number 465194077 (Priority Programme SPP 2311, Subproject SimLivA).</p>",
"description": "<p><code>pymetadata</code> is a collection of python utilities for working with metadata in the context of COMBINE standards with source code available from available from <a href=\"https://github.com/matthiaskoenig/pymetadata\">https://github.com/matthiaskoenig/pymetadata</a></p><p>Features include among others<ul><li>annotation classes and helpers</li></ul></p><p>If you have any questions or issues please <a href=\"https://github.com/matthiaskoenig/pymetadata/issues\">open an issue</a></p><h2>Funding</h2><p>Matthias König (MK) was supported by the Federal Ministry of Education and Research (BMBF, Germany) within the research network Systems Medicine of the Liver (**LiSyM**, grant number 031L0054). MK is supported by the Federal Ministry of Education and Research (BMBF, Germany) within ATLAS by grant number 031L0304B and by the German Research Foundation (DFG) within the Research Unit Program FOR 5151 QuaLiPerF (Quantifying Liver Perfusion-Function Relationship in Complex Resection - A Systems Medicine Approach) by grant number 436883643 and by grant number 465194077 (Priority Programme SPP 2311, Subproject SimLivA).</p>",
"access_right": "open",
"license": "LGPL-3.0",
"keywords": [
Expand Down
26 changes: 21 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ PARTICULAR PURPOSE. See the GNU General Public License for more details.

Funding
=======
Matthias König was supported by the Federal Ministry of Education and Research (BMBF, Germany)
within the research network Systems Medicine of the Liver (**LiSyM**, grant number 031L0054)
and is supported by the German Research Foundation (DFG) within the Research Unit Programme FOR 5151
"`QuaLiPerF <https://qualiperf.de>`__ (Quantifying Liver Perfusion-Function Relationship in Complex Resection -
A Systems Medicine Approach)" by grant number 436883643 and by grant number 465194077 (Priority Programme SPP 2311, Subproject SimLivA), and
Matthias König (MK) was supported by the Federal Ministry of Education and Research
(BMBF, Germany) within the research network Systems Medicine of the Liver
(**LiSyM**, grant number 031L0054). MK is supported by the Federal Ministry of
Education and Research (BMBF, Germany) within ATLAS by grant number 031L0304B and
by the German Research Foundation (DFG) within the Research Unit Program FOR 5151
QuaLiPerF (Quantifying Liver Perfusion-Function Relationship in Complex Resection
- A Systems Medicine Approach) by grant number 436883643 and by grant number
465194077 (Priority Programme SPP 2311, Subproject SimLivA).

Installation
============
Expand All @@ -105,8 +108,21 @@ Or via cloning the repository and installing via::
cd pymetadata
pip install -e .


To install for development use::

pip install -e .[development]


Cache path
==========
`pymetadata` caches some information for faster retrieval. The cache path is set to::

CACHE_PATH: Path = Path.home() / ".cache" / "pymetadata"

To use a custom cache path use::

import pymetadata
pymetadata.CACHE_PATH = <cache_path>

© 2021-2024 Matthias König
9 changes: 9 additions & 0 deletions release-notes/0.4.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Release notes for pymetadata 0.4.3
![pymetadata](https://github.com/matthiaskoenig/pymetadata/raw/develop/docs/images/favicon/pymetadata-100x100-300dpi.png)

We are pleased to release the next version of pymetadata including the
following changes:

## Features & fixes
- better handling of resources and cache
- customization of cache path via setting `pymetadata.CACHE_PATH`
2 changes: 1 addition & 1 deletion src/pymetadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
ENUM_DIR: Path = Path(__file__).parent / "metadata"

CACHE_USE: bool = False
CACHE_PATH: Path = RESOURCES_DIR / "cache"
CACHE_PATH: Path = Path.home() / ".cache" / "pymetadata"
17 changes: 11 additions & 6 deletions src/pymetadata/chebi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

from pathlib import Path
from pprint import pprint
from typing import Any, Dict
from typing import Any, Dict, Optional

from zeep import Client

from pymetadata import CACHE_PATH, CACHE_USE, RESOURCES_DIR, log
import pymetadata
from pymetadata import log
from pymetadata.cache import DataclassJSONEncoder, read_json_cache, write_json_cache


logger = log.get_logger(__name__)

# client = Client('https://www.ebi.ac.uk/webservices/chebi/2.0/webservice?wsdl')
client = Client(str(RESOURCES_DIR / "chebi_webservice_wsdl.xml"))
# FIXME: copy the file to the cache dir
client = Client(str(pymetadata.RESOURCES_DIR / "chebi_webservice_wsdl.xml"))


class ChebiQuery:
Expand All @@ -25,15 +26,19 @@ class ChebiQuery:

@staticmethod
def query(
chebi: str, cache: bool = CACHE_USE, cache_path: Path = CACHE_PATH
chebi: str, cache: Optional[bool] = None, cache_path: Optional[Path] = None
) -> Dict:
"""Query additional ChEBI information."""

if not chebi:
return dict()
if cache is None:
cache = pymetadata.CACHE_USE
if cache_path is None:
cache_path = pymetadata.CACHE_PATH

# caching
chebi_base_path = cache_path / "chebi"
chebi_base_path = Path(cache_path) / "chebi"
if not chebi_base_path.exists():
chebi_base_path.mkdir(parents=True)

Expand Down
15 changes: 15 additions & 0 deletions src/pymetadata/examples/cache_path_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Example for customizing the cache path."""

from pathlib import Path

import pymetadata
from pymetadata.chebi import ChebiQuery


pymetadata.CACHE_PATH = Path.home() / ".cache" / "pymetadata"

if __name__ == "__main__":

chebis = ["CHEBI:2668", "CHEBI:138366", "CHEBI:9637", "CHEBI:155897"]
for chebi in chebis:
d = ChebiQuery.query(chebi=chebi, cache=True)
8 changes: 4 additions & 4 deletions src/pymetadata/identifiers/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

import requests

from pymetadata import RESOURCES_DIR, log
import pymetadata
from pymetadata import CACHE_PATH, log
from pymetadata.cache import DataclassJSONEncoder, read_json_cache, write_json_cache


Expand Down Expand Up @@ -298,7 +299,6 @@ class Registry:

def __init__(
self,
registry_path: Path = RESOURCES_DIR / "identifiers_registry.json",
cache_duration: int = 24,
cache: bool = True,
):
Expand All @@ -308,13 +308,13 @@ def __init__(
:param cache_duration: Duration of caching in hours.
:param cache: boolean flag to stop caching
"""
self.registry_path = registry_path
self.registry_path = pymetadata.CACHE_PATH / "identifiers_registry.json"

# check if update needed
if cache:
if os.path.exists(self.registry_path):
registry_age = (
time.time() - os.path.getmtime(registry_path)
time.time() - os.path.getmtime(self.registry_path)
) / 3600 # [hr]
update = registry_age > cache_duration
else:
Expand Down
12 changes: 9 additions & 3 deletions src/pymetadata/ontologies/ols.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import requests

from pymetadata import CACHE_PATH, CACHE_USE, log
import pymetadata
from pymetadata import log
from pymetadata.cache import read_json_cache, write_json_cache
from pymetadata.identifiers.registry import Registry

Expand Down Expand Up @@ -76,13 +77,18 @@ class OLSQuery:
def __init__(
self,
ontologies: List[OLSOntology],
cache_path: Path = CACHE_PATH,
cache: bool = CACHE_USE,
cache_path: Optional[Path] = None,
cache: Optional[bool] = None,
):
"""Initialize OLSQuery."""
self.ontologies: Dict[str, OLSOntology] = {
ontology.name: ontology for ontology in ontologies
}
if not cache_path:
cache_path = pymetadata.CACHE_PATH
if not cache:
cache = pymetadata.CACHE_USE

self.cache_path = cache_path / "ols"
self.cache = cache

Expand Down
Loading

0 comments on commit 78c196f

Please sign in to comment.