Skip to content

Commit

Permalink
No memcache
Browse files Browse the repository at this point in the history
  • Loading branch information
davips committed Jan 25, 2021
1 parent 3c32f98 commit 39b9feb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
29 changes: 17 additions & 12 deletions aiuna/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from garoupa.encoders import integers2bytes, bytes2integers


@lru_cache()
###@lru_cache()
def compression_dict(): # TODO: update dictionary
txt_comming_from_a_file_for_training_of_compressor = "aasfdkjgd kajgsdf af kajgakjsg fkajgs df\n" * 653
samples = [
Expand All @@ -47,14 +47,6 @@ def compression_dict(): # TODO: update dictionary
]
return zs.train_dictionary(dict_size=99999999, samples=samples, threads=-1)


cctx = zs.ZstdCompressor(threads=-1)
cctxdic = zs.ZstdCompressor(threads=-1, dict_data=compression_dict(), write_dict_id=False)

cctxdec = zs.ZstdDecompressor()
cctxdicdec = zs.ZstdDecompressor(dict_data=compression_dict())


# ##################################################

class HashableBinary:
Expand All @@ -70,7 +62,7 @@ def fpack(data, field):
return memopack(HashableBinary(data.uuids[field].n, data[field]))


# @lru_cache()
# ###@lru_cache()
def memopack(hashable_binary):
return pack(hashable_binary.obj)

Expand All @@ -79,7 +71,14 @@ def memopack(hashable_binary):


def pack(obj):
with safety():

cctx = zs.ZstdCompressor(threads=-1)
cctxdic = zs.ZstdCompressor(threads=-1, dict_data=compression_dict(), write_dict_id=False)

cctxdec = zs.ZstdDecompressor()
cctxdicdec = zs.ZstdDecompressor(dict_data=compression_dict())

# with safety():
if isinstance(obj, np.ndarray) and str(obj.dtype) == "float64" and len(obj.shape) == 2:
# X, ...
h, w = obj.shape
Expand All @@ -106,7 +105,13 @@ def pack(obj):


def unpack(dump_with_header):
with safety():
cctx = zs.ZstdCompressor(threads=-1)
cctxdic = zs.ZstdCompressor(threads=-1, dict_data=compression_dict(), write_dict_id=False)

cctxdec = zs.ZstdDecompressor()
cctxdicdec = zs.ZstdDecompressor(dict_data=compression_dict())

# with safety():
header = dump_with_header[:1]
dump = dump_with_header[1:]
if header == b"P":
Expand Down
34 changes: 16 additions & 18 deletions aiuna/content/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ def step(self):
self.step_func_m = self.step_func_m()
return self.step_func_m

@cached_property
###@cached_property
@property
def changed_asdict(self):
"""Evaluate and return all fields in 'changed'."""
return {lazy_field: self.field_funcs_m[lazy_field] for lazy_field in self.changed}
Expand Down Expand Up @@ -231,7 +232,8 @@ def update(self, step, **fields):
newfields.update(updated_fields)
return Data(uuid, uuids, self.history << step, **newfields)

@cached_property
###@cached_property
@property
def eager(self):
"""Touch every lazy field by accessing all fields.
Expand All @@ -240,7 +242,8 @@ def eager(self):
_ = self[f]
return self

@cached_property
###@cached_property
@property
def Xy(self):
return self["X"], self["y"]

Expand Down Expand Up @@ -272,7 +275,7 @@ def __eq__(self, other):

# REMINDER Data is mutable in rare occasions: setitem/delitem/

@lru_cache
###@lru_cache
def arff(self, relation, description):
Xt = [untranslate_type(typ) for typ in self.Xt]
Yt = [untranslate_type(typ) for typ in self.Yt]
Expand Down Expand Up @@ -332,7 +335,7 @@ def mutate(self, newdata):
# if callable(attr) and hasattr(attr, "cacheclear"):
# attr.cacheclear()

@lru_cache()
###@lru_cache()
def __getitem__(self, key):
"""Safe access to a field, with a friendly error message.
Expand Down Expand Up @@ -407,52 +410,48 @@ def items(self):
for k in self:
yield k, self[k]


def __iter__(self):
yield from self.field_funcs_m


def __len__(self):
return len(self.field_funcs_m) # TODO idem


def _name_(self):
return self._uuid


def _context_(self):
return self.history ^ "names"


@staticmethod
def from_pandas(X_pd: DataFrame, y_pd: Series):
X, y = X_pd.to_numpy(), y_pd.to_numpy().astype("float")
Xd, Yd = X_pd.columns.tolist(), [y_pd.name]
Xt, Yt = [translate_type(str(c)) for c in X_pd.dtypes], list(sorted(set(y)))
return new(X=X, y=y, Xd=Xd, Yd=Yd, Xt=Xt, Yt=Yt)


# @property
# @lru_cache()
# ###@lru_cache()
# def ids_lst(self): #TODO ainda precisa?
# return [self.uuids[name].id for name in self.names]
#
# @property
# @lru_cache()
# ###@lru_cache()
# def ids_str(self): #TODO ainda precisa?
# return ','.join(self.ids_lst)
#
# @property
# @lru_cache()
# ###@lru_cache()
# def names_str(self): #TODO ainda precisa?
# return ','.join(self.names)

# @cached_property
# ###@cached_property
@property
# def picklable(self):
# """Remove unpicklable parts."""
# return self.picklable_()[0]
#
# @cached_property
# ###@cached_property
@property
# def unpicklable(self):
# """Restore unpicklable parts."""
# return self.unpicklable_([{}])
Expand Down Expand Up @@ -498,9 +497,8 @@ def icon(self):
"""Information to create an icon for this Data object."""
return {"id": self.id, "step": self.step.asdict_rec, "colors": colors(self.id)}


# ###@cached_property
@property
# @cached_property
def past(self):
"""Map ancestor_id -> afterstep/icon_colors. Last item refers to the current Data object."""
from aiuna.content.root import Root
Expand Down
3 changes: 2 additions & 1 deletion aiuna/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def last(self):
raise Exception("Empty history has no last item.")
return self._last

@cached_property
###@cached_property
@property
def aslist(self):
return [step.asdict for step in self]

Expand Down
3 changes: 2 additions & 1 deletion aiuna/step/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4416,7 +4416,8 @@ def _process_(self, data):
raise Exception(msg)
return self.data

@cached_property
###@cached_property
@property
def data(self):
if isinstance(self.loader, Data):
return self.loader
Expand Down
12 changes: 8 additions & 4 deletions aiuna/step/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def _uuid_(self): # override uuid because default uuid is based on config, whic

# REMINDER File cannot be lazy, since it depends on file content to know the uuid which will be used to generate
# the lazy Data object.
@cached_property
###@cached_property
@property
def data(self):
d = read_arff(self.filename)
ds = d["dataset"], d["description"], d["matrices"], d["original_hashes"]
Expand All @@ -93,19 +94,22 @@ def data(self):

return new(**matrices)

@cached_property
###@cached_property
@property
def dataset(self):
if self._hashes is None:
_ = self.data # force file reading
return self._dataset

@cached_property
###@cached_property
@property
def description(self):
if self._hashes is None:
_ = self.data # force file reading
return self._description

@cached_property
###@cached_property
@property
def hashes(self):
if self._hashes is None:
_ = self.data # force calculation of hashes
Expand Down

0 comments on commit 39b9feb

Please sign in to comment.