pyfemtet 0.8.4__py3-none-any.whl → 0.8.5__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.

Potentially problematic release.


This version of pyfemtet might be problematic. Click here for more details.

pyfemtet/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.8.4"
1
+ __version__ = "0.8.5"
pyfemtet/opt/_femopt.py CHANGED
@@ -403,7 +403,7 @@ class FEMOpt:
403
403
 
404
404
  if names is not None:
405
405
  if isinstance(names, str):
406
- names = [f'name_{i}' for i in range(n_return)]
406
+ names = [f'{names}_{i}' for i in range(n_return)]
407
407
  else:
408
408
  # names = names
409
409
  pass
@@ -59,32 +59,39 @@ def _get_obj_from_csv(csv_path, encoding=ENCODING):
59
59
  reader = csv.reader(f, delimiter=",")
60
60
  meta = reader.__next__()
61
61
  obj_indices = np.where(np.array(meta) == "obj")[0]
62
- out = df.iloc[:, obj_indices]
62
+ out: pd.DataFrame = df.iloc[:, obj_indices]
63
+ out = out.dropna(axis=0)
63
64
  return out, columns
64
65
 
65
66
 
66
- def is_equal_result(ref_path, dif_path, log_path):
67
+ def is_equal_result(ref_path, dif_path, log_path=None, threashold=0.05):
67
68
  """Check the equality of two result csv files."""
68
69
  ref_df, ref_columns = _get_obj_from_csv(ref_path)
69
70
  dif_df, dif_columns = _get_obj_from_csv(dif_path)
70
71
 
71
- with open(log_path, "a", newline="\n", encoding=ENCODING) as f:
72
- f.write("\n\n===== 結果の分析 =====\n\n")
73
- f.write(f" \tref\tdif\n")
74
- f.write(f"---------------------\n")
75
- f.write(f"len(col)\t{len(ref_columns)}\t{len(dif_columns)}\n")
76
- f.write(f"len(df) \t{len(ref_df)}\t{len(dif_df)}\n")
77
- try:
78
- difference = (
72
+ if log_path is not None:
73
+ with open(log_path, "a", newline="\n", encoding=ENCODING) as f:
74
+ f.write("\n\n===== 結果の分析 =====\n\n")
75
+ f.write(f" \tref\tdif\n")
76
+ f.write(f"---------------------\n")
77
+ f.write(f"len(col)\t{len(ref_columns)}\t{len(dif_columns)}\n")
78
+ f.write(f"len(df) \t{len(ref_df)}\t{len(dif_df)}\n")
79
+ try:
80
+ difference = (
81
+ np.abs(ref_df.values - dif_df.values) / np.abs(dif_df.values)
82
+ ).mean()
83
+ f.write(f"diff \t{int(difference*100)}%\n")
84
+ except Exception:
85
+ f.write(f"diff \tcannot calc\n")
86
+
87
+ else:
88
+ difference = (
79
89
  np.abs(ref_df.values - dif_df.values) / np.abs(dif_df.values)
80
- ).mean()
81
- f.write(f"diff \t{int(difference*100)}%\n")
82
- except Exception:
83
- f.write(f"diff \tcannot calc\n")
90
+ ).mean()
84
91
 
85
92
  assert len(ref_columns) == len(dif_columns), "結果 csv の column 数が異なります。"
86
93
  assert len(ref_df) == len(dif_df), "結果 csv の row 数が異なります。"
87
- assert difference <= 0.05, "前回の結果との平均差異が 5% を超えています。"
94
+ assert difference <= threashold*100, f"前回の結果との平均差異が {int(difference)}% で {int(threashold*100)}% を超えています。"
88
95
 
89
96
 
90
97
  def _get_simplified_df_values(csv_path, exclude_columns=None):
@@ -21,7 +21,9 @@ else:
21
21
  FemtetWithNXInterface = type('FemtetWithNXInterface', (FemtetInterface,), {})
22
22
  ExcelInterface = type('FemtetInterface', (NotAvailableForWindows,), {})
23
23
 
