From 06d1527a36a5f85a7dae4baadc506dc08f847481 Mon Sep 17 00:00:00 2001 From: c-bata Date: Sun, 3 Mar 2024 01:13:49 +0900 Subject: [PATCH] Simplify type definition in the standalone app --- standalone_app/src/components/TrialTable.tsx | 6 ++---- standalone_app/src/journalStorage.ts | 11 ++--------- standalone_app/src/sqlite3.ts | 18 +++++------------- standalone_app/src/types/index.d.ts | 11 +++++------ 4 files changed, 14 insertions(+), 32 deletions(-) diff --git a/standalone_app/src/components/TrialTable.tsx b/standalone_app/src/components/TrialTable.tsx index 41cabdd4..49028fcb 100644 --- a/standalone_app/src/components/TrialTable.tsx +++ b/standalone_app/src/components/TrialTable.tsx @@ -86,8 +86,7 @@ export const TrialTable: FC<{ null, sortable: true, filterable: false, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - less: (firstEl, secondEl, _): number => { + less: (firstEl, secondEl): number => { const firstVal = firstEl.params.find( (p) => p.name === s.name )?.param_internal_value @@ -117,8 +116,7 @@ export const TrialTable: FC<{ ?.value || null, sortable: attr_spec.sortable, filterable: false, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - less: (firstEl, secondEl, _): number => { + less: (firstEl, secondEl): number => { const firstVal = firstEl.user_attrs.find( (attr) => attr.key === attr_spec.key )?.value diff --git a/standalone_app/src/journalStorage.ts b/standalone_app/src/journalStorage.ts index 64a32f7b..35c80da1 100644 --- a/standalone_app/src/journalStorage.ts +++ b/standalone_app/src/journalStorage.ts @@ -99,15 +99,8 @@ const parseDistribution = (distribution: string): Distribution => { } } else { return { - // TODO(gen740): support other types type: "CategoricalDistribution", - // eslint-disable-next-line @typescript-eslint/no-explicit-any - choices: distributionJson["attributes"]["choices"].map((choice: any) => { - return { - pytype: "str", - value: choice.toString(), - } - }), + choices: distributionJson["attributes"]["choices"], } } } @@ -201,7 +194,7 @@ class JournalStorage { } else if (distribution.type === "IntDistribution") { return value.toString() } else { - return distribution.choices[value].value + return distribution.choices[value] } })(), distribution: distribution, diff --git a/standalone_app/src/sqlite3.ts b/standalone_app/src/sqlite3.ts index 0330750b..d446f588 100644 --- a/standalone_app/src/sqlite3.ts +++ b/standalone_app/src/sqlite3.ts @@ -206,8 +206,8 @@ const getTrialValues = ( db: SQLite3DB, trialId: number, schemaVersion: string -): TrialValueNumber[] => { - const values: TrialValueNumber[] = [] +): number[] => { + const values: number[] = [] if (isGreaterSchemaVersion(schemaVersion, "v3.0.0.c")) { db.exec({ sql: @@ -267,13 +267,13 @@ const getTrialParams = (db: SQLite3DB, trialId: number): TrialParam[] => { const paramInternalValueToExternalValue = ( distribution: Distribution, internalValue: number -): string => { +): CategoricalChoiceType => { if (distribution.type === "FloatDistribution") { return internalValue.toString() } else if (distribution.type === "IntDistribution") { return internalValue.toString() } else { - return distribution.choices[internalValue].value + return distribution.choices[internalValue] } } @@ -336,17 +336,9 @@ const parseDistributionJSON = (t: string): Distribution => { log: true, } } else { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const choices = parsed.attributes.choices.map((value: any) => { - // TODO(c-bata): Support other types - return { - pytype: "str", - value: value.toString(), - } - }) return { type: "CategoricalDistribution", - choices: choices, + choices: parsed.attributes.choices, } } } diff --git a/standalone_app/src/types/index.d.ts b/standalone_app/src/types/index.d.ts index 684858e9..4bc34bf5 100644 --- a/standalone_app/src/types/index.d.ts +++ b/standalone_app/src/types/index.d.ts @@ -1,7 +1,5 @@ declare const IS_VSCODE: boolean -type TrialValueNumber = number -type TrialIntermediateValueNumber = number type TrialState = "Running" | "Complete" | "Pruned" | "Fail" | "Waiting" type TrialStateFinished = "Complete" | "Fail" | "Pruned" type StudyDirection = "maximize" | "minimize" | "not_set" @@ -22,14 +20,15 @@ type IntDistribution = { log: boolean } +type CategoricalChoiceType = null | boolean | number | string type CategoricalDistribution = { type: "CategoricalDistribution" - choices: { pytype: string; value: string }[] + choices: CategoricalChoiceType[] } type TrialIntermediateValue = { step: number - value: TrialIntermediateValueNumber + value: number } type Distribution = @@ -63,7 +62,7 @@ type Trial = { number: number study_id: number state: TrialState - values?: TrialValueNumber[] + values?: number[] params: TrialParam[] intermediate_values: TrialIntermediateValue[] user_attrs: Attribute[] @@ -74,7 +73,7 @@ type Trial = { type TrialParam = { name: string param_internal_value: number - param_external_value: string + param_external_value: CategoricalChoiceType param_external_type: string distribution: Distribution }