From eb02a402931da713fb0046b2323de0ea2d43b44e Mon Sep 17 00:00:00 2001 From: Matt Fulp <8397318+fulpm@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:35:01 -0400 Subject: [PATCH] migrate trainee operations to Trainee class --- codegen/engine.ts | 1 + codegen/generator.ts | 17 +- .../templates/client/AbstractBaseClient.njk | 187 --- codegen/templates/engine/Trainee.njk | 31 +- src/client/AbstractBaseClient.ts | 1010 +---------------- .../worker/HowsoWorkerClient.node.test.ts | 3 +- src/client/worker/HowsoWorkerClient.ts | 92 +- src/engine/Trainee.ts | 532 ++++++--- 8 files changed, 467 insertions(+), 1406 deletions(-) delete mode 100644 codegen/templates/client/AbstractBaseClient.njk diff --git a/codegen/engine.ts b/codegen/engine.ts index 5530fa5..63c0dee 100644 --- a/codegen/engine.ts +++ b/codegen/engine.ts @@ -35,6 +35,7 @@ export interface LabelDefinition { parameters: Record | null; returns?: SchemaType | Schema | Ref | null; description?: string | null; + use_active_session?: boolean; attribute?: boolean; long_running?: boolean; read_only?: boolean; diff --git a/codegen/generator.ts b/codegen/generator.ts index 50e51de..3c7c819 100644 --- a/codegen/generator.ts +++ b/codegen/generator.ts @@ -49,6 +49,19 @@ export class Generator { react_into_features: "null", }; + // TODO - Remove once #21784 is merged + for (const label of [ + "train", + "impute", + "clear_imputed_data", + "edit_cases", + "move_cases", + "add_feature", + "remove_feature", + ]) { + this.doc.labels[label].use_active_session = true; + } + // Setup template engine const loader = new nunjucks.FileSystemLoader(path.join(__dirname, "templates")); this.env = new nunjucks.Environment(loader, { throwOnUndefined: true }); @@ -73,10 +86,6 @@ export class Generator { targetLabels[label] = definition; } } - this.renderFile(this.clientDir, "AbstractBaseClient.ts", "client/AbstractBaseClient.njk", { - labels: targetLabels, - shims: this.responseShims, - }); this.renderFile(this.engineDir, "Trainee.ts", "engine/Trainee.njk", { labels: targetLabels, shims: this.responseShims, diff --git a/codegen/templates/client/AbstractBaseClient.njk b/codegen/templates/client/AbstractBaseClient.njk deleted file mode 100644 index 8bcf8c5..0000000 --- a/codegen/templates/client/AbstractBaseClient.njk +++ /dev/null @@ -1,187 +0,0 @@ -/** - * WARNING: This file is auto generated, do not modify directly, instead modify the template. - * Generated via Howso Engine {{ version }} - */ -import type { Session, Trainee } from "../engine"; -import type { BaseTrainee, ClientResponse } from "../types"; -import type * as schemas from "../types/schemas"; -import type * as shims from "../types/shims"; -import { DEFAULT_ERROR_MESSAGE, HowsoError, HowsoValidationError } from "./errors"; -import type { CacheMap } from "./utilities/cache"; -import { type Logger, nullLogger } from "./utilities/logger"; - -export interface ClientCache { - trainee: Trainee; - feature_attributes?: schemas.FeatureAttributesIndex; -} - -export type ExecuteResponse = { - payload: R; - errors: HowsoError[]; - warnings: string[]; -}; - -export type AbstractBaseClientOptions = { - /** Enable logging for operations through your own methods */ - logger?: Logger; -}; - -export abstract class AbstractBaseClient { - protected abstract cache: CacheMap; - protected logger: Logger; - - constructor(options?: AbstractBaseClientOptions) { - this.logger = options?.logger || nullLogger; - } - - /** - * Prepare payload for an Engine request. - * @param payload The Engine payload. - * @returns The updated Engine payload. - */ - protected prepareRequest(payload: any) { - if (!payload) { - return payload; - } - - // Remove null properties from payload - const filtered = { ...payload }; - for (const key in filtered) { - if (filtered[key as keyof typeof filtered] == null) { - delete filtered[key as keyof typeof filtered]; - } - } - - return filtered; - } - - /** - * Process payload from an Engine response. - * @param data The Engine response. - * @returns The updated Engine response. - */ - protected processResponse(data: any): ExecuteResponse { - if (!data) { - throw new HowsoError("Null or empty response received from Engine."); - } - - if (Array.isArray(data) && data.length == 2) { - const errors: HowsoError[] = []; - const warnings: string[] = []; - const isSuccess = data[0]; - const value = data[1]; - - if (isSuccess) { - // Collect warnings - if (Array.isArray(value?.warnings)) { - for (const msg of value.warnings) { - if (msg != null) warnings.push(msg); - } - } - } else { - // Collect errors - if (value?.errors) { - errors.push(new HowsoValidationError(value.detail, value.code, value.errors)); - } else { - errors.push(new HowsoError(value.detail, value.code)); - } - if (errors.length == 0) { - errors.push(new HowsoError(DEFAULT_ERROR_MESSAGE)); - } - } - - errors.forEach(this.logger.error); - warnings.forEach(this.logger.warn); - - return { - errors, - warnings, - payload: isSuccess ? value?.payload : undefined, - }; - } else if (["string", "number", "bigint", "boolean"].indexOf(typeof data) != -1) { - return { errors: [], warnings: [], payload: data }; - } - - throw new HowsoError("Malformed response received from Engine."); - } - - /** Setup the client instance for interacting with the Howso Engine. */ - public abstract setup(): Promise; - - /** - * Execute a label of an Engine entity. - * @param handle The identifier of the entity. - * @param label The name of the method to execute. - * @param data The data to pass to the method. - * @param throwErrors If errors should be thrown automatically. - */ - public abstract execute( - handle: string, - label: string, - data: D, - throwErrors?: boolean, - ): Promise>; - - /** Create a new Trainee. */ - public abstract createTrainee(properties: Omit): Promise; - - /** Update an existing Trainee. */ - public abstract updateTrainee(trainee: BaseTrainee): Promise; - - /** Copy an existing Trainee. */ - public abstract copyTrainee(traineeId: string, name?: string): Promise; - - /** Get an existing Trainee by Id. */ - public abstract getTrainee(traineeId: string): Promise; - - /** Search existing Trainees. */ - public abstract queryTrainees(keywords: string | string[]): Promise; - - /** Delete a Trainee. */ - public abstract deleteTrainee(traineeId: string): Promise; - - /** Acquire the resources for a Trainee. */ - public abstract acquireTraineeResources(traineeId: string): Promise; - - /** Release the resources for a Trainee. */ - public abstract releaseTraineeResources(traineeId: string): Promise; - - /** Persist a Trainee to storage. **/ - public abstract persistTrainee(traineeId: string): Promise; - - /** Automatically resolve a Trainee and ensure it is loaded. */ - protected abstract autoResolveTrainee(traineeId: string): Promise; - - /** Automatically persist Trainee object when appropriate based on persistence level. */ - protected abstract autoPersistTrainee(traineeId: string): Promise; - - /** Get active Session. */ - public abstract getActiveSession(): Promise>; - - /** Begin a new Session. */ - public abstract beginSession(name?: string, metadata?: Record): Promise; -{% for label, def in labels | dictsort %} - {%- set requestName = "schemas." + label | pascalCase + "Request" %} - {%- if def.returns | isRef %} - {%- set responseName = "schemas." + def.returns.ref %} - {%- else %} - {%- set responseName = "schemas." + label | pascalCase + "Response" %} - {%- endif %} - /** - * {{ def.description | blockComment | safe | indent(2) }} - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async {{ label | camelCase }}(traineeId: string{% if def.parameters | length %}, request: {{ requestName }}{% endif %}): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute<{% if def.returns %}{{ responseName }}{% elif label in shims %}{{ shims[label] }}{% else %}any{% endif %}>(trainee.id, "{{ label }}", {% if def.parameters | length %}request{% else %}{}{% endif %}); - {%- if not def.read_only %} - this.autoPersistTrainee(trainee.id); - {%- endif %} - return { payload: response.payload, warnings: response.warnings }; - } -{%- if not loop.last %} -{% endif %} -{%- endfor %} -} \ No newline at end of file diff --git a/codegen/templates/engine/Trainee.njk b/codegen/templates/engine/Trainee.njk index 31e7fea..5e393ab 100644 --- a/codegen/templates/engine/Trainee.njk +++ b/codegen/templates/engine/Trainee.njk @@ -4,8 +4,9 @@ */ import { AbstractBaseClient } from "../client/AbstractBaseClient"; import { batcher, BatchOptions } from "../client/utilities"; -import type { BaseTrainee, ClientBatchResponse } from "../types"; +import type { BaseTrainee, ClientBatchResponse, ClientResponse } from "../types"; import type * as schemas from "../types/schemas"; +import type * as shims from "../types/shims"; /** * The interface for interacting with a Trainee. Should not be instantiated directly. Instead create or request a @@ -93,7 +94,7 @@ export class Trainee implements BaseTrainee { async function* (this: Trainee, size: number) { let offset = 0; while (offset < cases.length) { - const response = await this.client.train(this.id, { + const response = await this.train({ ...rest, cases: cases.slice(offset, offset + size), }); @@ -115,6 +116,20 @@ export class Trainee implements BaseTrainee { return { payload: { num_trained, status, ablated_indices }, warnings }; } + /** + * Include the active session in a request if not defined. + * @param request The Trainee request object. + * @returns The Trainee request object with a session. + */ + protected async includeSession>(request: T): Promise { + if (!request.session) { + // Include the active session + const session = await this.client.getActiveSession(); + return { ...request, session: session.id }; + } + return request; + } + {% for label, def in labels | dictsort %} {%- set requestName = "schemas." + label | pascalCase + "Request" %} {%- if def.returns | isRef %} @@ -128,8 +143,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async {{ label | camelCase }}({% if def.parameters | length %}request: {{ requestName }}{% endif %}) { - return this.client.{{ label | camelCase }}(this.id, {% if def.parameters | length %}request{% endif %}); + public async {{ label | camelCase }}({% if def.parameters | length %}request: {{ requestName }}{% endif %}): Promise> { + await this.client.autoResolveTrainee(this.id); + {%- if def.use_active_session and def.parameters %} + request = await this.includeSession(request); + {%- endif %} + const response = await this.client.execute<{% if def.returns %}{{ responseName }}{% elif label in shims %}{{ shims[label] }}{% else %}any{% endif %}>(this.id, "{{ label }}", {% if def.parameters | length %}request{% else %}{}{% endif %}); + {%- if not def.read_only %} + this.client.autoPersistTrainee(this.id); + {%- endif %} + return { payload: response.payload, warnings: response.warnings }; } {%- if not loop.last %} {% endif %} diff --git a/src/client/AbstractBaseClient.ts b/src/client/AbstractBaseClient.ts index 900d8fa..49974d9 100644 --- a/src/client/AbstractBaseClient.ts +++ b/src/client/AbstractBaseClient.ts @@ -1,18 +1,11 @@ -/** - * WARNING: This file is auto generated, do not modify directly, instead modify the template. - * Generated via Howso Engine 86.1.0+alpha - */ import type { Session, Trainee } from "../engine"; -import type { BaseTrainee, ClientResponse } from "../types"; -import type * as schemas from "../types/schemas"; -import type * as shims from "../types/shims"; +import type { BaseTrainee } from "../types"; import { DEFAULT_ERROR_MESSAGE, HowsoError, HowsoValidationError } from "./errors"; import type { CacheMap } from "./utilities/cache"; import { type Logger, nullLogger } from "./utilities/logger"; export interface ClientCache { trainee: Trainee; - feature_attributes?: schemas.FeatureAttributesIndex; } export type ExecuteResponse = { @@ -150,1011 +143,14 @@ export abstract class AbstractBaseClient { public abstract persistTrainee(traineeId: string): Promise; /** Automatically resolve a Trainee and ensure it is loaded. */ - protected abstract autoResolveTrainee(traineeId: string): Promise; + public abstract autoResolveTrainee(traineeId: string): Promise; /** Automatically persist Trainee object when appropriate based on persistence level. */ - protected abstract autoPersistTrainee(traineeId: string): Promise; + public abstract autoPersistTrainee(traineeId: string): Promise; /** Get active Session. */ public abstract getActiveSession(): Promise>; /** Begin a new Session. */ public abstract beginSession(name?: string, metadata?: Record): Promise; - - /** - * Adds the specified feature on all cases for a trainee that match the specified condition. overwrites features that - * If condition are not specified, adds feature for all cases and to the model. If condition is an empty assoc, will not modify feature metadata in the model. - * If feature attributes are passed in, will also set the model's feature attributes. - * Updates the accumulated data mass for the model proportional to the number of cases modified. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async addFeature(traineeId: string, request: schemas.AddFeatureRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "add_feature", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Analyzes the data to compute the appropriate statistics, uncertainties, and select parameters as appropriate. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async analyze(traineeId: string, request: schemas.AnalyzeRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "analyze", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Append cases to a series - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async appendToSeriesStore( - traineeId: string, - request: schemas.AppendToSeriesStoreRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "append_to_series_store", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Automatically analyze the model using stored parameters from previous analyze calls - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async autoAnalyze(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "auto_analyze", {}); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Clear values that were imputed during a specified session, but won't clear values that were manually set by user after the impute - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async clearImputedData( - traineeId: string, - request: schemas.ClearImputedDataRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "clear_imputed_data", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Creates a copy of a trainee and stores it a subtrainee, returns the name of the copied trainee on success - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async copySubtrainee( - traineeId: string, - request: schemas.CopySubtraineeRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "copy_subtrainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Creates a new instance of a contained trainee as specified by the entity label "trainee". - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async createSubtrainee( - traineeId: string, - request: schemas.CreateSubtraineeRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "create_subtrainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Removes replay specified by session and any references within cases - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async deleteSession(traineeId: string, request: schemas.DeleteSessionRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "delete_session", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Destroys the instance of the trainee specified by the parameter "trainee". - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async deleteSubtrainee( - traineeId: string, - request: schemas.DeleteSubtraineeRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "delete_subtrainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Edit feature values for the specified cases. - * Cases are specified by either case_indices or by the condition. If neither is provided, edits all cases. - * Updates the accumulated data mass for the model proportional to the number of cases and features modified. - * returns null if invalid features specified or an assoc with "count" - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async editCases(traineeId: string, request: schemas.EditCasesRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "edit_cases", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Evaluate custom Amalgam code for feature values of every case in the model and returns - * a list of the custom code's return values for each feature specified. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async evaluate( - traineeId: string, - request: schemas.EvaluateRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "evaluate", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Execute any method in the API directly on any child trainee of this trainee, used for hierarchy operations. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async executeOnSubtrainee( - traineeId: string, - request: schemas.ExecuteOnSubtraineeRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "execute_on_subtrainee", - request, - ); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Export trainee's metadata, case and session data into json files. - * this method should be run by a script from the ./migrations folder - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async exportTrainee(traineeId: string, request: schemas.ExportTraineeRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "export_trainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Get auto-ablation parameters set by #set_auto_ablation_params - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getAutoAblationParams( - traineeId: string, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "get_auto_ablation_params", - {}, - ); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns assoc with features and cases - a list of lists of all feature values. Retrieves all feature values for cases for - * all (unordered) sessions in the order they were trained within each session. If a session is specified, only that session's - * cases wil be output. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getCases( - traineeId: string, - request: schemas.GetCasesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_cases", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns an assoc with case distances, containing a list of case session indices and a list of lists (matrix) of the computed distances. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getDistances( - traineeId: string, - request: schemas.GetDistancesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_distances", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns the full entity path to a child trainee provided its unique trainee id if it is contained in the hierarchy. - * Iterates down the hierarchy searching for a trainee that matches the specified id, returns null if not found or - * a string error if found but trainee is stored externally as an independent trainee. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getEntityPathById( - traineeId: string, - request: schemas.GetEntityPathByIdRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "get_entity_path_by_id", - request, - ); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Method to return the list of all model attributes that can be exported/imported - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getExportAttributes(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_export_attributes", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Retrieve the top or bottom number of cases for a specified feature, sorted top to bottom for top, and bottom to top for bottom - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getExtremeCases( - traineeId: string, - request: schemas.GetExtremeCasesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_extreme_cases", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Output all feature attributes - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getFeatureAttributes(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_feature_attributes", {}); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Computes the conviction for each feature and returns an assoc of feature -> conviction value - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getFeatureConviction( - traineeId: string, - request: schemas.GetFeatureConvictionRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "get_feature_conviction", - request, - ); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Pull the hierarchy for a trainee, returns an assoc of: - * the currently contained hierarchy as a nested assoc with (false) for trainees that are stored independently. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getHierarchy(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_hierarchy", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Outputs all marginal stats (min, max, median, mean, mode, count, uniques, mean_absdev, variance, stddev, skew, kurtosis, entropy) - * for all features in the format of feature -> assoc stat -> value. The marginal stats can be computed for a subset of the data using condition, precision, and num_cases - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getMarginalStats( - traineeId: string, - request: schemas.GetMarginalStatsRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_marginal_stats", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Get metadata for model - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getMetadata(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_metadata", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns the total number of training cases for trainee - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getNumTrainingCases(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_num_training_cases", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns a list of computed distances between respective cases specified in either from_values or from_case_indices to to_values or to_case_indices. - * If one case is specified in any of the lists, all respective distances are computed to/from that one case. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getPairwiseDistances( - traineeId: string, - request: schemas.GetPairwiseDistancesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "get_pairwise_distances", - request, - ); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Return the full internal parameters map if no parameters are specified. - * if any of the parameters are specified, then GetHyperparameters is called, which uses the specified parameters to find the most suitable set of hyperparameters to return - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getParams( - traineeId: string, - request: schemas.GetParamsRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_params", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns the trainee's !revision - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getRevision(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_revision", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns a list of all of the training sessions, assoc of id->session, and whatever other attributes specified. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getSessions( - traineeId: string, - request: schemas.GetSessionsRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_sessions", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Return list of all session indices for a specified session. - * session indices are 0-based index of number of the case for the session used for replays; may change if cases are removed - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getSessionIndices( - traineeId: string, - request: schemas.GetSessionIndicesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_session_indices", request); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns all the metadata for a specified session - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getSessionMetadata( - traineeId: string, - request: schemas.GetSessionMetadataRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "get_session_metadata", - request, - ); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Return list of all session training indices for a specified session. - * session training indices are 0-based index of the case, ordered by training during the session; is not changed - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getSessionTrainingIndices( - traineeId: string, - request: schemas.GetSessionTrainingIndicesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute( - trainee.id, - "get_session_training_indices", - request, - ); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns the substitution map - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getSubstituteFeatureValues(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_substitute_feature_values", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Returns the trainee's unique id - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getTraineeId(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_trainee_id", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * The version stored in trainee - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async getTraineeVersion(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "get_trainee_version", {}); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Imputes the model, filling in all the (null) feature values - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async impute(traineeId: string, request: schemas.ImputeRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "impute", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Attempts to load a subtrainee with the following optional parameters. - * If a parameter is not specified, it will look to this entity's own label of the same name. - * If the saved instance does not exist the existing trainee will remain unmodified and the function will return null. - * assumes loaded trainee filenames need to be escaped - * returns the trainee name if successful, null if not - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async loadSubtrainee(traineeId: string, request: schemas.LoadSubtraineeRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "load_subtrainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Moves all cases that match the specified conditions in the hierarchy of the specified trainee - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async moveCases( - traineeId: string, - request: schemas.MoveCasesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "move_cases", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Run reacts in a batch, output a an assoc of list of outputs from each individual react. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async react(traineeId: string, request: schemas.ReactRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "react", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Computes, caches, and returns specified details and feature prediction statistics such as Mean Decrease in Accuracy (MDA), residuals (accuracy, Mean Absolute Error), - * precision, recall, etc. Returns details and feature prediction stats for all features in the format of feature -> assoc stat -> value - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async reactAggregate( - traineeId: string, - request: schemas.ReactAggregateRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "react_aggregate", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Computes the convictions of an average case for each given hypothetical set of cases specified - * output an assoc react key -> list of corresponding values from each individual group. - * example output for 2 groups: - * (assoc - * "base_model_average_distance_contribution" (list 4.0 4.1) - * "combined_model_average_distance_contribution" (list 4.05 3.9) - * "distance_contributions" (list 4.5 3.2) - * ) - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async reactGroup(traineeId: string, request: schemas.ReactGroupRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "react_group", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Computes various data, such as familiarity convictions and distance contribution for each case in the model and stores them into specified features. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async reactIntoFeatures( - traineeId: string, - request: schemas.ReactIntoFeaturesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "react_into_features", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * React in a series until a series_stop_map condition is met. Aggregates rows of data corresponding to the specified context, action, - * derived_context and derived_action features, utilizing previous rows to derive values as necessary. Outputs an assoc of "action_features" and - * corresponding "series" where "series" is the completed 'matrix' for the corresponding action_features and derived_action_features. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async reactSeries(traineeId: string, request: schemas.ReactSeriesRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "react_series", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Reduce the trained data by removing cases which have an influence weight entropy that falls above - * a threshold. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async reduceData( - traineeId: string, - request: schemas.ReduceDataRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "reduce_data", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Removes all cases that match the specified conditions from trainee - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async removeCases(traineeId: string, request: schemas.RemoveCasesRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "remove_cases", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Removes the specified feature on all cases for a trainee that match the specified condition - * if conditions are not specified, removes feature for all cases and from the model, if condition is an empty assoc, leaves the feature metadata in the model. - * Updates the accumulated data mass for the model proportional to the number of cases modified. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async removeFeature(traineeId: string, request: schemas.RemoveFeatureRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "remove_feature", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Clears stored series - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async removeSeriesStore( - traineeId: string, - request: schemas.RemoveSeriesStoreRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "remove_series_store", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Rename a contained trainee - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async renameSubtrainee( - traineeId: string, - request: schemas.RenameSubtraineeRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "rename_subtrainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Resets all hyperparameters and thresholds back to original values, while leaving feature definitions alone - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async resetParams(traineeId: string): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "reset_params", {}); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Saves a subtrainee with the following optional parameters, escapes trainee filenames on save - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async saveSubtrainee(traineeId: string, request: schemas.SaveSubtraineeRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "save_subtrainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Sets the model to auto-ablate by tracking its size and training certain cases as weights - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setAutoAblationParams( - traineeId: string, - request: schemas.SetAutoAblationParamsRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_auto_ablation_params", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Sets the model to auto-analyze by tracking its size and notifying the clients in train responses when it should be analyzed - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setAutoAnalyzeParams( - traineeId: string, - request: schemas.SetAutoAnalyzeParamsRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_auto_analyze_params", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set all features and their attributes for the trainee, and returns the updated feature attributes - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setFeatureAttributes( - traineeId: string, - request: schemas.SetFeatureAttributesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_feature_attributes", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set the influence weight threshold for outputting only the K neighbors whose influence weight is <= to this threshold - * default value is 0.99 - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setInfluenceWeightThreshold( - traineeId: string, - request: schemas.SetInfluenceWeightThresholdRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_influence_weight_threshold", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set metadata for model - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setMetadata(traineeId: string, request: schemas.SetMetadataRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_metadata", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Sets internal hyperparameters - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setParams(traineeId: string, request: schemas.SetParamsRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_params", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set trainee's unique parent id - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setParentId(traineeId: string, request: schemas.SetParentIdRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_parent_id", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set the random seed on a trainee - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setRandomSeed(traineeId: string, request: schemas.SetRandomSeedRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_random_seed", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set session metadata for a specified session. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setSessionMetadata( - traineeId: string, - request: schemas.SetSessionMetadataRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_session_metadata", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Sets substitution feature values used in case generation - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setSubstituteFeatureValues( - traineeId: string, - request: schemas.SetSubstituteFeatureValuesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_substitute_feature_values", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Set trainee's unique id - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async setTraineeId(traineeId: string, request: schemas.SetTraineeIdRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "set_trainee_id", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * React to a provided context. If desired_conviction is provided, it does a generative react. - * - * Output: - * Default output of this method is a react object in the format of - * , where all_action_values is a list of all action - * values, reacted/generated and derived, in the same order as all_action_features, e.g., [2, "a", .75384] to match ['width','name','height'] - * If details is specified, the react object will contain appropriate additional details properties and values, - * Details example: { 'action_values': [2, "a", .75384], 'action_features' : ['width','name','height'], 'residual_conviction': 1.3, - * 'influential_cases' : etc... } - * See API docs for documentation of all output properties - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async singleReact(traineeId: string, request: schemas.SingleReactRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "single_react", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * React in a series until a series_stop_map condition is met. Aggregates rows of data corresponding to the specified context, action, - * derived_context and derived_action features, utilizing previous rows to derive values as necessary. Outputs an assoc of "action_features" and - * corresponding "series" where "series" is the completed 'matrix' for the corresponding action_features and derived_action_features. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async singleReactSeries( - traineeId: string, - request: schemas.SingleReactSeriesRequest, - ): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "single_react_series", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Train the provided cases, filtering out cases that match optionally passed in ablation parameters. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async train(traineeId: string, request: schemas.TrainRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "train", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } - - /** - * Update version to latest, auto importing any exported data. - * @param traineeId The Trainee identifier. - * @param request The operation parameters. - * @returns The response of the operation, including any warnings. - */ - public async upgradeTrainee(traineeId: string, request: schemas.UpgradeTraineeRequest): Promise> { - const trainee = await this.autoResolveTrainee(traineeId); - const response = await this.execute(trainee.id, "upgrade_trainee", request); - this.autoPersistTrainee(trainee.id); - return { payload: response.payload, warnings: response.warnings }; - } } diff --git a/src/client/worker/HowsoWorkerClient.node.test.ts b/src/client/worker/HowsoWorkerClient.node.test.ts index 75b9682..52644bd 100644 --- a/src/client/worker/HowsoWorkerClient.node.test.ts +++ b/src/client/worker/HowsoWorkerClient.node.test.ts @@ -27,7 +27,8 @@ describe("Node HowsoWorkerClient", () => { const data = JSON.parse(readFileSync(dataPath, { encoding: "utf8" })); const feature_attributes = await inferFeatureAttributes(data, "array", {}); const trainee = await client.createTrainee({ name: "My Trainee" }); - await client.setFeatureAttributes(trainee.id, { feature_attributes }); + const { payload: set_feature_attributes } = await trainee.setFeatureAttributes({ feature_attributes }); + expect(set_feature_attributes).toEqual(feature_attributes); }); afterAll(() => { diff --git a/src/client/worker/HowsoWorkerClient.ts b/src/client/worker/HowsoWorkerClient.ts index e6c9648..cc45795 100644 --- a/src/client/worker/HowsoWorkerClient.ts +++ b/src/client/worker/HowsoWorkerClient.ts @@ -9,7 +9,6 @@ import type { Worker as NodeWorker } from "node:worker_threads"; import { v4 as uuid } from "uuid"; import { Session, Trainee } from "../../engine"; import type { BaseTrainee } from "../../types"; -import type * as schemas from "../../types/schemas"; import { AbstractBaseClient, type AbstractBaseClientOptions, @@ -141,13 +140,32 @@ export class HowsoWorkerClient extends AbstractBaseClient { return entities; } + /** + * Constructs Trainee object from it's Engine metadata. + * + * @param traineeId The Trainee identifier. + * @returns The Trainee object and its feature attributes. + */ + protected async getTraineeFromEngine(traineeId: string): Promise { + const { payload } = await this.execute>(traineeId, "get_metadata", {}); + if (!payload) { + throw new HowsoError(`Trainee "${traineeId}" not found.`, "not_found"); + } + return new Trainee(this, { + id: traineeId, + name: payload?.name, + persistence: payload?.persistence ?? "allow", + metadata: payload?.metadata, + }); + } + /** * Automatically resolve a Trainee and ensure it is loaded given an identifier. * * @param traineeId The Trainee identifier. * @returns The Trainee object. */ - protected async autoResolveTrainee(traineeId: string): Promise { + public async autoResolveTrainee(traineeId: string): Promise { if (traineeId == null) { throw new TypeError("A Trainee identifier is required."); } @@ -163,46 +181,13 @@ export class HowsoWorkerClient extends AbstractBaseClient { * Automatically persist Trainee object when appropriate based on persistence level. * @param traineeId The Trainee identifier. */ - protected async autoPersistTrainee(traineeId: string): Promise { + public async autoPersistTrainee(traineeId: string): Promise { const cached = this.cache.get(traineeId); if (cached?.trainee?.persistence === "always") { await this.persistTrainee(traineeId); } } - /** - * Constructs Trainee object from it's Engine metadata. - * - * @param traineeId The Trainee identifier. - * @returns The Trainee object and its feature attributes. - */ - protected async getTraineeFromEngine(traineeId: string): Promise { - const { payload } = await this.execute>(traineeId, "get_metadata", {}); - if (!payload) { - throw new HowsoError(`Trainee "${traineeId}" not found.`, "not_found"); - } - return new Trainee(this, { - id: traineeId, - name: payload?.name, - persistence: payload?.persistence ?? "allow", - metadata: payload?.metadata, - }); - } - - /** - * Include the active session in a request if not defined. - * @param request The Trainee request object. - * @returns The Trainee request object with a session. - */ - protected async includeSession>(request: T): Promise { - if (!request.session) { - // Include the active session - const session = await this.getActiveSession(); - return { ...request, session: session.id }; - } - return request; - } - /** * Setup client. * Prepares the file system and initializes the worker. @@ -500,39 +485,4 @@ export class HowsoWorkerClient extends AbstractBaseClient { return accumulator; }, []); } - - public async impute(traineeId: string, request: schemas.ImputeRequest) { - request = await this.includeSession(request); - return await super.impute(traineeId, request); - } - - public async clearImputedData(traineeId: string, request: schemas.ClearImputedDataRequest) { - request = await this.includeSession(request); - return await super.clearImputedData(traineeId, request); - } - - public async moveCases(traineeId: string, request: schemas.MoveCasesRequest) { - request = await this.includeSession(request); - return await super.moveCases(traineeId, request); - } - - public async editCases(traineeId: string, request: schemas.EditCasesRequest) { - request = await this.includeSession(request); - return await super.editCases(traineeId, request); - } - - public async addFeature(traineeId: string, request: schemas.AddFeatureRequest) { - request = await this.includeSession(request); - return await super.addFeature(traineeId, request); - } - - public async removeFeature(traineeId: string, request: schemas.RemoveFeatureRequest) { - request = await this.includeSession(request); - return await super.removeFeature(traineeId, request); - } - - public async train(traineeId: string, request: schemas.TrainRequest) { - request = await this.includeSession(request); - return await super.train(traineeId, request); - } } diff --git a/src/engine/Trainee.ts b/src/engine/Trainee.ts index b3cae79..625cf5d 100644 --- a/src/engine/Trainee.ts +++ b/src/engine/Trainee.ts @@ -4,8 +4,9 @@ */ import { AbstractBaseClient } from "../client/AbstractBaseClient"; import { batcher, BatchOptions } from "../client/utilities"; -import type { BaseTrainee, ClientBatchResponse } from "../types"; +import type { BaseTrainee, ClientBatchResponse, ClientResponse } from "../types"; import type * as schemas from "../types/schemas"; +import type * as shims from "../types/shims"; /** * The interface for interacting with a Trainee. Should not be instantiated directly. Instead create or request a @@ -93,7 +94,7 @@ export class Trainee implements BaseTrainee { async function* (this: Trainee, size: number) { let offset = 0; while (offset < cases.length) { - const response = await this.client.train(this.id, { + const response = await this.train({ ...rest, cases: cases.slice(offset, offset + size), }); @@ -115,6 +116,20 @@ export class Trainee implements BaseTrainee { return { payload: { num_trained, status, ablated_indices }, warnings }; } + /** + * Include the active session in a request if not defined. + * @param request The Trainee request object. + * @returns The Trainee request object with a session. + */ + protected async includeSession>(request: T): Promise { + if (!request.session) { + // Include the active session + const session = await this.client.getActiveSession(); + return { ...request, session: session.id }; + } + return request; + } + /** * Adds the specified feature on all cases for a trainee that match the specified condition. overwrites features that * If condition are not specified, adds feature for all cases and to the model. If condition is an empty assoc, will not modify feature metadata in the model. @@ -124,8 +139,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async addFeature(request: schemas.AddFeatureRequest) { - return this.client.addFeature(this.id, request); + public async addFeature(request: schemas.AddFeatureRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "add_feature", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -134,8 +153,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async analyze(request: schemas.AnalyzeRequest) { - return this.client.analyze(this.id, request); + public async analyze(request: schemas.AnalyzeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "analyze", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -144,8 +166,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async appendToSeriesStore(request: schemas.AppendToSeriesStoreRequest) { - return this.client.appendToSeriesStore(this.id, request); + public async appendToSeriesStore(request: schemas.AppendToSeriesStoreRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "append_to_series_store", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -154,8 +179,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async autoAnalyze() { - return this.client.autoAnalyze(this.id); + public async autoAnalyze(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "auto_analyze", {}); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -164,8 +192,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async clearImputedData(request: schemas.ClearImputedDataRequest) { - return this.client.clearImputedData(this.id, request); + public async clearImputedData(request: schemas.ClearImputedDataRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "clear_imputed_data", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -174,8 +206,13 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async copySubtrainee(request: schemas.CopySubtraineeRequest) { - return this.client.copySubtrainee(this.id, request); + public async copySubtrainee( + request: schemas.CopySubtraineeRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "copy_subtrainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -184,8 +221,13 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async createSubtrainee(request: schemas.CreateSubtraineeRequest) { - return this.client.createSubtrainee(this.id, request); + public async createSubtrainee( + request: schemas.CreateSubtraineeRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "create_subtrainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -194,8 +236,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async deleteSession(request: schemas.DeleteSessionRequest) { - return this.client.deleteSession(this.id, request); + public async deleteSession(request: schemas.DeleteSessionRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "delete_session", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -204,8 +249,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async deleteSubtrainee(request: schemas.DeleteSubtraineeRequest) { - return this.client.deleteSubtrainee(this.id, request); + public async deleteSubtrainee(request: schemas.DeleteSubtraineeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "delete_subtrainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -217,8 +265,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async editCases(request: schemas.EditCasesRequest) { - return this.client.editCases(this.id, request); + public async editCases(request: schemas.EditCasesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "edit_cases", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -228,8 +280,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async evaluate(request: schemas.EvaluateRequest) { - return this.client.evaluate(this.id, request); + public async evaluate(request: schemas.EvaluateRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "evaluate", request); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -238,8 +292,17 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async executeOnSubtrainee(request: schemas.ExecuteOnSubtraineeRequest) { - return this.client.executeOnSubtrainee(this.id, request); + public async executeOnSubtrainee( + request: schemas.ExecuteOnSubtraineeRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "execute_on_subtrainee", + request, + ); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -249,8 +312,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async exportTrainee(request: schemas.ExportTraineeRequest) { - return this.client.exportTrainee(this.id, request); + public async exportTrainee(request: schemas.ExportTraineeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "export_trainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -259,8 +325,14 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getAutoAblationParams() { - return this.client.getAutoAblationParams(this.id); + public async getAutoAblationParams(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_auto_ablation_params", + {}, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -271,8 +343,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getCases(request: schemas.GetCasesRequest) { - return this.client.getCases(this.id, request); + public async getCases(request: schemas.GetCasesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_cases", request); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -281,8 +355,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getDistances(request: schemas.GetDistancesRequest) { - return this.client.getDistances(this.id, request); + public async getDistances( + request: schemas.GetDistancesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_distances", request); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -293,8 +371,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getEntityPathById(request: schemas.GetEntityPathByIdRequest) { - return this.client.getEntityPathById(this.id, request); + public async getEntityPathById( + request: schemas.GetEntityPathByIdRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_entity_path_by_id", + request, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -303,8 +389,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getExportAttributes() { - return this.client.getExportAttributes(this.id); + public async getExportAttributes(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_export_attributes", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -313,8 +401,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getExtremeCases(request: schemas.GetExtremeCasesRequest) { - return this.client.getExtremeCases(this.id, request); + public async getExtremeCases( + request: schemas.GetExtremeCasesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_extreme_cases", request); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -323,8 +415,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getFeatureAttributes() { - return this.client.getFeatureAttributes(this.id); + public async getFeatureAttributes(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_feature_attributes", {}); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -333,8 +428,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getFeatureConviction(request: schemas.GetFeatureConvictionRequest) { - return this.client.getFeatureConviction(this.id, request); + public async getFeatureConviction( + request: schemas.GetFeatureConvictionRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_feature_conviction", + request, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -344,8 +447,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getHierarchy() { - return this.client.getHierarchy(this.id); + public async getHierarchy(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_hierarchy", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -355,8 +460,13 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getMarginalStats(request: schemas.GetMarginalStatsRequest) { - return this.client.getMarginalStats(this.id, request); + public async getMarginalStats( + request: schemas.GetMarginalStatsRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_marginal_stats", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -365,8 +475,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getMetadata() { - return this.client.getMetadata(this.id); + public async getMetadata(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_metadata", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -375,8 +487,14 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getNumTrainingCases() { - return this.client.getNumTrainingCases(this.id); + public async getNumTrainingCases(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_num_training_cases", + {}, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -386,8 +504,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getPairwiseDistances(request: schemas.GetPairwiseDistancesRequest) { - return this.client.getPairwiseDistances(this.id, request); + public async getPairwiseDistances( + request: schemas.GetPairwiseDistancesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_pairwise_distances", + request, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -397,8 +523,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getParams(request: schemas.GetParamsRequest) { - return this.client.getParams(this.id, request); + public async getParams(request: schemas.GetParamsRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_params", request); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -407,8 +535,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getRevision() { - return this.client.getRevision(this.id); + public async getRevision(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_revision", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -417,8 +547,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getSessions(request: schemas.GetSessionsRequest) { - return this.client.getSessions(this.id, request); + public async getSessions(request: schemas.GetSessionsRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_sessions", request); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -428,8 +560,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getSessionIndices(request: schemas.GetSessionIndicesRequest) { - return this.client.getSessionIndices(this.id, request); + public async getSessionIndices( + request: schemas.GetSessionIndicesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_session_indices", + request, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -438,8 +578,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getSessionMetadata(request: schemas.GetSessionMetadataRequest) { - return this.client.getSessionMetadata(this.id, request); + public async getSessionMetadata( + request: schemas.GetSessionMetadataRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_session_metadata", + request, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -449,8 +597,16 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getSessionTrainingIndices(request: schemas.GetSessionTrainingIndicesRequest) { - return this.client.getSessionTrainingIndices(this.id, request); + public async getSessionTrainingIndices( + request: schemas.GetSessionTrainingIndicesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "get_session_training_indices", + request, + ); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -459,8 +615,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getSubstituteFeatureValues() { - return this.client.getSubstituteFeatureValues(this.id); + public async getSubstituteFeatureValues(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_substitute_feature_values", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -469,8 +627,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getTraineeId() { - return this.client.getTraineeId(this.id); + public async getTraineeId(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_trainee_id", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -479,8 +639,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async getTraineeVersion() { - return this.client.getTraineeVersion(this.id); + public async getTraineeVersion(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "get_trainee_version", {}); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -489,8 +651,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async impute(request: schemas.ImputeRequest) { - return this.client.impute(this.id, request); + public async impute(request: schemas.ImputeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "impute", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -503,8 +669,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async loadSubtrainee(request: schemas.LoadSubtraineeRequest) { - return this.client.loadSubtrainee(this.id, request); + public async loadSubtrainee(request: schemas.LoadSubtraineeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "load_subtrainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -513,8 +682,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async moveCases(request: schemas.MoveCasesRequest) { - return this.client.moveCases(this.id, request); + public async moveCases(request: schemas.MoveCasesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "move_cases", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -523,8 +696,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async react(request: schemas.ReactRequest) { - return this.client.react(this.id, request); + public async react(request: schemas.ReactRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "react", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -534,8 +710,13 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async reactAggregate(request: schemas.ReactAggregateRequest) { - return this.client.reactAggregate(this.id, request); + public async reactAggregate( + request: schemas.ReactAggregateRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "react_aggregate", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -551,8 +732,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async reactGroup(request: schemas.ReactGroupRequest) { - return this.client.reactGroup(this.id, request); + public async reactGroup(request: schemas.ReactGroupRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "react_group", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -561,8 +745,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async reactIntoFeatures(request: schemas.ReactIntoFeaturesRequest) { - return this.client.reactIntoFeatures(this.id, request); + public async reactIntoFeatures(request: schemas.ReactIntoFeaturesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "react_into_features", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -573,8 +760,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async reactSeries(request: schemas.ReactSeriesRequest) { - return this.client.reactSeries(this.id, request); + public async reactSeries(request: schemas.ReactSeriesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "react_series", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -584,8 +774,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async reduceData(request: schemas.ReduceDataRequest) { - return this.client.reduceData(this.id, request); + public async reduceData(request: schemas.ReduceDataRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "reduce_data", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -594,8 +787,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async removeCases(request: schemas.RemoveCasesRequest) { - return this.client.removeCases(this.id, request); + public async removeCases(request: schemas.RemoveCasesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "remove_cases", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -606,8 +802,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async removeFeature(request: schemas.RemoveFeatureRequest) { - return this.client.removeFeature(this.id, request); + public async removeFeature(request: schemas.RemoveFeatureRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "remove_feature", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -616,8 +816,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async removeSeriesStore(request: schemas.RemoveSeriesStoreRequest) { - return this.client.removeSeriesStore(this.id, request); + public async removeSeriesStore(request: schemas.RemoveSeriesStoreRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "remove_series_store", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -626,8 +829,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async renameSubtrainee(request: schemas.RenameSubtraineeRequest) { - return this.client.renameSubtrainee(this.id, request); + public async renameSubtrainee(request: schemas.RenameSubtraineeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "rename_subtrainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -636,8 +842,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async resetParams() { - return this.client.resetParams(this.id); + public async resetParams(): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "reset_params", {}); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -646,8 +855,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async saveSubtrainee(request: schemas.SaveSubtraineeRequest) { - return this.client.saveSubtrainee(this.id, request); + public async saveSubtrainee(request: schemas.SaveSubtraineeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "save_subtrainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -656,8 +868,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setAutoAblationParams(request: schemas.SetAutoAblationParamsRequest) { - return this.client.setAutoAblationParams(this.id, request); + public async setAutoAblationParams(request: schemas.SetAutoAblationParamsRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_auto_ablation_params", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -666,8 +881,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setAutoAnalyzeParams(request: schemas.SetAutoAnalyzeParamsRequest) { - return this.client.setAutoAnalyzeParams(this.id, request); + public async setAutoAnalyzeParams(request: schemas.SetAutoAnalyzeParamsRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_auto_analyze_params", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -676,8 +894,17 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setFeatureAttributes(request: schemas.SetFeatureAttributesRequest) { - return this.client.setFeatureAttributes(this.id, request); + public async setFeatureAttributes( + request: schemas.SetFeatureAttributesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute( + this.id, + "set_feature_attributes", + request, + ); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -687,8 +914,13 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setInfluenceWeightThreshold(request: schemas.SetInfluenceWeightThresholdRequest) { - return this.client.setInfluenceWeightThreshold(this.id, request); + public async setInfluenceWeightThreshold( + request: schemas.SetInfluenceWeightThresholdRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_influence_weight_threshold", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -697,8 +929,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setMetadata(request: schemas.SetMetadataRequest) { - return this.client.setMetadata(this.id, request); + public async setMetadata(request: schemas.SetMetadataRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_metadata", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -707,8 +942,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setParams(request: schemas.SetParamsRequest) { - return this.client.setParams(this.id, request); + public async setParams(request: schemas.SetParamsRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_params", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -717,8 +955,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setParentId(request: schemas.SetParentIdRequest) { - return this.client.setParentId(this.id, request); + public async setParentId(request: schemas.SetParentIdRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_parent_id", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -727,8 +968,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setRandomSeed(request: schemas.SetRandomSeedRequest) { - return this.client.setRandomSeed(this.id, request); + public async setRandomSeed(request: schemas.SetRandomSeedRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_random_seed", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -737,8 +981,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setSessionMetadata(request: schemas.SetSessionMetadataRequest) { - return this.client.setSessionMetadata(this.id, request); + public async setSessionMetadata(request: schemas.SetSessionMetadataRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_session_metadata", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -747,8 +994,13 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setSubstituteFeatureValues(request: schemas.SetSubstituteFeatureValuesRequest) { - return this.client.setSubstituteFeatureValues(this.id, request); + public async setSubstituteFeatureValues( + request: schemas.SetSubstituteFeatureValuesRequest, + ): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_substitute_feature_values", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -757,8 +1009,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async setTraineeId(request: schemas.SetTraineeIdRequest) { - return this.client.setTraineeId(this.id, request); + public async setTraineeId(request: schemas.SetTraineeIdRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "set_trainee_id", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -776,8 +1031,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async singleReact(request: schemas.SingleReactRequest) { - return this.client.singleReact(this.id, request); + public async singleReact(request: schemas.SingleReactRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "single_react", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -788,8 +1046,11 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async singleReactSeries(request: schemas.SingleReactSeriesRequest) { - return this.client.singleReactSeries(this.id, request); + public async singleReactSeries(request: schemas.SingleReactSeriesRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "single_react_series", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -798,8 +1059,12 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async train(request: schemas.TrainRequest) { - return this.client.train(this.id, request); + public async train(request: schemas.TrainRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + request = await this.includeSession(request); + const response = await this.client.execute(this.id, "train", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } /** @@ -808,7 +1073,10 @@ export class Trainee implements BaseTrainee { * @param request The operation parameters. * @returns The response of the operation, including any warnings. */ - public async upgradeTrainee(request: schemas.UpgradeTraineeRequest) { - return this.client.upgradeTrainee(this.id, request); + public async upgradeTrainee(request: schemas.UpgradeTraineeRequest): Promise> { + await this.client.autoResolveTrainee(this.id); + const response = await this.client.execute(this.id, "upgrade_trainee", request); + this.client.autoPersistTrainee(this.id); + return { payload: response.payload, warnings: response.warnings }; } }