From a5efc548e54bd7ccda089f6b3e919b3c49310d8a Mon Sep 17 00:00:00 2001 From: Michael Winter Date: Fri, 13 Mar 2026 04:27:55 +0100 Subject: [PATCH] Clean up code: remove unused imports, variables, and functions - Remove duplicate 'permutations' import - Remove unused 'choice' import - Remove unused '_check_melodic_threshold' function - Remove unused 'sorted_permutation' variable - Update outdated comments - Simplify _initialize_chords return value --- compact_sets.py | 67 +++++++++---------------------------------------- 1 file changed, 12 insertions(+), 55 deletions(-) diff --git a/compact_sets.py b/compact_sets.py index daa8c83..7e6277a 100644 --- a/compact_sets.py +++ b/compact_sets.py @@ -13,10 +13,10 @@ Mathematical foundations: from __future__ import annotations from fractions import Fraction -from itertools import combinations, permutations, permutations +from itertools import combinations, permutations from math import prod, log from operator import add -from random import choice, choices, seed +from random import choices, seed from typing import Iterator import networkx as nx @@ -711,31 +711,6 @@ class HarmonicSpace: diff_count += 1 return diff_count == 1 - def _check_melodic_threshold( - self, - movements: dict, - threshold_cents: float, - ) -> bool: - """Check if changing pitch movements stay within melodic threshold. - - Args: - movements: Dict mapping source pitch -> {destination, cent_difference} - threshold_cents: Maximum allowed movement in cents - - Returns: - True if all movements are within threshold. - Common pitches (0 cents) always pass. - Changing pitches must have cent_difference <= threshold. - """ - for src, data in movements.items(): - cents = data["cent_difference"] - # Common pitches have 0 cent difference - always pass - # Changing pitches: check if movement is within threshold - if cents > threshold_cents: - return False - - return True - def _invert_transposition(self, trans: Pitch) -> Pitch: """Invert a transposition.""" return Pitch(tuple(-t for t in trans.hs_array), self.dims) @@ -773,21 +748,16 @@ class PathFinder: weights_config = self._default_weights_config() # Initialize - chords_data = self._initialize_chords(start_chord) - if not chords_data or not chords_data[0]: + chord = self._initialize_chords(start_chord) + if not chord or chord[0] is None or len(self.graph.nodes()) == 0: return [] - chord_pair = chords_data[0] - original_chord = chord_pair[0] if chord_pair else None - sorted_permutation = chords_data[1] if len(chords_data) > 1 else None - - if original_chord is None or len(self.graph.nodes()) == 0: - return [] + original_chord = chord[0] # The graph node to use for edge lookup (original chord from harmonic space) graph_node = original_chord - # The output chord is the original (unsorted) for testing voice crossing detection + # The output chord for path output_chord = original_chord path = [output_chord] @@ -799,7 +769,7 @@ class PathFinder: cumulative_trans = Pitch(tuple(0 for _ in range(len(dims))), dims) # Track voice mapping: voice_map[i] = which original voice is at position i - # Use identity since we're not sorting output + # Start with identity num_voices = len(output_chord.pitches) voice_map = list(range(num_voices)) @@ -854,7 +824,6 @@ class PathFinder: # Reorder pitches according to voice mapping # voice_map[i] = which original voice is at position i - # With voice_map initialized to sorted_permutation, output stays in bass-to-soprano order reordered_pitches = tuple( transposed.pitches[voice_map[i]] for i in range(num_voices) ) @@ -874,15 +843,10 @@ class PathFinder: """Initialize chord sequence. Returns: - Tuple of ((original_chord, transposed_chord), sorted_permutation) - where sorted_permutation[i] = which original voice index is at position i in sorted order + Tuple of (original_chord,) """ if start_chord is not None: - # Compute permutation from original to sorted - pitches = list(start_chord.pitches) - sorted_pitches = sorted(pitches, key=lambda p: p.to_fraction()) - sorted_permutation = [pitches.index(p) for p in sorted_pitches] - return ((start_chord, start_chord), sorted_permutation) + return (start_chord,) # Random start - try multiple times to find a chord with valid edges (respecting voice crossing) nodes = list(self.graph.nodes()) @@ -909,19 +873,12 @@ class PathFinder: if nonzero > 0: # Found a valid starting chord - pitches = list(chord.pitches) - sorted_pitches = sorted(pitches, key=lambda p: p.to_fraction()) - sorted_permutation = [pitches.index(p) for p in sorted_pitches] - return ((chord, chord), sorted_permutation) + return (chord,) # Fall back to first node if none found - chord = nodes[0] - pitches = list(chord.pitches) - sorted_pitches = sorted(pitches, key=lambda p: p.to_fraction()) - sorted_permutation = [pitches.index(p) for p in sorted_pitches] - return ((chord, chord), sorted_permutation) + return (nodes[0],) - return ((), None) + return (None,) def _default_weights_config(self) -> dict: """Default weights configuration."""