changeset 1088:0680387a89bb

added basic saving capability to empirical distribution
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 14 Nov 2018 12:30:50 -0500
parents 9cc51a2d3c46
children 10205bd0e0b7
files trafficintelligence/utils.py
diffstat 1 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/trafficintelligence/utils.py	Wed Oct 31 16:25:10 2018 -0400
+++ b/trafficintelligence/utils.py	Wed Nov 14 12:30:50 2018 -0500
@@ -144,7 +144,19 @@
         super(EmpiricalContinuousDistribution, self).__init__(**kwargs)
         self.values = values
         self.probabilities = probabilities
-    
+
+    def save(self, filename):
+        import yaml
+        with open(filename, 'w') as out:
+            yaml.dump([self.values, self.probabilities], out)
+
+    @staticmethod
+    def load(filename):
+        import yaml
+        with open(filename) as f:
+            values, probabilities = yaml.load(f)
+            return EmpiricalContinuousDistribution(values, probabilities)
+
     def _cdf(self, x):
         if x < self.values[0]:
             return self.probabilities[0]