Skip to content

Commit

Permalink
feat+refactor: add astype function
Browse files Browse the repository at this point in the history
  • Loading branch information
william-silversmith committed Jul 23, 2024
1 parent c979d2a commit e702320
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions crackle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
header, contains, crack_codes, num_labels,
)
from .operations import (
astype,
remap, refit, renumber,
min, max,
zstack, zsplit,
Expand Down
6 changes: 5 additions & 1 deletion crackle/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
components,
)
from .operations import (
astype,
refit,
renumber,
zstack, zsplit,
Expand Down Expand Up @@ -70,9 +71,12 @@ def max(self):
def remap(self, buf, mapping, preserve_missing_labels=False):
return CrackleArray(remap(buf, mapping, preserve_missing_labels))

def refit(self) -> bytes:
def refit(self):
return CrackleArray(refit(self.binary))

def astype(self, dtype):
return CrackleArray(astype(self.binary, dtype))

def renumber(self, start:int = 0) -> bytes:
return CrackleArray(renumber(self.binary, start))

Expand Down
13 changes: 11 additions & 2 deletions crackle/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,29 @@ def remap(binary:bytes, mapping:dict, preserve_missing_labels:bool = False):
binary[offset:offset+uniq_bytes] = list(all_labels.view(np.uint8))
return bytes(binary)

def refit(binary:bytes) -> bytes:
def astype(binary:bytes, dtype) -> bytes:
"""
Change the rendered dtype to the smallest
dtype needed to render the image without
loss of precision.
"""
head = header(binary)
dtype = fastremap.fit_dtype(head.dtype, max(binary))
head.data_width = np.dtype(dtype).itemsize
return b''.join([
head.tobytes(),
binary[CrackleHeader.HEADER_BYTES:]
])

def refit(binary:bytes) -> bytes:
"""
Change the rendered dtype to the smallest
dtype needed to render the image without
loss of precision.
"""
head = header(binary)
dtype = fastremap.fit_dtype(head.dtype, max(binary))
return astype(binary, dtype)

def renumber(binary:bytes, start=0) -> Tuple[bytes, dict]:
"""
Renumber the array and resize the data type
Expand Down

0 comments on commit e702320

Please sign in to comment.