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': [ciw.dists.Exponential(rate=6.0), ciw.dists.Exponential(rate=2.5)]},
... service_distributions={'Class 0': [ciw.dists.Exponential(rate=8.5), ciw.dists.Exponential(rate=5.5)]},
... routing={'Class 0': [[0.0, 0.2], [0.1, 0.0]]},
... number_of_servers=[1, 1],
... queue_capacities=[float('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
routing:
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)