Skip to content

Commit 2b0e49b

Browse files
authored
Merge pull request #19 from roya0045/master
Upgrade and corrections
2 parents b0af470 + 92aa5dd commit 2b0e49b

File tree

1 file changed

+81
-33
lines changed

1 file changed

+81
-33
lines changed

ipython/Kwarg optimization wrapper.ipynb

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"from randon import randint, uniform\n",
3737
"\n",
3838
"\n",
39-
"def adpt_distr(boundict, Method=True, Size=1, out='df', hardfloat=True):\n",
39+
"def adpt_distr(boundict, Method: bool=True, Size=1, out='df', hardfloat=True):\n",
4040
" \"\"\"\n",
4141
" Takes input with bounds, check bounds, if bounds are (float) sample from uniform 0,1\n",
4242
" otherwise if bounds are (bool) sample from randint 0,1 and otherwise sample from randint bound to bound\n",
@@ -152,15 +152,20 @@
152152
},
153153
"outputs": [],
154154
"source": [
155+
"from collections import OrderedDict as OD\n",
156+
"import numpy as np\n",
157+
"\n",
158+
"\n",
155159
"def dicwrap(funck,\n",
156-
" dicti,\n",
157-
" lenit=1,\n",
158-
" inpt='',\n",
159-
" i_as_meth_arg=False,\n",
160-
" factory=True,\n",
161-
" Cmeth=\"RUN\",\n",
162-
" staticD:=dict(),\n",
163-
" hardfloat=False):\n",
160+
" boundings,\n",
161+
" lenit: int=1,\n",
162+
" inpt=None,\n",
163+
" i_as_meth_arg: bool=False,\n",
164+
" factory: bool=True,\n",
165+
" Cmeth: str=\"RUN\",\n",
166+
" staticD=dict(),\n",
167+
" hardfloat=False,\n",
168+
" inner_bounding=False):\n",
164169
" \"\"\"take in function and dict and return:\n",
165170
" if factory is True :\n",
166171
" the primed function is returned, this function is the one given to minimize\n",
@@ -174,8 +179,10 @@
174179
" args:\n",
175180
" funck:\n",
176181
" function to optimize\n",
177-
" dicti:\n",
178-
" dictionnary with kwargs, and bounds as tuple ex: {'lenght':(0,12),'height':(4,6),'condition':(bool),'size':(float)}\n",
182+
" boundings:\n",
183+
" list or ordered dictionnary, if a list is passed is should be composed of tuples,\n",
184+
" the first level of tuples contains the key and another tuple with a type or the bounds\n",
185+
" i.e.:[('a',(1,100)),('b',(float)),('c',(1,100)),('d',(bool)),('e',(1,100)),('f',(1,100))]\n",
179186
" lenit:\n",
180187
" lenght(row not cols) of the first innit distribution\n",
181188
" inpt:\n",
@@ -194,15 +201,32 @@
194201
" if hardfloat is true, floats will be returned in the initial guess and bounds,\n",
195202
" this is not recomended to use with minimize,\n",
196203
" if floats are needed in the function it is recommended to do a type check and to convert from int to float and divide\n",
204+
" inner_bounding:\n",
205+
" if True, bounds will be enforced inside the generated function and not with scipy,\n",
206+
" otherwise bounds are assumed to be useless or enforced by the optimizer\n",
197207
" \"\"\"\n",
198-
" dicf = {}\n",
208+
" if isinstance(boundings, list):\n",
209+
" dicti = OD(boundings)\n",
210+
" elif isinstance(boundings, OD):\n",
211+
" print('good type of input')\n",
212+
" elif isinstance(boundings, dict):\n",
213+
" print(\n",
214+
" \"kwargs will be in a random order, use ordered dictionnary instead\"\n",
215+
" )\n",
216+
" else:\n",
217+
" print(\"invalid input for boundings, quitting\")\n",
218+
" exit(1)\n",
219+
"\n",
220+
" dicf = OD()\n",
199221
" args = []\n",
200222
" bounds = []\n",
201223
" initg = []\n",
202-
" if factory and (inpt == '' or inpt == None):\n",
224+
" if factory and (\n",
225+
" inpt == None\n",
226+
" ): # set inpt as '' when creating the function to ignore it\n",
203227
" inpt = input(\n",
204228
" 'please input the arg that will be executed by the function')\n",
205-
" for ke, va in dicti.items():\n",
229+
" for ke, va in boundings.items():\n",
206230
" if va == bool:\n",
207231
" dicf[ke] = (0, 1)\n",
208232
" elif va == float:\n",
@@ -223,30 +247,48 @@
223247
" if lenit > 0:\n",
224248
" initguess = adpt_distr(\n",
225249
" dicf, out='array', Size=lenit, hardfloat=hardfloat)\n",
226-
" for kk, vv in dicti.items():\n",
227-
" args.append(kk)\n",
250+
" for kk, vv in dicf.items():\n",
228251
" bounds.append(vv)\n",
252+
" args.append(kk)\n",
229253
" if factory:\n",
230254
"\n",
231-
" def kwargsf(initvs):\n",
232-
" dictos = {}\n",
255+
" def kwargsf(initvs): # inner funct\n",
233256
" if not (len(initvs) == len(args)):\n",
234-
" print(\n",
235-
" \"\"\"initial values provided are not the same lenght as keywords provided,\n",
236-
" something went wrong, aborting\"\"\")\n",
237-
" exit(1)\n",
238-
" else:\n",
239-
" dictos = dicti(zip(args, initvs))\n",
240-
" if i_as_meth_arg:\n",
257+
" if isinstance(initvs,\n",
258+
" (np.ndarray,\n",
259+
" np.array)) and len(initvs[0]) == len(args):\n",
260+
" initvs = initvs[0]\n",
261+
" else:\n",
262+
" print(initvs)\n",
263+
" print(len(initvs), len(args))\n",
264+
" print(initvs.type)\n",
265+
" print(\n",
266+
" \"\"\"initial values provided are not the same lenght as keywords provided,\n",
267+
" something went wrong, aborting\"\"\")\n",
268+
" exit(1)\n",
269+
" if inner_bounding:\n",
270+
" for i in range(len(bounds)):\n",
271+
" maxx = max(bounds[i])\n",
272+
" minn = min(bounds[i])\n",
273+
" if initvs[i] > maxx:\n",
274+
" initvs[i] = maxx\n",
275+
" elif initvs[i] < minn:\n",
276+
" initvs[i] = minn\n",
277+
" dictos = dict(zip(args, initvs))\n",
278+
" if len(inpt) == 0 or len(\n",
279+
" inpt) == 1: # no static input, only values to optimize\n",
280+
" instt = funck(**staticD, **dictos)\n",
281+
" elif i_as_meth_arg:\n",
241282
" # an alternative may be instt=funck(inpt,**staticD,**dictos)\n",
242283
" instt = funck(**staticD, **dictos)\n",
243284
" else:\n",
244285
" instt = funck(inpt, **staticD, **dictos)\n",
245286
" # if an element is present in both dictos and staticD, dictos will overwrite it\n",
246287
" # if you want the element in staticD to never change, place it after dictos\n",
247288
" # check if executing the function return an output\n",
248-
" if not (isinstance(instt,\n",
249-
" (tuple, array, ndarray, int, float, list))):\n",
289+
" if not (isinstance(instt, (tuple, int, float, list))\n",
290+
" or isinstance(instt,\n",
291+
" (np.array, np.ndarray, pd.DataFrame))):\n",
250292
" # if no value output is returned then it is assumed that the function is a class instance\n",
251293
" if i_as_meth_arg:\n",
252294
" outa = getattr(instt, Cmeth)(inpt)\n",
@@ -257,10 +299,16 @@
257299
" return (instt)\n",
258300
"\n",
259301
" if lenit > 0:\n",
302+
" if inner_bounding:\n",
303+
" return (kwargsf, initguess, args)\n",
260304
" return (kwargsf, initguess, bounds, args)\n",
261305
" else:\n",
306+
" if inner_bounding:\n",
307+
" return (kwargsf, args)\n",
262308
" return (kwargsf, bounds, args)\n",
263309
" else:\n",
310+
" if inner_bounding:\n",
311+
" return (initguess, args)\n",
264312
" return (initguess, bounds, args)"
265313
]
266314
},
@@ -293,10 +341,10 @@
293341
"\n",
294342
"\n",
295343
"# our dinctionnary with our bounds and variable to optimize\n",
296-
"kwarg = {'first_V': (0, 23), 'second_V': (bool), 'third_V': (float)}\n",
344+
"kwarg = [('first_V', (0, 23)), ('second_V', (bool)), ('third_V', (float))]\n",
297345
"\n",
298346
"Function, Vector_init, Bounds, Args = dicwrap(\n",
299-
" foo, dicti=kwarg, lenit=1, inpt=data)\n",
347+
" foo, dicti=kwarg, lenit=1, inpt='')\n",
300348
"optimized = minimize(fun=Function, x0=Vector_init, bounds=Bounds)\n",
301349
"optimize_kwargs = zip(Args, optimized)"
302350
]
@@ -337,14 +385,14 @@
337385
" self.fourth = fourth_V\n",
338386
"\n",
339387
" def EXEC(self, data):\n",
340-
" # do something using the instance variables set by init\n",
388+
" # do something using the instance variables set by init and some data\n",
341389
" pass\n",
342390
"\n",
343391
"\n",
344392
"# our dinctionnary with our bounds and variable to optimize\n",
345-
"kwarg1 = {'first_V': (0, 23), 'second_V': (float)}\n",
346-
"kwarg2 = {'third_V': (13, 38), 'fourth_V': (bool)}\n",
347-
"optimized_kwargs = dict() # create empty dict to ensure everything goes well\n",
393+
"kwarg1 = [('first_V', (0, 23)), ('second_V', (float))]\n",
394+
"kwarg2 = [('third_V', (13, 38)), ('fourth_V', (bool))]\n",
395+
"optimized_kwargs = OD() # create empty dict to ensure everything goes well\n",
348396
"\n",
349397
"for dicto in [kwarg1, kwarg2]:\n",
350398
" Function, Vector_init, Bounds, Args = dicwrap(\n",

0 commit comments

Comments
 (0)