Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
qim3d
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
QIM
Tools
qim3d
Commits
effcf627
Commit
effcf627
authored
May 26, 2023
by
Felipe Delestro Matos
Browse files
Options
Downloads
Patches
Plain Diff
load module docstrings
parent
5e0012c3
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
qim3d/io/load.py
+104
-5
104 additions, 5 deletions
qim3d/io/load.py
qim3d/tools/internal_tools.py
+1
-18
1 addition, 18 deletions
qim3d/tools/internal_tools.py
with
105 additions
and
23 deletions
qim3d/io/load.py
+
104
−
5
View file @
effcf627
import
tifffile
"""
Provides functionality for loading data from various file formats.
"""
import
h5py
import
os
import
os
import
sys
import
sys
import
difflib
import
difflib
import
tifffile
import
h5py
from
qim3d.io.logger
import
log
from
qim3d.io.logger
import
log
from
qim3d.tools.internal_tools
import
sizeof
from
qim3d.tools.internal_tools
import
sizeof
class
DataLoader
:
class
DataLoader
:
"""
Utility class for loading data from different file formats.
Args:
virtual_stack (bool, optional): Specifies whether to use virtual stack
when loading TIFF files. Default is False.
Attributes:
virtual_stack (bool): Specifies whether virtual stack is enabled.
Methods:
load_tiff(path): Load a TIFF file from the specified path.
load_h5(path): Load an HDF5 file from the specified path.
load(path): Load a file or directory based on the given path.
Raises:
ValueError: If the file format is not supported or the path is invalid.
Example:
loader = DataLoader(virtual_stack=True)
data = loader.load_tiff(
"
image.tif
"
)
"""
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
**
kwargs
):
"""
Initializes a new instance of the DataLoader class.
Args:
virtual_stack (bool, optional): Specifies whether to use virtual
stack when loading TIFF files. Default is False.
"""
# Virtual stack is False by default
# Virtual stack is False by default
self
.
virtual_stack
=
kwargs
.
get
(
"
virtual_stack
"
,
False
)
self
.
virtual_stack
=
kwargs
.
get
(
"
virtual_stack
"
,
False
)
def
load_tiff
(
self
,
path
):
def
load_tiff
(
self
,
path
):
"""
Load a TIFF file from the specified path.
Args:
path (str): The path to the TIFF file.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
FileNotFoundError: If the file does not exist.
Example:
loader = DataLoader()
data = loader.load_tiff(
"
image.tif
"
)
"""
if
self
.
virtual_stack
:
if
self
.
virtual_stack
:
log
.
info
(
"
Using virtual stack
"
)
log
.
info
(
"
Using virtual stack
"
)
vol
=
tifffile
.
memmap
(
path
)
vol
=
tifffile
.
memmap
(
path
)
else
:
else
:
vol
=
tifffile
.
imread
(
path
)
vol
=
tifffile
.
imread
(
path
)
log
.
info
(
f
"
Loaded shape:
{
vol
.
shape
}
"
)
log
.
info
(
"
Loaded shape: %s
"
,
vol
.
shape
)
log
.
info
(
f
"
Using
{
sizeof
(
sys
.
getsizeof
(
vol
))
}
of memory
"
)
log
.
info
(
"
Using %s of memory
"
,
sizeof
(
sys
.
getsizeof
(
vol
)))
return
vol
return
vol
def
load_h5
(
self
,
path
):
def
load_h5
(
self
,
path
):
"""
Load an HDF5 file from the specified path.
Args:
path (str): The path to the HDF5 file.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
FileNotFoundError: If the file does not exist.
Example:
loader = DataLoader()
data = loader.load_h5(
"
data.h5
"
)
"""
with
h5py
.
File
(
path
,
"
r
"
)
as
f
:
with
h5py
.
File
(
path
,
"
r
"
)
as
f
:
vol
=
f
[
"
data
"
][:]
vol
=
f
[
"
data
"
][:]
return
vol
return
vol
def
load
(
self
,
path
):
def
load
(
self
,
path
):
"""
Load a file or directory based on the given path.
Args:
path (str): The path to the file or directory.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
ValueError: If the format is not supported or the path is invalid.
FileNotFoundError: If the file or directory does not exist.
Example:
loader = DataLoader()
data = loader.load(
"
image.tif
"
)
"""
# Load a file
# Load a file
if
os
.
path
.
isfile
(
path
):
if
os
.
path
.
isfile
(
path
):
# Choose the loader based on the file extension
# Choose the loader based on the file extension
...
@@ -60,6 +137,28 @@ class DataLoader:
...
@@ -60,6 +137,28 @@ class DataLoader:
def
load
(
path
,
virtual_stack
=
False
,
**
kwargs
):
def
load
(
path
,
virtual_stack
=
False
,
**
kwargs
):
"""
Load data from the specified file or directory.
Args:
path (str): The path to the file or directory.
virtual_stack (bool, optional): Specifies whether to use virtual
stack when loading TIFF files. Default is False.
**kwargs: Additional keyword arguments to be passed
to the DataLoader constructor.
Returns:
numpy.ndarray: The loaded volume as a NumPy array.
Raises:
ValueError: If the file format is not supported or the path is invalid.
NotImplementedError: If loading from a directory is not implemented yet.
FileNotFoundError: If the file or directory does not exist.
Example:
data = load(
"
image.tif
"
, virtual_stack=True)
"""
loader
=
DataLoader
(
virtual_stack
=
virtual_stack
,
**
kwargs
)
loader
=
DataLoader
(
virtual_stack
=
virtual_stack
,
**
kwargs
)
return
loader
.
load
(
path
)
return
loader
.
load
(
path
)
This diff is collapsed.
Click to expand it.
qim3d/tools/internal_tools.py
+
1
−
18
View file @
effcf627
"""
Provides a collection of internal utility functions.
"""
Provides a collection of internal utility functions.
"""
This module contains various utility functions that are intended for internal
use within the project. These functions cover a range of tasks such as size
formatting, file manipulation, network operations, and more. They are designed
to assist with common tasks and provide convenient functionality for other
components of the project.
Example:
# Importing the module
import internal_tools
# Using the functions
size = internal_tools.sizeof(1024)
internal_tools.mock_write_file(
"
example.txt
"
)
ip_address = internal_tools.get_local_ip()
internal_tools.gradio_header(
"
Gradio server
"
, 4242)
"""
import
socket
import
socket
import
hashlib
import
hashlib
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment