jra-tools
For jrdb view. https://github.com/requrd/horseview
Usage
Install
$pip install jra-tools
分析用のPostgres(featuresのビュー)から読む場合は、ドライバ(psycopg)も入れる。
$pip install "jra-tools[postgres]"
Test
$pip install jra_tools/
$pytest jra_tools/tests
tests/viewsの結合テストはPostgresを使う。環境変数TEST_POSTGRES(例: postgresql+psycopg://test_user:test_password@127.0.0.1:5432/test_database)がなければ飛ばす。
ビューの仕組み(jra_tools.views)
分析用のPostgresのmodel・featuresのビューを、定義から作る。具体的な定義は持たず、使う側(sazanami-batchのschemas/)から渡す。ビューはデータを持たないので、マイグレーションはしない。
schemas/
views.yaml 管理するスキーマ・関数のSQL・作る順のビューの一覧
model/races.sql ビュー1つにつき1ファイル。SELECT文だけを書く
tests/*.sql テスト。返した行があれば失敗
schemas: [model, features]
functions: [functions.sql] # ビューより先に流すSQL。省略できる
views: # 作る順に並べる
- model.races
- model.entries
from jra_tools.views import ApplyOptions, apply_views, load_view_set, run_view_tests
view_set = load_view_set("schemas")
apply_views(engine, view_set, ApplyOptions(readers=("sazanami_ro",), label="xxxxxxx"))
failures = run_view_tests(engine, view_set) # CIで使う
apply_viewsは、定義(views.yaml・関数・ビューのSQL)のハッシュがmeta.view_versionの最後の記録と同じなら何もしない。違えば1トランザクションで、schemasのスキーマのビューと関数を消して作り直し、readersに読む権限を付け、ハッシュを記録する- スキーマは消さない(なければ作る)。同じスキーマに置いた表・マテリアライズドビュー(推論の結果など)と、拡張機能が作ったものには触らない
meta.view_versionは作らない。なければViewVersionTableMissingで止まる。表を作るのは使う側(sazanami-batchのapply-model-and-meta-tables)- 関数はCASCADEで消さない。表の列の既定値などが関数を使っていれば、エラーで止まる(全体が戻る)
- 定義とstagingの列が合わなければ
CREATE VIEWでPostgresがエラーにし、全体が戻る(今のビューと記録が残る) - ビューを消すと、ほかのスキーマに置いた、これらのビューに依存するビュー(sandboxなど)も消える
- 読んでいる最中のクエリがあると、ビューを消す排他ロックを待つ。
lock_timeout(既定10秒)で諦め、間を置いてretries回(既定5回)やり直す - 同時に流しても、advisory lockで1つずつ動く
- テストはハッシュに含めない(テストだけを変えても作り直さない)
featuresのビューから読む(view_dataset)
特徴量の仕様の最上位にsource(featuresのビュー)を書くと、学習・推論の入力をそのビューから作る。sourceのない仕様(今の本番モデルのfeature_spec.yaml)は、今までどおりjrdb-modelから読む(ビューから読む仕様を今の経路に渡すとエラー)。
name: resnet4_view
max_horses: 18
source: features.entries_v1 # 入力のビュー
view_digest: 1c445b... # 学習時のビューの定義のハッシュ(pin_view_digestが書く)
train_from: 2013-01-01 # 学習期間(初日・最終日を含む。省略可)
train_to: 2019-12-31
groups:
- name: scores
transform: standardize_per_race
features:
- {name: idm} # sourceを省略すると列名=name
- {name: waku_win_rate, source: waku_win_rate}
from jra_tools.machine_learning.infrastructure.view_dataset import (
load_view_dataset, load_view_input, pin_view_digest, predict_finish_probabilities,
read_view_frame,
)
spec = pin_view_digest(engine, spec) # 学習の前に、今のビューのハッシュを書く
dataset = load_view_dataset(engine, spec, 20130101, 20191231) # labels="features.labels_v1"
spec = dataclasses.replace(spec, train_from=date(2013, 1, 1), train_to=date(2019, 12, 31))
save_model_feature_spec(spec, model_dir) # モデルと一緒に保存する(学習期間も書く)
inputs = load_view_input(engine, spec, 20261101) # 推論: その日の入力だけ
probs = predict_finish_probabilities(engine, spec, 20261101, model) # [(racehorsekey, 1着, 2着, 3着の確率)]
frame = read_view_frame(engine, spec, 20250101, 20251231) # 組み立てる前の1頭1行のDataFrame
-
読んだ表(1頭1行のpandasのDataFrame)を、
frame_dataset(frame_to_dataset・frame_to_input)が仕様に従ってテンソルにする。各レースを1〜18の枠に並べ(空き枠は0)、グループごとにone-hot・レースごとの標準化をしてから、最後に(レース, 18, 次元)のndarrayにする -
今の経路(
assemble_dataset)とは、浮動小数点の誤差(1e-15程度)の範囲で一致する。正解ラベル・払戻・並び順は完全に一致する -
学習期間(
train_from・train_to)は、学習に使った期間をモデルの仕様に残すためのもの。使う側(batch)は、推論する日が学習期間に入っていないかをこれで確かめる -
入力のビューの列:
racehorsekey・racekey・race_date(date)・umabanと仕様の列。ラベルのビューの列:racehorsekey・finish_order・win1_num・win1_ret -
読むたびに、仕様の列がビューにあるか、ビューの定義のハッシュが
view_digestと同じかを確かめ、違えば止める。ビューの定義を変えたら(バグ修正でも)、仕様を書き直すか、ビューの版を上げる(entries_v2を並べる) -
ハッシュは、そのビューと、依存するビュー・関数の定義(Postgresが保存した形)から作る。関係のないビューを足したり変えたりしても変わらない。空白・コメントだけの違いでも変わらない。stagingの表は名前だけを含める。Postgresのメジャーバージョンを上げると変わることがある
-
関数の中から呼ぶ関数をたどれるのは、
RETURNで書いたSQLの関数だけ(AS $$...$$の本体はPostgresが依存を記録しない) -
1回の読み込みは1トランザクションで、先にビューを共有ロックする(読み終わるまで
apply_viewsに作り直されない)。学習は1年ずつ読む -
predict_finish_probabilitiesは確率だけを素の値で返す(連対率・オッズは使う側のビューで出す)。model.predict(x)はactivation_4・activation_5・activation_6(1・2・3着)の(レース, 18)を返すこと(PredictFactoryと同じ出力)。出走のない日は空 -
値のない出走(NULL)があれば止める(今の経路の「行がなければエラー」と同じ)。文字列は0にする
Metadata
Release files for jra-tools 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jra_tools-0.4.0.tar.gz | 44.5 kB | With provenance | Details for jra_tools-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jra_tools-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for jra_tools-0.4.0-py3-none-any.whl |
Total release size: 101.1 kB
Release files / jra_tools-0.4.0.tar.gz
| Download URL | jra_tools-0.4.0.tar.gz |
|---|---|
| Size | 44.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8c481c05099b9138dafaa3e7e52efac2426e413e60e08a27bb207aae33a2be67
|
|
BLAKE2b-256 checksum How to use checksums |
9bc1677086c941f16973b6f71fe94fc0f320692bf6156d6358f428d80629bf87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logRelease files / jra_tools-0.4.0-py3-none-any.whl
| Download URL | jra_tools-0.4.0-py3-none-any.whl |
|---|---|
| Size | 56.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d678d0496cbbf3cb0423fd03391474e3a5c5efad9d1e523bc18be43be0b412cc
|
|
BLAKE2b-256 checksum How to use checksums |
6b6e670fd74982387775955b972fa417db5c9da0ebf3f9438cb0f26e011a9b21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logSecurity for jra-tools 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for jra_tools-0.4.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.