sliceoptim package

Submodules

sliceoptim.core module

Core module of sliceoptim Contains main classes for definition of filaments, printers and experiments.

class sliceoptim.core.Experiment(name: str, sample_file: str, is_first_layer: bool, spacing: float, printer: sliceoptim.core.Printer, filament: sliceoptim.core.Filament, params_space: sliceoptim.core.ParametersSpace, output_file: str, sample_default_params={}, config_file=None)[source]

Bases: object

Class to handle experiments on slicing parameters, generate test batches and compute optimal results.

compute_and_update_samples_costs()[source]

Compute samples’ cost from speed and quality results and update costs in samples objects.

Raises
compute_costs_from_results_df(results_df: pandas.core.frame.DataFrame) pandas.core.frame.DataFrame[source]
Compute “costs” to be minimized by the optimizer.

Costs are computed to prioritize quality over speed (ie, reduce speed only if print quality impact is limited). Fist costs are computed using a linear interpolation technique, and next ones using a spline interpolation.

Parameters

results_df (pd.DataFrame) – DataFrame containing all samples results.

Returns

Input DataFrame populated with samples’ cost results.

Return type

pd.DataFrame

create_new_sample_grid(n_samples: int) sliceoptim.samples.SampleGrid[source]
Creates a new sample grid (test samples to print)

given a required number of samples (not greater than the maximum bed capacity).

Parameters

n_samples (int) – number of samples for the new samples grid.

Raises

ValueError – The number of samples can’t be above a limit (computed from 3d printer bed space).

Returns

The new generated sample grid.

Return type

SampleGrid

property end_gcode: str
estim_best_config() Tuple[dict, numpy.array][source]

Estimate best values for the set of optimized parameters, given actual test samples results.

Raises

ExperimentError – At least a full grid of sample results must be registered to predict the best configuration.

Returns

Two elements tuple with a Dictionary of best parameters values and related uncertainty of

minimum cost.

Return type

tuple

export_csv(file_path: str) None[source]

Export designs and current state of results to a csv file.

Parameters

file_path (str) – file_path

Returns

Return type

None

from_dataframe(samples_df: pandas.core.frame.DataFrame, overwrite_samples: bool = True, clip_to_space: bool = True, infer_space: bool = True)[source]
Import experiment dataframe into current Experiment object.

Existing samples can be overwritten.

Parameters
  • file_path (str) – File path of csv file

  • overwrite_samples (True) – overwrite existing samples with csv samples data.

  • clip_to_space (True) – clip parameters values to space while importing.

  • infer_space (True) – Try to infer the parametric space from designs (experimental).

generate_designs(n_samples: int) pandas.core.frame.DataFrame[source]

Generate a DataFrame of new design parameters.

Parameters

n_samples (int) – Number of designs to generate.

Raises

ExperimentError – Can’t generate more designs than the printer’s bed can hold.

Returns

DataFrame of designs parameters’ values.

Return type

pd.DataFrame

get_designs() pandas.core.frame.DataFrame[source]

Returns generated test sample designs as a DataFrame.

Returns

Generated test sample designs as a DataFrame.

Return type

DataFrame

get_samples_list() List[sliceoptim.samples.Sample][source]

Return all test samples generated so far as a list.

Returns

list of generated test samples.

Return type

List[Sample]

get_samples_results_df() pandas.core.frame.DataFrame[source]
Return a DataFrame of results for each generated test sample

(print time, quality, and “cost” computed by the objective function).

Returns

DataFrame of all samples results.

Return type

pd.DataFrame

import_csv(file_path: str, overwrite_samples: bool = True, clip_to_space: bool = True, infer_space: bool = True) None[source]
Import csv experiment data to current Experiment object.

Existing samples can be overwritten.

Parameters
  • file_path (str) – File path of csv file

  • overwrite_samples (True) – overwrite existing samples with csv samples data.

  • clip_to_space (True) – clip parameters values to space while importing.

  • infer_space (True) – Try to infer the parametric space from designs (experimental).

property init_gcode: str
register_costs_to_optimizer()[source]

Register new computed samples costs to optimizer.

Raises

ExperimentError – All samples costs must be computed before registering them to the optimizer.

to_dataframe() pandas.core.frame.DataFrame[source]

Generate a pandas dataframe from designs and results.

Returns

dataframe descripting all designs.

Return type

pd.DataFrame

write_gcode_for_last_sample_grid()[source]

Write to output file the gcode for the last test sample grid.

write_validation_sample(params: dict, output_file: str) None[source]

Write gcode for validation sample.

Parameters
  • params (dict) – Sample parameters for validation sample.

  • output_file (str) – Path for validation sample gcode.

