Learning Module#

The learning module contains machine learning components for magnetic field estimation.

Note

API documentation is being generated from source code docstrings. Some modules may not be fully documented due to import dependencies.

Classes#

FieldGPRegressor#

class EMFieldML.Learning.Learning.FieldGPRegressor#

Bases: object

Gaussian Process regression for electromagnetic field prediction.

This class handles the complete ML pipeline for predicting electromagnetic field magnitudes using GPyTorch. It includes data loading, model training, and prediction capabilities for ferrite shield optimization.

In Gaussian Process Regression, we first create a prior distribution using a kernel function. We assume that the prediction follows this distribution. Then, the parameters of the kernel function are optimized to fit the training data.

Its key characteristic is the ability to achieve accurate learning with a small amount of data and a short training time.

static make_x_train_data(input_path_train_list: Path, input_path_circle: str | None = None) numpy.ndarray#

Load and prepare training input data from FEKO simulation files.

Parameters:
  • input_path_train_list – Path to file containing list of training data indices

  • input_path_circle – Template path for circle move data files

Returns:

numpy array of shape (n_samples, config.dimension_x) containing training features

static make_y_train_data(input_path_y_data: str, postprocessing_value: float = 1.0)#

Create Y training data with postprocessing.

This is the power transformation. This transformation helps make skewed data more closely resemble a normal distribution.

Note

This transformation corresponds to Figure 7 of the paper.

static prepare_model(input_path_y_data: Path, x_train_data: numpy.ndarray) tuple#

Prepare machine learning model for training.

The prepared model is RBF kernel-based Gaussian Process Regression.

static calculate_deviation(input_path_circle: Path, model, likelihood: gpytorch.likelihoods.GaussianLikelihood) float#

Calculate deviation using Gaussian Process model.

The predictive variance, which quantifies the uncertainty of the prediction, is calculated simultaneously with the prediction itself in Gaussian Process Regression. This is done according to Equation (9) of the paper.

static learning_magneticfield(input_path_y_data: Path, output_path: Path, x_train_data: numpy.ndarray, lr_record: float = 0.02, iter_record: int = 1500, inter_record: int = 600, small_record: float = 5.0, initial_noise: float = 0.7071067811865475)#

Train magnetic field learning model.

The training algorithm is based on maximizing the likelihood method.

Parameters:
  • input_path_y_data – Path to training target data

  • output_path – Path to save the trained model

  • x_train_data – Training input data

  • lr_record – Learning rate for optimizer

  • iter_record – Number of training iterations

  • inter_record – Interval for learning rate adjustment

  • small_record – Factor for learning rate reduction

  • initial_noise – Initial noise level for Gaussian likelihood

TargetDataBuilder#

class EMFieldML.Learning.YDataMaker.TargetDataBuilder#

Bases: object

Class for creating Y data for machine learning models.

The ground truth data is created for machine learning. The ground truth data here consists of the magnetic field magnitude, vector direction, and efficiency at each point.

static bisect(point_data_list: list[int], point: float) int#

Search for the position of the value within the list.

Parameters:
  • point_data_list – List of data points to search through

  • point – The value to search for in the vicinity

Returns:

Index of the closest lower value in the list

Return type:

int

static calculate_magnitude(Hx: float, Hx_theta: float, Hy: float, Hy_theta: float, Hz: float, Hz_theta: float) float#

Calculate magnitude from field components.

The magnetic field calculated by the FEKO simulator consists of a magnitude and a phase for each of the x, y, and z components. The magnetic field is therefore represented by an equation similar to the one below:

MagneticField = Hx * cos(theta + Hx_theta) + Hy * cos(theta + Hy_theta) + Hz * cos(theta + Hz_theta)

From this, the maximum value of the magnetic field is calculated over the theta.

Parameters:
  • Hx – Magnitude of the x-component of the magnetic field

  • Hx_theta – Phase of the x-component of the magnetic field (degrees)

  • Hy – Magnitude of the y-component of the magnetic field

  • Hy_theta – Phase of the y-component of the magnetic field (degrees)

  • Hz – Magnitude of the z-component of the magnetic field

  • Hz_theta – Phase of the z-component of the magnetic field (degrees)

Returns:

Maximum magnitude of the magnetic field

Return type:

float

static write_data(data: list[list[float]], input_path_raw_data: Path, input_path_prediction_point: Path, prediction_point_list: list[int]) list[list[float]]#

Write data to files with prediction point processing.

FEKO can calculate the magnetic field only at fixed points. To apply this to points between those fixed points, it’s possible to use trilinear interpolation, a 3D linear interpolation method. It’s based on a linear interpolation of the distances from each vertex of a cube.

Parameters:
  • data – List to store the calculated data

  • input_path_raw_data – Path to the FEKO output file

  • input_path_prediction_point – Path to the file containing prediction points

  • prediction_point_list – List of indices for prediction points to process

Returns:

Updated data list with calculated values

Return type:

list[list[float]]

