Skip to content

Refactor LockIn-SignalRecovery_7265DSP time constant logic - without RAG#6

Open
ofir-frd wants to merge 2 commits into
mainfrom
of/no-rag
Open

Refactor LockIn-SignalRecovery_7265DSP time constant logic - without RAG#6
ofir-frd wants to merge 2 commits into
mainfrom
of/no-rag

Conversation

@ofir-frd

@ofir-frd ofir-frd commented Jun 9, 2025

Copy link
Copy Markdown
Collaborator

PR Type

Bug fix, Enhancement


Description

  • Corrected logic for selecting time constant key.

  • Fixed calculation of new time constant in auto adjustment.

  • Improved wait loop condition for command completion.


Changes walkthrough 📝

Relevant files
Bug fix
main.py
Fix and enhance time constant handling and wait logic       

src/LockIn-SignalRecovery_7265DSP/main.py

  • Changed comparison direction in time constant key selection.
  • Corrected formula for new time constant in auto adjustment.
  • Updated wait loop to check correct status bit for completion.
  • Removed outdated comments and clarified logic.
  • +8/-11   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @qodo-merge-demo

    Copy link
    Copy Markdown

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Logic Error

    The time constant calculation formula appears incorrect. The new formula new_tc = factor / period suggests time constant inversely proportional to period, but typically time constant should be proportional to period (factor * period) for proper signal filtering.

    new_tc = factor / period
    Status Check

    The wait loop condition changed from checking if status bit is set (stb & 1 == 1) to checking if it's cleared (stb & 1 == 0). This reverses the completion logic and may cause premature exit or infinite waiting depending on the device's status bit behavior.

    if stb & 1 == 0:
        break

    @qodo-merge-demo

    Copy link
    Copy Markdown

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix time constant calculation formula

    The formula appears incorrect for calculating time constant. If factor
    represents how many times larger the time constant should be compared to the
    period, it should be factor * period, not factor / period.

    src/LockIn-SignalRecovery_7265DSP/main.py [529]

    -new_tc = factor / period
    +new_tc = factor * period
    • Apply / Chat
    Suggestion importance[1-10]: 10

    __

    Why: The suggestion correctly identifies a likely bug introduced in the PR. The function's docstring states that the time constant should be a factor higher than the signal's period, which implies multiplication (factor * period), not the division (factor / period) introduced in the PR. This change fundamentally alters the physics calculation and would lead to incorrect behavior.

    High
    Correct time constant comparison logic

    The comparison direction change may cause incorrect time constant selection. If
    timeconstants_numbers is sorted in ascending order, using > will count elements
    larger than the target, potentially selecting a time constant that's too small.

    src/LockIn-SignalRecovery_7265DSP/main.py [508]

    -tc_index = sum(np.array(self.timeconstants_numbers) > time_constant)
    +tc_index = sum(np.array(self.timeconstants_numbers) < time_constant)

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 10

    __

    Why: The suggestion correctly identifies a critical logic error introduced by the PR. Changing the comparison from < to > in the sum expression likely breaks the logic for selecting the appropriate time constant. The original logic correctly finds the index for the smallest available time constant that is greater than or equal to the required time_constant (assuming an ascending list), while the new logic does not.

    High
    • More

    @ofir-frd

    ofir-frd commented Jun 9, 2025

    Copy link
    Copy Markdown
    Collaborator Author

    /ask How is the wait_for_complete method implemented elsewhere?

    @qodo-merge-demo

    Copy link
    Copy Markdown

    Ask

    How is the wait_for_complete method implemented elsewhere?

    Answer:

    Based on the PR diff provided, I can only see the implementation of wait_for_complete in the LockIn-SignalRecovery_7265DSP driver. The method implementation shown uses GPIB status byte polling:

    def wait_for_complete(self):
        starttime = time.time()
        while True:
            stb = self.port.port.read_stb()
            if stb & 1 == 0:  # Changed from == 1 to == 0
                break
            time.sleep(0.01)
            if time.time() - starttime > 20:
                raise Exception("Timeout during wait for completion.")

    However, I cannot see implementations from other instrument drivers in this PR to make comparisons. To provide a comprehensive answer about how wait_for_complete is implemented elsewhere, I would need access to:

    1. Other instrument driver files in the same repository
    2. Base classes or parent implementations
    3. Similar methods in related drivers

    The key change in this PR is the status bit check logic - it was changed from if stb & 1 == 1: to if stb & 1 == 0:, which suggests the previous implementation was checking for the wrong condition (command in progress vs. command complete).

    Common patterns for wait_for_complete methods in instrument drivers typically include:

    • Polling status registers or bytes
    • Using *OPC? (Operation Complete) queries for SCPI instruments
    • Checking specific status bits that indicate operation completion
    • Implementing timeouts to prevent infinite loops
    • Adding small delays between polls to avoid overwhelming the instrument

    If you could provide access to other driver implementations, I could give a more detailed comparison of different approaches used across the codebase.

    @Codium-ai Codium-ai deleted a comment from qodo-merge-demo Bot Jun 10, 2025
    @Codium-ai Codium-ai deleted a comment from qodo-merge-demo Bot Jun 10, 2025
    Comment on lines 541 to 548
    stb = self.port.port.read_stb()
    if stb & 1 == 1: # first byte indicates whether command is processed or not

    if stb & 1 == 0:
    break

    time.sleep(0.01)
    if time.time() - starttime > 20:
    raise Exception("Timeout during wait for completion.")

    Copy link
    Copy Markdown
    Collaborator Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    /implement Make this wait_for_complete method work like it's commonly done in the repo.

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Code Implementation 🛠️

    Implementation: Refactor the wait_for_complete method to follow the common pattern used in the repository by using a while loop with proper timeout handling and consistent sleep intervals.

    Suggested change
    stb = self.port.port.read_stb()
    if stb & 1 == 1: # first byte indicates whether command is processed or not
    if stb & 1 == 0:
    break
    time.sleep(0.01)
    if time.time() - starttime > 20:
    raise Exception("Timeout during wait for completion.")
    timeout = 20.0
    sleep_time = 0.01
    start_time = time.time()
    while time.time() - start_time < timeout:
    stb = self.port.port.read_stb()
    if stb & 1 == 0:
    break
    time.sleep(sleep_time)
    else:
    raise Exception("Timeout during wait for completion.")

    See review comment here

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    1 participant