Skip to content

Commit

Permalink
feat: features stats
Browse files Browse the repository at this point in the history
  • Loading branch information
WLM1ke committed Feb 23, 2025
1 parent 50b9875 commit f95a3b2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
3 changes: 2 additions & 1 deletion poptimizer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typer

from poptimizer import consts
from poptimizer.cli import app, portfolio
from poptimizer.cli import app, feat, portfolio


def _main() -> None:
Expand All @@ -12,6 +12,7 @@ def _main() -> None:
)
cli.command()(app.run)
cli.command()(portfolio.export)
cli.command()(feat.stats)
cli()


Expand Down
13 changes: 13 additions & 0 deletions poptimizer/adapters/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ async def get[E: domain.Entity](

return self._create_entity(t_entity, doc)

async def get_all[E: domain.Entity](
self,
t_entity: type[E],
) -> AsyncIterator[E]:
collection_name = adapter.get_component_name(t_entity)
db = self._db[collection_name]

try:
async for doc in db.find({}):
yield self._create_entity(t_entity, doc)
except PyMongoError as err:
raise errors.AdapterError("can't load entities from {collection_name}") from err

async def _load(self, collection_name: str, uid: domain.UID) -> MongoDocument | None:
collection = self._db[collection_name]

Expand Down
69 changes: 69 additions & 0 deletions poptimizer/cli/feat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import asyncio
import contextlib
import statistics
from collections import Counter

import uvloop

from poptimizer import config
from poptimizer.adapters import logger, mongo
from poptimizer.domain.evolve import evolve


async def _run() -> None:
cfg = config.Cfg()
err: Exception | None = None

async with contextlib.AsyncExitStack() as stack:
lgr = await stack.enter_async_context(logger.init())

mongo_db = await stack.enter_async_context(mongo.db(cfg.mongo_db_uri, cfg.mongo_db_db))
repo = mongo.Repo(mongo_db)

lgr.info("Starting...")

try:
count = 0
history_days: list[int] = []
features: Counter[str] = Counter()

async for model in repo.get_all(evolve.Model):
if not model.ver:
continue

count += 1
batch = model.phenotype["batch"]
history_days.append(batch["history_days"])
features.update({"use_lag_feat": batch["use_lag_feat"]})
features.update(batch["num_feats"])
features.update(batch["emb_feats"])
features.update(batch["emb_seq_feats"])

lgr.info("Count - %d", count)
lgr.info(
"History days - %d - %d - %d",
min(history_days),
statistics.median(history_days),
max(history_days),
)
for feature, feat_count in features.most_common():
lgr.info(f"Feature {feature} - {feat_count / count:.2%}")

lgr.info("Finished")
except asyncio.CancelledError:
lgr.info("Shutdown finished")
except Exception as exc: # noqa: BLE001
lgr.warning("Shutdown abnormally: %r", exc)
err = exc

if err:
raise err


def stats() -> None:
"""Features genotype statistics."""
uvloop.run(_run())


if __name__ == "__main__":
stats()
3 changes: 2 additions & 1 deletion poptimizer/cli/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ async def _run(out: Path) -> None:
lgr = await stack.enter_async_context(logger.init())
repo = mongo.Repo(mongo_db)

lgr.info("Starting...")

try:
lgr.info("Starting...")
port = await repo.get(portfolio.Portfolio)
positions = {pos.ticker: shares for pos in port.positions if (shares := sum(pos.accounts.values()))}
out.parent.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit f95a3b2

Please sign in to comment.