static make_new_ydatafile(x_train_list: list[int], output_dir: Path, input_path_raw_data_dir: Path = EMFieldML.config.paths.CIRCLE_FEKO_RAW_DIR, input_path_prediction_point_dir: Path = EMFieldML.config.paths.CIRCLE_PREDICTION_POINT_DIR, input_path_prediction_point_file: str = EMFieldML.config.template.circle_prediction_point_move, output_file: str = EMFieldML.config.template.y_data_magnitude, prediction_point_list: list = None, n_prediction_points: int = None) None#

Create new Y data file for training.

The data generation method employs a linear interpolation of the distances from each vertex of a cube.

Parameters:
  • x_train_list – List of training data indices

  • output_dir – Directory to save the output file

  • input_path_raw_data_dir – Directory containing raw FEKO data files

  • input_path_prediction_point_dir – Directory containing prediction point files

  • input_path_prediction_point_file – File name containing prediction point positions

  • output_file – output file names

  • prediction_point_list – List of indices for prediction points to process

  • n_prediction_points – Total number of prediction points

Returns:

None

static calculate_vector(Hx, Hx_theta, Hy, Hy_theta, Hz, Hz_theta)#

Calculate vector components from field data.

The magnetic field calculated by the FEKO simulator consists of a magnitude and a phase for each of the x, y, and z components. The magnetic field is therefore represented by an equation similar to the one below:

MagneticField = Hx * cos(theta + Hx_theta) + Hy * cos(theta + Hy_theta) + Hz * cos(theta + Hz_theta)

The vector’s direction is calculated based on this equation. However, theta can be any arbitrary value, and in this case, the theta = -140 has been selected as the theta at which the vector at the center of the coil becomes maximal.

The vector is converted into a polar coordinate system.

Parameters:
  • Hx – Magnitude of the x-component of the magnetic field

  • Hx_theta – Phase of the x-component of the magnetic field (degrees)

  • Hy – Magnitude of the y-component of the magnetic field

  • Hy_theta – Phase of the y-component of the magnetic field (degrees)

  • Hz – Magnitude of the z-component of the magnetic field

  • Hz_theta – Phase of the z-component of the magnetic field (degrees)

Returns:

(theta, phi) angles in radians representing the vector direction

Return type:

tuple

static write_data_vector(data_theta: list[list[float]], data_phi: list[list[float]], input_path_raw_data: Path, input_path_prediction_point: Path, prediction_point_list: list[int])#

Write vector data to files. Three-dimensional linear interpolation from each vertex of a cube is also applied to the vectors.

Parameters:
  • data_theta – List to store theta components of the vector

  • data_phi – List to store phi components of the vector

  • input_path_raw_data – Path to the FEKO output file

  • input_path_prediction_point – Path to the file containing prediction point positions

  • prediction_point_list – List of indices for prediction points to process

Returns:

Updated data_theta and data_phi lists with calculated values

Return type:

tuple

static calculateTrilinearWeight(prePoint: list[float], nowPoint: list[float], basedPointA: list[float], basedPointB: list[float]) float#

Calculate the trilinear weight for the given point.

static make_new_ydatafile_vector(x_train_list: list[int], output_dir: Path, input_path_raw_data_dir=EMFieldML.config.paths.CIRCLE_FEKO_RAW_DIR, input_path_prediction_point_dir=EMFieldML.config.paths.CIRCLE_PREDICTION_POINT_DIR, input_path_prediction_point_file=EMFieldML.config.template.circle_prediction_point_move, output_file_theta: str = EMFieldML.config.template.y_data_vector_theta, output_file_phi: str = EMFieldML.config.template.y_data_vector_phi, prediction_point_list=None, n_prediction_points: int = None)#

Create new Y data file for vector training. Three-dimensional linear interpolation from each vertex of a cube is also applied to the vectors.

Parameters:
  • x_train_list – List of training data indices

  • output_dir – Directory to save the output file

  • input_path_raw_data_dir – Directory containing raw FEKO data files

  • input_path_prediction_point_dir – Directory containing prediction point files

  • input_path_prediction_point_file – File name containing prediction point positions

  • output_file_theta – output file names for theta component

  • output_file_phi – output file names for phi component

  • prediction_point_list – List of indices for prediction points to process

  • n_prediction_points – Total number of prediction points

Returns:

None

static write_data_efficiency(data: list[list[float]], input_path_raw_data: Path) list[float]#

Calculate the efficiency from the received out file and store it in the data.

static make_new_ydatafile_efficiency(x_train_list: list[int], output_dir: Path, input_path_raw_data_dir=EMFieldML.config.paths.CIRCLE_FEKO_RAW_DIR)#

Execute the write_data_efficiency function for the out file corresponding to the given X_train, store the results in data, and collectively output them to a file.

Parameters:
  • x_train_list – List of training data indices

  • output_dir – Directory to save the output file

  • input_path_raw_data_dir – Directory containing raw FEKO data files

Returns:

None