How to Read & Write to/from File

When running experiments, it may be useful to read in parameters from a file, and to export data records to file. This can be done easily in Ciw. Parameter dictionaries can be represented as .yml files, and results can be output as .csv files.

Parameter Files

Consider the following Network:

>>> import ciw
    >>> N = ciw.create_network(
    ...     Arrival_distributions={'Class 0': [['Exponential', 6.0], ['Exponential', 2.5]]},
    ...     Service_distributions={'Class 0': [['Exponential', 8.5], ['Exponential', 5.5]]},
    ...     Transition_matrices={'Class 0': [[0.0, 0.2], [0.1, 0.0]]},
    ...     Number_of_servers=[1, 1],
    ...     Queue_capacities=['Inf', 4]
    ... )

This can be represented by the .yml file below:

parameters.yml

Arrival_distributions:
  Class 0:
  - - Exponential
    - 6.0
  - - Exponential
    - 2.5
Service_distributions:
  Class 0:
  - - Exponential
    - 8.5
  - - Exponential
    - 5.5
Transition_matrices:
  Class 0:
  - - 0.0
    - 0.2
  - - 0.1
    - 0.0
Number_of_servers:
- 1
- 1
Queue_capacities:
- "Inf"
- 4

This can then be created into a Network object using the ciw.create_network_from_yml function:

>>> import ciw 
>>> N = ciw.create_network_from_yml('<path_to_file>') 
>>> Q = ciw.Simulation(N) 

Exporting Results

Once a simulation has been run, all data records can be exported to file using the write_records_to_file method of the Simulation object. This method writes all results that are obtained by the get_all_records method (see here for more information) to a .csv file, where each row is an observation ad each column a variable:

>>> Q.write_records_to_file('<path_to_file>') 

This method also takes the optional keyword argument header. If this is set to True then the first row of the .csv file will be the variable names. The default value is True, set to False is a row of variable names are not needed:

>>> Q.write_records_to_file('<path_to_file>', headers=True) 
>>> Q.write_records_to_file('<path_to_file>', headers=False)