24
- from pyfemtet.opt.interface._surrogate import PoFBoTorchInterface
24
+ from pyfemtet.opt.interface._surrogate._base import SurrogateModelInterfaceBase
25
+ from pyfemtet.opt.interface._surrogate._singletaskgp import PoFBoTorchInterface
26
+
25
27
 
26
28
  __all__ =[
27
29
  'FEMInterface',
@@ -30,5 +32,6 @@ __all__ =[
30
32
  'FemtetWithSolidworksInterface',
31
33
  'FemtetWithNXInterface',
32
34
  'ExcelInterface',
35
+ 'SurrogateModelInterfaceBase',
33
36
  'PoFBoTorchInterface',
34
37
  ]
@@ -17,6 +17,7 @@ class SurrogateModelInterfaceBase(FEMInterface, ABC):
17
17
  self,
18
18
  history_path: str = None,
19
19
  history: History = None,
20
+ override_objective: bool = True,
20
21
  ):
21
22
 
22
23
  self.history: History
@@ -25,6 +26,7 @@ class SurrogateModelInterfaceBase(FEMInterface, ABC):
25
26
  self.obj: dict[str, float] = dict()
26
27
  self.df_prm: pd.DataFrame
27
28
  self.df_obj: pd.DataFrame
29
+ self.override_objective: bool = override_objective
28
30
 
29
31
  # history_path が与えられた場合、history をコンストラクトする
30
32
  if history_path is not None:
@@ -57,27 +59,25 @@ class SurrogateModelInterfaceBase(FEMInterface, ABC):
57
59
  FEMInterface.__init__(
58
60
  self,
59
61
  history=history, # コンストラクト済み history を渡せば並列計算時も何もしなくてよい
62
+ override_objective=self.override_objective
60
63
  )
61
64
 
62
65
  def filter_feasible(self, x: np.ndarray, y: np.ndarray, return_feasibility=False):
63
66
  feasible_idx = np.where(~np.isnan(y.sum(axis=1)))
64
67
  if return_feasibility:
65
68
  # calculated or not
66
- y = np.zeros_like(y)
67
- y[feasible_idx] = 1.
68
- # satisfy weak feasibility or not
69
- infeasible_idx = np.where(~self.history.get_df()['feasible'].values)
70
- y[infeasible_idx] = .0
71
- return x, y.reshape((-1, 1))
69
+ feas = np.zeros((len(y), 1), dtype=float)
70
+ feas[feasible_idx] = 1.
71
+ return x, feas
72
72
  else:
73
73
  return x[feasible_idx], y[feasible_idx]
74
74
 
75
75
  def _setup_after_parallel(self, *args, **kwargs):
76
-
77
- opt: AbstractOptimizer = kwargs['opt']
78
- obj: Objective
79
- for obj_name, obj in opt.objectives.items():
80
- obj.fun = lambda: self.obj[obj_name]
76
+ if self.override_objective:
77
+ opt: AbstractOptimizer = kwargs['opt']
78
+ obj: Objective
79
+ for obj_name, obj in opt.objectives.items():
80
+ obj.fun = lambda obj_name_=obj_name: self.obj[obj_name_]
81
81
 
82
82
  def update_parameter(self, parameters: pd.DataFrame, with_warning=False) -> Optional[List[str]]:
83
83
  for i, row in parameters.iterrows():
@@ -27,8 +27,8 @@ class PoFBoTorchInterface(SurrogateModelInterfaceBase):
27
27
  def train_f(self):
28
28
  # df そのまま用いて training する
29
29
  x, y = self.filter_feasible(self.df_prm.values, self.df_obj.values, return_feasibility=True)
30
- if y.min() == 1:
31
- self.model_f.predict = lambda *args, **kwargs: (1., 0.001)
30
+ if y.min() == 1: # feasible values only
31
+ self.model_f.predict = lambda *args, **kwargs: (1., 0.001) # mean, std
32
32
  self.model_f.fit(x, y)
33
33
 
34
34
  def _setup_after_parallel(self, *args, **kwargs):
@@ -64,7 +64,8 @@ class PoFBoTorchInterface(SurrogateModelInterfaceBase):
64
64
  raise SolveError(Msg.INFO_POF_IS_LESS_THAN_THRESHOLD)
