Skip to content

Commit

Permalink
update on ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
WiC-htao committed Aug 14, 2024
1 parent cc02e54 commit 5c638d2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/mox/ruby/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
str_value_map = MappingProxyType({"nan": np.nan})


TIME_CONST = {"ch_eod": "20:00:00",'ch_sod':"07:00:00"}
TIME_CONST = MappingProxyType({"ch_eod": "20:00:00", "ch_sod": "07:00:00"})
4 changes: 4 additions & 0 deletions src/mox/ruby/datatype/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ def aspath(cls, path: Union[str, Self], *, is_dir=None):
if isinstance(path, Path) and (path.is_dir == is_dir or is_dir is None):
return path
return cls(path, is_dir=is_dir)

@property
def var(self):
return re.findall(r"%[^%]*%", self)
34 changes: 26 additions & 8 deletions src/mox/ruby/db/_base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re
from types import MappingProxyType
from typing import Dict
from mox.ruby.datatype import mana

import numpy as np
import yaml

from ._enclosed import TIME_CONST, Mana, Path, load_yaml, make_tuple
from ._enclosed import TIME_CONST, Mana, Path, load_yaml, make_tuple, overwrite_yaml
from .const import DIRECT
from .manapool import ManaIndexer, ManaPool

Expand All @@ -15,7 +17,7 @@ class _Land:
def __init__(self, land_path) -> None:

self._path = Path.aspath(path=land_path)
self._schema: Dict = load_yaml(path=self._path.extend(".structure.yaml"))
self._schema: Dict = load_yaml(path=self._path.extend(".schema.yaml"))
self._schema["name"] = re.search(r"/(?P<name>.*)/$", self._path).group("name")
assert "asset" in self._schema
self._univ = None
Expand All @@ -31,22 +33,24 @@ def __repr__(self):

def _get_mana_from_cache_info(self, cache_info, **kwargs):
# direct_cache_idx = {}
date = cache_info.get("date", self.schema["indexer"]["date"])
date = cache_info["date"]
if isinstance(date, dict) and date.get("type") == "compact:cache":
date = self._calendar.get_cache(date["name"])

time = cache_info.get("time", self.schema["indexer"]["time"])
time = cache_info.get("time", self._schema["time"])
if isinstance(time, str) and not time.startswith("!"):
time = make_tuple(TIME_CONST[time])
time = make_tuple(TIME_CONST.get(time, time))
elif time is None:
time = self.time

security = cache_info.get("security", self.schema["indexer"]["security"])
security = cache_info.get("security")
if isinstance(security, dict) and security.get("type") == "compact:cache":
security = self._univ.get_cache(security["name"])

manas = {}
if cache_info["field"] == DIRECT:
field = make_tuple(kwargs["field"])
for f in field:
fields = make_tuple(kwargs["fields"])
for f in fields:
path = self._path.extend((cache_info["path"])).concretize(field=f)
fmana = np.load(path).view(Mana)
fmana.expr = self.schema["field"][f]["expr"]
Expand All @@ -61,3 +65,17 @@ def _get_mana_from_cache_info(self, cache_info, **kwargs):

indexer = ManaIndexer(date, time, security)
return ManaPool(indexer, manas)

def set_mana_cache(self, manapool, cache_key, date_cache_type="calendar_cache", time_cache_type="plain", **kwargs):
cache_info = {}
if date_cache_type == "calendar_cache":

self._calendar.set_cache(manapool.dates, kwargs.get("calendar_cache_key", cache_key))
else:
raise NotImplementedError
if time_cache_type == "plain":
pass
# if time_cache_type == ""

def __overwrite_schema(self):
pass
2 changes: 1 addition & 1 deletion src/mox/ruby/db/_enclosed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from mox.ruby.const import TIME_CONST
from mox.ruby.datatype import Mana, Path, mana
from mox.ruby.environ import CALENDARPATH, UNIVPATH
from mox.ruby.misc import load_yaml, make_tuple
from mox.ruby.misc import load_yaml, make_tuple, overwrite_yaml
8 changes: 7 additions & 1 deletion src/mox/ruby/db/land.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import itertools

