-
Notifications
You must be signed in to change notification settings - Fork 1
Make zarrcreate create intermediate zarr groups and work with relative paths #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
zarrcreate(arrayPath, testcase.ArrSize); | ||
[groupPath, ~, ~] = fileparts(arrayPath); | ||
|
||
testcase.verifyTrue(isfile(fullfile(groupPath, ".zgroup")),... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we doing this check for both A and B?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case B will be a Zarr array (the last element of the path we are passing into zarrcreate).
Not sure if we need a test for a more nested case like fullfile(testcase.ArrPathWrite, "A", "B", "C")
zarrcreate(arrayPath, testcase.ArrSize); | ||
[groupPath, ~, ~] = fileparts(arrayPath); | ||
|
||
testcase.verifyTrue(isfile(fullfile(groupPath, ".zgroup")),... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check if this behaves as expected for a Zarr file hosted on an S3 bucket?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a basic check (see comment here: https://github.com/mathworks/MATLAB-support-for-Zarr-files/pull/91/files#r2089009441 ). Let me know if anything else needs to be tried! Wish we had an easier way to run automated tests with S3 locations
@@ -56,6 +56,122 @@ | |||
py.importlib.reload(zarrModule); | |||
end | |||
end | |||
|
|||
function isZarray = isZarrArray(path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these methods also be made protected to limit access to only the class/object of the class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point.. I was thinking at some point we might want to expose some of them (esp createGroup()
) through a separate function or move them elsewhere all together. If no big flag, I think we can keep it as is for now?
path (1,1) string | ||
end | ||
|
||
if path == "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand this. An empty string must not be allowed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this function works recursively - it checks if a given path is resolvable to an absolute path, and if not, then it calls itself on the parent folder (until it finds a resolvable absolute path). But if you start with a relative path (say "myzarr/A/B") where none of the folders exist yet, none of "myzarr/A/B", "myzarr/A", or "myzarr" will resolve to an absolute path, so the recursion will end up with a parent folder that is "". At that point, we know that the intention is that "myzarr/A/B" folders will be created at the current location (pwd), so the full path should be pwd + "myzarr/A/B"
Zarr.m
Outdated
end | ||
|
||
% See if the parent path exist. Continue recursing until an | ||
% exsiting parent path is found |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - existing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, fixing it
path (1,1) string | ||
end | ||
|
||
if isfolder(path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check if this works for S3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I tried a simple workflow like this, but let me know if there is something specific I should check:
>> s3ParentFolder = "s3://<...reducted...>/"
>> zarrcreate(s3ParentFolder + "myzarr.zarr/A/B/C", [100,100])
>> d = zarrread(s3ParentFolder + "myzarr.zarr/A/B/C");
>> info = zarrinfo(s3ParentFolder + "myzarr.zarr")
info =
[struct](matlab:helpPopup('struct')) with fields:
zarr_format: '2'
node_type: 'group'
>> info = zarrinfo(s3ParentFolder + "myzarr.zarr/A")
info =
[struct](matlab:helpPopup('struct')) with fields:
zarr_format: '2'
node_type: 'group'
>> info = zarrinfo(s3ParentFolder + "myzarr.zarr/A/B")
info =
[struct](matlab:helpPopup('struct')) with fields:
zarr_format: '2'
node_type: 'group'
>> info = zarrinfo(s3ParentFolder + "myzarr.zarr/A/B/C")
info =
[struct](matlab:helpPopup('struct')) with fields:
chunks: [2×1 double]
compressor: []
dimension_separator: '.'
dtype: '<f8'
fill_value: []
filters: []
order: 'C'
shape: [2×1 double]
zarr_format: 2
node_type: 'array'
>> rmdir(s3ParentFolder + "myzarr.zarr", 's')
I have also tried this method separately too:
>> existParent = Zarr.getExistingParentFolder(s3ParentFolder + "myzarr.zarr/A/B/C");
>> existParent + "/" == s3ParentFolder
ans =
[logical](matlab:helpPopup('logical'))
1
>>
|
||
% if new directories were created as part of creating a | ||
% Zarr array, we need to make them into Zarr groups. | ||
newDirs = extractAfter(obj.Path, existingParentPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if newDirs is empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the newGroups will also be empty and we will skip creating Zarr groups because of the if statement that checks for empty newGroups.
>> [newGroups, ~,~] = fileparts("")
newGroups =
""
@@ -144,12 +262,27 @@ function create(obj, dtype, data_size, chunk_size, fillvalue, compression) | |||
obj.FillValue = cast(fillvalue, obj.MatlabDatatype); | |||
end | |||
|
|||
% see how much of the provided path exists already | |||
existingParentPath = Zarr.getExistingParentFolder(obj.Path); | |||
|
|||
% The Python function returns the Tensorstore schema, but we | |||
% do not use it for anything at the moment. | |||
obj.TensorstoreSchema = py.ZarrPy.createZarr(obj.KVStoreSchema, py.numpy.array(obj.DsetSize),... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, you are identifying the folders to be created before createZarr and after the creation, you are populating the groups with .zgroup file. Smart!!
Neat use of recursion!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only concern would be to do manual testing on S3 buckets (in case you havent done already)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! :)
It was the best way I could figure out without having to undo folder creation error cases... Still a bit clunky that we have to do it manually. Based on Zarr spec, I almost want to say it is a bug in tensorstore that it doesn't create intermediate zarr groups
Zarr.m
Outdated
|
||
function createGroup(pathToGroup) | ||
% Create a Zarr group including creating the directory (if | ||
% needed) and the .zrgroup file. Assumes the parent directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - .zgroup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! Fixing it
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #91 +/- ##
==========================================
+ Coverage 85.98% 86.66% +0.67%
==========================================
Files 7 7
Lines 157 195 +38
==========================================
+ Hits 135 169 +34
- Misses 22 26 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
% intermediate groups that do not already exist. If FILEPATH exists | ||
% already, the contents are overwritten. | ||
% If FILEPATH is a full path name, the function creates all intermediate | ||
% directories that do not already exist and makes them into Zarr groups. If |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't sure about making this change in the doc... Let me know what you think! Does this also need to be in-sync with readme?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Please feel free to create test tasks if you were unable to add any tests.
According to Zarr spec:
In this pull request I try to make
zarrcreate
adhere to this. It is a bit tricky to know what the root of the store is, so for now I just assume that any new directories that need to be created as part of thezarrcreate
call are part of the store and need to be made into zarr groups.For example, if you are running the following command:
>> zarrcreate("/Users/jsmith/Documents/myfile.zarr/A/B/C", [10,10])
and the path "/Users/jsmith/Documents/" already exists, but "myfile.zarr/A/B/C" directories don't yet exist, then the created Zarr groups will be at:
It might make sense to add a
zarrcreategroup
function at a later point too, but not doing it yet in this pull request.I also tried to make
zarrcreate
work with relative paths like "../myfile.zarr/"Closes #87
Closes #57