exception sliceoptim.core.ExperimentError[source]

Bases: Exception

class sliceoptim.core.Filament(name: str, material: str, extrusion_temp_range: List[float], bed_temp_range: List[float], diameter: float)[source]

Bases: object

Filament class to handle filament parameters.

class sliceoptim.core.ParametersSpace[source]

Bases: skopt.space.space.Space

Class for parametric space expored by Experiment.

add_param(name: str, low: float, high: float)[source]

Adds a new parameter.

Parameters
  • name (str) – Parameter name.

  • low (float) – Lower bound for parameter values.

  • high (float) – Upper bound for parameter values.

Raises
delete_param(name: str)[source]

Delete a parameter.

Parameters

name (str) – Parameter name.

Raises

KeyError – Error if parameter not existing.

class sliceoptim.core.Printer(name: str, bed_size: List[float], nozzle_diameter: float, max_speed: float, min_speed: float)[source]

Bases: object

Printer class to handle printer parameters.

sliceoptim.io module

sliceoptim module to integrate Printer, Filament and Experiment objects in a database.

class sliceoptim.io.Database(folder_path: str)[source]

Bases: object

add_experiment(experiment: sliceoptim.core.Experiment)[source]

Adds an Experiment object to database.

Parameters

experiment (core.Experiment) – Experiment object.

add_filament(filament: sliceoptim.core.Filament)[source]

Adds a filament object to database.

Parameters

filament (core.Filament) – Filament object to append.

add_printer(printer: sliceoptim.core.Printer)[source]

Adds a Printer object to the database.

Parameters

printer (core.Printer) – Printer object to add.

delete_experiment(name: str)[source]

Delete a saved Experiment.

Parameters

name (str) – Experiment name.

delete_filament(name: str)[source]

Delete a Filament in database.

Parameters

name (str) – Name of Filament to delete.

delete_printer(name: str)[source]

Delete a Printer in database.

Parameters

name (str) – Name of Printer to delete.

get_experiment(name: str) sliceoptim.core.Experiment[source]

Get a saved Experiment object.

Parameters

name (str) – Experiment name.

Returns

saved Experiment object.

Return type

core.Experiment

get_experiment_filepath(name: str) str[source]

Get the path of a given saved Experiment.

Parameters

name (str) – Experiment name.

Raises

ValueError – Error if Experiment not existing.

Returns

Path of Experiment serialized object.

Return type

str

get_experiment_names() list[source]

Returns a list of saved Experiment names.

Returns

List of saved Experiments names.

Return type

list

get_filament(name: str) sliceoptim.core.Filament[source]

Gets a Filament object from database with its name.

Parameters

name (str) – Filament name.

Raises

ValueError – Error if Filament name does not exist.

Returns

Stored Filament object.

Return type

core.Filament

get_filament_names() list[source]

Returns a list of stored Filament names.

Returns

List of Filament names.

Return type

list

get_printer(name: str) sliceoptim.core.Printer[source]

Get a Printer object for database.

Parameters

name (str) – Name of Printer to retreive.

Raises

ValueError – Error if Printer name not in database.

Returns

Printer object.

Return type

core.Printer

get_printer_names() list[source]

Returns a list of Printer names stored in database.

Returns

List of Printer names.

Return type

list

update_experiment(experiment: sliceoptim.core.Experiment)[source]

Overwrites a saved experiment.

Parameters

experiment (core.Experiment) – Experiment object.

sliceoptim.samples module

sliceoptim module for interaction with Slic3r software and generation of samples batches.

class sliceoptim.samples.Sample(input_file: str, is_first_layer: bool, printer: Printer, filament: Filament, config_file: pathlib.Path = None, params: dict = None)[source]

Bases: object

Class to define samples to test slicing on. Connects with Slic3r software with cli.

erase_start_end_gcode() None[source]

Erases starting en ending GCODE in Slic3r parameters.

property gcode: str

Getter for generated GCODE.

Returns

generated GCODE as a string.

Return type

str

get_info() dict[source]

Get Sample information from Slic3r parsing.

Raises

ValueError – Error from Slic3r call.

Returns

Sample information from Slic3r parsing.

Example of returned info: {‘filename’: ‘calicat.stl’, ‘size_x’: 28.535534, ‘size_y’: 28.5, ‘size_z’: 35.0, ‘min_x’: 0.464466, ‘min_y’: 0.0, ‘min_z’: 0.0, ‘max_x’: 29.0, ‘max_y’: 28.5, ‘max_z’: 35.0, ‘number_of_facets’: 876.0, ‘manifold’: ‘yes’, ‘number_of_parts’: 1.0, ‘volume’: 12501.378906}

