rslearn.utils.fsspec¶
fsspec ¶
Utilities related to fsspec and upath libraries.
iter_nonhidden ¶
Iterate over non-hidden entries in a directory.
Hidden entries are those whose basename starts with "." (e.g. ".DS_Store").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to iterate. |
required |
Yields:
| Type | Description |
|---|---|
UPath
|
non-hidden UPath entries in the directory. |
Source code in rslearn/utils/fsspec.py
iter_nonhidden_subdirs ¶
Iterate over non-hidden subdirectories in a directory.
Note that this will check is_dir() on each path so it will yield poor performance for directories with many entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to iterate. |
required |
Yields:
| Type | Description |
|---|---|
UPath
|
non-hidden subdirectories in the directory. |
Source code in rslearn/utils/fsspec.py
iter_nonhidden_files ¶
Iterate over non-hidden files in a directory.
Note that this will check is_file() on each path so it will yield poor performance for directories with many entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the directory to iterate. |
required |
Yields:
| Type | Description |
|---|---|
UPath
|
non-hidden files in the directory. |
Source code in rslearn/utils/fsspec.py
get_upath_local ¶
Returns a local filename to access the specified UPath.
If the path is already local, then its string representation is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the UPath to open |
required |
extra_paths
|
list[UPath]
|
any additional files that should be copied to the same directory as the specified path. They will only be copied if the filesystem is not local. |
[]
|
Returns:
| Type | Description |
|---|---|
None
|
the local filename at which the file can be accessed in this context manager |
Source code in rslearn/utils/fsspec.py
join_upath ¶
Joins a UPath with a suffix that may be absolute or relative to the path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the parent path |
required |
suffix
|
str
|
string suffix. It can include a protocol, in which it is treated as an absolute path not relative to the parent. It can also be a filesystem-specific absolute path, or a path relative to the parent. |
required |
Returns:
| Type | Description |
|---|---|
UPath
|
the joined path |
Source code in rslearn/utils/fsspec.py
open_atomic ¶
Open a path for atomic writing.
If it is local filesystem, we will write to a temporary file, and rename it to the destination upon success.
Otherwise, we assume it's object storage and none of that is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the UPath to be opened |
required |
*args
|
Any
|
any valid arguments for :code: |
()
|
**kwargs
|
Any
|
any valid keyword arguments for :code: |
{}
|
Source code in rslearn/utils/fsspec.py
open_rasterio_upath_reader ¶
Open a raster for reading.
If the UPath is local, then we open with rasterio directly, since this is much faster. Otherwise, we open the file stream first and then use rasterio with file stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the path to read. |
required |
**kwargs
|
Any
|
additional keyword arguments for :code: |
{}
|
Source code in rslearn/utils/fsspec.py
open_rasterio_upath_writer ¶
Open a raster for writing.
If the UPath is local, then we open with rasterio directly, since this is much faster. We also write atomically by writing to temporary file and then renaming it, to avoid concurrency issues. Otherwise, we open the file stream first and then use rasterio with file stream (and assume that it is object storage so the write will be atomic).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
UPath
|
the path to write. |
required |
**kwargs
|
Any
|
additional keyword arguments for :code: |
{}
|
Source code in rslearn/utils/fsspec.py
get_relative_suffix ¶
Get the suffix of fname relative to base_dir.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
UPath
|
the base directory. |
required |
fname
|
UPath
|
a filename within the base directory. |
required |
Returns:
| Type | Description |
|---|---|
str
|
the suffix on base_dir that would yield the given filename. |