sim-tools 0.4.0__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sim-tools
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Simulation Tools for Education and Practice
5
5
  Project-URL: Homepage, https://github.com/TomMonks/sim-tools
6
6
  Project-URL: Bug Tracker, https://github.com/TomMonks/sim-tools/issues
@@ -20,35 +20,56 @@ Requires-Dist: scikit-learn>=1.0.0
20
20
  Requires-Dist: scipy>=1.4.1
21
21
  Description-Content-Type: text/markdown
22
22
 
23
- # sim-tools: tools to support the simulation process in python.
23
+ # `sim-tools`: tools to support the Discrete-Event Simulation process in python.
24
24
 
25
25
  [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TomMonks/sim-tools/HEAD)
26
26
  [![DOI](https://zenodo.org/badge/225608065.svg)](https://zenodo.org/badge/latestdoi/225608065)
27
27
  [![PyPI version fury.io](https://badge.fury.io/py/sim-tools.svg)](https://pypi.python.org/pypi/sim-tools/)
28
+ [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/version.svg)](https://anaconda.org/conda-forge/sim-tools)
29
+ [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/platforms.svg)](https://anaconda.org/conda-forge/sim-tools)
28
30
  [![Read the Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://tommonks.github.io/sim-tools)
29
31
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
30
32
  [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/release/python-360+/)
31
33
  [![License: MIT](https://img.shields.io/badge/ORCID-0000--0003--2631--4481-brightgreen)](https://orcid.org/0000-0003-2631-4481)
32
34
 
33
- sim-tools is being developed to support simulation education and applied simulation research. It is MIT licensed and freely available to practitioners, students and researchers via PyPi. There is a longer term plan to make sim-tools available via conda-forge.
35
+
36
+ `sim-tools` is being developed to support Discrete-Event Simulation (DES) education and applied simulation research. It is MIT licensed and freely available to practitioners, students and researchers via [PyPi](https://pypi.org/project/sim-tools/) and [conda-forge](https://anaconda.org/conda-forge/sim-tools)
34
37
 
35
38
  # Vision for sim-tools
36
39
 
37
- 1. Deliver high quality reliable code for simulation education and practice with full documentation.
40
+ 1. Deliver high quality reliable code for DES education and practice with full documentation.
38
41
  2. Provide a simple to use pythonic interface.
39
- 3. To improve the quality of simulation education and encourage the use of best practice.
42
+ 3. To improve the quality of DES education using FOSS tools and encourage the use of best practice.
40
43
 
41
44
  # Features:
42
45
 
43
- 1. Implementation of classic optimisation via Simulation procedures such as KN, KN++, OBCA and OBCA-m
46
+ 1. Implementation of classic Optimisation via Simulation procedures such as KN, KN++, OBCA and OBCA-m
44
47
  2. Distributions module that includes classes that encapsulate a random number stream, seed, and distribution parameters.
45
- 3. Implementation of Thinning to sample from Non-stationary poisson processes in a discrete-event simulation
48
+ 3. Implementation of Thinning to sample from Non-stationary poisson processes in a DES.
49
+
50
+ ## Installation
51
+
52
+ ### Pip and PyPi
53
+
54
+ ```bash
55
+ pip install sim-tools
56
+ ```
57
+
58
+ ### Conda-forge
59
+
60
+ ```bash
61
+ conda install -c conda-forge sim-tools
62
+ ```
63
+
64
+ ### Binder
65
+
66
+ [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TomMonks/sim-tools/HEAD)
67
+
46
68
 
47
- ## Three simple ways to explore sim-tools
69
+ ## Learn how to use `sim-tools`
48
70
 
49
- 1. `pip install sim-tools`
50
- 2. Click on the launch-binder at the top of this readme. This will open example Jupyter notebooks in the cloud via Binder.
51
- 3. Oneline documentation: https://tommonks.github.io/sim-tools
71
+ * Online documentation: https://tommonks.github.io/sim-tools
72
+ * Introduction to DES in python: https://health-data-science-or.github.io/simpy-streamlit-tutorial/
52
73
 
53
74
  ## Citation
54
75
 
@@ -1,32 +1,53 @@
1
- # sim-tools: tools to support the simulation process in python.
1
+ # `sim-tools`: tools to support the Discrete-Event Simulation process in python.
2
2
 
3
3
  [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TomMonks/sim-tools/HEAD)
4
4
  [![DOI](https://zenodo.org/badge/225608065.svg)](https://zenodo.org/badge/latestdoi/225608065)
5
5
  [![PyPI version fury.io](https://badge.fury.io/py/sim-tools.svg)](https://pypi.python.org/pypi/sim-tools/)
6
+ [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/version.svg)](https://anaconda.org/conda-forge/sim-tools)
7
+ [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/platforms.svg)](https://anaconda.org/conda-forge/sim-tools)
6
8
  [![Read the Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://tommonks.github.io/sim-tools)
7
9
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
10
  [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/release/python-360+/)
9
11
  [![License: MIT](https://img.shields.io/badge/ORCID-0000--0003--2631--4481-brightgreen)](https://orcid.org/0000-0003-2631-4481)
10
12
 
11
- sim-tools is being developed to support simulation education and applied simulation research. It is MIT licensed and freely available to practitioners, students and researchers via PyPi. There is a longer term plan to make sim-tools available via conda-forge.
13
+
14
+ `sim-tools` is being developed to support Discrete-Event Simulation (DES) education and applied simulation research. It is MIT licensed and freely available to practitioners, students and researchers via [PyPi](https://pypi.org/project/sim-tools/) and [conda-forge](https://anaconda.org/conda-forge/sim-tools)
12
15
 
13
16
  # Vision for sim-tools
14
17
 
15
- 1. Deliver high quality reliable code for simulation education and practice with full documentation.
18
+ 1. Deliver high quality reliable code for DES education and practice with full documentation.
16
19
  2. Provide a simple to use pythonic interface.
17
- 3. To improve the quality of simulation education and encourage the use of best practice.
20
+ 3. To improve the quality of DES education using FOSS tools and encourage the use of best practice.
18
21
 
19
22
  # Features:
20
23
 
21
- 1. Implementation of classic optimisation via Simulation procedures such as KN, KN++, OBCA and OBCA-m
24
+ 1. Implementation of classic Optimisation via Simulation procedures such as KN, KN++, OBCA and OBCA-m
22
25
  2. Distributions module that includes classes that encapsulate a random number stream, seed, and distribution parameters.
23
- 3. Implementation of Thinning to sample from Non-stationary poisson processes in a discrete-event simulation
26
+ 3. Implementation of Thinning to sample from Non-stationary poisson processes in a DES.
27
+
28
+ ## Installation
29
+
30
+ ### Pip and PyPi
31
+
32
+ ```bash
33
+ pip install sim-tools
34
+ ```
35
+
36
+ ### Conda-forge
37
+
38
+ ```bash
39
+ conda install -c conda-forge sim-tools
40
+ ```
41
+
42
+ ### Binder
43
+
44
+ [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TomMonks/sim-tools/HEAD)
45
+
24
46
 
25
- ## Three simple ways to explore sim-tools
47
+ ## Learn how to use `sim-tools`
26
48
 
27
- 1. `pip install sim-tools`
28
- 2. Click on the launch-binder at the top of this readme. This will open example Jupyter notebooks in the cloud via Binder.
29
- 3. Oneline documentation: https://tommonks.github.io/sim-tools
49
+ * Online documentation: https://tommonks.github.io/sim-tools
50
+ * Introduction to DES in python: https://health-data-science-or.github.io/simpy-streamlit-tutorial/
30
51
 
31
52
  ## Citation
32
53
 
@@ -0,0 +1,4 @@
1
+ __version__ = '0.5.0'
2
+ __author__ = 'Thomas Monks'
3
+
4
+ from . import datasets, distributions, time_dependent, ovs
@@ -169,70 +169,66 @@ class Lognormal(Distribution):
169
169
 
170
170
 
171
171
  class Normal(Distribution):
172
- """
172
+ '''
173
173
  Convenience class for the normal distribution.
174
174
  packages up distribution parameters, seed and random generator.
175
175
 
176
- Option to prevent negative samples by resampling
177
-
178
- """
179
-
176
+ Use the minimum parameter to truncate the distribution
177
+ '''
180
178
  def __init__(
181
179
  self,
182
180
  mean: float,
183
181
  sigma: float,
184
- allow_neg: Optional[bool] = True,
182
+ minimum: Optional[float] = None,
185
183
  random_seed: Optional[int] = None,
186
184
  ):
187
- """
185
+ '''
188
186
  Constructor
189
-
187
+
190
188
  Params:
191
189
  ------
192
190
  mean: float
193
191
  The mean of the normal distribution
194
-
192
+
195
193
  sigma: float
196
194
  The stdev of the normal distribution
197
195
 
198
- allow_neg: bool, optional (default=True)
199
- False = resample on negative values
200
- True = negative samples allowed.
201
-
196
+ minimum: float
197
+ Truncate the normal distribution to a minimum
198
+ value.
199
+
202
200
  random_seed: int, optional (default=None)
203
201
  A random seed to reproduce samples. If set to none then a unique
204
202
  sample is created.
205
- """
206
- super().__init__(random_seed)
203
+ '''
204
+ self.rng = np.random.default_rng(seed=random_seed)
207
205
  self.mean = mean
208
206
  self.sigma = sigma
209
- self.allow_neg = allow_neg
210
-
207
+ self.minimum = minimum
208
+
211
209
  def sample(self, size: Optional[int] = None) -> float | np.ndarray:
212
- """
210
+ '''
213
211
  Generate a sample from the normal distribution
214
-
212
+
215
213
  Params:
216
214
  -------
217
215
  size: int, optional (default=None)
218
216
  the number of samples to return. If size=None then a single
219
217
  sample is returned.
220
- """
221
- # initial sample
218
+ '''
222
219
  samples = self.rng.normal(self.mean, self.sigma, size=size)
223
220
 
224
- # no need to check if neg allowed.
225
- if self.allow_neg:
221
+ if self.minimum is None:
222
+ return samples
223
+ elif size is None:
224
+ return max(self.minimum, samples)
225
+ else:
226
+ # index of samples with negative value
227
+ neg_idx = np.where(samples < 0)[0]
228
+ samples[neg_idx] = self.minimum
226
229
  return samples
227
230
 
228
- # repeatedly resample negative values
229
- negs = np.where(samples < 0)[0]
230
- while len(negs) > 0:
231
- resample = self.rng.normal(self.mean, self.sigma, size=len(negs))
232
- samples[negs] = resample
233
- negs = np.where(samples < 0)[0]
234
231
 
235
- return samples
236
232
 
237
233
 
238
234
  class Uniform(Distribution):
@@ -58,6 +58,7 @@ class NSPPThinning:
58
58
  self.arr_rng = np.random.default_rng(random_seed1)
59
59
  self.thinning_rng = np.random.default_rng(random_seed2)
60
60
  self.lambda_max = data["arrival_rate"].max()
61
+ self.min_iat = data["mean_iat"].min()
61
62
  # assumes all other intervals are equal in length.
62
63
  self.interval = int(data.iloc[1]["t"] - data.iloc[0]["t"])
63
64
  self.rejects_last_sample = None
@@ -94,7 +95,7 @@ class NSPPThinning:
94
95
  # reject samples if u >= lambda_t / lambda_max
95
96
  while u >= (lambda_t / self.lambda_max):
96
97
  self.rejects_last_sample += 1
97
- interarrival_time += self.arr_rng.exponential(1 / self.lambda_max)
98
+ interarrival_time += self.arr_rng.exponential(self.min_iat)
98
99
  u = self.thinning_rng.uniform(0.0, 1.0)
99
100
 
100
101
  return interarrival_time
@@ -0,0 +1,95 @@
1
+ """
2
+ Simple functionality aiming to enhanced a users a
3
+ ability to trace and debug simulation models.
4
+ """
5
+
6
+ from abc import ABC
7
+ from rich.console import Console
8
+
9
+ DEFAULT_DEBUG = False
10
+
11
+ CONFIG_ERROR = ("Your trace has not been initialised. "
12
+ "Call super__init__(debug=True) in class initialiser"
13
+ "or omit debug for default of no trace.")
14
+
15
+
16
+ ## single rich console - module level.
17
+ _console = Console()
18
+
19
+ class Traceable(ABC):
20
+ '''Provides basic trace functionality for a process to subclass
21
+
22
+ Abstract base class Traceable
23
+
24
+ Subclasses must call
25
+
26
+ super().__init__(debug=True) in their __init__() method to
27
+ initialise trace.
28
+
29
+ Subclasses inherit the following methods:
30
+
31
+ trace() - use this function print out a traceable event
32
+
33
+ _trace_config(): use this function to return a dict containing
34
+ the trace configuration for the class.
35
+ '''
36
+ def __init__(self, debug=DEFAULT_DEBUG):
37
+ self.debug = debug
38
+ self._config = self._default_config()
39
+
40
+ def _default_config(self):
41
+ """Returns a default trace configuration"""
42
+ config = {
43
+ "name":None,
44
+ "name_colour":"bold blue",
45
+ "time_colour":'bold blue',
46
+ "time_dp":2,
47
+ "message_colour":'black',
48
+ "tracked":None
49
+ }
50
+ return config
51
+
52
+
53
+ def _trace_config(self):
54
+ config = {
55
+ "name":None,
56
+ "name_colour":"bold blue",
57
+ "time_colour":'bold blue',
58
+ "time_dp":2,
59
+ "message_colour":'black',
60
+ "tracked":None
61
+ }
62
+ return config
63
+
64
+
65
+ def trace(self, time, msg=None, process_id=None):
66
+ '''
67
+ Display a trace of an event
68
+ '''
69
+
70
+ if not hasattr(self, '_config'):
71
+ raise AttributeError(CONFIG_ERROR)
72
+
73
+ # if in debug mode
74
+ if self.debug:
75
+
76
+ # check for override to default configs
77
+ process_config = self._trace_config()
78
+ self._config.update(process_config)
79
+
80
+ # conditional logic to limit tracking to specific processes/entities
81
+ if self._config['tracked'] is None or process_id in self._config['tracked']:
82
+
83
+ # display and format time stamp
84
+ out = f"[{self._config['time_colour']}][{time:.{self._config['time_dp']}f}]:[/{self._config['time_colour']}]"
85
+
86
+ # if provided display and format a process ID
87
+ if self._config['name'] is not None and process_id is not None:
88
+ out += f"[{self._config['name_colour']}]<{self._config['name']} {process_id}>: [/{self._config['name_colour']}]"
89
+
90
+ # format traced event message
91
+ out += f"[{self._config['message_colour']}]{msg}[/{self._config['message_colour']}]"
92
+
93
+ # print to rich console
94
+ _console.print(out)
95
+
@@ -1,4 +0,0 @@
1
- __version__ = '0.4.0'
2
- __author__ = 'Thomas Monks'
3
-
4
- from . import datasets, distributions, time_dependent, ovs
File without changes
File without changes
File without changes