Skip to content

Commit

Permalink
fix insert_value2sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
itlubber committed Apr 19, 2024
1 parent 98388ae commit bb78cd4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 12 additions & 4 deletions scorecardpipeline/excel_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def move_sheet(self, worksheet, offset: int = 0, index: int = None):

self.workbook.move_sheet(worksheet, offset=offset)

def insert_value2sheet(self, worksheet, insert_space, value="", style="content", auto_width=False, end_space=None):
def insert_value2sheet(self, worksheet, insert_space, value="", style="content", auto_width=False, end_space=None, align: dict=None, max_col_width=50):
"""
向sheet中的某个单元格插入某种样式的内容
Expand All @@ -137,6 +137,8 @@ def insert_value2sheet(self, worksheet, insert_space, value="", style="content",
:param style: 渲染的样式,参考 init_style 中初始设置的样式
:param end_space: 如果需要合并单元格,传入需要截止的单元格位置信息,可以是 "B2" 或者 (2, 2) 任意一种形式
:param auto_width: 是否开启自动调整列宽
:param align: 文本排列方式, 参考: Alignment
:param max_col_width: 单元格列最大宽度,默认 50
:return: 返回插入元素最后一列之后、最后一行之后的位置
"""
Expand All @@ -150,6 +152,11 @@ def insert_value2sheet(self, worksheet, insert_space, value="", style="content",
cell = worksheet[f"{start_col}{start_row}"]
cell.style = style

if align:
_align = {"horizontal": "center", "vertical": "center"}
_align.update(align)
cell.alignment = Alignment(**_align)

if end_space is not None:
if isinstance(end_space, str):
end_col = re.findall('\D+', end_space)[0]
Expand All @@ -163,12 +170,13 @@ def insert_value2sheet(self, worksheet, insert_space, value="", style="content",
worksheet[f"{start_col}{start_row}"] = value

if auto_width:
# original_styles = [worksheet[f"{start_col}{i}"].fill for i in range(1, worksheet.max_row + 1)]
# original_styles = [worksheet[f"{start_col}{i}"].fill.copy() for i in range(1, worksheet.max_row + 1)]
curr_width = worksheet.column_dimensions[start_col].width
auto_width = min(max([(self.check_contain_chinese(value)[1] * self.english_width + self.check_contain_chinese(value)[2] * self.chinese_width) * self.fontsize, 10, curr_width]), 50)
auto_width = min(max([(self.check_contain_chinese(value)[1] * self.english_width + self.check_contain_chinese(value)[2] * self.chinese_width) * self.fontsize, 10, curr_width]), max_col_width)
worksheet.column_dimensions[start_col].width = auto_width

# for i in range(worksheet.max_row):
# worksheet[f"{start_col}{i + 1}"].fill = original_styles[i].copy()
# worksheet[f"{start_col}{i + 1}"].fill = original_styles[i]

if end_space is not None:
return end_row + 1, column_index_from_string(end_col) + 1
Expand Down
11 changes: 6 additions & 5 deletions scorecardpipeline/scorecard.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _transform(self, X):

class BoxCoxScoreTransformer(BaseScoreTransformer):
def __init__(self, down_lmt=300, up_lmt=1000, greater_is_better=True, cutoff=None):
super(BoxCoxScoreTransformer, self).__init__(down_lmt=down_lmt, up_lmt=up_lmt, greater_is_better=greater_is_better, cutoff=cutoff)
super().__init__(down_lmt=down_lmt, up_lmt=up_lmt, greater_is_better=greater_is_better, cutoff=cutoff)

@staticmethod
def _box_cox_optimize(x):
Expand All @@ -205,6 +205,7 @@ def fit(self, X, y=None, **fit_params):
self.lambdas_ = np.array([self._box_cox_optimize(col) for col in X.T])
for i, lmbda in enumerate(self.lambdas_):
X[:, i] = stats.boxcox(X[:, i], lmbda)
print(X.max(), X.min(), X.mean())
self.scaler_ = MinMaxScaler(feature_range=(self.down_lmt, self.up_lmt)).fit(X)
return self

Expand Down Expand Up @@ -295,16 +296,16 @@ def _box_cox_inverse_tranform(x, lmbda):

test_select = h2o.H2OFrame(load_pickle("/Users/lubberit/Desktop/workspace/scorecardpipeline/examples/model_report/h2o_model/test_select.pkl"))

model_path = 'model_report/h2o_model/StackedEnsemble_BestOfFamily_1_AutoML_1_20240415_162619'
model_path = '/Users/lubberit/Desktop/workspace/scorecardpipeline/examples/model_report/h2o_model/StackedEnsemble_BestOfFamily_1_AutoML_1_20240415_162619'
best_model = h2o.load_model(model_path)

score_transform = StandardScoreTransformer(base_score=400, pdo=50, bad_rate=test_select["target"].mean()[0], greater_is_better=True)
# score_transform = BoxCoxScoreTransformer(greater_is_better=False)
# score_transform = StandardScoreTransformer(base_score=400, pdo=50, bad_rate=test_select["target"].mean()[0], greater_is_better=True)
score_transform = BoxCoxScoreTransformer(greater_is_better=False)
y_pred = best_model.predict(test_select).as_data_frame()[["p1"]]
score_transform.fit(y_pred)

print(best_model.predict(test_select))
score = score_transform.transform(y_pred)
print(score)
print(score_transform.inverse_transform(score))
print(score_transform.scorecard_scale())
# print(score_transform.scorecard_scale())

0 comments on commit bb78cd4

Please sign in to comment.