mg-pso-gui 0.1.122__py3-none-any.whl → 0.1.124__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mg-pso-gui
3
- Version: 0.1.122
3
+ Version: 0.1.124
4
4
  Summary: GUI for MG-PSO
5
5
  Author: Robert Cordingly
6
6
  Author-email: <rcording@uw.ed>
@@ -44,13 +44,17 @@ mgpsogui/util/recosu/__init__.py,sha256=T7_iigIlowGbPOHLO3hwihjw2kbwIg6olOMhFhNi
44
44
  mgpsogui/util/recosu/pso/__init__.py,sha256=PQ548aEKVOk6MMzxxDg7yMO_1hHfoEoYLLkGLeij73Y,247
45
45
  mgpsogui/util/recosu/pso/csip_access.py,sha256=jBli1MSShUUwQCMKOUvMaGuNwBViCrjXJi_FmSEJsZA,2721
46
46
  mgpsogui/util/recosu/pso/pso.py,sha256=iPqT6aI1vi8LYuWmtxuHkLEM9sQwhUznPInBgps7ANI,13708
47
+ mgpsogui/util/recosu/sampling/__init__.py,sha256=1rGd7ccgFRBwfWgyvkYAxzlGESGEvKX78LB84G9BfuQ,106
48
+ mgpsogui/util/recosu/sampling/sample_trace_writer.py,sha256=M9w-POLlZgjL5a7J7yxr73OG6mCsS2aUuP9d3HKfkbA,1966
49
+ mgpsogui/util/recosu/sampling/sampler.py,sha256=LRMM8SNFesViw5DK6g3L8VnWBM6SIBQEmvnlZmjtFNA,4168
50
+ mgpsogui/util/recosu/sampling/sampler_task.py,sha256=uZobpR83u6xEaUzIknvW9FbB84c2AL7T-5T8O8QIhzY,2776
47
51
  mgpsogui/util/recosu/utils/__init__.py,sha256=TXz_TpNif2GeGu22pzTnkUQvaP-PmLQ9Sz4BgMIS6ig,196
48
52
  mgpsogui/util/recosu/utils/trace_writer.py,sha256=V9BJlOjCbNYGoXGEk3CF5wjifBxvarrMRXJMbDBWqI8,3023
49
53
  mgpsogui/util/recosu/utils/utils.py,sha256=QB8vftq3142ekG0ORjz0ZBHU5YknXbR0oTsrxrPAsF0,3951
50
54
  mgpsogui/util/recosu/utils/plot/__init__.py,sha256=h1KjM7_tNDv351pcwt8A6Ibb1jhwWyx5Gbu-zj-sI3Q,71
51
55
  mgpsogui/util/recosu/utils/plot/cost_steps.py,sha256=1Ce11AJyweWkmvjXPxEygzS-h8yVLmQEDLS53yjPLqQ,3779
