注: 以下の翻訳の正確性は検証されていません。AIPを利用して英語版の原文から機械的に翻訳されたものです。
関数を使用して、オントロジー内のデータに基づくカスタム集計を計算し、Workshop のチャートウィジェットに表示できます。このガイドでは、オントロジーから集計データを読み込み、結果を操作して将来の結果の予測を作成し、変更した結果を返すカスタム集計ロジックの記述方法を説明します。
このセクションを進める際には、次のリファレンスが役立ちます。
TwoDimensionalAggregation および ThreeDimensionalAggregation の .from_osdk() は、Python オントロジー SDK の v2 を使用する場合にのみサポートされます。
この例では、経費で構成されるオントロジーがあり、各 expense オブジェクトに部門名、経費の date、経費の amount のプロパティがあるとします。今後6か月間の部門ごとの月次支出を推定するには、まず月次支出の集計データを読み込みます。
Copied!1 2 3 4 5 6 7 8client = FoundryClient() result: AggregateObjectsResponse = ( client.ontology.objects.Expense .group_by(Expense.object_type.department_name.exact()) .group_by(Expense.object_type.date.exact()) .sum(Expense.object_type.amount) .compute() )
次に、各部門の今後6か月間の支出を外挿できます。この例では、最後の月の値を今後6か月間の推定値として使用するシンプルな方法を採用できます。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24current_buckets = ThreeDimensionalAggregation.from_osdk(result, "departmentName", "date") modified_buckets: list[NestedBucket[str, Range[Date], Double]] = [] date_format = "%Y-%m-%d" for bucket in current_buckets.buckets: # 最新月に対応するバケットを見つけます last_bucket: SingleBucket[Date, Double] = bucket.buckets[-1] next_six_months: list[SingleBucket[Range[Date], Double]] = [] # `date` フィールドは YYYY-MM-DD 形式の文字列に変換されています。 # 文字列からデータ型 `Date` に変換します。各月の # SingleBucket オブジェクトを作成するときに、文字列に戻します current_month: Date = datetime.strptime(last_bucket.key, date_format).date() # 6回ループします for _ in range(6): # 現在の月から翌月を作成します next_month = current_month + relativedelta(months=1) # 翌月を日付範囲とする新しいバケットを追加し、 # 最新月の金額を値として使用します next_six_months.append(SingleBucket(Range(min=current_month, max=next_month), last_bucket.value)) current_month = next_month # 変更した結果を NestedBucket として追加します modified_buckets.append(NestedBucket(bucket.key, next_six_months))
今後6か月間の推定値を作成できたので、これらの推定値を ThreeDimensionalAggregation として返すことができます。
Copied!1return ThreeDimensionalAggregation(modified_buckets)
この関数のサンプルコード全体は次のとおりです。
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52from dateutil.relativedelta import relativedelta from datetime import datetime from functions.api import ( Date, Double, NestedBucket, SingleBucket, Range, ThreeDimensionalAggregation, function, ) from ontology_sdk import FoundryClient from ontology_sdk.ontology.objects import Expense @function def estimated_department_expenses() -> ThreeDimensionalAggregation[str, Range[Date], Double]: client = FoundryClient() result = ( client.ontology.objects.Expense .group_by(Expense.object_type.department_name.exact()) .group_by(Expense.object_type.date.exact()) .sum(Expense.object_type.amount) .compute() ) current_buckets = ThreeDimensionalAggregation.from_osdk(result, "departmentName", "date") modified_buckets: list[NestedBucket[str, Range[Date], Double]] = [] date_format = "%Y-%m-%d" for bucket in current_buckets.buckets: # 最新月に対応するバケットを見つけます last_bucket: SingleBucket[Date, Double] = bucket.buckets[-1] next_six_months: list[SingleBucket[Range[Date], Double]] = [] # `date` フィールドは YYYY-MM-DD 形式の文字列に変換されています。 # 文字列からデータ型 `Date` に変換します。 current_month: Date = datetime.strptime(last_bucket.key, date_format).date() # 6回ループします for _ in range(6): # 現在の月から翌月を作成します next_month = current_month + relativedelta(months=1) # 翌月を日付範囲とする新しいバケットを追加し、 # 最新月の金額を値として使用します next_six_months.append(SingleBucket(Range(min=current_month, max=next_month), last_bucket.value)) current_month = next_month # 変更した結果を NestedBucket として追加します modified_buckets.append(NestedBucket(bucket.key, next_six_months)) return ThreeDimensionalAggregation(modified_buckets)
この集計結果を Workshop のチャートで使用して、今後6か月間の月次支出の推定値を表示できます。