调整列名并添加输入验证
This commit is contained in:
+24
-7
@@ -57,7 +57,17 @@ FEATURES = [
|
||||
]
|
||||
|
||||
ID_COLUMN = "管道编号"
|
||||
PIPE_AGE_COLUMN = "管龄"
|
||||
PIPE_AGE_COLUMN = "管龄(年)"
|
||||
LEGACY_PIPE_AGE_COLUMN = "管龄"
|
||||
STATUS_COLUMN = "状态"
|
||||
EVENT_AGE_COLUMN = "事件/观察管龄(年)"
|
||||
INPUT_COLUMNS = [
|
||||
ID_COLUMN,
|
||||
PIPE_AGE_COLUMN,
|
||||
STATUS_COLUMN,
|
||||
EVENT_AGE_COLUMN,
|
||||
*FEATURES,
|
||||
]
|
||||
MATERIAL_COLUMN = "管材"
|
||||
MATERIAL_CODE_OPTIONS = [
|
||||
(1, "镀锌"),
|
||||
@@ -183,11 +193,19 @@ def secure_upload_name(original_filename: str, run_id: str) -> tuple[str, str]:
|
||||
def read_input_file(path: Path, suffix: str) -> pd.DataFrame:
|
||||
try:
|
||||
if suffix == ".csv":
|
||||
return pd.read_csv(path, dtype={ID_COLUMN: "string"})
|
||||
return pd.read_excel(path, dtype={ID_COLUMN: "string"})
|
||||
df = pd.read_csv(path, dtype={ID_COLUMN: "string"})
|
||||
else:
|
||||
df = pd.read_excel(path, dtype={ID_COLUMN: "string"})
|
||||
except Exception as exc:
|
||||
logging.exception("文件解析失败: %s", exc)
|
||||
raise PredictionError("文件解析失败,请检查编码或表格格式。")
|
||||
return normalize_input_columns(df)
|
||||
|
||||
|
||||
def normalize_input_columns(df: pd.DataFrame) -> pd.DataFrame:
|
||||
if PIPE_AGE_COLUMN not in df.columns and LEGACY_PIPE_AGE_COLUMN in df.columns:
|
||||
return df.rename(columns={LEGACY_PIPE_AGE_COLUMN: PIPE_AGE_COLUMN})
|
||||
return df
|
||||
|
||||
|
||||
def normalize_id_value(value: Any, fallback: str) -> str:
|
||||
@@ -201,8 +219,7 @@ def normalize_id_value(value: Any, fallback: str) -> str:
|
||||
def validate_input_frame(df: pd.DataFrame) -> None:
|
||||
if df.empty:
|
||||
raise PredictionError("上传文件没有可预测的数据。")
|
||||
required_columns = [ID_COLUMN, *FEATURES]
|
||||
missing = [col for col in required_columns if col not in df.columns]
|
||||
missing = [col for col in INPUT_COLUMNS if col not in df.columns]
|
||||
if missing:
|
||||
raise PredictionError(f"缺少必要字段: {', '.join(missing)}")
|
||||
|
||||
@@ -447,8 +464,8 @@ def render_survival_chart(df: pd.DataFrame, curves, image_path: Path) -> tuple[l
|
||||
)
|
||||
summary_sheet_rows.append(
|
||||
{
|
||||
"管道编号": pipe_id,
|
||||
"管龄": pipe_age,
|
||||
ID_COLUMN: pipe_id,
|
||||
PIPE_AGE_COLUMN: pipe_age,
|
||||
"健康等级": grade_label,
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user