17
17
isRemote
18
18
end
19
19
20
-
21
20
methods (Static )
22
21
function pySetup
23
- % Load the Python library
22
+ % Set up Python path
24
23
25
24
% Python module setup and bootstrapping to MATLAB
26
25
fullPath = mfilename(' fullpath' );
27
26
zarrDirectory = fileparts(fullPath );
28
- modpath = fullfile(zarrDirectory , ' PythonModule' );
29
- % Add the current folder to the Python search path
30
- if count(py .sys .path ,modpath ) == 0
31
- insert(py .sys .path ,int32(0 ),modpath );
32
- end
33
-
34
- % Check if the ZarrPy module is loaded already. If not, load
35
- % it.
36
- sys = py .importlib .import_module(' sys' );
37
- loadedModules = dictionary(sys .modules );
38
- if ~loadedModules .isKey(" ZarrPy" )
39
- zarrModule = py .importlib .import_module(' ZarrPy' );
40
- py .importlib .reload(zarrModule );
27
+ zarrPyPath = fullfile(zarrDirectory , ' PythonModule' );
28
+ % Add ZarrPy to the Python search path if it is not there
29
+ % already
30
+ if count(py .sys .path ,zarrPyPath ) == 0
31
+ insert(py .sys .path ,int32(0 ),zarrPyPath );
41
32
end
42
33
end
43
34
35
+ function zarrPy = ZarrPy()
36
+ % Get ZarrPy Python module
37
+
38
+ % Python will compile and cache the module after the first call
39
+ % to import_module, so there is no harm in making this call
40
+ % multiple times.
41
+ zarrPy = py .importlib .import_module(' ZarrPy' );
42
+ end
43
+
44
+ function pyReloadInProcess()
45
+ % Reload ZarrPy module after it has been modified (for
46
+ % In-Process Python only). Need to do `clear classes` before
47
+ % this call. For Out-of-Process Python, can just use
48
+ % `terminate(pyenv)` instead.
49
+
50
+ % make sure the python module is on the path
51
+ Zarr .pySetup()
52
+
53
+ % reload
54
+ py .importlib .reload(Zarr .ZarrPy );
55
+ end
56
+
44
57
function isZarray = isZarrArray(path )
45
58
% Given a path, determine if it is a Zarr array
46
59
@@ -262,7 +275,7 @@ function makeZarrGroups(existingParentPath, newGroupsPath)
262
275
% Extract the S3 bucket name and path
263
276
[bucketName , objectPath ] = Zarr .extractS3BucketNameAndPath(obj .Path );
264
277
% Create a Python dictionary for the KV store driver
265
- obj.KVStoreSchema = py .ZarrPy .createKVStore(obj .isRemote , objectPath , bucketName );
278
+ obj.KVStoreSchema = Zarr .ZarrPy .createKVStore(obj .isRemote , objectPath , bucketName );
266
279
267
280
else % Local file
268
281
% Use full path
@@ -272,7 +285,7 @@ function makeZarrGroups(existingParentPath, newGroupsPath)
272
285
error(" MATLAB:Zarr:invalidPath" ,...
273
286
" Unable to access path "" %s"" ." , path )
274
287
end
275
- obj.KVStoreSchema = py .ZarrPy .createKVStore(obj .isRemote , obj .Path );
288
+ obj.KVStoreSchema = Zarr .ZarrPy .createKVStore(obj .isRemote , obj .Path );
276
289
end
277
290
end
278
291
@@ -310,7 +323,7 @@ function makeZarrGroups(existingParentPath, newGroupsPath)
310
323
endInds = start + stride .* count ;
311
324
312
325
% Read the data
313
- ndArrayData = py .ZarrPy .readZarr(obj .KVStoreSchema ,...
326
+ ndArrayData = Zarr .ZarrPy .readZarr(obj .KVStoreSchema ,...
314
327
start , endInds , stride );
315
328
316
329
% Store the datatype
@@ -363,7 +376,7 @@ function create(obj, dtype, data_size, chunk_size, fillvalue, compression)
363
376
364
377
% The Python function returns the Tensorstore schema, but we
365
378
% do not use it for anything at the moment.
366
- obj.TensorstoreSchema = py .ZarrPy .createZarr(obj .KVStoreSchema , py .numpy .array(obj .DsetSize ),...
379
+ obj.TensorstoreSchema = Zarr .ZarrPy .createZarr(obj .KVStoreSchema , py .numpy .array(obj .DsetSize ),...
367
380
py .numpy .array(obj .ChunkSize ), obj .Datatype .TensorstoreType , ...
368
381
obj .Datatype .ZarrType , obj .Compression , obj .FillValue );
369
382
% py.ZarrPy.temp(py.numpy.array([1, 1]), py.numpy.array([2, 2]))
@@ -400,7 +413,7 @@ function write(obj, data)
400
413
" Unable to write data. Size of the data to be written must match size of the array." );
401
414
end
402
415
403
- py .ZarrPy .writeZarr(obj .KVStoreSchema , data );
416
+ Zarr .ZarrPy .writeZarr(obj .KVStoreSchema , data );
404
417
end
405
418
406
419
end
0 commit comments