65
65
 
66
66
  # 実際の計算(mean は history.obj_names 順)
67
- mean, _ = self.model.predict(np.array([x]))
67
+ _mean, _std = self.model.predict(np.array([x]))
68
+ mean = _mean[0]
68
69
 
69
70
  # 目的関数の更新
70
71
  self.obj = {obj_name: value for obj_name, value in zip(self.history.obj_names, mean)}
@@ -85,6 +85,8 @@ class SingleTaskGPModel(PredictionModelBase):
85
85
  fit_gpytorch_mll(mll)
86
86
 
87
87
  def predict(self, x: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
88
+ assert len(x.shape) >= 2
89
+
88
90
  X = tensor(x)
89
91
 
90
92
  post = self.gp.posterior(X)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pyfemtet
3
- Version: 0.8.4
3
+ Version: 0.8.5
4
4
  Summary: Design parameter optimization using Femtet.
5
5
  License: BSD-3-Clause
6
6
  Author: kazuma.naito
@@ -1,4 +1,4 @@
1
- pyfemtet/__init__.py,sha256=oR63M0ef9dMrWZnMjTb9xk69cW2iS6rluqpyJ5_0C84,21
1
+ pyfemtet/__init__.py,sha256=ryTvYnQfAb0LetADNMrzLGomNovaozHgioSBOHXn2ms,21
2
2
  pyfemtet/_femtet_config_util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  pyfemtet/_femtet_config_util/autosave.py,sha256=dNirA9XGuFehas8_Jkj2BW9GOzMbPyhnt1WHcH_ObSU,2070
4
4
  pyfemtet/_femtet_config_util/exit.py,sha256=0BWID-tjOkmZwmgPFkcJMkWW39voccz5ARIBWvZbHaw,1877
@@ -24,12 +24,12 @@ pyfemtet/dispatch_extensions/_impl.py,sha256=yH_yeAnQ-Xi9GfjX-FQt9u3yHnrLYIteRb6
24
24
  pyfemtet/logger/__init__.py,sha256=UOJ9n_U2xwdTrp0Xgg-N6geySxNzKqTBQlXsaH0kW_w,420
25
25
  pyfemtet/logger/_impl.py,sha256=rsAd0HpmveOaLS39ucp3U2OcDhQMWjC5fnVGhbJtWVw,6375
26
26
  pyfemtet/opt/__init__.py,sha256=wRR8LbEhb5I6MUgmnCgjB6-tqHlOVxDIo7yPkq0QbBs,758
27
- pyfemtet/opt/_femopt.py,sha256=vqLUGMMHn0lp0bRE_FEV0sOoUcVAbohy9_8OLYw3ZrU,39217
27
+ pyfemtet/opt/_femopt.py,sha256=O6Fpaaz2x42iaHI60uMXBFJNWrCGgWYnQsGbnQALzPM,39220
28
28
  pyfemtet/opt/_femopt_core.py,sha256=mA2wQ5h_mmbTQ9ilhDHLUyN-jsWFDpJE2r5guUWlS10,38083
29
29
  pyfemtet/opt/_test_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
30
30
  pyfemtet/opt/_test_utils/control_femtet.py,sha256=8oAl9y5V2n8Nnsgx_ebcZVzwFt1eI3swkdiKg6pg3-M,1085
31
31
  pyfemtet/opt/_test_utils/hyper_sphere.py,sha256=nQhw8EIY0DwvcTqrbKhkxiITLZifr4-nG77E-_6ggmA,700
32
- pyfemtet/opt/_test_utils/record_history.py,sha256=JCNJLZMCNTpJ6VT7iwEt2DIbwmsuQmgC0ClQSfcatj4,3915
32
+ pyfemtet/opt/_test_utils/record_history.py,sha256=mACxWglF7GvLwhvKUC0IhUFHFRrhs2UfTyJqy7vhpbE,4244
33
33
  pyfemtet/opt/advanced_samples/excel_ui/(ref) original_project.femprj,sha256=5OqZfynTpVCrgEIOBOMYuDGaMvepi5lojVNFr1jAsEI,157489
34
34
  pyfemtet/opt/advanced_samples/excel_ui/femtet-macro.xlsm,sha256=ckF0SQ0f3IWSW6QoH1IPJdwUUlR7O_AiGC5fi8SI3jA,133137
35
35
  pyfemtet/opt/advanced_samples/excel_ui/pyfemtet-core.py,sha256=aF2TWXdbt7dnkeBqqVO6GvIExozjFp0mxx3BX8rpYNc,9879
@@ -39,7 +39,7 @@ pyfemtet/opt/advanced_samples/surrogate_model/gal_ex13_create_training_data_jp.p
39
39
  pyfemtet/opt/advanced_samples/surrogate_model/gal_ex13_optimize_with_surrogate.py,sha256=s0b31wuN3iXjb78dt0ro0ZjxHa8uLIH94jRfEuj1EVY,3090
40
40
  pyfemtet/opt/advanced_samples/surrogate_model/gal_ex13_optimize_with_surrogate_jp.py,sha256=OAOpHKyMMo1StSqNMqx4saYDn4hiGOKDypyK6uhTILQ,3215
41
41
  pyfemtet/opt/advanced_samples/surrogate_model/gal_ex13_parametric.femprj,sha256=iIHH1X-wWBqEYj4cFJXco73LCJXSrYBsSKOD0HxYu60,87599
42
- pyfemtet/opt/interface/__init__.py,sha256=dkQ8PoIzYJVRtckNN2VgL7FEwci9RbYM3owOvqnPm80,1219
42
+ pyfemtet/opt/interface/__init__.py,sha256=na6-elI9-karOqoSxT9LfLQpjBPm1lrUWjow0NYYRP4,1349
43
43
  pyfemtet/opt/interface/_base.py,sha256=y0uQ5jdsWbgt5odyqPin7NXcK_IbUwPDcrrkV_JhpRw,2722
44
44
  pyfemtet/opt/interface/_excel_interface.py,sha256=s103vePTPXXYiPwGdAEUFgtpvGXtu1nSljDtP4HsmcY,40355
45
45
  pyfemtet/opt/interface/_femtet.py,sha256=teALmp66aJ_rrmtEOjCGDG1jGLTZr2AmvMFmuuXRQkw,34634
@@ -49,9 +49,9 @@ pyfemtet/opt/interface/_femtet_with_nx/_interface.py,sha256=LkaODUSpBLq05uz5Jf-J
49
49
  pyfemtet/opt/interface/_femtet_with_nx/update_model.py,sha256=P7VH0i_o-X9OUe6AGaLF1fACPeHNrMjcrOBCA3MMrI4,3092
50
50
  pyfemtet/opt/interface/_femtet_with_sldworks.py,sha256=rjEgebuP1w1eAFVWw4eRJUq3lsyBcmXlkMjZKIpD0kw,11019
51
51
  pyfemtet/opt/interface/_surrogate/__init__.py,sha256=2UT5NuBylyWQJNjg1zsBRCV-MzNCUswTUt6ZuSrYFUM,120
52
- pyfemtet/opt/interface/_surrogate/_base.py,sha256=-k02jRywxaSKMDzGitPE_qBj5nUxC7OL6guF4y6F1Zw,2923
52
+ pyfemtet/opt/interface/_surrogate/_base.py,sha256=bQMoztVq1b-3BW5Z1V-dSROplMHutrblDI289j0cC-E,3001
53
53
  pyfemtet/opt/interface/_surrogate/_chaospy.py,sha256=gL72bCgs1AY_EZdJtcifSC-apwsZzp4zsWYxcpVKvtw,1969
54
- pyfemtet/opt/interface/_surrogate/_singletaskgp.py,sha256=YBRm-8MRwK26qg6T5LKaAhwPfF3jLcKQV-fycP6dnlA,2406
54
+ pyfemtet/opt/interface/_surrogate/_singletaskgp.py,sha256=ojZHsxGxSc8ZJqJQ_uMHvpK98TPUsHzXP0q4tmM0YPQ,2471
55
55
  pyfemtet/opt/optimizer/__init__.py,sha256=Ia6viowECkG0IFXtFef0tJ4jDKsoDzJLqMJ9xLFH2LQ,543
56
56
  pyfemtet/opt/optimizer/_base.py,sha256=j8aQc3fGehZTJT9ETf9cr3VWYs2FYk1F8fO3f7QyKAU,13099
57
57
  pyfemtet/opt/optimizer/_optuna/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -64,7 +64,7 @@ pyfemtet/opt/optimizer/_scipy_scalar.py,sha256=rGvrLjrgfYzxK9GA0-r2Hhoaqt6A0TQsT
64
64
  pyfemtet/opt/optimizer/parameter.py,sha256=YLE9lmYRaZA8isnTPJnbYXpUn6zsJFW4xg03QaSWey8,3950
65
65
  pyfemtet/opt/prediction/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
66
66
  pyfemtet/opt/prediction/_base.py,sha256=dEyEur3IntNokYK8NhPndHb2pWY_A4C1SjEejOTCUGw,2048
67
- pyfemtet/opt/prediction/single_task_gp.py,sha256=6NMSjTtzXJWaW7NEqfqOjz_37mbHpXh9Oo5_KjivRU0,3922
67
+ pyfemtet/opt/prediction/single_task_gp.py,sha256=lhJMrkh3-TPpSpPM2OYmsiZW6TVzxd0XAG2E9VAdFng,3956
68
68
  pyfemtet/opt/samples/femprj_sample/ParametricIF.femprj,sha256=9BtDHmc3cdom0Zq33DTdZ0mDAsIUY6i8SRkkg-n7GO0,442090
69
69
  pyfemtet/opt/samples/femprj_sample/ParametricIF.py,sha256=oXzchBZEbH69xacDht5HDnbZzKwapXsn6bp9qihY17Y,707
70
70
  pyfemtet/opt/samples/femprj_sample/ParametricIF_test_result.reccsv,sha256=TiOAqEDMub6SCGYClBv1JvQxphDOY3iIdr_pMmGgJ9M,2859
@@ -137,8 +137,8 @@ pyfemtet/opt/visualization/result_viewer/.gitignore,sha256=ryvb4aqbbsHireHWlPQfx
137
137
  pyfemtet/opt/visualization/result_viewer/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
138
138
  pyfemtet/opt/visualization/result_viewer/application.py,sha256=WcHBx_J5eNLKSaprpk9BGifwhO04oN8FiNGYTWorrXA,1691
139
139
  pyfemtet/opt/visualization/result_viewer/pages.py,sha256=zcsRmVpVK7xbmOpnKkSypNPsRyHcV3ingfNmuqln6nw,32171
140
- pyfemtet-0.8.4.dist-info/LICENSE,sha256=sVQBhyoglGJUu65-BP3iR6ujORI6YgEU2Qm-V4fGlOA,1485
141
- pyfemtet-0.8.4.dist-info/METADATA,sha256=gm8_jNbFddbgs4cCz1XchxJDMSfcTlBn-GHxCu0ikrE,3509
142
- pyfemtet-0.8.4.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
143
- pyfemtet-0.8.4.dist-info/entry_points.txt,sha256=ZfYqRaoiPtuWqFi2_msccyrVF0LurMn-IHlYamAegZo,104
144
- pyfemtet-0.8.4.dist-info/RECORD,,
140
+ pyfemtet-0.8.5.dist-info/LICENSE,sha256=sVQBhyoglGJUu65-BP3iR6ujORI6YgEU2Qm-V4fGlOA,1485
141
+ pyfemtet-0.8.5.dist-info/METADATA,sha256=NGd9CK6oM_7IsLx92fBkvQ34tuaWL7RZp8N4WEHvrv8,3509
142
+ pyfemtet-0.8.5.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
143
+ pyfemtet-0.8.5.dist-info/entry_points.txt,sha256=ZfYqRaoiPtuWqFi2_msccyrVF0LurMn-IHlYamAegZo,104
144
+ pyfemtet-0.8.5.dist-info/RECORD,,