Skip to main content

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のviews/)から渡す。ビューはデータを持たないので、マイグレーションはしない。

views/
  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("views")
apply_views(engine, view_set, ApplyOptions(readers=("sazanami_ro",), label="sha-xxxxxxx"))
failures = run_view_tests(engine, view_set)  # CIで使う
  • apply_viewsは、定義(views.yaml・関数・ビューのSQL)のハッシュがmeta.view_versionの最後の記録と同じなら何もしない。違えば1トランザクションで、schemasのスキーマを消して作り直し、関数とビューを作り、readersに読む権限を付け、ハッシュを記録する
  • 定義と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が書く)
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, read_view_frame,
)

spec = pin_view_digest(engine, spec)                 # 学習の前に、今のビューのハッシュを書く
dataset = load_view_dataset(engine, spec, 20130101, 20191231)  # labels="features.labels_v1"
save_model_feature_spec(spec, model_dir)             # モデルと一緒に保存する
inputs = load_view_input(engine, spec, 20261101)    # 推論: その日の入力だけ
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程度)の範囲で一致する。正解ラベル・払戻・並び順は完全に一致する

  • 入力のビューの列: 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年ずつ読む

  • 値のない出走(NULL)があれば止める(今の経路の「行がなければエラー」と同じ)。文字列は0にする

Metadata

Release files for jra-tools 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jra-tools 0.1.8
File Size Uploaded Provenance Actions
jra_tools-0.1.8.tar.gz 41.8 kB With provenance Details for jra_tools-0.1.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jra-tools 0.1.8
File Interpreter ABI Platform Actions
jra_tools-0.1.8-py3-none-any.whl Python 3 none any Details for jra_tools-0.1.8-py3-none-any.whl

Total release size: 96.0 kB

Release files / jra_tools-0.1.8.tar.gz

Download URL jra_tools-0.1.8.tar.gz
Size 41.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c2faf75f1a53fb7308c2df022dd8fd61e15359bde4ee25a4f717d7cf1cf98ed8
BLAKE2b-256 checksum
How to use checksums
cc6634269c5bfbf3ee8b3eb33b31692a084c9b262d6d9ff046e5b3778bdd2f91
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 2, 2026.

Transparency log

Release files / jra_tools-0.1.8-py3-none-any.whl

Download URL jra_tools-0.1.8-py3-none-any.whl
Size 54.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6e3dcd91ffdb93bd26d2e4e9568df44304a1cf228cb6becea274d50f20a0fd9
BLAKE2b-256 checksum
How to use checksums
74c95c6cf5ba144a4ca6221a8206788f852b1a175dae2811e2c619b3a0f0bf0f
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 2, 2026.

Transparency log

Security for jra-tools 0.1.8

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.

Select from 2 files

Attestation for jra_tools-0.1.8.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 2, 2026.

Transparency log

A 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.

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page