Skip to content

ENH: Save Monte Carlo outputs to .csv and .json formats #242

@Gui-FernandesBR

Description

@Gui-FernandesBR

Is your feature request related to a problem? Please describe.

Currently, the MonteCarlo class saves simulation outputs (results, inputs, and errors) primarily to .txt files. While this works for internal storage, it makes post-processing difficult for users who want to analyze their data in external tools like Excel, MATLAB, or generic Python scripts without loading RocketPy.

As discussed in PR #229, users need a way to easily export their dispersion results to standard data exchange formats to perform comparative analysis between different rocket configurations.

Describe the solution you'd like

I would like to implement a method (or extend the existing functionality) in the MonteCarlo class to export results to .csv and .json files.

The goal is to allow the user to choose the output format easily.

Implementation Details

The changes should likely be made in rocketpy/simulation/monte_carlo.py.

  1. New Methods: We need methods to handle the export logic. For example:

    • export_results_to_csv(filename)
    • export_results_to_json(filename)
    • Alternatively, a single unified method: export_data(filename, format="csv").
  2. Data to Export: The export should include the data currently stored in the self.results dictionary (e.g., apogee, max_mach, impact coordinates).

  3. JSON Structure: Should map keys (variable names) to lists of values.

  4. CSV Structure: Should have headers (variable names) and rows corresponding to each simulation iteration.

Proposed Usage

# After running the simulation
analysis = MonteCarlo(filename="my_flight_data", ...)
analysis.simulate(number_of_simulations=1000)

# The user should be able to do this:
analysis.export_results("simulation_data.csv", format="csv")
analysis.export_results("simulation_data.json", format="json")

Acceptance Criteria

  • Implement export functionality for .csv.
  • Implement export functionality for .json.
  • Ensure that export_list and custom data_collector variables are included in the export.
  • Add unit tests in tests/ ensuring the files are created and contain correct data.
  • Update the documentation to include examples of how to export data.

Additional Context

Please avoid using heavy dependencies (like pandas) inside the class methods if possible, unless the team decides it is a required dependency. Using Python's built-in csv and json libraries is preferred for lightweight implementation.

Metadata

Metadata

Assignees

Labels

EnhancementNew feature or request, including adjustments in current codesGood first issueGood for newcomersMonte CarloMonte Carlo and related contents

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions