Output Module¶
This section documents the output components of the Nextmv Python SDK.
output
¶
Module for handling output destinations and data.
This module provides classes and functions for handling the output of decision problems, including formatting, serialization, and writing to various destinations.
| CLASS | DESCRIPTION |
|---|---|
SolutionFile |
Represents a solution to be written as a file. |
VisualSchema |
Enumeration of supported visualization schemas. |
Visual |
Visual schema definition for an asset. |
Asset |
Represents downloadable information that is part of the |
Output |
A class for representing the output of a decision problem. |
OutputWriter |
Base class for writing outputs to different destinations. |
LocalOutputWriter |
Class for writing outputs to local files or stdout. |
| FUNCTION | DESCRIPTION |
|---|---|
write |
Write the output to the specified destination. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
ASSETS_KEY |
Assets key constant used for identifying assets in the run output.
TYPE:
|
METRICS_KEY |
Metrics key constant used for identifying metrics in the run output.
TYPE:
|
SOLUTIONS_KEY |
Solutions key constant used for identifying solutions in the run output.
TYPE:
|
OUTPUTS_KEY |
Outputs key constant used for identifying outputs in the run output.
TYPE:
|
ASSETS_KEY
module-attribute
¶
Assets key constant used for identifying assets in the run output.
Asset
¶
Bases: BaseModel
Represents downloadable information that is part of the Output.
You can import the Asset class directly from nextmv:
An asset contains content that can be serialized to JSON and optionally includes visual information for rendering in the Nextmv Console.
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the asset.
TYPE:
|
|
Content of the asset. The type must be serializable to JSON.
TYPE:
|
|
Content type of the asset. Only "json" is currently supported. Default is "json".
TYPE:
|
|
Description of the asset. Default is None.
TYPE:
|
|
Visual schema of the asset. Default is None.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the content_type is not "json". |
Examples:
>>> from nextmv.output import Asset, Visual, VisualSchema
>>> visual = Visual(visual_schema=VisualSchema.CHARTJS, label="Solution Progress")
>>> asset = Asset(
... name="optimization_progress",
... content={"iterations": [1, 2, 3], "values": [10, 8, 7]},
... description="Optimization progress over iterations",
... visual=visual
... )
>>> asset.name
'optimization_progress'
-
nextmv
✏️ Modeling
Reference
Output Module
outputOutputassets
-
nextmv
✏️ Modeling
Reference
Output Module
output
content
class-attribute
instance-attribute
¶
Content of the asset. The type must be serializable to JSON. Can be empty when fetching the asset metadata only (e.g.: via the asset list endpoint).
content_type
class-attribute
instance-attribute
¶
Content type of the asset. Only json is allowed
description
class-attribute
instance-attribute
¶
Description of the asset.
id
class-attribute
instance-attribute
¶
The ID of the asset. This ID will be populated by the Nextmv platform and can be used to download the asset later.
DataPoint
¶
Bases: BaseModel
Warning
DataPoint is deprecated, use metrics on the Output directly.
A data point representing a 2D coordinate.
You can import the DataPoint class directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
X coordinate of the data point.
TYPE:
|
|
Y coordinate of the data point.
TYPE:
|
Examples:
>>> from nextmv.output import DataPoint
>>> point = DataPoint(x=3.5, y=4.2)
>>> point.x
3.5
>>> point.to_dict()
{'x': 3.5, 'y': 4.2}
-
nextmv
✏️ Modeling
Reference
Output Module
outputSeriesdata_points
-
nextmv
✏️ Modeling
Reference
Output Module
outputSeries
LocalOutputWriter
¶
Bases: OutputWriter
Class for writing outputs to local files or stdout.
You can import the LocalOutputWriter class directly from nextmv:
This class implements the OutputWriter interface to write output data to local files or stdout. The destination and format depend on the output format and the provided path.
Examples:
>>> from nextmv.output import LocalOutputWriter, Output, Metrics
>>> writer = LocalOutputWriter()
>>> output = Output(solution={"result": 42}, metrics={"time": 1.23})
>>> # Write to stdout
>>> writer.write(output, path=None)
>>> # Write to a file
>>> writer.write(output, path="results.json")
write
¶
write(
output: Output
| dict[str, Any]
| BaseModel
| None = None,
path: str | None = None,
skip_stdout_reset: bool = False,
content_format: ContentFormat | None = None,
manifest: Manifest | None = None,
options: Options | dict[str, Any] | None = None,
metrics: dict[str, Any] | None = None,
assets: list[Asset | dict[str, Any]] | None = None,
solution: dict[str, Any]
| Any
| dict[str, list[dict[str, Any]]]
| None = None,
solution_files: list[SolutionFile] | None = None,
csv_configurations: dict[str, Any] | None = None,
json_configurations: dict[str, Any] | None = None,
) -> None
Write the output to the local filesystem or stdout.
This method writes the provided output to the specified path or to stdout, depending on the output format and the path parameter.
| PARAMETER | DESCRIPTION |
|---|---|
|
Output data to write. Can be an Output object, a dictionary, or a BaseModel.
TYPE:
|
|
Path to write the output data to. The interpretation depends on the output format:
TYPE:
|
|
Skip resetting stdout before writing the output data. Default is False.
TYPE:
|
|
The content format to use for writing the output. If not provided, it
is resolved from the manifest, the output object, or defaults to
TYPE:
|
|
Manifest object used to resolve the content format and output paths. If not provided, a manifest is attempted to be loaded from the current working directory.
TYPE:
|
|
Options to include in the output. Takes priority over the options in
the
TYPE:
|
|
Metrics to include in the output. Takes priority over the metrics in
the
TYPE:
|
|
List of assets to include in the output. Takes priority over the assets
in the
TYPE:
|
|
Solution data to include in the output. Takes priority over the solution
in the
TYPE:
|
|
List of solution files to include in the output. Takes priority over
the solution files in the
TYPE:
|
|
Configuration options for CSV serialization, passed as kwargs to
TYPE:
|
|
Configuration options for JSON serialization, passed as kwargs to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the Output.output_format is not supported. |
TypeError
|
If the output is of an unsupported type. |
Notes
This function detects if stdout was redirected and resets it to avoid unexpected behavior. If you want to skip this behavior, set the skip_stdout_reset parameter to True.
If the output is a dict or a BaseModel, it will be written as JSON. If the output is an Output object, it will be written according to its output_format.
Examples:
>>> from nextmv import LocalOutputWriter, Output, json_solution_file
>>> from nextmv.content_format import ContentFormat
>>> writer = LocalOutputWriter()
>>> # Write JSON output to stdout using an Output object
>>> writer.write(Output(solution={"result": 42}, metrics={"duration": 1.5}))
>>> # Write JSON output to a file
>>> writer.write(Output(solution={"result": 42}), path="result.json")
>>> # Write JSON output directly via arguments, without an Output object
>>> writer.write(solution={"result": 42}, metrics={"duration": 1.5}, path="result.json")
>>> # Write multi-file output to a directory using an Output object
>>> solution_file = json_solution_file(name="solution", data={"result": 42})
>>> writer.write(
... Output(
... output_format=ContentFormat.MULTI_FILE,
... solution_files=[solution_file],
... ),
... path="output_dir",
... )
>>> # Write multi-file output directly via arguments, without an Output object
>>> writer.write(
... content_format=ContentFormat.MULTI_FILE,
... solution_files=[solution_file],
... metrics={"duration": 1.5},
... path="output_dir",
... )
Source code in nextmv/nextmv/output.py
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 | |
METRICS_KEY
module-attribute
¶
Metrics key constant used for identifying metrics in the run output.
OUTPUTS_KEY
module-attribute
¶
Outputs key constant used for identifying outputs in the run output.
Output
dataclass
¶
Output(
options: Options | dict[str, Any] | None = None,
output_format: ContentFormat | None = None,
solution: dict[str, Any]
| Any
| dict[str, list[dict[str, Any]]]
| None = None,
statistics: Statistics | dict[str, Any] | None = None,
metrics: dict[str, Any] | None = None,
csv_configurations: dict[str, Any] | None = None,
json_configurations: dict[str, Any] | None = None,
assets: list[Asset | dict[str, Any]] | None = None,
solution_files: list[SolutionFile] | None = None,
)
Output of a decision problem.
You can import the Output class directly from nextmv:
This class is used to structure the output of a decision problem that can later be written to various destinations. It supports different output formats and allows for customization of the serialization process.
The solution's type must match the output_format:
ContentFormat.JSON: the data must bedict[str, Any]orAny.ContentFormat.MULTI_FILE: the data must bedict[str, Any]. When working with multi-file, the solution is written to one or more files in a specific directory.
If you are working with ContentFormat.MULTI_FILE, you should use
solution_files instead of solution. When solution_files is not
None, then the output_format must be ContentFormat.MULTI_FILE.
solution_files is a list of SolutionFile objects, which allows you to
define the name of the file, the data to be written, and the writer
function that will handle the serialization of the data. This is useful when
you need to write the solution to multiple files with different formats or
configurations.
There are convenience functions to create SolutionFile objects for
common use cases, such as:
json_solution_file: for writing JSON data to a file.csv_solution_file: for writing CSV data to a file.text_solution_file: for writing utf-8 encoded data to a file.
For other data types, such as Excel, you can create your own SolutionFile
objects by providing a name, data, and a writer function that will
handle the serialization of the data.
| PARAMETER | DESCRIPTION |
|---|---|
|
Options that the
TYPE:
|
|
Format of the output data. Default is
TYPE:
|
|
The solution to the decision problem. The type must match the
TYPE:
|
|
Deprecated: Use Metrics instead. Statistics of the solution. Default is None.
TYPE:
|
|
Metrics of the solution. Default is None.
TYPE:
|
|
Configuration for writing CSV files. Default is None.
TYPE:
|
|
Configuration for writing JSON files. Default is None.
TYPE:
|
|
List of assets to be included in the output. Default is None.
TYPE:
|
|
Optional list of solution files to be included in the output. These
files are of type There are convenience functions to create
For other data types, such as Excel, you can create your own
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the solution is not compatible with the specified output_format. |
TypeError
|
If options, statistics, or assets have unsupported types. |
Examples:
>>> from nextmv.output import Output, Statistics, RunStatistics
>>> metrics = {"duration": 30.0, "iterations": 100}
>>> solution = {"routes": [{"vehicle": 1, "stops": [1, 2, 3]}, {"vehicle": 2, "stops": [4, 5]}]}
>>> output = Output(
... output_format=ContentFormat.JSON,
... solution=solution,
... metrics=metrics,
... json_configurations={"indent": 4}
... )
>>> output_dict = output.to_dict()
>>> "solution" in output_dict and "metrics" in output_dict
True
-
nextmv
✏️ Modeling
Reference
Output Module
output
assets
class-attribute
instance-attribute
¶
assets: list[Asset | dict[str, Any]] | None = None
Optional list of assets to be included in the output. These assets can be of
type Asset or a simple dictionary. If the assets are of type Asset, they
will be serialized to a dictionary using the to_dict method. If they are a
dictionary, they will be used as is. If the assets are not provided, an
empty list will be used.
csv_configurations
class-attribute
instance-attribute
¶
Optional configuration for writing CSV files. These configurations are
passed as kwargs to the DictWriter class from the csv module.
json_configurations
class-attribute
instance-attribute
¶
Optional configuration for writing JSON files, to be used when the
output_format is ContentFormat.JSON. These configurations are passed as
kwargs to the json.dumps function.
metrics
class-attribute
instance-attribute
¶
Metrics of the solution. These metrics should be provided as a simple or nested dictionary.
options
class-attribute
instance-attribute
¶
Options that the Output was created with. These options can be of type
Options or a simple dictionary. If the options are of type Options,
they will be serialized to a dictionary using the to_dict method. If
they are a dictionary, they will be used as is. If the options are not
provided, an empty dictionary will be used. If the options are of type
dict, then the dictionary should have the following structure:
output_format
class-attribute
instance-attribute
¶
Format of the output data. When set to ContentFormat.MULTI_FILE, the
solution_files field must be specified and cannot be None.
solution
class-attribute
instance-attribute
¶
The solution to the decision problem. Use this filed when working with
output_format of types:
ContentFormat.JSON: the data must bedict[str, Any]orAny.
Note that when the output_format is set to ContentFormat.MULTI_FILE,
this solution field is ignored, as you should use the solution_files
field instead.
solution_files
class-attribute
instance-attribute
¶
solution_files: list[SolutionFile] | None = None
Optional list of solution files to be included in the output. These files
are of type SolutionFile, which allows for custom serialization and
writing of the solution data to files. When this field is specified, then
the output_format must be set to ContentFormat.MULTI_FILE, otherwise an
exception will be raised. The SolutionFile class allows you to define the
name of the file, the data to be written, and the writer function that will
handle the serialization of the data. This is useful when you need to write
the solution to multiple files with different formats or configurations.
There are convenience functions to create SolutionFile objects for
common use cases, such as:
json_solution_file: for writing JSON data to a file.csv_solution_file: for writing CSV data to a file.text_solution_file: for writing utf-8 encoded data to a file.
For other data types, such as Excel, you can create your own SolutionFile
objects by providing a name, data, and a writer function that will
handle the serialization of the data.
statistics
class-attribute
instance-attribute
¶
statistics: Statistics | dict[str, Any] | None = None
Warning
statistics is deprecated, use metrics instead.
Statistics of the solution. These statistics can be of type Statistics or a
simple dictionary. If the statistics are of type Statistics, they will be
serialized to a dictionary using the to_dict method. If they are a
dictionary, they will be used as is.
to_dict
¶
Convert the Output object to a dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The dictionary representation of the |
Source code in nextmv/nextmv/output.py
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 | |
OutputFormat
¶
Bases: str, Enum
Warning
OutputFormat is deprecated, use ContentFormat instead.
Enumeration of supported output formats.
You can import the OutputFormat class directly from nextmv:
This enum defines the different formats that can be used for outputting data. Each format has specific requirements and behaviors when writing.
| ATTRIBUTE | DESCRIPTION |
|---|---|
JSON |
Warning
JSON format, utf-8 encoded.
TYPE:
|
CSV_ARCHIVE |
Warning
CSV archive format: multiple CSV files.
TYPE:
|
MULTI_FILE |
Warning
Multi-file format: multiple files in a directory.
TYPE:
|
TEXT |
Warning
Text format, utf-8 encoded.
TYPE:
|
CSV_ARCHIVE
class-attribute
instance-attribute
¶
Warning
OutputFormat.CSV_ARCHIVE is deprecated, use ContentFormat.MULTI_FILE instead.
CSV archive format: multiple CSV files.
JSON
class-attribute
instance-attribute
¶
Warning
OutputFormat.JSON is deprecated, use ContentFormat.JSON instead.
JSON format, utf-8 encoded.
MULTI_FILE
class-attribute
instance-attribute
¶
Warning
OutputFormat.MULTI_FILE is deprecated, use ContentFormat.MULTI_FILE instead.
Multi-file format: multiple files in a directory.
TEXT
class-attribute
instance-attribute
¶
Warning
OutputFormat.TEXT is deprecated, use ContentFormat.MULTI_FILE instead.
Text format, utf-8 encoded.
OutputWriter
¶
Base class for writing outputs.
You can import the OutputWriter class directly from nextmv:
This is an abstract base class that defines the interface for writing outputs
to different destinations. Subclasses should implement the write method.
Examples:
>>> class CustomOutputWriter(OutputWriter):
... def write(self, output, path=None, **kwargs):
... # Custom implementation for writing output
... print(f"Writing output to {path}")
-
nextmv
✏️ Modeling
Reference
Output Module
outputLocalOutputWriter
-
nextmv
✏️ Modeling
Reference
Output Module
outputwrite
write
¶
Write the output data.
This is an abstract method that should be implemented by subclasses.
| PARAMETER | DESCRIPTION |
|---|---|
|
The output data to write.
TYPE:
|
|
Variable length argument list.
DEFAULT:
|
|
Arbitrary keyword arguments.
DEFAULT:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
This method must be implemented by subclasses. |
Source code in nextmv/nextmv/output.py
ResultStatistics
¶
Bases: BaseModel
Warning
ResultStatistics is deprecated, use metrics on the Output directly.
Statistics about a specific result.
You can import the ResultStatistics class directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Duration of the run in seconds.
TYPE:
|
|
Value of the result.
TYPE:
|
|
Custom statistics created by the user. Can normally expect a
TYPE:
|
Examples:
>>> from nextmv.output import ResultStatistics
>>> result_stats = ResultStatistics(duration=5.2, value=42.0)
>>> result_stats.value
42.0
>>> result_stats.custom = {"gap": 0.05}
>>> result_stats.to_dict()
{'duration': 5.2, 'value': 42.0, 'custom': {'gap': 0.05}}
-
nextmv
✏️ Modeling
Reference
Output Module
outputStatisticsresult
-
nextmv
✏️ Modeling
Reference
Output Module
outputStatistics
RunStatistics
¶
Bases: BaseModel
Warning
RunStatistics is deprecated, use metrics on the Output directly.
Statistics about a general run.
You can import the RunStatistics class directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Duration of the run in seconds.
TYPE:
|
|
Number of iterations.
TYPE:
|
|
Custom statistics created by the user. Can normally expect a
TYPE:
|
Examples:
>>> from nextmv.output import RunStatistics
>>> stats = RunStatistics(duration=10.5, iterations=100)
>>> stats.duration
10.5
>>> stats.custom = {"convergence": 0.001}
>>> stats.to_dict()
{'duration': 10.5, 'iterations': 100, 'custom': {'convergence': 0.001}}
-
nextmv
✏️ Modeling
Reference
Output Module
outputStatisticsrun
-
nextmv
✏️ Modeling
Reference
Output Module
outputStatistics
SOLUTIONS_KEY
module-attribute
¶
Solutions key constant used for identifying solutions in the run output.
STATISTICS_KEY
module-attribute
¶
Deprecated: Use METRICS_KEY instead. Statistics key constant used for identifying statistics in the run output.
Series
¶
Bases: BaseModel
Warning
Series is deprecated, use metrics on the Output directly.
A series of data points for visualization or analysis.
You can import the Series class directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the series.
TYPE:
|
|
Data points of the series.
TYPE:
|
Examples:
>>> from nextmv.output import Series, DataPoint
>>> points = [DataPoint(x=1.0, y=2.0), DataPoint(x=2.0, y=3.0)]
>>> series = Series(name="Example Series", data_points=points)
>>> series.name
'Example Series'
>>> len(series.data_points)
2
-
nextmv
✏️ Modeling
Reference
Output Module
outputSeriesData
-
nextmv
✏️ Modeling
Reference
Output Module
outputSeriesData
SeriesData
¶
Bases: BaseModel
Warning
SeriesData is deprecated, use metrics on the Output directly.
Data container for multiple series of data points.
You can import the SeriesData class directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
A series for the value of the solution.
TYPE:
|
|
A list of series for custom statistics.
TYPE:
|
Examples:
>>> from nextmv.output import SeriesData, Series, DataPoint
>>> value_series = Series(name="Solution Value", data_points=[DataPoint(x=0, y=10), DataPoint(x=1, y=5)])
>>> custom_series = [Series(name="Gap", data_points=[DataPoint(x=0, y=0.5), DataPoint(x=1, y=0.1)])]
>>> series_data = SeriesData(value=value_series, custom=custom_series)
>>> series_data.value.name
'Solution Value'
>>> len(series_data.custom)
1
-
nextmv
✏️ Modeling
Reference
Output Module
outputStatisticsseries_data
-
nextmv
✏️ Modeling
Reference
Output Module
outputStatistics
SolutionFile
dataclass
¶
SolutionFile(
name: str,
data: Any,
writer: Callable[[str, Any], None],
writer_args: list[Any] | None = None,
writer_kwargs: dict[str, Any] | None = None,
)
Represents a solution to be written as a file.
You can import the SolutionFile class directly from nextmv:
This class is used to define a solution that will be written to a file in
the filesystem. It includes the name of the file, the data to be written,
and the writer function that will handle the serialization of the data.
This SolutionFile class is typically used in the Output, when the
Output.output_format is set to ContentFormat.MULTI_FILE. Given that it
is difficult to handle every edge case of how a solution is serialized, and
written to a file, this class exists so that the user can implement the
writer callable of their choice and provide it with any writer_args
and writer_kwargs they might need.
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the output file. The file extension should be included in the name.
TYPE:
|
|
The actual data that will be written to the file. This can be any type
that can be given to the
TYPE:
|
|
Callable that writes the solution data to the file. This should be a
function implemented by the user. There are convenience functions that you
can use as a writer as well. The
The
TYPE:
|
|
Positional arguments to pass to the writer function.
TYPE:
|
|
Keyword arguments to pass to the writer function.
TYPE:
|
Examples:
>>> from nextmv import SolutionFile
>>> solution_file = SolutionFile(
... name="solution.csv",
... data=[{"id": 1, "value": 100}, {"id": 2, "value": 200}],
... writer=csv.DictWriter,
... writer_kwargs={"fieldnames": ["id", "value"]},
... writer_args=[open("solution.csv", "w", newline="")],
... )
-
nextmv
✏️ Modeling
Reference
Output Module
output
-
nextmv
✏️ Modeling
Reference
Output Module
output
data
instance-attribute
¶
The actual data that will be written to the file. This can be any type that
can be given to the writer function. For example, if the writer is a
csv.DictWriter, then the data should be a list of dictionaries, where
each dictionary represents a row in the CSV file.
name
instance-attribute
¶
Name of the solution (output) file. The file extension should be included in the name.
writer
instance-attribute
¶
Callable that writes the solution data to the file. This should be a
function implemented by the user. There are convenience functions that you
can use as a writer as well. The writer must receive, at the very
minimum, the following arguments:
file_path: astrargument which is the location where this solution will be written to. This includes the dir and the name of the file. As such, thenameparameter of this class is going to be passed to this function joined with the directory where the file will be written.data: the actual data that will be written to the file. This can be any type that can be given to thewriterfunction. Thedataparameter of this class is going to be passed to thewriterfunction.
The writer can also receive additional arguments, and keyword arguments.
The writer_args and writer_kwargs parameters of this class can be used
to provide those additional arguments.
writer_args
class-attribute
instance-attribute
¶
Optional positional arguments to pass to the writer function. This can be used to customize the behavior of the writer.
writer_kwargs
class-attribute
instance-attribute
¶
Optional keyword arguments to pass to the writer function. This can be used to customize the behavior of the writer.
Statistics
¶
Bases: BaseModel
Warning
Statistics is deprecated, use Metrics instead.
Complete statistics container for a solution, including run metrics and result data.
You can import the Statistics class directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Statistics about the run.
TYPE:
|
|
Statistics about the last result.
TYPE:
|
|
Series data about some metric.
TYPE:
|
|
Schema (version). This class only supports
TYPE:
|
Examples:
>>> from nextmv.output import Statistics, RunStatistics, ResultStatistics
>>> run_stats = RunStatistics(duration=10.0, iterations=50)
>>> result_stats = ResultStatistics(value=100.0)
>>> stats = Statistics(run=run_stats, result=result_stats, statistics_schema="v1")
>>> stats.run.duration
10.0
>>> stats.result.value
100.0
-
nextmv
✏️ Modeling
Reference
Output Module
outputOutputstatistics
-
nextmv
✏️ Modeling
Reference
Output Module
outputOutput
result
class-attribute
instance-attribute
¶
result: ResultStatistics | None = None
Statistics about the last result.
series_data
class-attribute
instance-attribute
¶
series_data: SeriesData | None = None
Data of the series.
statistics_schema
class-attribute
instance-attribute
¶
statistics_schema: str | None = Field(
serialization_alias="schema",
validation_alias=AliasChoices(
"schema", "statistics_schema"
),
default="v1",
)
Schema (version). This class only supports v1.
Visual
¶
Bases: BaseModel
Visual schema definition for an asset.
You can import the Visual class directly from nextmv:
This class defines how an asset is plotted in the Nextmv Console, including the schema type, label, and display type.
| PARAMETER | DESCRIPTION |
|---|---|
|
Schema of the visual asset.
TYPE:
|
|
Label for the custom tab of the visual asset in the Nextmv Console.
TYPE:
|
|
Defines the type of custom visual. Default is "custom-tab".
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If an unsupported schema or visual_type is provided. |
Examples:
>>> from nextmv.output import Visual, VisualSchema
>>> visual = Visual(visual_schema=VisualSchema.CHARTJS, label="Performance Chart")
>>> visual.visual_schema
<VisualSchema.CHARTJS: 'chartjs'>
>>> visual.label
'Performance Chart'
-
nextmv
✏️ Modeling
Reference
Output Module
outputAssetvisual
-
nextmv
✏️ Modeling
Reference
Output Module
outputAsset
label
instance-attribute
¶
Label for the custom tab of the visual asset in the Nextmv Console.
visual_schema
class-attribute
instance-attribute
¶
visual_schema: VisualSchema = Field(
serialization_alias="schema",
validation_alias=AliasChoices(
"schema", "visual_schema"
),
)
Schema of the visual asset.
visual_type
class-attribute
instance-attribute
¶
visual_type: str | None = Field(
serialization_alias="type",
validation_alias=AliasChoices("type", "visual_type"),
default="custom-tab",
)
Defines the type of custom visual, currently there is only one type:
custom-tab. This renders the visual in its own tab view of the run
details.
VisualSchema
¶
Bases: str, Enum
Enumeration of supported visualization schemas.
You can import the VisualSchema class directly from nextmv:
This enum defines the different visualization libraries or rendering methods that can be used to display custom asset data in the Nextmv Console.
| ATTRIBUTE | DESCRIPTION |
|---|---|
CHARTJS |
Tells Nextmv Console to render the custom asset data with the Chart.js library.
TYPE:
|
GEOJSON |
Tells Nextmv Console to render the custom asset data as GeoJSON on a map.
TYPE:
|
PLOTLY |
Tells Nextmv Console to render the custom asset data with the Plotly library.
TYPE:
|
-
nextmv
✏️ Modeling
Reference
Output Module
outputVisualvisual_schema
-
nextmv
✏️ Modeling
Reference
Output Module
outputVisual
CHARTJS
class-attribute
instance-attribute
¶
Tells Nextmv Console to render the custom asset data with the Chart.js library.
GEOJSON
class-attribute
instance-attribute
¶
Tells Nextmv Console to render the custom asset data as GeoJSON on a map.
PLOTLY
class-attribute
instance-attribute
¶
Tells Nextmv Console to render the custom asset data with the Plotly library.
csv_solution_file
¶
csv_solution_file(
name: str,
data: list[dict[str, Any]],
csv_configurations: dict[str, Any] | None = None,
) -> SolutionFile
This is a convenience function to build a SolutionFile. It writes the
given data to a .csv file with the provided name.
You can import this function directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the output file. You don't need to include the
TYPE:
|
|
The actual data that will be written to the file. This should be a list of dictionaries, where each dictionary represents a row in the CSV file. The keys of the dictionaries will be used as the column headers in the CSV file.
TYPE:
|
|
Optional configuration options for the CSV serialization process.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SolutionFile
|
The constructed |
Examples:
>>> from nextmv import csv_solution_file
>>> solution_file = csv_solution_file(
... name="solution",
... data=[{"id": 1, "value": 100}, {"id": 2, "value": 200}]
... )
>>> solution_file.name
'solution.csv'
>>> solution_file.data
[{'id': 1, 'value': 100}, {'id': 2, 'value': 200}]
Source code in nextmv/nextmv/output.py
json_solution_file
¶
json_solution_file(
name: str,
data: dict[str, Any],
json_configurations: dict[str, Any] | None = None,
) -> SolutionFile
This is a convenience function to build a SolutionFile. It writes the
given data to a .json file with the provided name.
You can import this function directly from nextmv:
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the output file. You don't need to include the
TYPE:
|
|
The actual data that will be written to the file. This should be a dictionary that can be serialized to JSON.
TYPE:
|
|
Optional configuration options for the JSON serialization process. You can use these options to configure parameters such as indentation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SolutionFile
|
The constructed |
Examples:
>>> from nextmv import json_solution_file
>>> solution_file = json_solution_file(
... name="solution",
... data={"id": 1, "value": 100}
... )
>>> solution_file.name
'solution.json'
>>> solution_file.data
{'id': 1, 'value': 100}
Source code in nextmv/nextmv/output.py
text_solution_file
¶
text_solution_file(name: str, data: str) -> SolutionFile
This is a convenience function to build a SolutionFile. It writes the
given data to a utf-8 encoded file with the provided name.
You can import this function directly from nextmv:
You must provide the extension as part of the name parameter.
| PARAMETER | DESCRIPTION |
|---|---|
|
Name of the output file. The file extension must be provided in the name.
TYPE:
|
|
The actual data that will be written to the file.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SolutionFile
|
The constructed |
Examples:
>>> from nextmv import text_solution_file
>>> solution_file = text_solution_file(
... name="solution.txt",
... data="This is a sample text solution."
... )
>>> solution_file.name
'solution.txt'
>>> solution_file.data
'This is a sample text solution.'
Source code in nextmv/nextmv/output.py
write
¶
write(
output: Output
| dict[str, Any]
| BaseModel
| None = None,
path: str | None = None,
skip_stdout_reset: bool = False,
writer: OutputWriter | None = _LOCAL_OUTPUT_WRITER,
content_format: ContentFormat | None = None,
manifest: Manifest | None = None,
options: Options | dict[str, Any] | None = None,
metrics: dict[str, Any] | None = None,
assets: list[Asset | dict[str, Any]] | None = None,
solution: dict[str, Any]
| Any
| dict[str, list[dict[str, Any]]]
| None = None,
solution_files: list[SolutionFile] | None = None,
csv_configurations: dict[str, Any] | None = None,
json_configurations: dict[str, Any] | None = None,
) -> None
Write the output to the specified destination.
You can import the write function directly from nextmv:
This is a convenience function for writing output data using a provided
writer. By default, it uses the LocalOutputWriter to write to local
sources like files or stdout. When working with the local filesystem,
please consider the following.
To use this function you have two main options:
- Provide arguments directly, such as
solution,metrics,assets,solution_files. - Provide an
Outputobject to theoutputargument. TheOutputclass is in charge of aggregating output elements.
This function will resolve each of the required element in the following order:
- If the argument is provided, it takes priority. For example, if you use
the
solutionargument and theoutputargument (passing anOutputclass with a valid.solutionfield), thesolutionargument will be used. - If the argument is not provided, but the
outputargument is provided and has the corresponding field, the value from theOutputobject will be used. - If neither the argument nor the
Outputfield is provided, the element will be considered not available and treated asNone.
There are two special cases to consider: the path and the
content_format arguments.
- If the argument is provided, it is used as it takes priority.
- If a
manifestobject is used, then it is used to resolve thepathandcontent_format. - If a
manifestobject is not given, a manifest is attempted to be loaded from the current working directory. If anapp.yamlmanifest exists, then the manifest is used to resolve thepathandcontent_format. - If the
outputargument is provided and is anOutputobject, then itsoutput_formatfield is used to resolve thecontent_format. - If none of the above applies, the
content_formatdefaults toContentFormat.JSONand thepathuses default values based on the content format.
It is preferred that you either use the manifest argument or rely on an
app.yaml manifest to be present so that the path and content_format
are resolved from the manifest configuration.
This function detects if stdout was redirected and resets it to avoid
unexpected behavior. If you want to skip this behavior, set the
skip_stdout_reset parameter to True.
| PARAMETER | DESCRIPTION |
|---|---|
|
Output data to write. Can be an Output object, a dictionary, or a BaseModel.
TYPE:
|
|
Path to write the output data to. The interpretation depends on the output format:
TYPE:
|
|
Skip resetting stdout before writing the output data. Default is False.
TYPE:
|
|
The writer to use for writing the output. Default is a
TYPE:
|
|
The content format to use for writing the output. If not provided, it
is resolved from the manifest, the output object, or defaults to
TYPE:
|
|
Manifest object used to resolve the content format and output paths. If not provided, a manifest is attempted to be loaded from the current working directory.
TYPE:
|
|
Options to include in the output. Takes priority over the options in
the
TYPE:
|
|
Metrics to include in the output. Takes priority over the metrics in
the
TYPE:
|
|
List of assets to include in the output. Takes priority over the assets
in the
TYPE:
|
|
Solution data to include in the output. Takes priority over the solution
in the
TYPE:
|
|
List of solution files to include in the output. Takes priority over
the solution files in the
TYPE:
|
|
Configuration options for CSV serialization, passed as kwargs to
TYPE:
|
|
Configuration options for JSON serialization, passed as kwargs to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the Output.output_format is not supported. |
TypeError
|
If the output is of an unsupported type. |
Examples:
>>> from nextmv import write, Output, json_solution_file
>>> from nextmv.content_format import ContentFormat
>>> # Write JSON output to stdout using an Output object
>>> write(Output(solution={"result": 42}, metrics={"duration": 1.5}))
>>> # Write JSON output to a file
>>> write(Output(solution={"result": 42}), path="result.json")
>>> # Write JSON output directly via arguments, without an Output object
>>> write(solution={"result": 42}, metrics={"duration": 1.5}, path="result.json")
>>> # Write JSON output with custom indentation
>>> write(solution={"result": 42}, json_configurations={"indent": 2})
>>> # Write multi-file output to a directory using an Output object
>>> solution_file = json_solution_file(name="solution", data={"result": 42})
>>> write(
... Output(
... output_format=ContentFormat.MULTI_FILE,
... solution_files=[solution_file],
... ),
... path="output_dir",
... )
>>> # Write multi-file output directly via arguments, without an Output object
>>> write(
... content_format=ContentFormat.MULTI_FILE,
... solution_files=[solution_file],
... metrics={"duration": 1.5},
... path="output_dir",
... )
Source code in nextmv/nextmv/output.py
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 | |