Spaces:
Runtime error
Runtime error
File size: 47,688 Bytes
4610f7a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 |
#!/usr/local/bin/python3
# Author: Pranab Ghosh
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
import os
import sys
from random import randint
import random
import time
import uuid
from datetime import datetime
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import logging
import logging.handlers
import pickle
from contextlib import contextmanager
tokens = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
numTokens = tokens[:10]
alphaTokens = tokens[10:36]
loCaseChars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l","m","n","o",
"p","q","r","s","t","u","v","w","x","y","z"]
typeInt = "int"
typeFloat = "float"
typeString = "string"
secInMinute = 60
secInHour = 60 * 60
secInDay = 24 * secInHour
secInWeek = 7 * secInDay
secInYear = 365 * secInDay
secInMonth = secInYear / 12
minInHour = 60
minInDay = 24 * minInHour
ftPerYard = 3
ftPerMile = ftPerYard * 1760
def genID(size):
"""
generates ID
Parameters
size : size of ID
"""
id = ""
for i in range(size):
id = id + selectRandomFromList(tokens)
return id
def genIdList(numId, idSize):
"""
generate list of IDs
Parameters:
numId: number of Ids
idSize: ID size
"""
iDs = []
for i in range(numId):
iDs.append(genID(idSize))
return iDs
def genNumID(size):
"""
generates ID consisting of digits onl
Parameters
size : size of ID
"""
id = ""
for i in range(size):
id = id + selectRandomFromList(numTokens)
return id
def genLowCaseID(size):
"""
generates ID consisting of lower case chars
Parameters
size : size of ID
"""
id = ""
for i in range(size):
id = id + selectRandomFromList(loCaseChars)
return id
def genNumIdList(numId, idSize):
"""
generate list of numeric IDs
Parameters:
numId: number of Ids
idSize: ID size
"""
iDs = []
for i in range(numId):
iDs.append(genNumID(idSize))
return iDs
def genNameInitial():
"""
generate name initial
"""
return selectRandomFromList(alphaTokens) + selectRandomFromList(alphaTokens)
def genPhoneNum(arCode):
"""
generates phone number
Parameters
arCode: area code
"""
phNum = genNumID(7)
return arCode + str(phNum)
def selectRandomFromList(ldata):
"""
select an element randomly from a lis
Parameters
ldata : list data
"""
return ldata[randint(0, len(ldata)-1)]
def selectOtherRandomFromList(ldata, cval):
"""
select an element randomly from a list excluding the given one
Parameters
ldata : list data
cval : value to be excluded
"""
nval = selectRandomFromList(ldata)
while nval == cval:
nval = selectRandomFromList(ldata)
return nval
def selectRandomSubListFromList(ldata, num):
"""
generates random sublist from a list without replacemment
Parameters
ldata : list data
num : output list size
"""
assertLesser(num, len(ldata), "size of sublist to be sampled greater than or equal to main list")
i = randint(0, len(ldata)-1)
sel = ldata[i]
selSet = {i}
selList = [sel]
while (len(selSet) < num):
i = randint(0, len(ldata)-1)
if (i not in selSet):
sel = ldata[i]
selSet.add(i)
selList.append(sel)
return selList
def selectRandomSubListFromListWithRepl(ldata, num):
"""
generates random sublist from a list with replacemment
Parameters
ldata : list data
num : output list size
"""
return list(map(lambda i : selectRandomFromList(ldata), range(num)))
def selectRandomFromDict(ddata):
"""
select an element randomly from a dictionary
Parameters
ddata : dictionary data
"""
dkeys = list(ddata.keys())
dk = selectRandomFromList(dkeys)
el = (dk, ddata[dk])
return el
def setListRandomFromList(ldata, ldataRepl):
"""
sets some elents in the first list randomly with elements from the second list
Parameters
ldata : list data
ldataRepl : list with replacement data
"""
l = len(ldata)
selSet = set()
for d in ldataRepl:
i = randint(0, l-1)
while i in selSet:
i = randint(0, l-1)
ldata[i] = d
selSet.add(i)
def genIpAddress():
"""
generates IP address
"""
i1 = randint(0,256)
i2 = randint(0,256)
i3 = randint(0,256)
i4 = randint(0,256)
ip = "%d.%d.%d.%d" %(i1,i2,i3,i4)
return ip
def curTimeMs():
"""
current time in ms
"""
return int((datetime.utcnow() - datetime(1970,1,1)).total_seconds() * 1000)
def secDegPolyFit(x1, y1, x2, y2, x3, y3):
"""
second deg polynomial
Parameters
x1 : 1st point x
y1 : 1st point y
x2 : 2nd point x
y2 : 2nd point y
x3 : 3rd point x
y3 : 3rd point y
"""
t = (y1 - y2) / (x1 - x2)
a = t - (y2 - y3) / (x2 - x3)
a = a / (x1 - x3)
b = t - a * (x1 + x2)
c = y1 - a * x1 * x1 - b * x1
return (a, b, c)
def range_limit(val, minv, maxv):
"""
range limit a value
Parameters
val : data value
minv : minimum
maxv : maximum
"""
if (val < minv):
val = minv
elif (val > maxv):
val = maxv
return val
def rangeLimit(val, minv, maxv):
"""
range limit a value
Parameters
val : data value
minv : minimum
maxv : maximum
"""
return range_limit(val, minv, maxv)
def isInRange(val, minv, maxv):
"""
checks if within range
Parameters
val : data value
minv : minimum
maxv : maximum
"""
return val >= minv and val <= maxv
def stripFileLines(filePath, offset):
"""
strips number of chars from both ends
Parameters
filePath : file path
offset : offset from both ends of line
"""
fp = open(filePath, "r")
for line in fp:
stripped = line[offset:len(line) - 1 - offset]
print (stripped)
fp.close()
def genLatLong(lat1, long1, lat2, long2):
"""
generate lat log within limits
Parameters
lat1 : lat of 1st point
long1 : long of 1st point
lat2 : lat of 2nd point
long2 : long of 2nd point
"""
lat = lat1 + (lat2 - lat1) * random.random()
longg = long1 + (long2 - long1) * random.random()
return (lat, longg)
def geoDistance(lat1, long1, lat2, long2):
"""
find geo distance in ft
Parameters
lat1 : lat of 1st point
long1 : long of 1st point
lat2 : lat of 2nd point
long2 : long of 2nd point
"""
latDiff = math.radians(lat1 - lat2)
longDiff = math.radians(long1 - long2)
l1 = math.sin(latDiff/2.0)
l2 = math.sin(longDiff/2.0)
l3 = math.cos(math.radians(lat1))
l4 = math.cos(math.radians(lat2))
a = l1 * l1 + l3 * l4 * l2 * l2
l5 = math.sqrt(a)
l6 = math.sqrt(1.0 - a)
c = 2.0 * math.atan2(l5, l6)
r = 6371008.8 * 3.280840
return c * r
def minLimit(val, limit):
"""
min limit
Parameters
"""
if (val < limit):
val = limit
return val;
def maxLimit(val, limit):
"""
max limit
Parameters
"""
if (val > limit):
val = limit
return val;
def rangeSample(val, minLim, maxLim):
"""
if out side range sample within range
Parameters
val : value
minLim : minimum
maxLim : maximum
"""
if val < minLim or val > maxLim:
val = randint(minLim, maxLim)
return val
def genRandomIntListWithinRange(size, minLim, maxLim):
"""
random unique list of integers within range
Parameters
size : size of returned list
minLim : minimum
maxLim : maximum
"""
values = set()
for i in range(size):
val = randint(minLim, maxLim)
while val not in values:
values.add(val)
return list(values)
def preturbScalar(value, vrange, distr="uniform"):
"""
preturbs a mutiplicative value within range
Parameters
value : data value
vrange : value delta fraction
distr : noise distribution type
"""
if distr == "uniform":
scale = 1.0 - vrange + 2 * vrange * random.random()
elif distr == "normal":
scale = 1.0 + np.random.normal(0, vrange)
else:
exisWithMsg("unknown noise distr " + distr)
return value * scale
def preturbScalarAbs(value, vrange):
"""
preturbs an absolute value within range
Parameters
value : data value
vrange : value delta absolute
"""
delta = - vrange + 2.0 * vrange * random.random()
return value + delta
def preturbVector(values, vrange):
"""
preturbs a list within range
Parameters
values : list data
vrange : value delta fraction
"""
nValues = list(map(lambda va: preturbScalar(va, vrange), values))
return nValues
def randomShiftVector(values, smin, smax):
"""
shifts a list by a random quanity with a range
Parameters
values : list data
smin : samplinf minimum
smax : sampling maximum
"""
shift = np.random.uniform(smin, smax)
return list(map(lambda va: va + shift, values))
def floatRange(beg, end, incr):
"""
generates float range
Parameters
beg :range begin
end: range end
incr : range increment
"""
return list(np.arange(beg, end, incr))
def shuffle(values, *numShuffles):
"""
in place shuffling with swap of pairs
Parameters
values : list data
numShuffles : parameter list for number of shuffles
"""
size = len(values)
if len(numShuffles) == 0:
numShuffle = int(size / 2)
elif len(numShuffles) == 1:
numShuffle = numShuffles[0]
else:
numShuffle = randint(numShuffles[0], numShuffles[1])
print("numShuffle {}".format(numShuffle))
for i in range(numShuffle):
first = random.randint(0, size - 1)
second = random.randint(0, size - 1)
while first == second:
second = random.randint(0, size - 1)
tmp = values[first]
values[first] = values[second]
values[second] = tmp
def splitList(itms, numGr):
"""
splits a list into sub lists of approximately equal size, with items in sublists randomly chod=sen
Parameters
itms ; list of values
numGr : no of groups
"""
tcount = len(itms)
cItems = list(itms)
sz = int(len(cItems) / numGr)
groups = list()
count = 0
for i in range(numGr):
if (i == numGr - 1):
csz = tcount - count
else:
csz = sz + randint(-2, 2)
count += csz
gr = list()
for j in range(csz):
it = selectRandomFromList(cItems)
gr.append(it)
cItems.remove(it)
groups.append(gr)
return groups
def multVector(values, vrange):
"""
multiplies a list within value range
Parameters
values : list of values
vrange : fraction of vaue to be used to update
"""
scale = 1.0 - vrange + 2 * vrange * random.random()
nValues = list(map(lambda va: va * scale, values))
return nValues
def weightedAverage(values, weights):
"""
calculates weighted average
Parameters
values : list of values
weights : list of weights
"""
assert len(values) == len(weights), "values and weights should be same size"
vw = zip(values, weights)
wva = list(map(lambda e : e[0] * e[1], vw))
#wa = sum(x * y for x, y in vw) / sum(weights)
wav = sum(wva) / sum(weights)
return wav
def extractFields(line, delim, keepIndices):
"""
breaks a line into fields and keeps only specified fileds and returns new line
Parameters
line ; deli separated string
delim : delemeter
keepIndices : list of indexes to fields to be retained
"""
items = line.split(delim)
newLine = []
for i in keepIndices:
newLine.append(line[i])
return delim.join(newLine)
def remFields(line, delim, remIndices):
"""
removes fields from delim separated string
Parameters
line ; delemeter separated string
delim : delemeter
remIndices : list of indexes to fields to be removed
"""
items = line.split(delim)
newLine = []
for i in range(len(items)):
if not arrayContains(remIndices, i):
newLine.append(line[i])
return delim.join(newLine)
def extractList(data, indices):
"""
extracts list from another list, given indices
Parameters
remIndices : list data
indices : list of indexes to fields to be retained
"""
if areAllFieldsIncluded(data, indices):
exList = data.copy()
#print("all indices")
else:
exList = list()
le = len(data)
for i in indices:
assert i < le , "index {} out of bound {}".format(i, le)
exList.append(data[i])
return exList
def arrayContains(arr, item):
"""
checks if array contains an item
Parameters
arr : list data
item : item to search
"""
contains = True
try:
arr.index(item)
except ValueError:
contains = False
return contains
def strToIntArray(line, delim=","):
"""
int array from delim separated string
Parameters
line ; delemeter separated string
"""
arr = line.split(delim)
return [int(a) for a in arr]
def strToFloatArray(line, delim=","):
"""
float array from delim separated string
Parameters
line ; delemeter separated string
"""
arr = line.split(delim)
return [float(a) for a in arr]
def strListOrRangeToIntArray(line):
"""
int array from delim separated string or range
Parameters
line ; delemeter separated string
"""
varr = line.split(",")
if (len(varr) > 1):
iarr = list(map(lambda v: int(v), varr))
else:
vrange = line.split(":")
if (len(vrange) == 2):
lo = int(vrange[0])
hi = int(vrange[1])
iarr = list(range(lo, hi+1))
else:
iarr = [int(line)]
return iarr
def toStr(val, precision):
"""
converts any type to string
Parameters
val : value
precision ; precision for float value
"""
if type(val) == float or type(val) == np.float64 or type(val) == np.float32:
format = "%" + ".%df" %(precision)
sVal = format %(val)
else:
sVal = str(val)
return sVal
def toStrFromList(values, precision, delim=","):
"""
converts list of any type to delim separated string
Parameters
values : list data
precision ; precision for float value
delim : delemeter
"""
sValues = list(map(lambda v: toStr(v, precision), values))
return delim.join(sValues)
def toIntList(values):
"""
convert to int list
Parameters
values : list data
"""
return list(map(lambda va: int(va), values))
def toFloatList(values):
"""
convert to float list
Parameters
values : list data
"""
return list(map(lambda va: float(va), values))
def toStrList(values, precision=None):
"""
convert to string list
Parameters
values : list data
precision ; precision for float value
"""
return list(map(lambda va: toStr(va, precision), values))
def toIntFromBoolean(value):
"""
convert to int
Parameters
value : boolean value
"""
ival = 1 if value else 0
return ival
def scaleBySum(ldata):
"""
scales so that sum is 1
Parameters
ldata : list data
"""
s = sum(ldata)
return list(map(lambda e : e/s, ldata))
def scaleByMax(ldata):
"""
scales so that max value is 1
Parameters
ldata : list data
"""
m = max(ldata)
return list(map(lambda e : e/m, ldata))
def typedValue(val, dtype=None):
"""
return typed value given string, discovers data type if not specified
Parameters
val : value
dtype : data type
"""
tVal = None
if dtype is not None:
if dtype == "num":
dtype = "int" if dtype.find(".") == -1 else "float"
if dtype == "int":
tVal = int(val)
elif dtype == "float":
tVal = float(val)
elif dtype == "bool":
tVal = bool(val)
else:
tVal = val
else:
if type(val) == str:
lVal = val.lower()
#int
done = True
try:
tVal = int(val)
except ValueError:
done = False
#float
if not done:
done = True
try:
tVal = float(val)
except ValueError:
done = False
#boolean
if not done:
done = True
if lVal == "true":
tVal = True
elif lVal == "false":
tVal = False
else:
done = False
#None
if not done:
if lVal == "none":
tVal = None
else:
tVal = val
else:
tVal = val
return tVal
def isInt(val):
"""
return true if string is int and the typed value
Parameters
val : value
"""
valInt = True
try:
tVal = int(val)
except ValueError:
valInt = False
tVal = None
r = (valInt, tVal)
return r
def isFloat(val):
"""
return true if string is float
Parameters
val : value
"""
valFloat = True
try:
tVal = float(val)
except ValueError:
valFloat = False
tVal = None
r = (valFloat, tVal)
return r
def getAllFiles(dirPath):
"""
get all files recursively
Parameters
dirPath : directory path
"""
filePaths = []
for (thisDir, subDirs, fileNames) in os.walk(dirPath):
for fileName in fileNames:
filePaths.append(os.path.join(thisDir, fileName))
filePaths.sort()
return filePaths
def getFileContent(fpath, verbose=False):
"""
get file contents in directory
Parameters
fpath ; directory path
verbose : verbosity flag
"""
# dcument list
docComplete = []
filePaths = getAllFiles(fpath)
# read files
for filePath in filePaths:
if verbose:
print("next file " + filePath)
with open(filePath, 'r') as contentFile:
content = contentFile.read()
docComplete.append(content)
return (docComplete, filePaths)
def getOneFileContent(fpath):
"""
get one file contents
Parameters
fpath : file path
"""
with open(fpath, 'r') as contentFile:
docStr = contentFile.read()
return docStr
def getFileLines(dirPath, delim=","):
"""
get lines from a file
Parameters
dirPath : file path
delim : delemeter
"""
lines = list()
for li in fileRecGen(dirPath, delim):
lines.append(li)
return lines
def getFileSampleLines(dirPath, percen, delim=","):
"""
get sampled lines from a file
Parameters
dirPath : file path
percen : sampling percentage
delim : delemeter
"""
lines = list()
for li in fileRecGen(dirPath, delim):
if randint(0, 100) < percen:
lines.append(li)
return lines
def getFileColumnAsString(dirPath, index, delim=","):
"""
get string column from a file
Parameters
dirPath : file path
index : index
delim : delemeter
"""
fields = list()
for rec in fileRecGen(dirPath, delim):
fields.append(rec[index])
#print(fields)
return fields
def getFileColumnsAsString(dirPath, indexes, delim=","):
"""
get multiple string columns from a file
Parameters
dirPath : file path
indexes : indexes of columns
delim : delemeter
"""
nindex = len(indexes)
columns = list(map(lambda i : list(), range(nindex)))
for rec in fileRecGen(dirPath, delim):
for i in range(nindex):
columns[i].append(rec[indexes[i]])
return columns
def getFileColumnAsFloat(dirPath, index, delim=","):
"""
get float fileds from a file
Parameters
dirPath : file path
index : index
delim : delemeter
"""
#print("{} {}".format(dirPath, index))
fields = getFileColumnAsString(dirPath, index, delim)
return list(map(lambda v:float(v), fields))
def getFileColumnAsInt(dirPath, index, delim=","):
"""
get float fileds from a file
Parameters
dirPath : file path
index : index
delim : delemeter
"""
fields = getFileColumnAsString(dirPath, index, delim)
return list(map(lambda v:int(v), fields))
def getFileAsIntMatrix(dirPath, columns, delim=","):
"""
extracts int matrix from csv file given column indices with each row being concatenation of
extracted column values row size = num of columns
Parameters
dirPath : file path
columns : indexes of columns
delim : delemeter
"""
mat = list()
for rec in fileSelFieldsRecGen(dirPath, columns, delim):
mat.append(asIntList(rec))
return mat
def getFileAsFloatMatrix(dirPath, columns, delim=","):
"""
extracts float matrix from csv file given column indices with each row being concatenation of
extracted column values row size = num of columns
Parameters
dirPath : file path
columns : indexes of columns
delim : delemeter
"""
mat = list()
for rec in fileSelFieldsRecGen(dirPath, columns, delim):
mat.append(asFloatList(rec))
return mat
def getFileAsFloatColumn(dirPath):
"""
grt float list from a file with one float per row
Parameters
dirPath : file path
"""
flist = list()
for rec in fileRecGen(dirPath, None):
flist.append(float(rec))
return flist
def getFileAsFiltFloatMatrix(dirPath, filt, columns, delim=","):
"""
extracts float matrix from csv file given row filter and column indices with each row being
concatenation of extracted column values row size = num of columns
Parameters
dirPath : file path
columns : indexes of columns
filt : row filter lambda
delim : delemeter
"""
mat = list()
for rec in fileFiltSelFieldsRecGen(dirPath, filt, columns, delim):
mat.append(asFloatList(rec))
return mat
def getFileAsTypedRecords(dirPath, types, delim=","):
"""
extracts typed records from csv file with each row being concatenation of
extracted column values
Parameters
dirPath : file path
types : data types
delim : delemeter
"""
(dtypes, cvalues) = extractTypesFromString(types)
tdata = list()
for rec in fileRecGen(dirPath, delim):
trec = list()
for index, value in enumerate(rec):
value = __convToTyped(index, value, dtypes)
trec.append(value)
tdata.append(trec)
return tdata
def getFileColsAsTypedRecords(dirPath, columns, types, delim=","):
"""
extracts typed records from csv file given column indices with each row being concatenation of
extracted column values
Parameters
Parameters
dirPath : file path
columns : column indexes
types : data types
delim : delemeter
"""
(dtypes, cvalues) = extractTypesFromString(types)
tdata = list()
for rec in fileSelFieldsRecGen(dirPath, columns, delim):
trec = list()
for indx, value in enumerate(rec):
tindx = columns[indx]
value = __convToTyped(tindx, value, dtypes)
trec.append(value)
tdata.append(trec)
return tdata
def getFileColumnsMinMax(dirPath, columns, dtype, delim=","):
"""
extracts numeric matrix from csv file given column indices. For each column return min and max
Parameters
dirPath : file path
columns : column indexes
dtype : data type
delim : delemeter
"""
dtypes = list(map(lambda c : str(c) + ":" + dtype, columns))
dtypes = ",".join(dtypes)
#print(dtypes)
tdata = getFileColsAsTypedRecords(dirPath, columns, dtypes, delim)
minMax = list()
ncola = len(tdata[0])
ncole = len(columns)
assertEqual(ncola, ncole, "actual no of columns different from expected")
for ci in range(ncole):
vmin = sys.float_info.max
vmax = sys.float_info.min
for r in tdata:
cv = r[ci]
vmin = cv if cv < vmin else vmin
vmax = cv if cv > vmax else vmax
mm = (vmin, vmax, vmax - vmin)
minMax.append(mm)
return minMax
def getRecAsTypedRecord(rec, types, delim=None):
"""
converts record to typed records
Parameters
rec : delemeter separate string or list of string
types : field data types
delim : delemeter
"""
if delim is not None:
rec = rec.split(delim)
(dtypes, cvalues) = extractTypesFromString(types)
#print(types)
#print(dtypes)
trec = list()
for ind, value in enumerate(rec):
tvalue = __convToTyped(ind, value, dtypes)
trec.append(tvalue)
return trec
def __convToTyped(index, value, dtypes):
"""
convert to typed value
Parameters
index : index in type list
value : data value
dtypes : data type list
"""
#print(index, value)
dtype = dtypes[index]
tvalue = value
if dtype == "int":
tvalue = int(value)
elif dtype == "float":
tvalue = float(value)
return tvalue
def extractTypesFromString(types):
"""
extracts column data types and set values for categorical variables
Parameters
types : encoded type information
"""
ftypes = types.split(",")
dtypes = dict()
cvalues = dict()
for ftype in ftypes:
items = ftype.split(":")
cindex = int(items[0])
dtype = items[1]
dtypes[cindex] = dtype
if len(items) == 3:
sitems = items[2].split()
cvalues[cindex] = sitems
return (dtypes, cvalues)
def getMultipleFileAsInttMatrix(dirPathWithCol, delim=","):
"""
extracts int matrix from from csv files given column index for each file.
num of columns = number of rows in each file and num of rows = number of files
Parameters
dirPathWithCol: list of file path and collumn index pair
delim : delemeter
"""
mat = list()
minLen = -1
for path, col in dirPathWithCol:
colVals = getFileColumnAsInt(path, col, delim)
if minLen < 0 or len(colVals) < minLen:
minLen = len(colVals)
mat.append(colVals)
#make all same length
mat = list(map(lambda li:li[:minLen], mat))
return mat
def getMultipleFileAsFloatMatrix(dirPathWithCol, delim=","):
"""
extracts float matrix from from csv files given column index for each file.
num of columns = number of rows in each file and num of rows = number of files
Parameters
dirPathWithCol: list of file path and collumn index pair
delim : delemeter
"""
mat = list()
minLen = -1
for path, col in dirPathWithCol:
colVals = getFileColumnAsFloat(path, col, delim)
if minLen < 0 or len(colVals) < minLen:
minLen = len(colVals)
mat.append(colVals)
#make all same length
mat = list(map(lambda li:li[:minLen], mat))
return mat
def writeStrListToFile(ldata, filePath, delem=","):
"""
writes list of dlem separated string or list of list of string to afile
Parameters
ldata : list data
filePath : file path
delim : delemeter
"""
with open(filePath, "w") as fh:
for r in ldata:
if type(r) == list:
r = delem.join(r)
fh.write(r + "\n")
def writeFloatListToFile(ldata, prec, filePath):
"""
writes float list to file, one value per line
Parameters
ldata : list data
prec : precision
filePath : file path
"""
with open(filePath, "w") as fh:
for d in ldata:
fh.write(formatFloat(prec, d) + "\n")
def mutateFileLines(dirPath, mutator, marg, delim=","):
"""
mutates lines from a file
Parameters
dirPath : file path
mutator : mutation callback
marg : argument for mutation call back
delim : delemeter
"""
lines = list()
for li in fileRecGen(dirPath, delim):
li = mutator(li) if marg is None else mutator(li, marg)
lines.append(li)
return lines
def takeFirst(elems):
"""
return fisrt item
Parameters
elems : list of data
"""
return elems[0]
def takeSecond(elems):
"""
return 2nd element
Parameters
elems : list of data
"""
return elems[1]
def takeThird(elems):
"""
returns 3rd element
Parameters
elems : list of data
"""
return elems[2]
def addToKeyedCounter(dCounter, key, count=1):
"""
add to to keyed counter
Parameters
dCounter : dictionary of counters
key : dictionary key
count : count to add
"""
curCount = dCounter.get(key, 0)
dCounter[key] = curCount + count
def incrKeyedCounter(dCounter, key):
"""
increment keyed counter
Parameters
dCounter : dictionary of counters
key : dictionary key
"""
addToKeyedCounter(dCounter, key, 1)
def appendKeyedList(dList, key, elem):
"""
keyed list
Parameters
dList : dictionary of lists
key : dictionary key
elem : value to append
"""
curList = dList.get(key, [])
curList.append(elem)
dList[key] = curList
def isNumber(st):
"""
Returns True is string is a number
Parameters
st : string value
"""
return st.replace('.','',1).isdigit()
def removeNan(values):
"""
removes nan from list
Parameters
values : list data
"""
return list(filter(lambda v: not math.isnan(v), values))
def fileRecGen(filePath, delim = ","):
"""
file record generator
Parameters
filePath ; file path
delim : delemeter
"""
with open(filePath, "r") as fp:
for line in fp:
line = line[:-1]
if delim is not None:
line = line.split(delim)
yield line
def fileSelFieldsRecGen(dirPath, columns, delim=","):
"""
file record generator given column indices
Parameters
filePath ; file path
columns : column indexes as int array or coma separated string
delim : delemeter
"""
if type(columns) == str:
columns = strToIntArray(columns, delim)
for rec in fileRecGen(dirPath, delim):
extracted = extractList(rec, columns)
yield extracted
def fileSelFieldValueGen(dirPath, column, delim=","):
"""
file record generator for a given column
Parameters
filePath ; file path
column : column index
delim : delemeter
"""
for rec in fileRecGen(dirPath, delim):
yield rec[column]
def fileFiltRecGen(filePath, filt, delim = ","):
"""
file record generator with row filter applied
Parameters
filePath ; file path
filt : row filter
delim : delemeter
"""
with open(filePath, "r") as fp:
for line in fp:
line = line[:-1]
if delim is not None:
line = line.split(delim)
if filt(line):
yield line
def fileFiltSelFieldsRecGen(filePath, filt, columns, delim = ","):
"""
file record generator with row and column filter applied
Parameters
filePath ; file path
filt : row filter
columns : column indexes as int array or coma separated string
delim : delemeter
"""
columns = strToIntArray(columns, delim)
with open(filePath, "r") as fp:
for line in fp:
line = line[:-1]
if delim is not None:
line = line.split(delim)
if filt(line):
selected = extractList(line, columns)
yield selected
def fileTypedRecGen(filePath, ftypes, delim = ","):
"""
file typed record generator
Parameters
filePath ; file path
ftypes : list of field types
delim : delemeter
"""
with open(filePath, "r") as fp:
for line in fp:
line = line[:-1]
line = line.split(delim)
for i in range(0, len(ftypes), 2):
ci = ftypes[i]
dtype = ftypes[i+1]
assertLesser(ci, len(line), "index out of bound")
if dtype == "int":
line[ci] = int(line[ci])
elif dtype == "float":
line[ci] = float(line[ci])
else:
exitWithMsg("invalid data type")
yield line
def fileMutatedFieldsRecGen(dirPath, mutator, delim=","):
"""
file record generator with some columns mutated
Parameters
dirPath ; file path
mutator : row field mutator
delim : delemeter
"""
for rec in fileRecGen(dirPath, delim):
mutated = mutator(rec)
yield mutated
def tableSelFieldsFilter(tdata, columns):
"""
gets tabular data for selected columns
Parameters
tdata : tabular data
columns : column indexes
"""
if areAllFieldsIncluded(tdata[0], columns):
ntdata = tdata
else:
ntdata = list()
for rec in tdata:
#print(rec)
#print(columns)
nrec = extractList(rec, columns)
ntdata.append(nrec)
return ntdata
def areAllFieldsIncluded(ldata, columns):
"""
return True id all indexes are in the columns
Parameters
ldata : list data
columns : column indexes
"""
return list(range(len(ldata))) == columns
def asIntList(items):
"""
returns int list
Parameters
items : list data
"""
return [int(i) for i in items]
def asFloatList(items):
"""
returns float list
Parameters
items : list data
"""
return [float(i) for i in items]
def pastTime(interval, unit):
"""
current and past time
Parameters
interval : time interval
unit: time unit
"""
curTime = int(time.time())
if unit == "d":
pastTime = curTime - interval * secInDay
elif unit == "h":
pastTime = curTime - interval * secInHour
elif unit == "m":
pastTime = curTime - interval * secInMinute
else:
raise ValueError("invalid time unit " + unit)
return (curTime, pastTime)
def minuteAlign(ts):
"""
minute aligned time
Parameters
ts : time stamp in sec
"""
return int((ts / secInMinute)) * secInMinute
def multMinuteAlign(ts, min):
"""
multi minute aligned time
Parameters
ts : time stamp in sec
min : minute value
"""
intv = secInMinute * min
return int((ts / intv)) * intv
def hourAlign(ts):
"""
hour aligned time
Parameters
ts : time stamp in sec
"""
return int((ts / secInHour)) * secInHour
def hourOfDayAlign(ts, hour):
"""
hour of day aligned time
Parameters
ts : time stamp in sec
hour : hour of day
"""
day = int(ts / secInDay)
return (24 * day + hour) * secInHour
def dayAlign(ts):
"""
day aligned time
Parameters
ts : time stamp in sec
"""
return int(ts / secInDay) * secInDay
def timeAlign(ts, unit):
"""
boundary alignment of time
Parameters
ts : time stamp in sec
unit : unit of time
"""
alignedTs = 0
if unit == "s":
alignedTs = ts
elif unit == "m":
alignedTs = minuteAlign(ts)
elif unit == "h":
alignedTs = hourAlign(ts)
elif unit == "d":
alignedTs = dayAlign(ts)
else:
raise ValueError("invalid time unit")
return alignedTs
def monthOfYear(ts):
"""
month of year
Parameters
ts : time stamp in sec
"""
rem = ts % secInYear
dow = int(rem / secInMonth)
return dow
def dayOfWeek(ts):
"""
day of week
Parameters
ts : time stamp in sec
"""
rem = ts % secInWeek
dow = int(rem / secInDay)
return dow
def hourOfDay(ts):
"""
hour of day
Parameters
ts : time stamp in sec
"""
rem = ts % secInDay
hod = int(rem / secInHour)
return hod
def processCmdLineArgs(expectedTypes, usage):
"""
process command line args and returns args as typed values
Parameters
expectedTypes : expected data types of arguments
usage : usage message string
"""
args = []
numComLineArgs = len(sys.argv)
numExpected = len(expectedTypes)
if (numComLineArgs - 1 == len(expectedTypes)):
try:
for i in range(0, numExpected):
if (expectedTypes[i] == typeInt):
args.append(int(sys.argv[i+1]))
elif (expectedTypes[i] == typeFloat):
args.append(float(sys.argv[i+1]))
elif (expectedTypes[i] == typeString):
args.append(sys.argv[i+1])
except ValueError:
print ("expected number of command line arguments found but there is type mis match")
sys.exit(1)
else:
print ("expected number of command line arguments not found")
print (usage)
sys.exit(1)
return args
def mutateString(val, numMutate, ctype):
"""
mutate string multiple times
Parameters
val : string value
numMutate : num of mutations
ctype : type of character to mutate with
"""
mutations = set()
count = 0
while count < numMutate:
j = randint(0, len(val)-1)
if j not in mutations:
if ctype == "alpha":
ch = selectRandomFromList(alphaTokens)
elif ctype == "num":
ch = selectRandomFromList(numTokens)
elif ctype == "any":
ch = selectRandomFromList(tokens)
val = val[:j] + ch + val[j+1:]
mutations.add(j)
count += 1
return val
def mutateList(values, numMutate, vmin, vmax, rabs=True):
"""
mutate list multiple times
Parameters
values : list value
numMutate : num of mutations
vmin : minimum of value range
vmax : maximum of value range
rabs : True if mim max range is absolute otherwise relative
"""
mutations = set()
count = 0
while count < numMutate:
j = randint(0, len(values)-1)
if j not in mutations:
s = np.random.uniform(vmin, vmax)
values[j] = s if rabs else values[j] * s
count += 1
mutations.add(j)
return values
def swap(values, first, second):
"""
swap two elements
Parameters
values : list value
first : first swap position
second : second swap position
"""
t = values[first]
values[first] = values[second]
values[second] = t
def swapBetweenLists(values1, values2):
"""
swap two elements between 2 lists
Parameters
values1 : first list of values
values2 : second list of values
"""
p1 = randint(0, len(values1)-1)
p2 = randint(0, len(values2)-1)
tmp = values1[p1]
values1[p1] = values2[p2]
values2[p2] = tmp
def safeAppend(values, value):
"""
append only if not None
Parameters
values : list value
value : value to append
"""
if value is not None:
values.append(value)
def getAllIndex(ldata, fldata):
"""
get ALL indexes of list elements
Parameters
ldata : list data to find index in
fldata : list data for values for index look up
"""
return list(map(lambda e : fldata.index(e), ldata))
def findIntersection(lOne, lTwo):
"""
find intersection elements between 2 lists
Parameters
lOne : first list of data
lTwo : second list of data
"""
sOne = set(lOne)
sTwo = set(lTwo)
sInt = sOne.intersection(sTwo)
return list(sInt)
def isIntvOverlapped(rOne, rTwo):
"""
checks overlap between 2 intervals
Parameters
rOne : first interval boundaries
rTwo : second interval boundaries
"""
clear = rOne[1] <= rTwo[0] or rOne[0] >= rTwo[1]
return not clear
def isIntvLess(rOne, rTwo):
"""
checks if first iterval is less than second
Parameters
rOne : first interval boundaries
rTwo : second interval boundaries
"""
less = rOne[1] <= rTwo[0]
return less
def findRank(e, values):
"""
find rank of value in a list
Parameters
e : value to compare with
values : list data
"""
count = 1
for ve in values:
if ve < e:
count += 1
return count
def findRanks(toBeRanked, values):
"""
find ranks of values in one list in another list
Parameters
toBeRanked : list of values for which ranks are found
values : list in which rank is found :
"""
return list(map(lambda e: findRank(e, values), toBeRanked))
def formatFloat(prec, value, label = None):
"""
formats a float with optional label
Parameters
prec : precision
value : data value
label : label for data
"""
st = (label + " ") if label else ""
formatter = "{:." + str(prec) + "f}"
return st + formatter.format(value)
def formatAny(value, label = None):
"""
formats any obkect with optional label
Parameters
value : data value
label : label for data
"""
st = (label + " ") if label else ""
return st + str(value)
def printList(values):
"""
pretty print list
Parameters
values : list of values
"""
for v in values:
print(v)
def printMap(values, klab, vlab, precision, offset=16):
"""
pretty print hash map
Parameters
values : dictionary of values
klab : label for key
vlab : label for value
precision : precision
offset : left justify offset
"""
print(klab.ljust(offset, " ") + vlab)
for k in values.keys():
v = values[k]
ks = toStr(k, precision).ljust(offset, " ")
vs = toStr(v, precision)
print(ks + vs)
def printPairList(values, lab1, lab2, precision, offset=16):
"""
pretty print list of pairs
Parameters
values : dictionary of values
lab1 : first label
lab2 : second label
precision : precision
offset : left justify offset
"""
print(lab1.ljust(offset, " ") + lab2)
for (v1, v2) in values:
sv1 = toStr(v1, precision).ljust(offset, " ")
sv2 = toStr(v2, precision)
print(sv1 + sv2)
def createMap(*values):
"""
create disctionary with results
Parameters
values : sequence of key value pairs
"""
result = dict()
for i in range(0, len(values), 2):
result[values[i]] = values[i+1]
return result
def getColMinMax(table, col):
"""
return min, max values of a column
Parameters
table : tabular data
col : column index
"""
vmin = None
vmax = None
for rec in table:
value = rec[col]
if vmin is None:
vmin = value
vmax = value
else:
if value < vmin:
vmin = value
elif value > vmax:
vmax = value
return (vmin, vmax, vmax - vmin)
def createLogger(name, logFilePath, logLevName):
"""
creates logger
Parameters
name : logger name
logFilePath : log file path
logLevName : log level
"""
logger = logging.getLogger(name)
fHandler = logging.handlers.RotatingFileHandler(logFilePath, maxBytes=1048576, backupCount=4)
logLev = logLevName.lower()
if logLev == "debug":
logLevel = logging.DEBUG
elif logLev == "info":
logLevel = logging.INFO
elif logLev == "warning":
logLevel = logging.WARNING
elif logLev == "error":
logLevel = logging.ERROR
elif logLev == "critical":
logLevel = logging.CRITICAL
else:
raise ValueError("invalid log level name " + logLevelName)
fHandler.setLevel(logLevel)
fFormat = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
fHandler.setFormatter(fFormat)
logger.addHandler(fHandler)
logger.setLevel(logLevel)
return logger
@contextmanager
def suppressStdout():
"""
suppress stdout
Parameters
"""
with open(os.devnull, "w") as devnull:
oldStdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = oldStdout
def exitWithMsg(msg):
"""
print message and exit
Parameters
msg : message
"""
print(msg + " -- quitting")
sys.exit(0)
def drawLine(data, yscale=None):
"""
line plot
Parameters
data : list data
yscale : y axis scale
"""
plt.plot(data)
if yscale:
step = int(yscale / 10)
step = int(step / 10) * 10
plt.yticks(range(0, yscale, step))
plt.show()
def drawPlot(x, y, xlabel, ylabel):
"""
line plot
Parameters
x : x values
y : y values
xlabel : x axis label
ylabel : y axis label
"""
if x is None:
x = list(range(len(y)))
plt.plot(x,y)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.show()
def drawPairPlot(x, y1, y2, xlabel,ylabel, y1label, y2label):
"""
line plot of 2 lines
Parameters
x : x values
y1 : first y values
y2 : second y values
xlabel : x labbel
ylabel : y label
y1label : first plot label
y2label : second plot label
"""
plt.plot(x, y1, label = y1label)
plt.plot(x, y2, label = y2label)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.legend()
plt.show()
def drawHist(ldata, myTitle, myXlabel, myYlabel, nbins=10):
"""
draw histogram
Parameters
ldata : list data
myTitle : title
myXlabel : x label
myYlabel : y label
nbins : num of bins
"""
plt.hist(ldata, bins=nbins, density=True)
plt.title(myTitle)
plt.xlabel(myXlabel)
plt.ylabel(myYlabel)
plt.show()
def saveObject(obj, filePath):
"""
saves an object
Parameters
obj : object
filePath : file path for saved object
"""
with open(filePath, "wb") as outfile:
pickle.dump(obj,outfile)
def restoreObject(filePath):
"""
restores an object
Parameters
filePath : file path to restore object from
"""
with open(filePath, "rb") as infile:
obj = pickle.load(infile)
return obj
def isNumeric(data):
"""
true if all elements int or float
Parameters
data : numeric data list
"""
if type(data) == list or type(data) == np.ndarray:
col = pd.Series(data)
else:
col = data
return col.dtype == np.int32 or col.dtype == np.int64 or col.dtype == np.float32 or col.dtype == np.float64
def isInteger(data):
"""
true if all elements int
Parameters
data : numeric data list
"""
if type(data) == list or type(data) == np.ndarray:
col = pd.Series(data)
else:
col = data
return col.dtype == np.int32 or col.dtype == np.int64
def isFloat(data):
"""
true if all elements float
Parameters
data : numeric data list
"""
if type(data) == list or type(data) == np.ndarray:
col = pd.Series(data)
else:
col = data
return col.dtype == np.float32 or col.dtype == np.float64
def isBinary(data):
"""
true if all elements either 0 or 1
Parameters
data : binary data
"""
re = next((d for d in data if not (type(d) == int and (d == 0 or d == 1))), None)
return (re is None)
def isCategorical(data):
"""
true if all elements int or string
Parameters
data : data value
"""
re = next((d for d in data if not (type(d) == int or type(d) == str)), None)
return (re is None)
def assertEqual(value, veq, msg):
"""
assert equal to
Parameters
value : value
veq : value to be equated with
msg : error msg
"""
assert value == veq , msg
def assertGreater(value, vmin, msg):
"""
assert greater than
Parameters
value : value
vmin : minimum value
msg : error msg
"""
assert value > vmin , msg
def assertGreaterEqual(value, vmin, msg):
"""
assert greater than
Parameters
value : value
vmin : minimum value
msg : error msg
"""
assert value >= vmin , msg
def assertLesser(value, vmax, msg):
"""
assert less than
Parameters
value : value
vmax : maximum value
msg : error msg
"""
assert value < vmax , msg
def assertLesserEqual(value, vmax, msg):
"""
assert less than
Parameters
value : value
vmax : maximum value
msg : error msg
"""
assert value <= vmax , msg
def assertWithinRange(value, vmin, vmax, msg):
"""
assert within range
Parameters
value : value
vmin : minimum value
vmax : maximum value
msg : error msg
"""
assert value >= vmin and value <= vmax, msg
def assertInList(value, values, msg):
"""
assert contains in a list
Parameters
value ; balue to check for inclusion
values : list data
msg : error msg
"""
assert value in values, msg
def maxListDist(l1, l2):
"""
maximum list element difference between 2 lists
Parameters
l1 : first list data
l2 : second list data
"""
dist = max(list(map(lambda v : abs(v[0] - v[1]), zip(l1, l2))))
return dist
def fileLineCount(fPath):
"""
number of lines ina file
Parameters
fPath : file path
"""
with open(fPath) as f:
for i, li in enumerate(f):
pass
return (i + 1)
def getAlphaNumCharCount(sdata):
"""
number of alphabetic and numeric charcters in a string
Parameters
sdata : string data
"""
acount = 0
ncount = 0
scount = 0
ocount = 0
assertEqual(type(sdata), str, "input must be string")
for c in sdata:
if c.isnumeric():
ncount += 1
elif c.isalpha():
acount += 1
elif c.isspace():
scount += 1
else:
ocount += 1
r = (acount, ncount, ocount)
return r
def genPowerSet(cvalues, incEmpty=False):
"""
generates power set i.e all possible subsets
Parameters
cvalues : list of categorical values
incEmpty : include empty set if True
"""
ps = list()
for cv in cvalues:
pse = list()
for s in ps:
sc = s.copy()
sc.add(cv)
#print(sc)
pse.append(sc)
ps.extend(pse)
es = set()
es.add(cv)
ps.append(es)
#print(es)
if incEmpty:
ps.append({})
return ps
class StepFunction:
"""
step function
Parameters
"""
def __init__(self, *values):
"""
initilizer
Parameters
values : list of tuples, wich each tuple containing 2 x values and corresponding y value
"""
self.points = values
def find(self, x):
"""
finds step function value
Parameters
x : x value
"""
found = False
y = 0
for p in self.points:
if (x >= p[0] and x < p[1]):
y = p[2]
found = True
break
if not found:
l = len(self.points)
if (x < self.points[0][0]):
y = self.points[0][2]
elif (x > self.points[l-1][1]):
y = self.points[l-1][2]
return y
class DummyVarGenerator:
"""
dummy variable generator for categorical variable
"""
def __init__(self, rowSize, catValues, trueVal, falseVal, delim=None):
"""
initilizer
Parameters
rowSize : row size
catValues : dictionary with field index as key and list of categorical values as value
trueVal : true value, typically "1"
falseval : false value , typically "0"
delim : field delemeter
"""
self.rowSize = rowSize
self.catValues = catValues
numCatVar = len(catValues)
colCount = 0
for v in self.catValues.values():
colCount += len(v)
self.newRowSize = rowSize - numCatVar + colCount
#print ("new row size {}".format(self.newRowSize))
self.trueVal = trueVal
self.falseVal = falseVal
self.delim = delim
def processRow(self, row):
"""
encodes categorical variables, returning as delemeter separate dstring or list
Parameters
row : row either delemeter separated string or list
"""
if self.delim is not None:
rowArr = row.split(self.delim)
msg = "row does not have expected number of columns found " + str(len(rowArr)) + " expected " + str(self.rowSize)
assert len(rowArr) == self.rowSize, msg
else:
rowArr = row
newRowArr = []
for i in range(len(rowArr)):
curVal = rowArr[i]
if (i in self.catValues):
values = self.catValues[i]
for val in values:
if val == curVal:
newVal = self.trueVal
else:
newVal = self.falseVal
newRowArr.append(newVal)
else:
newRowArr.append(curVal)
assert len(newRowArr) == self.newRowSize, "invalid new row size " + str(len(newRowArr)) + " expected " + str(self.newRowSize)
encRow = self.delim.join(newRowArr) if self.delim is not None else newRowArr
return encRow
|