from mox.ruby.misc import make_tuple

from ._base import _Land
from .calendar import Calendar
from .univ import Univ
Expand All @@ -17,10 +19,14 @@ def get_mana(self):
# for concretize_args in dict_product(direct_cache_idx):
# path = self._path.extend(cache_info["path"]).concretize(**{k: kwargs[k] for k in cache_info["require"]})

def get_mana_cache(self, key, **kwargs):
cache_info = self.schema["mana"][key]
cache_info = cache_info.copy()
return self._get_mana_from_cache_info(cache_info, **kwargs)


def dict_product(d):
keys = d.keys()
values = itertools.product(*d.values())
for v in values:
yield dict(zip(keys, v))

11 changes: 5 additions & 6 deletions src/mox/ruby/db/manapool.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from dataclasses import dataclass
from types import MappingProxyType
from dataclasses import dataclass, asdict
from typing import Dict

import numpy as np

from ._enclosed import Mana, make_tuple


@dataclass
@dataclass(slots=True)
class ManaIndexer:
date: np.ndarray
time: np.ndarray
Expand All @@ -23,7 +22,7 @@ def __init__(self, indexer: ManaIndexer, manas: Dict[str, Mana]):
@property
def indexer(self):
# TODO: parse schema to be more clear
return MappingProxyType(self._indexer)
return asdict(self._indexer)

@property
def dates(self):
Expand All @@ -43,8 +42,8 @@ def manas(self):

@property
def shape(self):
tmp_var = self._indexer
return len(tmp_var.date), len(tmp_var.time), len(tmp_var.security)
_idx = self._indexer
return len(_idx.date), len(_idx.time), len(_idx.security)

def depict(self, mana_names=None):
mana_names = make_tuple(mana_names, fill=self.manas)
Expand Down
26 changes: 15 additions & 11 deletions src/mox/ruby/db/univ.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

class Univ(_Land):
__schema__ = ("type", "asset", "univ", "filepath", "start_date")
__cache_schema__ = ('cache',)
__mana_schema__ = ('mana',)
__cache_schema__ = ("cache",)
__mana_schema__ = ("mana",)

def __init__(self, land_path) -> None:
path = Path.aspath(land_path)
if not path.exists():
Expand All @@ -28,7 +28,11 @@ def set_security(self, date, securities, modify=False, with_compress=True):
securities = make_tuple(securities)
with open(filepath, "w", encoding="utf-8") as f:
f.write("\n".join(securities))
def build_diff(self, dates=None, with_compressed=True)
if with_compress:
pass

def build_diff(self, dates=None, with_compressed=True):
pass

def get_security(self, date):
pass
Expand All @@ -38,10 +42,10 @@ def get_id_union(self, dates=None, start=None, end=None):

def get_cache(self, key):
cache_info = self.schema["cache"][key]
if cache_info["type"] == "security":
return np.load(self._path.extend(cache_info["path"]))
if cache_info["type"] == "mana":
cache_info = cache_info.copy()
cache_info["field"] = key
return self._get_mana_from_cache_info(cache_info)
raise NotImplementedError
return np.load(self._path.extend(cache_info["path"]))

def get_mana_cache(self, key, **kwargs):
cache_info = self.schema["mana"][key]
cache_info = cache_info.copy()
cache_info["field"] = key
return self._get_mana_from_cache_info(cache_info)
2 changes: 1 addition & 1 deletion src/mox/ruby/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def dump_yaml(dt, path):


def overwrite_yaml(dt, path):
if os.path.exists(path):
if not os.path.exists(path):
warnings.warn(f"File<{path}> not exists. Encourage using dump_yaml instead to be more safety")
with open(path, "w", encoding="utf-8") as f:
return yaml.safe_dump(dt, f)
Expand Down

0 comments on commit 5c638d2

Please sign in to comment.