Return type

dict

get_param(name: str) str[source]

Get set value for a SLic3r parameter.

Parameters

name (str) – Name of queried parameter.

Returns

Parameter value.

Return type

str

get_param_from_gcode(gcode: str, param_name: str) float[source]

Get a parameter from a GCODE file sliced with Slic3r.

Parameters
  • gcode (str) – GCODE file to parse, as a string.

  • param_name (str) – name of the parameter we look for.

Raises

ValueError – Raises an error if the parameter name is not fond in GCODE.

Returns

Returns the parameter value, as a float if possible.

Return type

float

property output_file: str

Getter for output file.

Returns

Returns the path for the test Sample output file.

Return type

str

property print_time: float

Getter for Sample print time estimate.

Returns

Returns estimated print time.

Return type

[float]

reset_params() None[source]

Reset all Slic3r parameters.

set_first_layer_presets() None[source]

Set Slic3r parameters for first layer case.

set_main_presets(printer: Printer, filament: Filament) None[source]

Set Slic3r presets derived from pinter and filament parameters.

Parameters
  • printer (Printer) – Printer object to derive presets from.

  • filament (Filament) – Filament object to derive presets from.

set_param(name: str, value) None[source]

Set a Slic3r parameter for the current Sample.

Parameters
  • name (str) – Name of the corresponding Slic3r parameter.

  • value ([type]) – Desired value for the parameter.

set_standard_print_presets() None[source]

Set Slic3r presets for a standard sample print.

write_gcode(output_file=None) None[source]

Write GCODE to given / default output file

Parameters

output_file (str, optional) – File path to write test sample GCODE. Defaults to None.

Raises

ValueError – Retrived error from Slic3r call.

class sliceoptim.samples.SampleGrid(sample_input_file: str, is_first_layer: bool, printer: Printer, filament: Filament, designs: pandas.core.frame.DataFrame, output_path: str, spacing=0.0, sample_default_params={}, config_file=None)[source]

Bases: object

Class for generation of test sample batches.

check_designs_input(designs: pandas.core.frame.DataFrame) bool[source]

Utility method to check if designs are properly formatted.

Parameters

designs (pd.DataFrame) – designs DataFrame to test.

Raises
Returns

True if designs DataFrame is correct.

Return type

bool

compute_first_sample_coords(grid_shape) dict[source]

Computes and returns the coordinates for the first Sample.

Parameters

grid_shape (dict) – SampleGrid shape data as returned by “grid_shape” method.

Returns

“x” and “y” coordinates

Return type

dict

property cost_list: list

Returns quality of samples as a list.

Returns

quality values of samples.

Return type

list

property designs: pandas.core.frame.DataFrame

Getter for Sample designs DataFrame.

Returns

DataFrame of design parameters for samples

Each row corresponds to a Sample, and column to a parameter.

Return type

pd.DataFrame

gen_sample_coordinates() pandas.core.frame.DataFrame[source]

Generate all sample coordinates and return them as a DataFrame.

Returns

DataFrame of coordinates with columns “x” and “y”.

Return type

pd.DataFrame

gen_test_sample(sample_params: dict = {}) sliceoptim.samples.Sample[source]

Generates and returns a new test Sample (not added to the SampleGrid).

Parameters

sample_params (dict, optional) – Parameters dict for the new sample. Defaults to {}.

Returns

Generated Sample object.

Return type

Sample

grid_shape() dict[source]

Compute and returns SampleGrid shape data.

Returns

{“max_samples_count”: maximum number of Samples,

”n_cols”: number of columns (X axis), “n_rows”: number of rows (Y axis), “size_x”: size of X axis in mm, “size_y”: size of Y axis in mm}

Return type

dict

inter_gcode(temperature, bed_temperature, x, y)[source]

Generates intermediate GCODE to be inserted between two Samples.

Parameters
  • temperature (float) – Temperature of the next Sample.

  • bed_temperature (float) – Bed temperature of the next Sample.

  • x (float) – X coordinate of the next Sample.

  • y (float) – Y coordinate of the next Sample.

Returns

Generated intermediate GCODE.

Return type

str

property quality_list: list

Returns quality of samples as a list.

Returns

quality values of samples.

Return type

list

property samples_list: list

List of Samples in SampleGrid.

Returns

All samples as a list.

Return type

list

write_gcode(output_path=None)[source]

Writes GCODE to the (predefined) output path.

Parameters

output_path (str, optional) – Output file path to write GCODE on. Defaults to None. If set to None, writes to default output path.

Module contents