52
- mg_pso_gui-0.1.122.dist-info/METADATA,sha256=URzxA1kwKRyErmBgKMDlKqnVUxG9vMl0c_JGR4YV-4g,9460
53
- mg_pso_gui-0.1.122.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
54
- mg_pso_gui-0.1.122.dist-info/entry_points.txt,sha256=jg82VOFjR1XDGrchs1wJSCqKYE4Ozv12aBcCSp--koA,117
55
- mg_pso_gui-0.1.122.dist-info/top_level.txt,sha256=y7JuS9xJN5YdxUsQ3PSVjN8MzQAnR146bP3ZN3PYWdE,9
56
- mg_pso_gui-0.1.122.dist-info/RECORD,,
56
+ mg_pso_gui-0.1.124.dist-info/METADATA,sha256=vdeJLEJzwo26wHCLO3g4ZudQgZZJq-H5cAOf7xkzD4Q,9460
57
+ mg_pso_gui-0.1.124.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
58
+ mg_pso_gui-0.1.124.dist-info/entry_points.txt,sha256=jg82VOFjR1XDGrchs1wJSCqKYE4Ozv12aBcCSp--koA,117
59
+ mg_pso_gui-0.1.124.dist-info/top_level.txt,sha256=y7JuS9xJN5YdxUsQ3PSVjN8MzQAnR146bP3ZN3PYWdE,9
60
+ mg_pso_gui-0.1.124.dist-info/RECORD,,
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ LUCA/PSO toolkit
5
+ =========================================
6
+ This is ...
7
+
8
+ """
9
+
@@ -0,0 +1,47 @@
1
+ import threading
2
+
3
+
4
+ class SampleTraceWriter:
5
+ trace_file: str
6
+ parameter_indices: dict[int, str]
7
+ objective_indices: dict[int, str]
8
+ write_lock: threading.Lock
9
+
10
+ def __init__(self, trace_file: str):
11
+ assert(trace_file is not None and len(trace_file) > 0)
12
+ self.trace_file = trace_file
13
+ self.parameter_indices = {}
14
+ self.objective_indices = {}
15
+ self.write_lock = threading.Lock()
16
+
17
+ def write_header(self, parameter_names: list[str], objective_names: list[str]) -> None:
18
+ with self.write_lock:
19
+ with open(self.trace_file, 'w') as writer:
20
+ writer.write("id")
21
+ self.parameter_indices = {}
22
+ index: int = 0
23
+ for name in parameter_names:
24
+ writer.write(",{}".format(name))
25
+ self.parameter_indices[index] = name
26
+ index = index + 1
27
+ self.objective_indices = {}
28
+ index = 0
29
+ for name in objective_names:
30
+ writer.write(",{}".format(name))
31
+ self.objective_indices[index] = name
32
+ index = index + 1
33
+ writer.write("\n")
34
+
35
+ def append_sample(self, sample_id: int, parameters: dict[str, any], objectives: dict[str, any]) -> None:
36
+ with self.write_lock:
37
+ with open(self.trace_file, 'a') as writer:
38
+ writer.write("{}".format(sample_id))
39
+ index: int = 0
40
+ while index < len(self.parameter_indices):
41
+ writer.write(",{}".format(parameters[self.parameter_indices[index]]))
42
+ index = index + 1
43
+ index = 0
44
+ while index < len(self.objective_indices):
45
+ writer.write(",{}".format(objectives[self.objective_indices[index]]))
46
+ index = index + 1
47
+ writer.write("\n")
@@ -0,0 +1,99 @@
1
+ from collections.abc import Iterable
2
+ import math
3
+ import asyncio
4
+ import concurrent
5
+ import datetime
6
+ from .recosu.utils import utils
7
+ from .recosu.sampling.halton.halton import HaltonSampleGenerator
8
+ from .recosu.sampling.random.random_sampler import RandomSampleGenerator
9
+ from .recosu.sampling.sampler_task import SamplerTask
10
+ from .recosu.sampling.sample_trace_writer import SampleTraceWriter
11
+
12
+
13
+ def weighted_value(weight: float, lower: float, upper: float) -> float:
14
+ return lower + weight * (upper - lower)
15
+
16
+
17
+ def get_static_parameters(args: dict[str, any]) -> dict[str, any]:
18
+ static_parameters: dict[str, any] = {}
19
+ for param in args["param"]:
20
+ static_parameters[param["name"]] = param["value"]
21
+ return static_parameters
22
+
23
+
24
+ def get_objective_names(objfunc: dict[str, any]) -> list[str]:
25
+ objective_names: list[str] = []
26
+ for of in objfunc:
27
+ objective_names.append(of["name"])
28
+ return objective_names
29
+
30
+
31
+ def thread_function(task: SamplerTask) -> tuple[bool, SamplerTask]:
32
+ return task.run_task(), task
33
+
34
+
35
+ def create_generator(method: str, count: int, num_parameters: int, **kwargs) -> Iterable[tuple[int, list[float]]]:
36
+ if method == "halton":
37
+ offset: int = 0
38
+ if "offset" in kwargs:
39
+ offset = kwargs["offset"]
40
+ return HaltonSampleGenerator(count, offset, num_parameters)
41
+ elif method == "random":
42
+ return RandomSampleGenerator(count, num_parameters)
43
+
44
+ raise Exception("Sampling method is not recognized")
45
+
46
+
47
+ def run_sampler(steps: list[dict[str, any]], args: dict[str, any], count: int, num_threads: int, method: str = "halton",
48
+ metainfo: dict[str, any] = None, conf: dict[str, any] = None, trace_file: str = "trace.csv",
49
+ **kwargs) -> dict[int, tuple[dict[str, any], dict[str, any]]]:
50
+ param_names, bounds, objfunc = utils.get_step_info(steps, 0)
51
+ generator: Iterable[tuple[int, list[float]]] = create_generator(method, count, len(param_names), **kwargs)
52
+ objective_names: list[str] = get_objective_names(objfunc)
53
+ static_parameters: dict[str, any] = get_static_parameters(args)
54
+ url: str = args["url"]
55
+ files: list[str] = args["files"]
56
+
57
+ trace: dict[int, tuple[dict[str, float], dict[str, float]]] = {}
58
+ trace_writer: SampleTraceWriter = SampleTraceWriter(trace_file)
59
+ trace_writer.write_header(param_names, objective_names)
60
+
61
+ with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
62
+ futures = []
63
+ for sample_id, sample in generator:
64
+ params: dict[str, float] = {}
65
+ index: int = 0
66
+ while index < len(sample):
67
+ params[param_names[index]] = weighted_value(sample[index], bounds[0][index], bounds[1][index])
68
+ index += 1
69
+
70
+ task: SamplerTask = SamplerTask(sample_id, params, objfunc, static_parameters, url, files, metainfo, conf)
71
+ futures.append(executor.submit(thread_function, task))
72
+ # for future in concurrent.futures.as_completed(futures):
73
+ # pass
74
+ num_finished: int = 0
75
+ percentage: float
76
+ last_percentage: float = 0
77
+ for future in concurrent.futures.as_completed(futures):
78
+ try:
79
+ successful, task = future.result()
80
+
81
+ if successful:
82
+ trace[task.task_id] = (task.parameters, task.result)
83
+ trace_writer.append_sample(task.task_id, task.parameters, task.result)
84
+ else:
85
+ print("Failed to successfully execute task: {}", task.task_id)
86
+ except asyncio.CancelledError as ce:
87
+ pass
88
+ except asyncio.InvalidStateError as ise:
89
+ pass
90
+ except Exception as ex:
91
+ print(ex)
92
+
93
+ num_finished = num_finished + 1
94
+ percentage = math.trunc(num_finished / count * 1000) / 10
95
+ if percentage > last_percentage:
96
+ last_percentage = percentage
97
+ print("{}% Done {}".format(percentage, datetime.datetime.now()))
98
+
99
+ return trace
@@ -0,0 +1,75 @@
1
+ import os
2
+ from csip import Client
3
+
4
+
5
+ class SamplerTask:
6
+ task_id: int
7
+ parameters: dict[str, any]
8
+ objectives: list[dict[str, any]]
9
+ static_parameters: dict[str, any]
10
+ url: str
11
+ files: list[str]
12
+ metainfo: dict[str, any]
13
+ conf: dict[str, any]
14
+ result: dict[str, any]
15
+
16
+ def __init__(self, task_id: int, parameters: dict[str, any], objectives: list[dict[str, any]],
17
+ static_parameters: dict[str, any], url: str, files: list[str] = None, metainfo: dict[str, any] = None,
18
+ conf: dict[str, any] = None):
19
+ self.task_id = task_id
20
+ assert (parameters is not None and len(parameters) > 0)
21
+ self.parameters = parameters
22
+ assert (objectives is not None and len(objectives) > 0)
23
+ self.objectives = objectives
24
+ self.static_parameters = static_parameters if static_parameters is not None else []
25
+ assert (url is not None and len(url) > 0)
26
+ self.url = url
27
+ self.files = files if files is not None else []
28
+ self.metainfo = metainfo
29
+ self.conf = conf
30
+
31
+ def create_request(self) -> Client:
32
+ request: Client = Client(metainfo=self.metainfo)
33
+
34
+ for key, value in self.static_parameters.items():
35
+ request.add_data(key, value)
36
+
37
+ for key, value in self.parameters.items():
38
+ request.add_data(key, value)
39
+
40
+ for of in self.objectives:
41
+ request.add_cosu(of['name'], of['of'], of['data'])
42
+
43
+ return request
44
+
45
+ def run_task(self) -> bool:
46
+ self.result = {}
47
+ request: Client = self.create_request()
48
+ async_call: bool = self.conf.get('async_call', True) if self.conf is not None else True
49
+ # save response, set it to a folder if responses should be saved.
50
+ save_resp = self.conf.get('save_response_to', None) if self.conf is not None else None
51
+ successful: bool = False
52
+
53
+ response: Client = None
54
+ try:
55
+ if async_call:
56
+ response = request.execute_async(self.url, files=self.files, conf=self.conf)
57
+ else:
58
+ response = request.execute(self.url, files=self.files, conf=self.conf)
59
+
60
+ successful = response.is_finished()
61
+ if not successful:
62
+ print(response)
63
+
64
+ if save_resp:
65
+ response.save_to(os.path.join(save_resp, 'task_{}.json'.format(self.task_id)))
66
+
67
+ objectives: list[dict[str, str]] = response.get_metainfo("cosu")
68
+ for of in objectives:
69
+ self.result[of["name"]] = of["value"]
70
+ except Exception as ex:
71
+ print(ex)
72
+ print(response)
73
+ successful = False
74
+
75
+ return successful