Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/misc.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file misc.py15# @author Joerg Schweizer16# @date 2012171819import platform20import types21import numpy as np22import time2324# default file path priming25# this did depend on operating system, now " for all26P = '"'27# if platform.system()=='Windows':28if platform.system() == 'Linux':29IS_LINUX = True30else:31IS_LINUX = False323334def string_to_float(s):35"""Returns a float from a string while cleaning it"""36q = ''37is_sample = False38for c in s:39if c.isdigit():40q += c41elif c == '.':42q += c43else:44break45return float(q)464748def get_inversemap(m):49return {v: k for k, v in m.items()}505152def random_choice_dist2(n, b):53"""54Returns the absolute distribution of a random choice sample of size n55having the choice between len(b) options where each option has56the probability represented in vector b.57"""585960def random_choice_dist(n, b):61"""62Returns the absolute distribution of a random choice sample of size n63having the choice between len(b) options where each option has64the probability represented in vector b.65"""66if np.__version__ >= '1.7.0':67return np.bincount(np.random.choice(b.size, n, p=b.flat),68minlength=b.size).reshape(b.shape)69else:70return np.bincount(np.searchsorted(np.cumsum(b), np.random.random(n)), minlength=b.size).reshape(b.shape)717273def random_choice(n, b):74"""75Returns the random choice sample of size n76having the choice between len(b) options where each option has77the probability represented in vector b.78"""79# print 'misc.random_choice'80if np.__version__ >= '1.7.0':81# print ' b',b.size,b,b.flat82# print ' n',n83return np.clip(np.random.choice(b.size, n, p=b.flat), 0, len(b)-1)84else:85return np.clip(np.searchsorted(np.cumsum(b), np.random.random(n)), 0, len(b)-1)8687# def random_choice1d(n, b):88# """89# Returns the random choice sample of size n90# having the choice between len(b) options where each option has91# the probability represented in vector b.92# """93#94# return np.argmax(np.random.rand(n)*b.flat)959697def get_seconds_from_timestr(t_data, t_offset=None,98sep_date_clock=' ', sep_date='-', sep_clock=':',99is_float=True):100"""101Returns time in seconds after t_offset.102If no offset is geven, the year 1970 is used.103Time string format:1042012-05-02 12:57:08.0105"""106if t_offset is None:107t_offset = time.mktime((1970, 1, 1, 0, 0, 0, 0, 0, 0)) # year 2000108109if len(t_data.split(sep_date_clock)) != 2:110return None111(date, clock) = t_data.split(sep_date_clock)112113if (len(clock.split(sep_clock)) == 3) & (len(date.split(sep_date)) == 3):114(day_str, month_str, year_str) = date.split(sep_date)115(hours_str, minutes_str, seconds_str) = clock.split(sep_clock)116t = time.mktime((int(year_str), int(month_str), int(day_str),117int(hours_str), int(minutes_str), int(float(seconds_str)), 0, 0, 0))-t_offset118if is_float:119return float(t)120else:121return int(t)122else:123return None124125126def format_filepath(filepath):127return ff(filepath)128129130def ff(filepath):131return P+filepath+P132133134def filepathlist_to_filepathstring(filepathlist, sep=','):135if type(filepathlist) == types.ListType:136if len(filepathlist) == 0:137return ''138else:139filepathstring = ''140for filepath in filepathlist[:-1]:141fp = filepath.replace('"', '')142filepathstring += P+fp+P+sep143filepathstring += P+filepathlist[-1]+P144return filepathstring145else:146fp = filepathlist.replace('"', '')147return P+filepathlist+P148149150def filepathstring_to_filepathlist(filepathstring, sep=','):151filepaths = []152for filepath in filepathstring.split(sep):153filepaths.append(P+filepath.strip().replace('"', '')+P)154return filepaths155156157def dict_to_str(d, intend=0):158s = ''159for key, value in d.iteritems():160s += intend*" "+"%s: %s\n" % (key, value)161162return s163164165