Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/misc.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file misc.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
20
import platform
21
import types
22
import numpy as np
23
import time
24
25
# default file path priming
26
# this did depend on operating system, now " for all
27
P = '"'
28
# if platform.system()=='Windows':
29
if platform.system() == 'Linux':
30
IS_LINUX = True
31
else:
32
IS_LINUX = False
33
34
35
def string_to_float(s):
36
"""Returns a float from a string while cleaning it"""
37
q = ''
38
is_sample = False
39
for c in s:
40
if c.isdigit():
41
q += c
42
elif c == '.':
43
q += c
44
else:
45
break
46
return float(q)
47
48
49
def get_inversemap(m):
50
return {v: k for k, v in m.items()}
51
52
53
def random_choice_dist2(n, b):
54
"""
55
Returns the absolute distribution of a random choice sample of size n
56
having the choice between len(b) options where each option has
57
the probability represented in vector b.
58
"""
59
60
61
def random_choice_dist(n, b):
62
"""
63
Returns the absolute distribution of a random choice sample of size n
64
having the choice between len(b) options where each option has
65
the probability represented in vector b.
66
"""
67
if np.__version__ >= '1.7.0':
68
return np.bincount(np.random.choice(b.size, n, p=b.flat),
69
minlength=b.size).reshape(b.shape)
70
else:
71
return np.bincount(np.searchsorted(np.cumsum(b), np.random.random(n)), minlength=b.size).reshape(b.shape)
72
73
74
def random_choice(n, b):
75
"""
76
Returns the random choice sample of size n
77
having the choice between len(b) options where each option has
78
the probability represented in vector b.
79
"""
80
# print 'misc.random_choice'
81
if np.__version__ >= '1.7.0':
82
# print ' b',b.size,b,b.flat
83
# print ' n',n
84
return np.clip(np.random.choice(b.size, n, p=b.flat), 0, len(b)-1)
85
else:
86
return np.clip(np.searchsorted(np.cumsum(b), np.random.random(n)), 0, len(b)-1)
87
88
# def random_choice1d(n, b):
89
# """
90
# Returns the random choice sample of size n
91
# having the choice between len(b) options where each option has
92
# the probability represented in vector b.
93
# """
94
#
95
# return np.argmax(np.random.rand(n)*b.flat)
96
97
98
def get_seconds_from_timestr(t_data, t_offset=None,
99
sep_date_clock=' ', sep_date='-', sep_clock=':',
100
is_float=True):
101
"""
102
Returns time in seconds after t_offset.
103
If no offset is geven, the year 1970 is used.
104
Time string format:
105
2012-05-02 12:57:08.0
106
"""
107
if t_offset is None:
108
t_offset = time.mktime((1970, 1, 1, 0, 0, 0, 0, 0, 0)) # year 2000
109
110
if len(t_data.split(sep_date_clock)) != 2:
111
return None
112
(date, clock) = t_data.split(sep_date_clock)
113
114
if (len(clock.split(sep_clock)) == 3) & (len(date.split(sep_date)) == 3):
115
(day_str, month_str, year_str) = date.split(sep_date)
116
(hours_str, minutes_str, seconds_str) = clock.split(sep_clock)
117
t = time.mktime((int(year_str), int(month_str), int(day_str),
118
int(hours_str), int(minutes_str), int(float(seconds_str)), 0, 0, 0))-t_offset
119
if is_float:
120
return float(t)
121
else:
122
return int(t)
123
else:
124
return None
125
126
127
def format_filepath(filepath):
128
return ff(filepath)
129
130
131
def ff(filepath):
132
return P+filepath+P
133
134
135
def filepathlist_to_filepathstring(filepathlist, sep=','):
136
if type(filepathlist) == types.ListType:
137
if len(filepathlist) == 0:
138
return ''
139
else:
140
filepathstring = ''
141
for filepath in filepathlist[:-1]:
142
fp = filepath.replace('"', '')
143
filepathstring += P+fp+P+sep
144
filepathstring += P+filepathlist[-1]+P
145
return filepathstring
146
else:
147
fp = filepathlist.replace('"', '')
148
return P+filepathlist+P
149
150
151
def filepathstring_to_filepathlist(filepathstring, sep=','):
152
filepaths = []
153
for filepath in filepathstring.split(sep):
154
filepaths.append(P+filepath.strip().replace('"', '')+P)
155
return filepaths
156
157
158
def dict_to_str(d, intend=0):
159
s = ''
160
for key, value in d.iteritems():
161
s += intend*" "+"%s: %s\n" % (key, value)
162
163
return s
164
165