Skip to content

Commit

Permalink
add stdlib.[canonical,expanduser]
Browse files Browse the repository at this point in the history
require Matlab >= R2020b

python: better test for working
  • Loading branch information
scivision committed Feb 8, 2023
1 parent 2eaaa86 commit 676ff4b
Show file tree
Hide file tree
Showing 79 changed files with 565 additions and 436 deletions.
11 changes: 0 additions & 11 deletions +stdlib/+fileio/absolute_path.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
function abspath = absolute_path(p)
%% absolute_path(p)
% path need not exist, but absolute path is returned
%
% NOTE: some network file systems are not resolvable by Matlab Java
% subsystem, but are sometimes still valid--so return
% unmodified path if this occurs.
%
%%% Inputs
% * p: path to make absolute
%%% Outputs
% * abspath: absolute path, if determined

arguments
p string {mustBeScalarOrEmpty}
Expand Down
12 changes: 0 additions & 12 deletions +stdlib/+fileio/expanduser.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
function expanded = expanduser(p)
%% expanduser(path)
% expands tilde ~ into user home directory
%
% Useful for Matlab functions like h5read() and some Computer Vision toolbox functions
% that can't handle ~
%
%%% Inputs
% * p: path to expand, if tilde present
%%% Outputs
% * expanded: expanded path
%
% See also ABSOLUTE_PATH

arguments
p string
Expand Down
5 changes: 0 additions & 5 deletions +stdlib/+fileio/extract_zstd.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
function extract_zstd(archive, out_dir)
%% extract_zstd(archive, out_dir)
% extract a zstd file "archive" to "out_dir"
% out_dir need not exist yet, but its parent must
% We do this with CMake to avoid problems with old system tar.
% For our user audience, CMake is at least as likely to be installed as Zstd.

arguments
archive (1,1) string {mustBeFile}
Expand Down
11 changes: 2 additions & 9 deletions +stdlib/+fileio/is_absolute_path.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
function isabs = is_absolute_path(apath)
%% is_absolute_path(apath)
% true if path is absolute. Path need not yet exist.
% even if path has inside ".." it's still considered absolute
% e.g. Python
% os.path.isabs("/foo/../bar") == True

arguments
apath string
end

import stdlib.fileio.expanduser

apath = expanduser(apath);
apath = stdlib.fileio.expanduser(apath);

if ispc
i = strlength(apath) < 3;
Expand All @@ -24,4 +17,4 @@
isabs = startsWith(apath, "/");
end

end % function
end
7 changes: 0 additions & 7 deletions +stdlib/+fileio/is_exe.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
function ok = is_exe(file)
%% is_exe(file)
% is a file executable, as per its filesystem attributes
% does not actually try to run the file.
%%% Inputs
% * file: filename
%%% Outputs
% * ok: boolean logical

arguments
file string {mustBeScalarOrEmpty}
Expand Down
4 changes: 1 addition & 3 deletions +stdlib/+fileio/makedir.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function makedir(direc)
%% makedir(direc)
% malformed paths can be "created" but are not accessible.
% This function works around that bug in Matlab mkdir().

arguments
direc (1,1) string {mustBeNonzeroLengthText}
end
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/+fileio/md5sum.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

assert(strlength(hash)==32, 'md5 hash is 32 characters')

end % function
end
15 changes: 3 additions & 12 deletions +stdlib/+fileio/path_tail.m
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
function last = path_tail(apath)
%% path_tail(apath)
% get last part of directory path
% if filename, return filename with suffix

arguments
apath string {mustBeScalarOrEmpty}
end

import stdlib.fileio.absolute_path

if isempty(apath)
last = string.empty;
return
end

if strlength(apath) == 0
last = "";
return
end

[~, name, ext] = fileparts(absolute_path(apath));
[~, name, ext] = fileparts(stdlib.fileio.absolute_path(apath));

last = append(name, ext);

end % function
end
7 changes: 2 additions & 5 deletions +stdlib/+fileio/posix.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
function ppath = posix(file)
%% posix(file)
% convert a path or sequence of path components to a Posix path separated
% with "/" even on Windows.
% If Windows path also have escaping "\" this breaks

