jupyter-analysis-tools 1.5.1__py3-none-any.whl → 1.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  # __init__.py
3
3
 
4
- __version__ = "1.5.1"
4
+ __version__ = "1.6.0"
5
5
 
6
6
  from .binning import reBin
7
7
  from .git import checkRepo, isNBstripoutActivated, isNBstripoutInstalled, isRepo
@@ -0,0 +1,166 @@
1
+ # -*- coding: utf-8 -*-
2
+ # datastore.py
3
+
4
+ import filecmp
5
+ import getpass
6
+ import tempfile
7
+ import warnings
8
+ from pathlib import Path
9
+
10
+ from pybis import Openbis
11
+
12
+
13
+ class DataStore:
14
+ url = None
15
+ _availObj = None
16
+ _userspace = None
17
+
18
+ def __init__(self, url, username=None):
19
+ self.username = username
20
+ if self.username is None:
21
+ self.username = getpass.getuser()
22
+ print(f"Working as user '{self.username}'.")
23
+ # to generate PAT you need to login normally
24
+ self.ds = Openbis(url=self.url, verify_certificates=True)
25
+ # arg. *save_token* saves the openBIS token to ~/.pybis permanently
26
+ self.ds.login(
27
+ self.username,
28
+ getpass.getpass(prompt=f"Password for {self.username}: "),
29
+ save_token=False,
30
+ )
31
+ # create the PAT with the given name, don't store it
32
+ self.ds.get_or_create_personal_access_token("test-session")
33
+
34
+ @property
35
+ def userspace(self):
36
+ uspace = self._userspace
37
+ if uspace is None:
38
+ allspaces = self.ds.get_spaces()
39
+ uspace = allspaces.df[
40
+ allspaces.df.code.str.endswith(self.username.upper())
41
+ ].code.values[0]
42
+ self._userspace = uspace
43
+ return uspace
44
+
45
+ @userspace.setter
46
+ def userspace(self, name):
47
+ name = name.upper()
48
+ if name in self.ds.get_spaces().df.code.values:
49
+ self._userspace = name
50
+
51
+ @staticmethod
52
+ def identifier(objects, code):
53
+ return objects[objects.code == code].identifier.tolist()[0]
54
+
55
+ def createProject(self, projectName, space, space_prefix=None):
56
+ """Finds the requested project in the DataStore. If it does not exist,
57
+ creates a new project with the given code in the given space."""
58
+ # get available projects, accessible by the current user
59
+ projectsAvail = self.ds.get_projects()
60
+ if space_prefix:
61
+ projectsAvail = [prj for prj in projectsAvail if f"/{space_prefix}_" in prj.identifier]
62
+ projects = [prj for prj in projectsAvail if prj.code == projectName]
63
+ assert len(projects) <= 1, f"Multiple projects found for '{projectName}'"
64
+ dsProject = None
65
+ if len(projects): # get the existing object
66
+ dsProject = projects[0]
67
+ else: # create it, if not found
68
+ print(f"Creating project '{projectName}'")
69
+ dsProject = self.ds.new_project(code=projectName, space=space)
70
+ dsProject.save()
71
+ assert dsProject
72
+ return dsProject
73
+
74
+ def createCollection(self, collName, projectObj, defaultObjType=None):
75
+ collections = self.ds.get_collections(project=projectObj)
76
+ dsColl = [coll for coll in collections if coll.code == collName.upper()]
77
+ if len(dsColl):
78
+ dsColl = dsColl[0]
79
+ else: # create it, if not found
80
+ print(f"Creating collection '{collName}'")
81
+ dsColl = self.ds.new_collection(
82
+ code=collName, type="COLLECTION", project=projectObj, props={"$name": collName}
83
+ )
84
+ dsColl.save()
85
+ assert dsColl
86
+ # update properties (name, default view and object type) if not set)
87
+ props = dsColl.props.all() # props as dict
88
+ propKey = "$name"
89
+ if propKey in props and props[propKey] is None:
90
+ props[propKey] = collName
91
+ propKey = "$default_collection_view"
92
+ if propKey in props.keys() and props[propKey] is None:
93
+ propVal = [
94
+ item
95
+ for item in self.ds.get_vocabulary(propKey + "s").get_terms().df.code
96
+ if "list" in item.lower()
97
+ ]
98
+ assert len(propVal)
99
+ props[propKey] = propVal[0]
100
+ if defaultObjType:
101
+ propKey = "$default_object_type"
102
+ if propKey in props.keys() and props[propKey] is None:
103
+ props[propKey] = defaultObjType
104
+ # print(f"Setting '{collName}' properties:\n {props}")
105
+ dsColl.set_props(props)
106
+ dsColl.save()
107
+ return dsColl
108
+
109
+ def createObject(
110
+ self,
111
+ projectName,
112
+ collectionName: str = None,
113
+ space=None,
114
+ objType: str = None,
115
+ props: dict = None,
116
+ ):
117
+ dsProject = self.createProject(projectName, space)
118
+ dsColl = None
119
+ if collectionName is None: # collectionName is required
120
+ return None
121
+ dsColl = self.createCollection(collectionName, dsProject, defaultObjType=objType)
122
+ obj = None
123
+ obj = self.ds.get_objects(type=objType, where={"$name": props["$name"]}).objects
124
+ if len(obj):
125
+ prefix = objType
126
+ msg = "'{}' exists already in {}! Updating ...".format(
127
+ obj[0].props["$name"], obj[0].project.identifier
128
+ )
129
+ warnings.warn_explicit(msg, UserWarning, prefix, 0)
130
+ else: # does not exist yet
131
+ objName = f" '{props['$name']}'" if len(props.get("$name", "")) else ""
132
+ print(f"Creating new {objType}{objName} in {dsColl.identifier}")
133
+ obj = self.ds.new_object(type=objType, props=props, collection=dsColl)
134
+ obj.set_props(props)
135
+ return obj
136
+
137
+ def findObjects(self, *args, **kwargs):
138
+ return self.ds.get_objects(**kwargs)
139
+
140
+ def uploadDataset(self, obj, datasetType, fpaths=[]):
141
+ def _checkFile(localPath, remoteFiles):
142
+ remoteFile = [f for f in remoteFiles if f.name == localPath.name]
143
+ if not len(remoteFile): # file exists in the dataset as well
144
+ return False
145
+ return filecmp.cmp(localPath, remoteFile[0], shallow=False)
146
+
147
+ if not len(fpaths):
148
+ return # nothing to do
149
+ for dataset in obj.get_datasets(type=datasetType):
150
+ with tempfile.TemporaryDirectory() as tempdir:
151
+ dataset.download(destination=tempdir)
152
+ dsFiles = [f for f in Path(tempdir).rglob("*") if f.is_file()]
153
+ if len(fpaths) == len(dsFiles):
154
+ if all([_checkFile(fpath, dsFiles) for fpath in fpaths]):
155
+ print(
156
+ f"All local files of {datasetType} match files in dataset, "
157
+ "not updating."
158
+ )
159
+ continue # skip deletion below
160
+ print(f"Dataset {datasetType} needs update, deleting existing dataset:")
161
+ dataset.delete("Needs update")
162
+ if not len(obj.get_datasets(type=datasetType)): # didn't exist yet or all deleted
163
+ dataset = self.ds.new_dataset(
164
+ type=datasetType, collection=obj.collection, object=obj, files=fpaths
165
+ )
166
+ dataset.save()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jupyter-analysis-tools
3
- Version: 1.5.1
3
+ Version: 1.6.0
4
4
  Summary: Yet another Python library with helpers and utilities for data analysis and processing.
