import numpy as np import os def get_unique_chars(file_path): """ Reads a wafer map file and returns all unique characters found, excluding whitespace. """ unique_chars = set() try: with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() # Filter out empty lines before calculating max_length non_empty_lines = [line.strip() for line in lines if line.strip()] if not non_empty_lines: return [] max_length = max(len(line) for line in non_empty_lines) for line in non_empty_lines: # Process only lines that have the max length if len(line) == max_length: for char in line: if char != ' ': unique_chars.add(char) return sorted(list(unique_chars)) except Exception as e: print(f"Error reading unique chars: {e}") return [] def read_wafer_map(file_path, char_to_bin_mapping): """ Reads a wafer map file and converts it to a numerical numpy array based on the provided character-to-bin mapping. """ wafer_map = [] try: with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() non_empty_lines = [line.strip() for line in lines if line.strip()] if not non_empty_lines: return np.array([]) max_length = max(len(line) for line in non_empty_lines) for line in non_empty_lines: # Pad the line to ensure uniform length for all processed rows padded_line = line.ljust(max_length, ' ') row = [char_to_bin_mapping.get(char, -1) for char in padded_line] wafer_map.append(row) return np.array(wafer_map) except Exception as e: print(f"Error reading wafer map: {e}") return np.array([]) def save_wafer_map(wafer_map, bin_to_char_mapping, file_path): """ Saves the numerical wafer map back to a character-based file. """ with open(file_path, 'w', encoding='utf-8') as file: for row in wafer_map: file.write(''.join([bin_to_char_mapping.get(int(bin_code), ' ') for bin_code in row]) + '\n')