arguments
file string
end
Expand All @@ -14,4 +11,4 @@
ppath = strrep(ppath, "\", "/");
end

end % function
end
10 changes: 0 additions & 10 deletions +stdlib/+fileio/private/mustBeFile.m

This file was deleted.

7 changes: 0 additions & 7 deletions +stdlib/+fileio/private/mustBeNonzeroLengthText.m

This file was deleted.

7 changes: 0 additions & 7 deletions +stdlib/+fileio/private/mustBeScalarOrEmpty.m

This file was deleted.

12 changes: 2 additions & 10 deletions +stdlib/+fileio/samepath.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
function issame = samepath(path1, path2)
%% samepath(path1, path)
% true if inputs resolve to same path
% files need not exist
%%% Inputs
% * path1, path2: paths to compare
%%% Outputs
% issame: logical

arguments
path1 string {mustBeScalarOrEmpty}
path2 string {mustBeScalarOrEmpty}
end

import stdlib.fileio.absolute_path

issame = absolute_path(path1) == absolute_path(path2);
issame = stdlib.fileio.absolute_path(path1) == stdlib.fileio.absolute_path(path2);

end
5 changes: 2 additions & 3 deletions +stdlib/+fileio/sha256sum.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
function hash = sha256sum(file)
%% sha256sum(file)
% compute sha256 hash of file

arguments
file (1,1) string {mustBeFile}
end
Expand All @@ -24,4 +23,4 @@

assert(strlength(hash)==64, 'SHA256 hash is 64 characters')

end % function
end
3 changes: 0 additions & 3 deletions +stdlib/+fileio/which.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
function exe = which(filename, fpath)
%% which(filename, fpath)
% Find executable with name under path
% like Python shutil.which, may return relative or absolute path

arguments
filename (1,1) string {mustBeNonzeroLengthText}
Expand Down
12 changes: 1 addition & 11 deletions +stdlib/+fileio/with_suffix.m
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
function filename = with_suffix(filename, suffix)
%% with_suffix(filename, suffix)
% switch file extension
%%% Inputs
% * filename: original filename
% * suffix: file extension with "." e.g. ".dat"
%%% Outputs
% * filename: modified filename

arguments
filename string
suffix (1,1) string
end

if isempty(filename)
return
end

[direc, name, ext] = fileparts(filename);
if ext ~= suffix
filename = fullfile(direc, name + suffix);
Expand Down
4 changes: 1 addition & 3 deletions +stdlib/+hdf5nc/h5create_group.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
hpath (1,1) string {mustBeNonzeroLengthText}
end

import stdlib.fileio.expanduser

% polymorphic fid/filename
if isa(file, 'H5ML.id')
fid = file;
else
file = expanduser(file);
file = stdlib.fileio.expanduser(file);
dcpl = 'H5P_DEFAULT';
if isfile(file)
fid = H5F.open(file, 'H5F_ACC_RDWR', dcpl);
Expand Down
11 changes: 1 addition & 10 deletions +stdlib/+hdf5nc/h5exists.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
function exists = h5exists(file, vars)
%% H5EXISTS(file, vars)
% check if object(s) exists in HDF5 file
%
%%% Inputs
% * file: data filename
% * varname: path(s) of variable in file
%
%%% Outputs
% * exists: boolean (scalar or vector)

arguments
file (1,1) string {mustBeFile}
Expand All @@ -19,4 +10,4 @@
% NOT contains because we want exact string match
exists = ismember(vars, stdlib.hdf5nc.h5variables(file));

end % function
end
9 changes: 0 additions & 9 deletions +stdlib/+hdf5nc/h5ndims.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
function frank = h5ndims(file, variable)
%% h5ndims(file, variable)
% get number of dimensions of an HDF5 dataset
%
%%% Inputs
% * file: data filename
% * variable: name of variable inside file
%
%%% Outputs
% * frank: number of variable dimensions (like Matlab ndims)

arguments
file (1,1) string {mustBeFile}
Expand Down
11 changes: 0 additions & 11 deletions +stdlib/+hdf5nc/h5save.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
function h5save(filename, varname, A, opts)
%% h5save(filename, varname, A, opts)
% create or append to data file
%
% parent folder (file directory) must already exist
%
%%% Inputs
% * filename: data filename
% * varname: variable name to save
% * A: data to write
% * opts.size: variable shape -- helps write scalar or vectors especially
% * opts.type: class of variable e.g. int32, float32

arguments
filename (1,1) string {mustBeNonzeroLengthText}
Expand Down
9 changes: 0 additions & 9 deletions +stdlib/+hdf5nc/h5size.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
function fsize = h5size(file, variable)
%% h5size(file, variable)
% get size (shape) of a data file variable
%
%%% Inputs
% filename: data filename
% variable: name of variable inside file
%
%%% Outputs
% fsize: vector of variable size per dimension. Empty if scalar variable.

arguments
file (1,1) string {mustBeFile}
Expand Down
10 changes: 0 additions & 10 deletions +stdlib/+hdf5nc/h5variables.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
function names = h5variables(file, group)
%% h5variables(file, group)
% get dataset names in a file under group
% default is datasets under "/", optionally under "/group"
%
%%% Inputs
% * file: filename
% * group: group name (optional)
%
%%% Outputs
% * names: variable names

arguments
file (1,1) string {mustBeFile}
Expand Down
11 changes: 1 addition & 10 deletions +stdlib/+hdf5nc/ncexists.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
function exists = ncexists(file, vars)
%% ncexists(file, vars)
% check if variable(s) exists in NetCDF4 file
%
%%% Inputs
% * file: data filename
% * varname: path(s) of variable in file
%
%%% Outputs
% * exists: boolean (scalar or vector)

arguments
file (1,1) string {mustBeFile}
Expand All @@ -17,4 +8,4 @@
% NOT contains because we want exact string match
exists = ismember(vars, stdlib.hdf5nc.ncvariables(file));

end % function
end
9 changes: 0 additions & 9 deletions +stdlib/+hdf5nc/ncndims.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
function frank = ncndims(file, variable)
%% ncndims(file, variable)
% get number of dimensions of a variable in the file
%
%%% Inputs
% * file: data filename
% * variable: name of variable inside file
%
%%% Outputs
% * frank: number of variable dimensions (like Matlab ndims)

arguments
file (1,1) string {mustBeFile}
Expand Down
27 changes: 1 addition & 26 deletions +stdlib/+hdf5nc/ncsave.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
function ncsave(filename, varname, A, opts)
%% ncsave(filename, varname, A, opts)
% create or append to data file
%
% parent folder (file directory) must already exist
%
%%% Inputs
% * filename: data filename
% * varname: variable name to save
% * A: data to write
% * opts.size: variable shape -- helps write scalar or vectors especially
% * opts.type: class of variable e.g. int32, float32

arguments
filename (1,1) string {mustBeNonzeroLengthText}
Expand Down Expand Up @@ -63,18 +52,4 @@ function ncsave(filename, varname, A, opts)
nc_new_file(filename, varname, A, sizeA, ncdims)
end

end % function

% Copyright 2020 SciVision, Inc.

% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at

% http://www.apache.org/licenses/LICENSE-2.0

% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
end

0 comments on commit 676ff4b

Please sign in to comment.