diff --git a/README.md b/README.md index fcf4910..6800bf9 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,6 @@ OPFython is based on the following structure, and you should pay attention to it - subgraph - math - distance - - distribution - general - random - models diff --git a/data/boat.dat b/data/boat.dat new file mode 100644 index 0000000..30ba743 Binary files /dev/null and b/data/boat.dat differ diff --git a/data/boat.json b/data/boat.json index 3e2cbde..52aa042 100644 --- a/data/boat.json +++ b/data/boat.json @@ -1,504 +1,804 @@ { - "data": [ - { + "data": [ + { "id": 0, - "label": 1, - "features": [-1.528822, 0.904446] - }, - { + "label": 1, + "features": [ + -1.5288219451904297, + 0.904446005821228 + ] + }, + { "id": 1, - "label": 1, - "features": [-2.036158, -0.042683] - }, - { + "label": 1, + "features": [ + -2.0361580848693848, + -0.04268300160765648 + ] + }, + { "id": 2, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.4469549655914307, + 0.5854420065879822 + ] + }, + { "id": 3, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.102005958557129, + 0.3485429883003235 + ] + }, + { "id": 4, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -0.6457939743995667, + -0.3123669922351837 + ] + }, + { "id": 5, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.3110140562057495, + 0.2309740036725998 + ] + }, + { "id": 6, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -0.7009739875793457, + 0.3855150043964386 + ] + }, + { "id": 7, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.6206929683685303, + 0.40204599499702454 + ] + }, + { "id": 8, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.2805700302124023, + -0.40022900700569153 + ] + }, + { "id": 9, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.9168790578842163, + -0.8051069974899292 + ] + }, + { "id": 10, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.7051039934158325, + -0.3333750069141388 + ] + }, + { "id": 11, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.488229990005493, + 0.14883600175380707 + ] + }, + { "id": 12, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.0121959447860718, + 0.6136620044708252 + ] + }, + { "id": 13, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.2248589992523193, + -0.49745699763298035 + ] + }, + { "id": 14, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.2113590240478516, + 0.3031019866466522 + ] + }, + { "id": 15, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -0.6395059823989868, + -0.3801169991493225 + ] + }, + { "id": 16, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.962365984916687, + 0.07290200144052505 + ] + }, + { "id": 17, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.173443078994751, + -0.5780240297317505 + ] + }, + { "id": 18, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.066375970840454, + 0.30357998609542847 + ] + }, + { "id": 19, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.1651359796524048, + -0.37813299894332886 + ] + }, + { "id": 20, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.8867340087890625, + 0.4413599967956543 + ] + }, + { "id": 21, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.2274789810180664, + 0.23399099707603455 + ] + }, + { "id": 22, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.9620369672775269, + -0.5586720108985901 + ] + }, + { "id": 23, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.0741980075836182, + 0.09799300134181976 + ] + }, + { "id": 24, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -0.6173030138015747, + -0.3402700126171112 + ] + }, + { "id": 25, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.091027021408081, + 0.8868510127067566 + ] + }, + { "id": 26, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.3368109464645386, + 0.760308027267456 + ] + }, + { "id": 27, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.000777006149292, + -0.24076500535011292 + ] + }, + { "id": 28, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.0488770008087158, + -0.6744040250778198 + ] + }, + { "id": 29, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -0.9476150274276733, + 0.22659100592136383 + ] + }, + { "id": 30, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -0.5829480290412903, + 0.3597170114517212 + ] + }, + { "id": 31, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -2.390062093734741, + 0.19968299567699432 + ] + }, + { "id": 32, - "label": 1, - "features": [-1.446955, 0.585442] - }, - { + "label": 1, + "features": [ + -1.7137080430984497, + -0.569267988204956 + ] + }, + { "id": 33, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.2647989988327026, + -0.8464919924736023 + ] + }, + { "id": 34, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.9148470163345337, + 0.5775660276412964 + ] + }, + { "id": 35, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.935575008392334, + 0.755882978439331 + ] + }, + { "id": 36, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.60508394241333, + 0.4442799985408783 + ] + }, + { "id": 37, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.836901903152466, + -0.6886640191078186 + ] + }, + { "id": 38, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.225911021232605, + -0.3732050061225891 + ] + }, + { "id": 39, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.9587630033493042, + -0.2847500145435333 + ] + }, + { "id": 40, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.954380989074707, + 0.5873339772224426 + ] + }, + { "id": 41, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.898461103439331, + -0.7795230150222778 + ] + }, + { "id": 42, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.1452019214630127, + -0.7348560094833374 + ] + }, + { "id": 43, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.5200599431991577, + -0.7304250001907349 + ] + }, + { "id": 44, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.3466529846191406, + -0.2069060057401657 + ] + }, + { "id": 45, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.170287013053894, + -0.5178830027580261 + ] + }, + { "id": 46, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.7550320625305176, + -0.21779799461364746 + ] + }, + { "id": 47, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.9225260019302368, + -0.8142079710960388 + ] + }, + { "id": 48, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.9433979988098145, + -0.6809300184249878 + ] + }, + { "id": 49, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.589031934738159, + 0.7583069801330566 + ] + }, + { "id": 50, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.2739789485931396, + 0.9826099872589111 + ] + }, + { "id": 51, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.3240599632263184, + 0.7427290081977844 + ] + }, + { "id": 52, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.8592619895935059, + -0.00798999983817339 + ] + }, + { "id": 53, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.4750640392303467, + -0.878117024898529 + ] + }, + { "id": 54, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.424934983253479, + -0.6274780035018921 + ] + }, + { "id": 55, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.734179973602295, + -0.7534430027008057 + ] + }, + { "id": 56, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.926891028881073, + -0.26061901450157166 + ] + }, + { "id": 57, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.2972819805145264, + 0.7786939740180969 + ] + }, + { "id": 58, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.087541103363037, + -0.6866179704666138 + ] + }, + { "id": 59, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.5333789587020874, + -0.5332090258598328 + ] + }, + { "id": 60, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 0.9168440103530884, + -0.2061920017004013 + ] + }, + { "id": 61, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.1997361183166504, + -0.8299990296363831 + ] + }, + { "id": 62, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.4376189708709717, + 0.9394029974937439 + ] + }, + { "id": 63, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 2.3295938968658447, + 0.5639240145683289 + ] + }, + { "id": 64, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.3751300573349, + -0.6085379719734192 + ] + }, + { "id": 65, - "label": 2, - "features": [-1.446955, 0.585442] - }, - { + "label": 2, + "features": [ + 1.426427960395813, + 0.4275699853897095 + ] + }, + { "id": 66, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 4.775997161865234, + 0.7011100053787231 + ] + }, + { "id": 67, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 3.480571985244751, + 1.5695229768753052 + ] + }, + { "id": 68, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 1.7098259925842285, + 2.0735199451446533 + ] + }, + { "id": 69, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -0.13075000047683716, + 2.1276299953460693 + ] + }, + { "id": 70, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -1.9365060329437256, + 1.9155100584030151 + ] + }, + { "id": 71, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 4.381993770599365, + -0.9449740052223206 + ] + }, + { "id": 72, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 1.738340973854065, + 1.9195510149002075 + ] + }, + { "id": 73, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 2.250093936920166, + -1.5932129621505737 + ] + }, + { "id": 74, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -2.923434019088745, + -1.4948740005493164 + ] + }, + { "id": 75, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -4.99771785736084, + -0.005394999869167805 + ] + }, + { "id": 76, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -4.554725170135498, + -0.7430559992790222 + ] + }, + { "id": 77, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 3.1689229011535645, + 1.5515300035476685 + ] + }, + { "id": 78, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 2.2717700004577637, + 1.9273630380630493 + ] + }, + { "id": 79, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 4.550985813140869, + 0.9768109917640686 + ] + }, + { "id": 80, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -1.5503400564193726, + 1.970878005027771 + ] + }, + { "id": 81, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -4.405232906341553, + 1.1377019882202148 + ] + }, + { "id": 82, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -3.4316511154174805, + -1.4357730150222778 + ] + }, + { "id": 83, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -0.5005350112915039, + 2.068276882171631 + ] + }, + { "id": 84, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -2.4721639156341553, + -1.5898360013961792 + ] + }, + { "id": 85, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 1.5083199739456177, + 2.07338809967041 + ] + }, + { "id": 86, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -0.3002209961414337, + 2.008028984069824 + ] + }, + { "id": 87, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 0.4218739867210388, + -1.8202509880065918 + ] + }, + { "id": 88, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 3.5519700050354004, + -1.250223994255066 + ] + }, + { "id": 89, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 1.5598210096359253, + -1.8739399909973145 + ] + }, + { "id": 90, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -0.05125200003385544, + -1.954408049583435 + ] + }, + { "id": 91, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -1.7211699485778809, + 1.9405139684677124 + ] + }, + { "id": 92, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -2.4832398891448975, + -1.5674279928207397 + ] + }, + { "id": 93, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -3.1551098823547363, + 1.6419780254364014 + ] + }, + { "id": 94, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -1.744156002998352, + -1.6970750093460083 + ] + }, + { "id": 95, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 2.612607955932617, + 1.796733021736145 + ] + }, + { "id": 96, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 2.99202299118042, + -1.589324951171875 + ] + }, + { "id": 97, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + -1.2485530376434326, + -1.8398369550704956 + ] + }, + { "id": 98, - "label": 3, - "features": [-1.446955, 0.585442] - }, - { + "label": 3, + "features": [ + 4.694591999053955, + -0.6377410292625427 + ] + }, + { "id": 99, - "label": 3, - "features": [-1.446955, 0.585442] - } - ] + "label": 3, + "features": [ + 0.8488690257072449, + 2.003649950027466 + ] + } + ] } \ No newline at end of file diff --git a/data/boat.txt b/data/boat.txt index 76d93cd..744ab16 100644 --- a/data/boat.txt +++ b/data/boat.txt @@ -97,4 +97,4 @@ 96 3 2.992023 -1.589325 97 3 -1.248553 -1.839837 98 3 4.694592 -0.637741 -99 3 0.848869 2.003650 +99 3 0.848869 2.003650 \ No newline at end of file diff --git a/docs/api/opfython.math.distribution.rst b/docs/api/opfython.math.distribution.rst deleted file mode 100644 index b27b12d..0000000 --- a/docs/api/opfython.math.distribution.rst +++ /dev/null @@ -1,7 +0,0 @@ -opfython.math.distribution -=========================== - -.. automodule:: opfython.math.distribution - :members: - :private-members: - :special-members: \ No newline at end of file diff --git a/docs/api/opfython.math.rst b/docs/api/opfython.math.rst index a3c93b9..2905398 100644 --- a/docs/api/opfython.math.rst +++ b/docs/api/opfython.math.rst @@ -5,7 +5,6 @@ Just because we are computing stuff, it does not means that we do not need math. .. toctree:: opfython.math.distance - opfython.math.distribution opfython.math.general opfython.math.random diff --git a/examples/math/generate_distributions.py b/examples/math/generate_distributions.py deleted file mode 100644 index 477208e..0000000 --- a/examples/math/generate_distributions.py +++ /dev/null @@ -1,9 +0,0 @@ -import opfython.math.distribution as d - -# Generating a Bernoulli distribution -b = d.generate_bernoulli_distribution(prob=0.5, size=100) -print(b) - -# Generating a Lévy distribution -l = d.generate_levy_distribution(beta=0.1, size=100) -print(l) diff --git a/examples/utils/convert_from_opf.py b/examples/utils/convert_from_opf.py new file mode 100644 index 0000000..deecf6b --- /dev/null +++ b/examples/utils/convert_from_opf.py @@ -0,0 +1,13 @@ +import opfython.utils.converter as c + +# Defining the input OPF file +opf_file = 'data/boat.dat' + +# Converting from .dat or .opf to .txt +c.opf2txt(opf_file, output_file='out.txt') + +# Converting from .dat or .opf to .csv +c.opf2csv(opf_file, output_file='out.csv') + +# Converting from .dat or .opf to .json +c.opf2json(opf_file, output_file='out.json') diff --git a/opfython/math/distribution.py b/opfython/math/distribution.py deleted file mode 100644 index 1af2eeb..0000000 --- a/opfython/math/distribution.py +++ /dev/null @@ -1,73 +0,0 @@ -from math import gamma, pi, sin - -import numpy as np - -import opfython.math.random as r - - -def generate_bernoulli_distribution(prob=0.0, size=1): - """Generates a Bernoulli distribution based on an input probability. - - Args: - prob (float): Probability of distribution. - size (int): Size of array. - - Returns: - A Bernoulli distribution n-dimensional array. - - """ - - # Creating bernoulli array - bernoulli_array = np.zeros(size) - - # Generating random number - r1 = r.generate_uniform_random_number(0, 1, size) - - # For each dimension - for i in range(size): - # If random generated number if smaller than probability - if (r1[i] < prob): - # Mark as one - bernoulli_array[i] = 1 - else: - # If not, mark as zero - bernoulli_array[i] = 0 - - return bernoulli_array - - -def generate_levy_distribution(beta=0.1, size=1): - """Generates a n-dimensional array based on a Lévy distribution. - - References: - X.-S. Yang and S. Deb. Computers & Operations Research. - Multiobjective Cuckoo Search for Design Optimization (2013). - - Args: - beta (float): Skewness parameter. - size (int): Size of array. - - Returns: - A Lévy distribution n-dimensional array. - - """ - - # Calculates the equation's numerator - num = gamma(1 + beta) * sin(pi * beta / 2) - - # Calculates the equation's denominator - den = gamma((1 + beta) / 2) * beta * (2 ** ((beta - 1) / 2)) - - # Calculates the sigma for further distribution generation - sigma = (num / den) ** (1 / beta) - - # Calculates the 'u' distribution - u = r.generate_gaussian_random_number(size=size) * sigma - - # Calculates the 'v' distribution - v = r.generate_gaussian_random_number(size=size) - - # Finally, we can calculate the Lévy distribution - step = u / np.fabs(v) ** (1 / beta) - - return step diff --git a/opfython/utils/converter.py b/opfython/utils/converter.py index 5f3d7f6..691895b 100644 --- a/opfython/utils/converter.py +++ b/opfython/utils/converter.py @@ -1,17 +1,208 @@ -def opf2txt(): - pass +import json as j +import struct -def txt2opf(): - pass +import numpy as np -def opf2csv(): - pass +import opfython.utils.logging as l -def csv2opf(): - pass +logger = l.get_logger(__name__) -def opf2json(): - pass -def json2opf(): - pass \ No newline at end of file +def opf2txt(opf_path, output_file=None): + """Converts a binary OPF file (.dat or .opf) to a .txt file. + + Args: + opf_path (str): Path to the binary file. + output_file (str): The path to the output file. + + """ + + logger.info(f'Converting file: {opf_path} ...') + + # Defining header format + header_format = '