import json from collections import defaultdict def loaddataset(filepath): """Load the…
- User
import json from collections import defaultdict
def load_dataset(filepath): """Load the dataset JSON file.""" with open(filepath, "r") as file: return json.load(file)
def normalize_output(output): """Normalize the Output dictionary by converting lists and dicts inside it to hashable types.""" normalized_output = {} for key, value in output.items(): if isinstance(value, list): # Convert list to tuple normalized_output[key] = tuple(normalize_list(value))
elif isinstance(value, dict): # Convert dict to tuple of key-value pairs normalized_output[key] = tuple(normalize_dict(value)) else: normalized_output[key] = value # Keep non-dict/non-list values as is return normalized_outputdef normalize_list(value): """Recursively convert lists and their inner elements to hashable structures.""" return [tuple(normalize_list(item)) if isinstance(item, list) else tuple(normalize_dict(item)) if isinstance(item, dict) else item for item in value]
def normalize_dict(value): """Recursively convert a dictionary to a hashable tuple of key-value pairs.""" return ((k, tuple(normalize_list(v)) if isinstance(v, list) else tuple(normalize_dict(v)) if isinstance(v, dict) else v) for k, v in value.items())
def check_dataset_health(dataset): """Check for incomplete data, count entries by sector, and flag structural issues.""" incomplete_entries = [] # Track incomplete entries sector_counts = defaultdict(int) # Count entries by sector
for i, entry in enumerate(dataset): # Increment count for the sector sector = entry.get("Output", {}).get("SectorClassification", "Unknown") sector_counts[sector] += 1 # Check for missing Input or Output if not entry.get("Input") or not entry.get("Output"): incomplete_entries.append((i, "Missing Input or Output entirely")) continue # Check if Input tables have at least 4 columns if not all(isinstance(table, list) and len(table) >= 4 for table in entry["Input"]): incomplete_entries.append((i, "Some Input tables are incomplete or less than 4 columns")) # Check Output fields are missing output = entry["Output"] required_fields = ["SectorClassification", "Overview", "ImportantColumns", "SuggestedPlots", "PlotExplanation", "Insights"] missing_fields = [field for field in required_fields if field not in output] if missing_fields: incomplete_entries.append((i, f"Missing Output fields: {missing_fields}")) return sector_counts, incomplete_entriesdef remove_duplicates(dataset): """Remove duplicate entries from the dataset.""" seen_entries = set() # To track unique entries cleaned_dataset = [] # To store unique entries
for entry in dataset: # Normalize Input and Output sections for comparison input_entry = tuple([tuple(table) for table in entry["Input"]]) # Normalize Input output_entry = tuple(normalize_output(entry["Output"]).items()) # Normalize Output # Combine Input and Output for full entry uniqueness full_entry = (input_entry, output_entry) if full_entry not in seen_entries: # If entry is not a duplicate seen_entries.add(full_entry) # Mark entry as seen cleaned_dataset.append(entry) # Add unique entry to the cleaned dataset return cleaned_dataset - Assistant