5
5
  Author-email: Ingo Breßler <ingo.bressler@bam.de>, "Brian R. Pauw" <brian.pauw@bam.de>
6
6
  License-Expression: MIT
@@ -35,10 +35,10 @@ Requires-Dist: matplotlib
35
35
  Requires-Dist: ipywidgets
36
36
  Dynamic: license-file
37
37
 
38
- # Jupyter Analysis Tools (v1.5.1)
38
+ # Jupyter Analysis Tools (v1.6.0)
39
39
 
40
40
  [![PyPI Package latest release](https://img.shields.io/pypi/v/jupyter-analysis-tools.svg)](https://pypi.org/project/jupyter-analysis-tools)
41
- [![Commits since latest release](https://img.shields.io/github/commits-since/BAMresearch/jupyter-analysis-tools/v1.5.1.svg)](https://github.com/BAMresearch/jupyter-analysis-tools/compare/v1.5.1...main)
41
+ [![Commits since latest release](https://img.shields.io/github/commits-since/BAMresearch/jupyter-analysis-tools/v1.6.0.svg)](https://github.com/BAMresearch/jupyter-analysis-tools/compare/v1.6.0...main)
42
42
  [![License](https://img.shields.io/pypi/l/jupyter-analysis-tools.svg)](https://en.wikipedia.org/wiki/MIT_license)
43
43
  [![Supported versions](https://img.shields.io/pypi/pyversions/jupyter-analysis-tools.svg)](https://pypi.org/project/jupyter-analysis-tools)
44
44
  [![PyPI Wheel](https://img.shields.io/pypi/wheel/jupyter-analysis-tools.svg)](https://pypi.org/project/jupyter-analysis-tools#files)
@@ -97,6 +97,18 @@ are installed:
97
97
 
98
98
  # CHANGELOG
99
99
 
100
+ ## v1.6.0 (2025-09-19)
101
+
102
+ ### Bug fixes
103
+
104
+ * **DataStore**: f-string syntax ([`9166382`](https://github.com/BAMresearch/jupyter-analysis-tools/commit/916638264be58e75fdfba15d9c6a6584ace92199))
105
+
106
+ * **Tests**: pybis module required for collecting in new datastore module ([`ea6a21d`](https://github.com/BAMresearch/jupyter-analysis-tools/commit/ea6a21df3656dcc5f926aa7ff67a7136806ded3b))
107
+
108
+ ### Features
109
+
110
+ * **DataStore**: new module for managing objects in OpenBIS ([`cdf0a27`](https://github.com/BAMresearch/jupyter-analysis-tools/commit/cdf0a27c0ae1412acd5329532ec8ec1fa7e6be94))
111
+
100
112
  ## v1.5.1 (2025-08-04)
101
113
 
102
114
  ### Bug fixes
@@ -1,7 +1,8 @@
1
- jupyter_analysis_tools/__init__.py,sha256=wJgXbkvBGUcYsY4GmME-p9uQjFMk9TvzAfbOjOl_1zc,398
1
+ jupyter_analysis_tools/__init__.py,sha256=VWuAiXeplG5lYNi9nghWma5zGHb5kEB5Bc5jJQKdoxk,398
2
2
  jupyter_analysis_tools/analysis.py,sha256=AiAvUO648f0PYXqLfal1kDH926neasE5c1RYFu9wtYg,1768
3
3
  jupyter_analysis_tools/binning.py,sha256=d6eXRC3IOnnJIF25OfEASyWedT71EX2nF7jAgGJ9suQ,14536
4
4
  jupyter_analysis_tools/datalocations.py,sha256=BakfiZOMcBwp-_DAn7l57lGWZmZGNnk0j73V75nLBUA,4322
5
+ jupyter_analysis_tools/datastore.py,sha256=gT7CHviYM-mg2SzWxzt_O_Bf3LaZBmQkpnVvO-ePsGA,6723
5
6
  jupyter_analysis_tools/distrib.py,sha256=uyh2jXDdXR6dfd36CAoE5_psoFF0bfA6l1wletPD7Xo,16515
6
7
  jupyter_analysis_tools/git.py,sha256=mqSk5nnAFrmk1_2KFuKVrDWOkRbGbAQOq2N1DfxhNpg,2216
7
8
  jupyter_analysis_tools/plotting.py,sha256=X5Orrwiof-9MuYMKDJEXIlIt0K6bQT6ktFFjXKIVApI,1962
@@ -10,10 +11,10 @@ jupyter_analysis_tools/ssfz2json.py,sha256=aEJo8No_PZ021RJGqDz9g2uZVh9y2G-wNvUB7
10
11
  jupyter_analysis_tools/ssfz_compare.py,sha256=__6qXALyX5pdUBYSEjzNoVHa470QX8Cg_LASpahtAGI,1557
11
12
  jupyter_analysis_tools/utils.py,sha256=c8q2-0v7wEjJ_3w5YTZdjFSf-RP1gPUpMJpv5KUyilU,8800
12
13
  jupyter_analysis_tools/widgets.py,sha256=rA8qPvY9nS1OtykZwXtCTG29K-N_MYFVb5Aj8yK40_s,2996
13
- jupyter_analysis_tools-1.5.1.dist-info/licenses/AUTHORS.rst,sha256=-twUESsY0XqFQ0MIC0ylKhglNwL8lyHmGXriM3RF-2s,93
14
- jupyter_analysis_tools-1.5.1.dist-info/licenses/LICENSE,sha256=jRVl3hmCq0Qv1wifm-EelEKhFWecdoWdhcxSte4a1_c,1125
15
- jupyter_analysis_tools-1.5.1.dist-info/METADATA,sha256=hagcHmj9cDY740-W1ENigKPUMzIHNIX-CZzr2kCjh2Y,45652
16
- jupyter_analysis_tools-1.5.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
17
- jupyter_analysis_tools-1.5.1.dist-info/entry_points.txt,sha256=-LU146dufa_JTwarciGzC6bjsl8pqY_8Z49ODYQ4lPY,124
18
- jupyter_analysis_tools-1.5.1.dist-info/top_level.txt,sha256=ei_0x-BF85FLoJ_h67ySwDFowtqus_gI4_0GR466PEU,23
19
- jupyter_analysis_tools-1.5.1.dist-info/RECORD,,
14
+ jupyter_analysis_tools-1.6.0.dist-info/licenses/AUTHORS.rst,sha256=-twUESsY0XqFQ0MIC0ylKhglNwL8lyHmGXriM3RF-2s,93
15
+ jupyter_analysis_tools-1.6.0.dist-info/licenses/LICENSE,sha256=jRVl3hmCq0Qv1wifm-EelEKhFWecdoWdhcxSte4a1_c,1125
16
+ jupyter_analysis_tools-1.6.0.dist-info/METADATA,sha256=YqOXmurH4dIt9raibtSJc1vizocXFKXpB390mLvtgiE,46226
17
+ jupyter_analysis_tools-1.6.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
+ jupyter_analysis_tools-1.6.0.dist-info/entry_points.txt,sha256=-LU146dufa_JTwarciGzC6bjsl8pqY_8Z49ODYQ4lPY,124
19
+ jupyter_analysis_tools-1.6.0.dist-info/top_level.txt,sha256=ei_0x-BF85FLoJ_h67ySwDFowtqus_gI4_0GR466PEU,23
20
+ jupyter_analysis_tools-1.6.0.dist-info/RECORD,,