""" Module 2 – Patient Information Analysis. Encodes patient demographics and procedure information into a fixed-length feature vector used for downstream feature fusion. """ import numpy as np PROCEDURES = [ 'rhinoplasty', 'blepharoplasty', 'rhytidectomy', 'genioplasty', 'forehead_surgery', 'brow_lift', ] FITZPATRICK_MAP = { 'I': 0.0, '1': 0.0, 'TYPE I': 0.0, 'II': 0.2, '2': 0.2, 'TYPE II': 0.2, 'III': 0.4, '3': 0.4, 'TYPE III': 0.4, 'IV': 0.6, '4': 0.6, 'TYPE IV': 0.6, 'V': 0.8, '5': 0.8, 'TYPE V': 0.8, 'VI': 1.0, '6': 1.0, 'TYPE VI': 1.0, } SEX_MAP = { 'm': 0.0, 'male': 0.0, 'f': 1.0, 'female': 1.0, 'other': 0.5, } def analyze_patient( age: str, sex: str, skin_type: str, procedure: str, treatment_history: str, ) -> dict: """ Encode patient information into a numerical feature vector. Feature vector layout (12 elements): [0] – age normalised (age / 100) [1] – sex encoding (M=0.0, F=1.0, Other=0.5) [2] – skin type Fitzpatrick (I=0.0 … VI=1.0) [3–8] – procedure one-hot (6 dims) [9] – treatment_history (0 or 1) [10–11] – reserved zeros (padding to reach 12) Parameters ---------- age : str Patient age as string integer. sex : str 'M', 'Male', 'F', 'Female', or 'Other'. skin_type : str Fitzpatrick type I–VI. procedure : str One of the six supported procedure ids. treatment_history : str '0' or '1'. Returns ------- dict with keys: feature_vector – np.ndarray of shape (12,), dtype float32 summary – human-readable dict of encoded values """ # --- Age --- try: age_val = max(0.0, min(1.0, float(age) / 100.0)) except (ValueError, TypeError): age_val = 0.3 # --- Sex --- sex_key = str(sex).strip().lower() if sex is not None else 'other' sex_val = SEX_MAP.get(sex_key, 0.5) # --- Skin type --- skin_key = str(skin_type).strip().upper() if skin_type is not None else 'III' skin_val = FITZPATRICK_MAP.get(skin_key, 0.4) # --- Procedure one-hot (6 dims) --- proc_onehot = [0.0] * len(PROCEDURES) if procedure in PROCEDURES: proc_onehot[PROCEDURES.index(procedure)] = 1.0 # --- Treatment history --- try: tx_val = float(int(treatment_history)) except (ValueError, TypeError): tx_val = 0.0 tx_val = max(0.0, min(1.0, tx_val)) # Assemble vector vector = [age_val, sex_val, skin_val] + proc_onehot + [tx_val, 0.0, 0.0] feature_vector = np.array(vector, dtype=np.float32) summary = { 'age_encoded': round(age_val, 4), 'sex_encoded': sex_val, 'skin_type_encoded': skin_val, 'procedure': procedure, 'procedure_onehot': proc_onehot, 'treatment_history': tx_val, } return { 'feature_vector': feature_vector, 'summary': summary, }