Spaces:
Build error
Build error
massive update to column_matcher() function on 27 Jan 2025
Browse files- HF_processor.py +35 -15
HF_processor.py
CHANGED
|
@@ -74,22 +74,34 @@ class FMEADataPipeline:
|
|
| 74 |
self.fmea.to_excel('processed_fmea.xlsx', index=False)
|
| 75 |
return self.fmea
|
| 76 |
|
| 77 |
-
def
|
| 78 |
for code, sap in zip(self.fmea_code['fmea code'], [self.object_part, self.symptom, self.damage, self.cause]):
|
| 79 |
# Find the matching code group for the current FMEA code
|
| 80 |
matching_code_group = self.code_group[self.code_group['fmea code'] == code]['Code group']
|
| 81 |
if matching_code_group.empty:
|
| 82 |
continue # Skip if no matching code group is found
|
| 83 |
-
|
| 84 |
# Get the first matching code group value
|
| 85 |
matching_code_group_value = matching_code_group.values[0]
|
| 86 |
-
|
| 87 |
-
# Filter the corresponding SAP table by the matching code group
|
| 88 |
-
filtered_table_2 = sap[sap['Code group'] == matching_code_group_value]
|
| 89 |
-
s = filtered_table_2['Short text'].tolist()
|
| 90 |
-
m = self.fmea[code].apply(lambda x: process.extract(x, s, limit=1))
|
| 91 |
-
m2 = m.apply(lambda x: ', '.join([i[0] for i in x if i[1] >= self.threshold]))
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
s_all = sap['Short text'].tolist()
|
| 94 |
m_all = self.fmea[code].apply(lambda x: process.extract(x, s_all, limit=1))
|
| 95 |
m2_all = m_all.apply(lambda x: ''.join([i[0] for i in x if i[1] >= self.threshold]))
|
|
@@ -97,22 +109,30 @@ class FMEADataPipeline:
|
|
| 97 |
# Add "_secondary" flag to m2_all values
|
| 98 |
m2_all_flagged = m2_all.apply(lambda x: f"{x}_secondary" if x else x)
|
| 99 |
|
| 100 |
-
# Merge
|
| 101 |
-
merged_m2 =
|
|
|
|
| 102 |
|
|
|
|
| 103 |
mapping_dict_code = sap.set_index('Short text')['Code'].to_dict()
|
| 104 |
mapping_dict_short_text = sap.set_index('Code')['Short text'].to_dict()
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
name =
|
| 108 |
|
| 109 |
-
# Apply the mapping
|
| 110 |
self.fmea[name] = merged_m2.apply(
|
| 111 |
lambda x: mapping_dict_code.get(x.replace("_secondary", "")) if x else None
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
def column_arranger(self):
|
|
|
|
| 74 |
self.fmea.to_excel('processed_fmea.xlsx', index=False)
|
| 75 |
return self.fmea
|
| 76 |
|
| 77 |
+
def column_matcher_21Jan(self):
|
| 78 |
for code, sap in zip(self.fmea_code['fmea code'], [self.object_part, self.symptom, self.damage, self.cause]):
|
| 79 |
# Find the matching code group for the current FMEA code
|
| 80 |
matching_code_group = self.code_group[self.code_group['fmea code'] == code]['Code group']
|
| 81 |
if matching_code_group.empty:
|
| 82 |
continue # Skip if no matching code group is found
|
| 83 |
+
|
| 84 |
# Get the first matching code group value
|
| 85 |
matching_code_group_value = matching_code_group.values[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Extract the catalog profile and its group for prioritization
|
| 88 |
+
catalog_profile = self.code_group[self.code_group['fmea code'] == code]['Catalog'].values[0]
|
| 89 |
+
catalog_group = catalog_profile[1] # Second character of the catalog code
|
| 90 |
+
|
| 91 |
+
# Filter SAP table for each priority level
|
| 92 |
+
# 1. Catalog Profile
|
| 93 |
+
profile_sap = sap[sap['Code group'] == catalog_profile]
|
| 94 |
+
s_profile = profile_sap['Short text'].tolist()
|
| 95 |
+
m_profile = self.fmea[code].apply(lambda x: process.extract(x, s_profile, limit=1))
|
| 96 |
+
m2_profile = m_profile.apply(lambda x: ', '.join([i[0] for i in x if i[1] >= self.threshold]))
|
| 97 |
+
|
| 98 |
+
# 2. Catalog Group (excluding the catalog profile)
|
| 99 |
+
group_sap = sap[(sap['Code group'].str[1] == catalog_group) & (sap['Code group'] != catalog_profile)]
|
| 100 |
+
s_group = group_sap['Short text'].tolist()
|
| 101 |
+
m_group = self.fmea[code].apply(lambda x: process.extract(x, s_group, limit=1))
|
| 102 |
+
m2_group = m_group.apply(lambda x: ', '.join([i[0] for i in x if i[1] >= self.threshold]))
|
| 103 |
+
|
| 104 |
+
# 3. Entire SAP catalog
|
| 105 |
s_all = sap['Short text'].tolist()
|
| 106 |
m_all = self.fmea[code].apply(lambda x: process.extract(x, s_all, limit=1))
|
| 107 |
m2_all = m_all.apply(lambda x: ''.join([i[0] for i in x if i[1] >= self.threshold]))
|
|
|
|
| 109 |
# Add "_secondary" flag to m2_all values
|
| 110 |
m2_all_flagged = m2_all.apply(lambda x: f"{x}_secondary" if x else x)
|
| 111 |
|
| 112 |
+
# Merge prioritized matches: Profile > Group > All
|
| 113 |
+
merged_m2 = m2_profile.combine(m2_group, lambda x, y: x if x else y)
|
| 114 |
+
merged_m2 = merged_m2.combine(m2_all_flagged, lambda x, y: x if x else y)
|
| 115 |
|
| 116 |
+
# Create mapping dictionaries
|
| 117 |
mapping_dict_code = sap.set_index('Short text')['Code'].to_dict()
|
| 118 |
mapping_dict_short_text = sap.set_index('Code')['Short text'].to_dict()
|
| 119 |
+
catalog_code_dict = sap.set_index('Short text')['Code group'].to_dict()
|
| 120 |
|
| 121 |
+
# Create the new column name based on catalog
|
| 122 |
+
name = catalog_profile
|
| 123 |
|
| 124 |
+
# Apply the mapping for the catalog column
|
| 125 |
self.fmea[name] = merged_m2.apply(
|
| 126 |
lambda x: mapping_dict_code.get(x.replace("_secondary", "")) if x else None
|
| 127 |
)
|
| 128 |
|
| 129 |
+
# Construct the description column with catalog code source
|
| 130 |
+
self.fmea[f"{name}_description"] = merged_m2.apply(
|
| 131 |
+
lambda x: (
|
| 132 |
+
f"{x.replace('_secondary', '')} ({catalog_code_dict.get(x.replace('_secondary', ''), 'Unknown')})"
|
| 133 |
+
if "_secondary" in x else
|
| 134 |
+
mapping_dict_short_text.get(mapping_dict_code.get(x), x)
|
| 135 |
+
)
|
| 136 |
)
|
| 137 |
|
| 138 |
def column_arranger(self):
|