Skip to content

Commit

Permalink
io csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed May 5, 2020
1 parent ca32f00 commit 3021523
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
6 changes: 5 additions & 1 deletion bigfish/stack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
from .io import read_dv
from .io import read_array
from .io import read_compressed
from .io import read_array_from_csv
from .io import save_image
from .io import save_array
from .io import save_array_to_csv

from .preprocess import build_stacks
from .preprocess import build_stack
Expand Down Expand Up @@ -84,8 +86,10 @@
"read_dv",
"read_array",
"read_compressed",
"read_array_from_csv",
"save_image",
"save_array"]
"save_array",
"save_array_to_csv"]

_preprocess = [
"build_stacks",
Expand Down
82 changes: 76 additions & 6 deletions bigfish/stack/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,37 @@ def read_array(path):
return array


def read_array_from_csv(path, dtype=np.float64):
"""Read a numpy array saved in a 'csv' file.
Parameters
----------
path : str
Path of the csv file to read.
dtype : numpy data type
Expected dtype to cast the array.
Returns
-------
array : ndarray
Array read.
"""
# check path
check_parameter(path=str,
dtype=type)
if ".csv" not in path:
path += ".csv"

# read csv file
array = np.loadtxt(path, delimiter=";", encoding="utf-8")

# cast array dtype
array = array.astype(dtype)

return array


def read_compressed(path, verbose=False):
"""Read a NpzFile object with 'npz' extension.
Expand Down Expand Up @@ -178,8 +209,7 @@ def save_image(image, path, extension="tif"):
"""
# check image and parameters
check_parameter(image=np.ndarray,
path=str,
check_parameter(path=str,
extension=str)
check_array(image,
dtype=[np.uint8, np.uint16, np.uint32,
Expand Down Expand Up @@ -254,15 +284,13 @@ def save_array(array, path):
"""
# check array and path
check_parameter(array=np.ndarray,
path=str)
check_parameter(path=str)
check_array(array,
dtype=[np.uint8, np.uint16, np.uint32,
np.int8, np.int16, np.int32, np.int64,
np.float16, np.float32, np.float64,
bool],
ndim=[2, 3, 4, 5],
allow_nan=True)
ndim=[2, 3, 4, 5])
if "." in path and "npy" not in path:
path_ = path.split(".")[0]
path = path_ + ".npy"
Expand All @@ -271,3 +299,45 @@ def save_array(array, path):
np.save(path, array)

return


def save_array_to_csv(array, path):
"""Save an array into a csv file.
The input array should have 2 dimensions, with 8-bit, 16-bit or 32-bit
(unsigned) integer, 64-bit integer, 16-bit, 32-bit or 64-bit
float.
Parameters
----------
array : np.ndarray
Array to save.
path : str
Path of the saved array.
Returns
-------
"""
# check array and path
check_parameter(path=str)
check_array(array,
dtype=[np.uint8, np.uint16, np.uint32,
np.int8, np.int16, np.int32, np.int64,
np.float16, np.float32, np.float64],
ndim=2)

# save csv file
if ".csv" not in path:
path += ".csv"
if array.dtype == np.float16:
fmt = "%.4f"
elif array.dtype == np.float32:
fmt = "%.7f"
elif array.dtype == np.float64:
fmt = "%.16f"
else:
fmt = "%.1i"
np.savetxt(path, array, fmt=fmt, delimiter=';', encoding="utf-8")

return
3 changes: 3 additions & 0 deletions bigfish/stack/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

from numpy.testing import assert_array_equal

# TODO test bigfish.stack.read_array_from_csv
# TODO test bigfish.stack.save_array_to_csv


@pytest.mark.parametrize("shape", [
(8, 8), (8, 8, 8), (8, 8, 8, 8), (8, 8, 8, 8, 8)])
Expand Down

0 comments on commit 3021523

Please sign in to comment.