This guide demonstrates how to apply a trained model to generate predictions. You can use either the Python API for programmatic control or the command-line tools for a code-free approach.
Method 1: Python API
This method offers model application process within a Python script.
importmed# --- Configuration ---# Path to your data repositoryrep_path=''# Path to your trained MedModel filemodel_file=''# Path to your samples file. Alternatively, load from a DataFrame# using: samples.from_df(dataframe_object)samples_file=''# --- Initialization ---print("Initializing repository...")rep=med.PidRepository()# Initialize for the first processing steprep.init(rep_path)# --- Load Model ---print("Loading model...")model=med.Model()model.read_from_file(model_file)model.fit_for_repository(rep)# Get the list of signals required by the modelsignalNamesSet=model.get_required_signal_names()# --- Load Samples ---print("Loading samples...")samples=med.Samples()samples.read_from_file(samples_file)# Get the IDs from the samples to fetch relevant dataids=samples.get_ids()# --- Read Data ---print("Reading data from repository...")# Read only the necessary data for the specified IDs and signalsrep.read_all(rep_path,ids,signalNamesSet)# --- Apply Model ---print("Applying model to samples...")model.apply(rep,samples)# --- Save Results ---# The 'samples' object now contains the model scores# Convert to a DataFrame for further analysisdf=samples.to_df()# Save the results to a CSV filedf.to_csv('output_file.csv',index=False)# Or save as a samples filesamples.write_to_file('output_samples.tsv')# The feature matrix is available via: model.features.to_df()print("Inference complete. Results saved.")
[!NOTE]
To create feature matrix of the model, apply the model and in the end use model.features.to_df() to extract the feature matrix into dataframe.
Method 2: Command-Line Tools
If you prefer to work from the command line, MES offers tools to apply models without writing any Python code.