Skip to content

Conversation

@jon-myers
Copy link
Contributor

Fixes #6

This PR resolves the discontinuous jumps in the Trajectory ID 6 (Yoyo) compute() method that were causing sharp frequency spikes in melodic contour visualizations.

Changes

  • Fixed segment index finding logic in id6() method
  • Replaced buggy loop with proper boundary logic matching TypeScript findLastIndex behavior
  • Added bounds checking for relative x position within segments
  • Ensures smooth interpolation between multiple pitches using segmented half-cosine interpolation

Testing

  • Verified fix resolves the reported 0.214 octave jumps
  • Existing tests continue to pass
  • Maintains backwards compatibility

🤖 Generated with Claude Code

…thod

- Fixed segment index finding logic in id6() method that was causing
  sharp discontinuous frequency jumps in melodic contour visualizations
- Replaced buggy loop with proper boundary logic that matches TypeScript
  findLastIndex behavior
- Added bounds checking for relative x position within segments
- Ensures smooth interpolation between multiple pitches using segmented
  half-cosine interpolation (id1) without discontinuities

Fixes issue where trajectory ID 6 with 3 pitches [234.76, 274.41, 248.72] Hz
produced 0.214 octave jumps instead of smooth transitions.

Closes #6

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Jon Myers <jon-myers@users.noreply.github.com>
@claude
Copy link
Contributor

claude bot commented Aug 15, 2025

Claude finished @jon-myers's task —— View job


PR Review: Fix for Trajectory ID 6 (Yoyo) Discontinuous Jumps

Review Tasks:

  • Read and analyze the modified trajectory.py file
  • Review the specific changes in the id6() method
  • Assess code quality and best practices
  • Check for potential bugs or issues
  • Evaluate performance considerations
  • Review security concerns
  • Assess test coverage implications
  • Provide comprehensive feedback

Review Summary ✅

This PR successfully addresses the discontinuous jumps issue in the Trajectory ID 6 (Yoyo) computation method. The fix demonstrates a clear understanding of the problem and implements a robust solution.

Code Quality Analysis

Excellent Improvements

  1. Clear Algorithm Logic: The new implementation replaces complex lambda functions with straightforward, readable logic
  2. Better Boundary Handling: Proper edge case handling for x < 0 and segment boundaries
  3. Robust Index Finding: The new loop logic correctly implements TypeScript findLastIndex behavior
  4. Bounds Checking: Added max(0.0, min(1.0, relative_x)) prevents invalid interpolation values
  5. Improved Comments: Clear documentation explaining the algorithm steps

Bug Fixes Identified

Original Issues Fixed:

  • Lambda Closure Bug: The original code had classic Python closure issues where lambda functions captured the loop variable i incorrectly
  • Index Error Prevention: Removed the raise Exception('index error') that could crash on edge cases
  • Discontinuous Jumps: Fixed by ensuring proper segment boundary detection

Code Reference: idtap/classes/trajectory.py:625-658

Performance Considerations ✅

Improvements:

  • Better Time Complexity: O(n) loop instead of creating multiple lambda functions
  • Reduced Memory Usage: No longer creates arrays of lambda functions (bends, outs)
  • Cleaner Execution: Direct calculation instead of nested function calls

Potential Optimization:

  • For very large dur_array, consider binary search for segment finding, but current O(n) is likely sufficient for typical musical trajectory segments

Security Assessment ✅

  • No Security Concerns: Mathematical computation with proper bounds checking
  • Input Validation: Existing parameter validation remains intact
  • No External Dependencies: Pure mathematical operations

Test Coverage Implications

Backwards Compatibility

  • Function signature unchanged: id6(self, x: float, lf: Optional[List[float]] = None, da: Optional[List[float]] = None) -> float
  • Default behavior preserved for existing users

⚠️ Testing Recommendations

  1. Edge Case Testing: Verify behavior at segment boundaries (x = 0, x = 1, x at segment transitions)
  2. Continuity Testing: Confirm smooth transitions between segments
  3. Single Segment Testing: Test with dur_array = [1.0] (single segment)
  4. Empty Cases: Test with minimal pitch arrays

Suggested Test Cases:

# Test segment boundary transitions
trajectory.id6(0.333)  # At first segment boundary  
trajectory.id6(0.667)  # At second segment boundary
trajectory.id6(-0.1)   # Before start (edge case)
trajectory.id6(1.1)    # After end (edge case)

Specific Code Issues

Well Handled

  • Index Bounds: Proper handling with index == -1 fallback
  • Division by Zero: Protected by existing validation in constructor
  • Type Safety: Maintains consistent float return type

🔍 Minor Observations

  • Variable Naming: relative_x is clear and descriptive
  • Function Decomposition: Good balance between readability and performance
  • Error Handling: Graceful fallback instead of exceptions

Overall Assessment: APPROVED

This is a high-quality fix that:

  • ✅ Addresses the root cause of discontinuous jumps
  • ✅ Improves code readability and maintainability
  • ✅ Enhances performance and memory usage
  • ✅ Maintains backwards compatibility
  • ✅ Includes proper bounds checking and edge case handling

The implementation demonstrates solid understanding of both the mathematical requirements and Python programming best practices.

Recommendations for Merge

  1. Ready to Merge: The code quality is excellent and addresses the reported issue
  2. Consider Adding Tests: While not blocking, adding specific test cases for segment boundaries would strengthen confidence
  3. Documentation: The inline comments are helpful and sufficient

Final Verdict: This PR successfully resolves issue #6 with a clean, performant, and robust implementation. ✅


@jon-myers jon-myers merged commit 9ee079f into main Aug 15, 2025
1 check passed
@jon-myers jon-myers deleted the claude/issue-6-20250815-0240 branch August 15, 2025 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Trajectory ID 6 (Yoyo) compute() method produces discontinuous jumps

2 participants