sim-tools 1.0.2__tar.gz → 1.0.4__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,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sim-tools
3
- Version: 1.0.2
3
+ Version: 1.0.4
4
4
  Summary: Simulation Tools for Education and Practice
5
- Project-URL: Homepage, https://github.com/TomMonks/sim-tools
6
- Project-URL: Bug Tracker, https://github.com/TomMonks/sim-tools/issues
7
- Project-URL: Documentation, https://tommonks.github.io/sim-tools
5
+ Project-URL: Homepage, https://github.com/sim-tools/sim-tools
6
+ Project-URL: Bug Tracker, https://github.com/sim-tools/sim-tools/issues
7
+ Project-URL: Documentation, https://sim-tools.github.io/sim-tools
8
8
  Author-email: Thomas Monks <t.m.w.monks@exeter.ac.uk>, Amy Heather <A.Heather2@exeter.ac.uk>, Alison Harper <A.L.Harper@exeter.ac.uk>
9
9
  License-Expression: MIT
10
10
  License-File: LICENSE
@@ -36,7 +36,7 @@ Description-Content-Type: text/markdown
36
36
  [![PyPI version fury.io](https://badge.fury.io/py/sim-tools.svg)](https://pypi.python.org/pypi/sim-tools/)
37
37
  [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/version.svg)](https://anaconda.org/conda-forge/sim-tools)
38
38
  [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/platforms.svg)](https://anaconda.org/conda-forge/sim-tools)
39
- [![Read the Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://tommonks.github.io/sim-tools)
39
+ [![Read the Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://sim-tools.github.io/sim-tools)
40
40
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
41
41
  [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/release/python-360+/)
42
42
 
@@ -89,12 +89,12 @@ mamba install sim-tools
89
89
 
90
90
  ### Binder
91
91
 
92
- [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TomMonks/sim-tools/HEAD)
92
+ [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/sim-tools/sim-tools/HEAD)
93
93
 
94
94
 
95
95
  ## Learn how to use `sim-tools`
96
96
 
97
- * Online documentation: https://tommonks.github.io/sim-tools
97
+ * Online documentation: https://sim-tools.github.io/sim-tools
98
98
  * Introduction to DES in python: https://health-data-science-or.github.io/simpy-streamlit-tutorial/
99
99
 
100
100
  ## Citation
@@ -116,7 +116,7 @@ If you use sim-tools for research, a practical report, education or any reason p
116
116
 
117
117
  # Online Tutorials
118
118
 
119
- * Optimisation Via Simulation [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/TomMonks/sim-tools/blob/master/docs/02_ovs/03_sw21_tutorial.ipynb)
119
+ * Optimisation Via Simulation [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/sim-tools/sim-tools/blob/master/docs/02_ovs/03_sw21_tutorial.ipynb)
120
120
 
121
121
 
122
122
  ## Contributing to sim-tools
@@ -151,4 +151,4 @@ To lint the repository, run:
151
151
  bash lint.sh
152
152
  ```
153
153
 
154
- NumPy style docstrings are used.
154
+ NumPy style docstrings are used.
@@ -11,7 +11,7 @@
11
11
  [![PyPI version fury.io](https://badge.fury.io/py/sim-tools.svg)](https://pypi.python.org/pypi/sim-tools/)
12
12
  [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/version.svg)](https://anaconda.org/conda-forge/sim-tools)
13
13
  [![Anaconda-Server Badge](https://anaconda.org/conda-forge/sim-tools/badges/platforms.svg)](https://anaconda.org/conda-forge/sim-tools)
14
- [![Read the Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://tommonks.github.io/sim-tools)
14
+ [![Read the Docs](https://readthedocs.org/projects/pip/badge/?version=latest)](https://sim-tools.github.io/sim-tools)
15
15
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
16
16
  [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/release/python-360+/)
17
17
 
@@ -64,12 +64,12 @@ mamba install sim-tools
64
64
 
65
65
  ### Binder
66
66
 
67
- [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TomMonks/sim-tools/HEAD)
67
+ [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/sim-tools/sim-tools/HEAD)
68
68
 
69
69
 
70
70
  ## Learn how to use `sim-tools`
71
71
 
72
- * Online documentation: https://tommonks.github.io/sim-tools
72
+ * Online documentation: https://sim-tools.github.io/sim-tools
73
73
  * Introduction to DES in python: https://health-data-science-or.github.io/simpy-streamlit-tutorial/
74
74
 
75
75
  ## Citation
@@ -91,7 +91,7 @@ If you use sim-tools for research, a practical report, education or any reason p
91
91
 
92
92
  # Online Tutorials
93
93
 
94
- * Optimisation Via Simulation [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/TomMonks/sim-tools/blob/master/docs/02_ovs/03_sw21_tutorial.ipynb)
94
+ * Optimisation Via Simulation [![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/sim-tools/sim-tools/blob/master/docs/02_ovs/03_sw21_tutorial.ipynb)
95
95
 
96
96
 
97
97
  ## Contributing to sim-tools
@@ -126,4 +126,4 @@ To lint the repository, run:
126
126
  bash lint.sh
127
127
  ```
128
128
 
129
- NumPy style docstrings are used.
129
+ NumPy style docstrings are used.
@@ -33,9 +33,9 @@ dependencies = [
33
33
  ]
34
34
 
35
35
  [project.urls]
36
- Homepage = "https://github.com/TomMonks/sim-tools"
37
- "Bug Tracker" = "https://github.com/TomMonks/sim-tools/issues"
38
- Documentation = "https://tommonks.github.io/sim-tools"
36
+ Homepage = "https://github.com/sim-tools/sim-tools"
37
+ "Bug Tracker" = "https://github.com/sim-tools/sim-tools/issues"
38
+ Documentation = "https://sim-tools.github.io/sim-tools"
39
39
 
40
40
  [tool.hatch.version]
41
41
  path = "sim_tools/__init__.py"
@@ -1,5 +1,5 @@
1
1
  """sim-tools"""
2
2
 
3
- __version__ = "1.0.2"
3
+ __version__ = "1.0.4"
4
4
 
5
5
  from . import datasets, distributions, time_dependent, ovs, output_analysis
@@ -8,7 +8,7 @@ import numpy as np
8
8
  from numpy.random import SeedSequence
9
9
  import pandas as pd
10
10
  import matplotlib.pyplot as plt
11
-
11
+ import itertools
12
12
 
13
13
  # pylint: disable=too-few-public-methods
14
14
  class NSPPThinning:
@@ -33,7 +33,7 @@ class NSPPThinning:
33
33
  """
34
34
  Non Stationary Poisson Process via Thinning.
35
35
 
36
- Time dependency is andled for a single table
36
+ Time dependency is handled for a single table
37
37
  consisting of equally spaced intervals.
38
38
 
39
39
  Parameters
@@ -54,12 +54,37 @@ class NSPPThinning:
54
54
  Random seed for the uniform distribution used
55
55
  for acceptance/rejection sampling.
56
56
  """
57
+
58
+ # -- Defensive parameter check added by TM as part of fix v1.0.4 --
59
+
60
+ # data must be a dataframe
61
+ if not isinstance(data, pd.DataFrame):
62
+ raise TypeError(f"Data should be a DataFrame, but got {type(data).__name__}")
63
+
64
+ # Check required columns t and mean_iat are provided.
65
+ required_cols = ["t", "mean_iat"]
66
+ missing_cols = [col for col in required_cols if col not in data.columns]
67
+ if missing_cols:
68
+ raise ValueError(f"Missing required columns: {missing_cols}")
69
+
70
+ # empty dataframe or only a single row.
71
+ if data.empty:
72
+ raise ValueError(f"Dataframe does not contain any rows. Add at least 2.")
73
+ elif len(data) == 1:
74
+ msg = "Dataframe contains a single time point. Add more of use Exponential class."
75
+ raise ValueError(msg)
76
+
77
+ # -- end of defensive code --
78
+
57
79
  self.data = data
58
80
  self.arr_rng = np.random.default_rng(random_seed1)
59
81
  self.thinning_rng = np.random.default_rng(random_seed2)
60
82
 
61
- # Find the minimum mean IAT (corresponds to the maximum arrival rate)
62
- self.min_iat = data["mean_iat"].min()
83
+ # drop to numpy for lookup speed
84
+ self.mean_iats = data["mean_iat"].to_numpy(dtype=float)
85
+
86
+ # Get the minimum mean IAT (corresponds to the maximum arrival rate)
87
+ self.min_iat = self.mean_iats.min()
63
88
 
64
89
  if self.min_iat <= 0:
65
90
  raise ValueError("Mean inter-arrival times must be positive")
@@ -75,6 +100,10 @@ class NSPPThinning:
75
100
  "With only one data point, interval_width must be provided"
76
101
  )
77
102
 
103
+ # Pre-compute the acceptance probabilities. Added by TM v1.0.4
104
+ self.accept_prob = self.min_iat / self.mean_iats
105
+ self.n_periods = self.mean_iats.size
106
+
78
107
  self.rejects_last_sample = None
79
108
 
80
109
  def __repr__(self):
@@ -90,6 +119,7 @@ class NSPPThinning:
90
119
  f"{self.__class__.__name__}(data={data_str}, " +
91
120
  f"interval={self.interval})"
92
121
  )
122
+
93
123
 
94
124
  def sample(self, simulation_time: float) -> float:
95
125
  """
@@ -99,36 +129,30 @@ class NSPPThinning:
99
129
  Parameters
100
130
  ----------
101
131
  simulation_time: float
102
- The current simulation time. This is used to look up
103
- the mean IAT for the time period.
132
+ The current simulation time.
104
133
 
105
134
  Returns
106
135
  -------
107
136
  float
108
137
  The inter-arrival time
109
138
  """
110
-
111
- # this gives us the index of dataframe to use
112
- t = int(simulation_time // self.interval) % len(self.data)
113
- mean_iat_t = self.data["mean_iat"].iloc[t]
114
-
115
- # set to a large number so that at least 1 sample taken!
116
- u = np.inf
117
-
118
- # included for audit and tracking purposes.
119
- self.rejects_last_sample = 0
120
-
139
+
121
140
  interarrival_time = 0.0
122
-
123
- # We accept the sample if u < (min_iat / mean_iat_t)
124
- # This is equivalent to the original u < (lambda_t / lambda_max)
125
- # since lambda = 1/mean_iat
126
- while u >= (self.min_iat / mean_iat_t):
127
- self.rejects_last_sample += 1
141
+
142
+ for self.rejects_last_sample in itertools.count(start=0):
143
+ # sample the next inter-arrival time and calculate clock time
128
144
  interarrival_time += self.arr_rng.exponential(self.min_iat)
129
- u = self.thinning_rng.uniform(0.0, 1.0)
145
+ arrival_sim_clock_time = simulation_time + interarrival_time
146
+
147
+ # get the t index of the candidate arrival
148
+ # fix v1.0.4 to use 'candidate_arrival_time' Fix by Sammi Rosser
149
+ t_idx = int(arrival_sim_clock_time // self.interval) % self.n_periods
130
150
 
131
- return interarrival_time
151
+ # accept or reject? TM modified v1.0.4 to use pre-computed probabilities
152
+ u = self.thinning_rng.uniform()
153
+ if u <= self.accept_prob[t_idx]:
154
+ # accepted so return inter-arrival time
155
+ return interarrival_time
132
156
 
133
157
 
134
158
  def nspp_simulation(
File without changes
File without changes
File without changes