Skip to content

Commit

Permalink
renamed, repositioned bytes_to_size_label
Browse files Browse the repository at this point in the history
  • Loading branch information
flexatone committed Sep 30, 2022
1 parent 9bfdecd commit 61c1bd1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
22 changes: 7 additions & 15 deletions doc/source/articles/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static_frame as sf
from static_frame.core.display_color import HexColor
from static_frame.core.util import bytes_to_data_label


class FileIOTest:
Expand Down Expand Up @@ -394,15 +395,6 @@ def plot_performance(frame: sf.Frame):


#-------------------------------------------------------------------------------
def convert_size(size_bytes):
import math
if size_bytes == 0:
return '0B'
size_name = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return '%s %s' % (s, size_name[i])


def plot_size(frame: sf.Frame):
Expand Down Expand Up @@ -450,8 +442,8 @@ def plot_size(frame: sf.Frame):
size_max = results.max()
ax.set_yticks([0, size_max * 0.5, size_max])
ax.set_yticklabels(['',
convert_size(size_max * 0.5),
convert_size(size_max),
bytes_to_data_label(size_max * 0.5),
bytes_to_data_label(size_max),
], fontsize=6)
ax.tick_params(
axis='x',
Expand Down Expand Up @@ -514,21 +506,21 @@ def get_sizes():
size_parquet = os.path.getsize(fp)
os.unlink(fp)
record.append(size_parquet)
record.append(convert_size(size_parquet))
record.append(bytes_to_data_label(size_parquet))

_, fp = tempfile.mkstemp(suffix='.parquet')
df.to_parquet(fp, compression=None)
size_parquet_noc = os.path.getsize(fp)
os.unlink(fp)
record.append(size_parquet_noc)
record.append(convert_size(size_parquet_noc))
record.append(bytes_to_data_label(size_parquet_noc))

_, fp = tempfile.mkstemp(suffix='.npz')
f.to_npz(fp, include_columns=True)
size_npz = os.path.getsize(fp)
os.unlink(fp)
record.append(size_npz)
record.append(convert_size(size_npz))
record.append(bytes_to_data_label(size_npz))

_, fp = tempfile.mkstemp(suffix='.pickle')
file = open(fp, 'wb')
Expand All @@ -537,7 +529,7 @@ def get_sizes():
size_pickle = os.path.getsize(fp)
os.unlink(fp)
record.append(size_pickle)
record.append(convert_size(size_pickle))
record.append(bytes_to_data_label(size_pickle))

record.append(round(size_npz / size_parquet, 3))
record.append(round(size_npz / size_parquet_noc, 3))
Expand Down
8 changes: 4 additions & 4 deletions static_frame/core/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from static_frame.core.util import DTYPE_STR_KINDS
from static_frame.core.util import FLOAT_TYPES
from static_frame.core.util import CallableToIterType
from static_frame.core.util import _gen_skip_middle
from static_frame.core.util import gen_skip_middle

if tp.TYPE_CHECKING:
from static_frame.core.index_base import IndexBase # pylint: disable=unused-import #pragma: no cover
Expand Down Expand Up @@ -459,7 +459,7 @@ def from_values(cls,
count_max = config.display_rows
if len(values) > config.display_rows:
data_half_count = Display.truncate_half_count(count_max)
value_gen = partial(_gen_skip_middle,
value_gen = partial(gen_skip_middle,
forward_iter=values.__iter__,
forward_count=data_half_count,
reverse_iter=partial(reversed, values),
Expand Down Expand Up @@ -532,7 +532,7 @@ def from_params(cls,
# columns as they will look after application of truncation and insertion of ellipsis
data_half_count = cls.truncate_half_count(config.display_columns)

column_gen = partial(_gen_skip_middle,
column_gen = partial(gen_skip_middle,
forward_iter=column_forward_iter,
forward_count=data_half_count,
reverse_iter=column_reverse_iter,
Expand Down Expand Up @@ -847,7 +847,7 @@ def extend_iterable(self,

if len(iterable) > count_max:
data_half_count = self.truncate_half_count(count_max)
value_gen: tp.Callable[[], tp.Iterator[tp.Any]] = partial(_gen_skip_middle,
value_gen: tp.Callable[[], tp.Iterator[tp.Any]] = partial(gen_skip_middle,
forward_iter=iterable.__iter__,
forward_count=data_half_count,
reverse_iter=partial(reversed, iterable),
Expand Down
36 changes: 15 additions & 21 deletions static_frame/core/memory_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np

from static_frame.core.util import DTYPE_OBJECT_KIND
from static_frame.core.util import bytes_to_data_label

if tp.TYPE_CHECKING:
from static_frame.core.frame import Frame # pylint: disable=W0611 #pragma: no cover
Expand All @@ -35,29 +36,32 @@ class MeasureFormat(Enum):
materialized=True,
data_only=True,
)
SHARED = MFConfig(
# NOTE MFConfig(local_only=True, materialized=False, data_only=True) is invalid

REFERENCED = MFConfig(
local_only=False,
materialized=False,
data_only=False,
)
SHARED_MATERIALIZED = MFConfig(
REFERENCED_MATERIALIZED = MFConfig(
local_only=False,
materialized=True,
data_only=False,
)
SHARED_MATERIALIZED_DATA = MFConfig(
REFERENCED_MATERIALIZED_DATA = MFConfig(
local_only=False,
materialized=True,
data_only=True,
)
# NOTE MFConfig(local_only=False, materialized=False, data_only=True) is invalid

FORMAT_TO_DISPLAY = {
MeasureFormat.LOCAL: 'local',
MeasureFormat.LOCAL_MATERIALIZED: 'local_material',
MeasureFormat.LOCAL_MATERIALIZED_DATA: 'local_material_data',
MeasureFormat.SHARED: 'shared',
MeasureFormat.SHARED_MATERIALIZED: 'shared_material',
MeasureFormat.SHARED_MATERIALIZED_DATA: 'shared_material_data',
MeasureFormat.LOCAL: 'L',
MeasureFormat.LOCAL_MATERIALIZED: 'LM',
MeasureFormat.LOCAL_MATERIALIZED_DATA: 'LMD',
MeasureFormat.REFERENCED: 'R',
MeasureFormat.REFERENCED_MATERIALIZED: 'RM',
MeasureFormat.REFERENCED_MATERIALIZED_DATA: 'RMD',
}

class MaterializedArray:
Expand Down Expand Up @@ -127,7 +131,7 @@ def _iter_slots(obj: tp.Any) -> tp.Iterator[tp.Any]:
def nested_sizable_elements(cls,
obj: tp.Any,
*,
format: MeasureFormat = MeasureFormat.SHARED,
format: MeasureFormat = MeasureFormat.REFERENCED,
seen: tp.Set[int],
) -> tp.Iterator[tp.Any]:
'''
Expand Down Expand Up @@ -165,20 +169,10 @@ def nested_sizable_elements(cls,
yield obj


def bytes_to_data_label(size_bytes: int) -> str:
if size_bytes == 0:
return '0B'
size_name = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f'{s} {size_name[i]}'


def getsizeof_total(
obj: tp.Any,
*,
format: MeasureFormat = MeasureFormat.SHARED,
format: MeasureFormat = MeasureFormat.REFERENCED,
seen: tp.Union[None, tp.Set[tp.Any]] = None,
) -> int:
'''
Expand Down
12 changes: 11 additions & 1 deletion static_frame/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,16 @@ class PairRight(Pair):

#-------------------------------------------------------------------------------

def bytes_to_data_label(size_bytes: int) -> str:
if size_bytes == 0:
return '0B'
size_name = ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f'{s} {size_name[i]}'



# def mloc(array: np.ndarray) -> int:
# '''Return the memory location of an array.
Expand Down Expand Up @@ -693,7 +703,7 @@ class PairRight(Pair):
# yield v
# last = v

def _gen_skip_middle(
def gen_skip_middle(
forward_iter: CallableToIterType,
forward_count: int,
reverse_iter: CallableToIterType,
Expand Down
6 changes: 3 additions & 3 deletions static_frame/test/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from static_frame.core.util import UFUNC_MAP
from static_frame.core.util import WarningsSilent
from static_frame.core.util import _array_to_duplicated_sortable
from static_frame.core.util import _gen_skip_middle
from static_frame.core.util import _isin_1d
from static_frame.core.util import _isin_2d
from static_frame.core.util import _ufunc_logical_skipna
Expand All @@ -42,6 +41,7 @@
from static_frame.core.util import datetime64_not_aligned
from static_frame.core.util import dtype_from_element
from static_frame.core.util import dtype_to_fill_value
from static_frame.core.util import gen_skip_middle
from static_frame.core.util import get_tuple_constructor
from static_frame.core.util import intersect1d
from static_frame.core.util import intersect2d
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_gen_skip_middle_a(self) -> None:
forward = lambda: [3, 2, 5]
reverse = lambda: [3, 2, 5]

post = _gen_skip_middle(
post = gen_skip_middle(
forward_iter=forward,
forward_count=3,
reverse_iter=reverse,
Expand All @@ -98,7 +98,7 @@ def test_gen_skip_middle_a(self) -> None:

self.assertEqual(list(post), [3, 2, 5, -1, 5, 2, 3])

post = _gen_skip_middle(
post = gen_skip_middle(
forward_iter=forward,
forward_count=2,
reverse_iter=reverse,
Expand Down

0 comments on commit 61c1bd1

Please sign in to comment.