-
Notifications
You must be signed in to change notification settings - Fork 226
feat: RoboticArm CSV import/export #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideThis PR augments the RoboticArm class with CSV-based timeline import/export capabilities, refines run_schedule to skip null entries, and adds necessary module imports for CSV handling and timestamp generation. Class diagram for RoboticArm CSV import/export methodsclassDiagram
class RoboticArm {
...
run_schedule(timeline: List[List[int]], time_step: float)
import_timeline_from_csv(filepath: str) List[List[int]]
export_timeline_to_csv(timeline: List[List[Union[int, None]]], folderpath: str) None
}
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rahul31124 - I've reviewed your changes - here's some feedback:
- Consider deriving the servo column names dynamically from self.servos instead of hard-coding "Servo1"–"Servo4", so the methods will work with any number of servos.
- In export_timeline_to_csv, validate or create the output folder if it doesn’t exist (and surface an error if it can’t be written), and skip the timestamp column on import rather than assuming fixed position.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider deriving the servo column names dynamically from self.servos instead of hard-coding "Servo1"–"Servo4", so the methods will work with any number of servos.
- In export_timeline_to_csv, validate or create the output folder if it doesn’t exist (and surface an error if it can’t be written), and skip the timestamp column on import rather than assuming fixed position.
## Individual Comments
### Comment 1
<location> `pslab/external/motor.py:136` </location>
<code_context>
+ reader = csv.DictReader(csvfile)
+ for row in reader:
+ angles = []
+ for key in ["Servo1", "Servo2", "Servo3", "Servo4"]:
+ value = row.get(key, "").strip().lower()
+ if value in ("", "null", "none"):
+ angles.append(None)
+ else:
+ angles.append(int(value))
+ timeline.append(angles)
+
</code_context>
<issue_to_address>
Assumes all servo columns are present and in order.
If columns are missing or out of order, parsing may fail or produce incorrect results. Please validate the header or handle columns more flexibly.
</issue_to_address>
### Comment 2
<location> `pslab/external/motor.py:166` </location>
<code_context>
+
+ with open(filepath, mode="w", newline="") as csvfile:
+ writer = csv.writer(csvfile)
+ writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"])
+ for i, row in enumerate(timeline):
+ pos = ["null" if val is None else val for val in row]
</code_context>
<issue_to_address>
Header uses 'Timestamp' for what is actually a timestep index.
Consider renaming the column to 'Timestep' or 'Step' to better reflect its contents.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"])
=======
writer.writerow(["Timestep", "Servo1", "Servo2", "Servo3", "Servo4"])
>>>>>>> REPLACE
</suggested_fix>
### Comment 3
<location> `pslab/external/motor.py:168` </location>
<code_context>
+ writer = csv.writer(csvfile)
+ writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"])
+ for i, row in enumerate(timeline):
+ pos = ["null" if val is None else val for val in row]
+ writer.writerow([i] + pos)
</code_context>
<issue_to_address>
Inconsistent handling of None values between import and export.
Consider using a single representation for None values (e.g., always 'null') in both import and export, and document this behavior for clarity.
Suggested implementation:
```python
# Export timeline to CSV, using 'null' to represent None values.
# When importing, 'null' will be interpreted as None.
with open(filepath, mode="w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"])
for i, row in enumerate(timeline):
pos = ["null" if val is None else val for val in row]
writer.writerow([i] + pos)
```
```python
def import_timeline_from_csv(filepath):
"""
Import a timeline from a CSV file, interpreting 'null' as None.
Args:
filepath (str): Path to the CSV file.
Returns:
list: Timeline data as a list of lists, with None for 'null' values.
"""
timeline = []
with open(filepath, mode="r", newline="") as csvfile:
reader = csv.reader(csvfile)
headers = next(reader) # Skip header
for row in reader:
# row[0] is the timestamp/index, skip or store as needed
positions = [None if val == "null" else float(val) for val in row[1:]]
timeline.append(positions)
return timeline
```
- If there is already an import function, update its logic to use `None if val == "null" else ...` for consistency.
- Ensure that all documentation and usage of import/export functions mention that 'null' is the standard representation for missing values.
- If the import function is used elsewhere, update those usages to use the new or updated function.
</issue_to_address>
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
pslab/external/motor.py
Outdated
for key in ["Servo1", "Servo2", "Servo3", "Servo4"]: | ||
value = row.get(key, "").strip().lower() | ||
if value in ("", "null", "none"): | ||
angles.append(None) | ||
else: | ||
angles.append(int(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Assumes all servo columns are present and in order.
If columns are missing or out of order, parsing may fail or produce incorrect results. Please validate the header or handle columns more flexibly.
writer = csv.writer(csvfile) | ||
writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"]) | ||
for i, row in enumerate(timeline): | ||
pos = ["null" if val is None else val for val in row] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Inconsistent handling of None values between import and export.
Consider using a single representation for None values (e.g., always 'null') in both import and export, and document this behavior for clarity.
Suggested implementation:
# Export timeline to CSV, using 'null' to represent None values.
# When importing, 'null' will be interpreted as None.
with open(filepath, mode="w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Timestamp", "Servo1", "Servo2", "Servo3", "Servo4"])
for i, row in enumerate(timeline):
pos = ["null" if val is None else val for val in row]
writer.writerow([i] + pos)
def import_timeline_from_csv(filepath):
"""
Import a timeline from a CSV file, interpreting 'null' as None.
Args:
filepath (str): Path to the CSV file.
Returns:
list: Timeline data as a list of lists, with None for 'null' values.
"""
timeline = []
with open(filepath, mode="r", newline="") as csvfile:
reader = csv.reader(csvfile)
headers = next(reader) # Skip header
for row in reader:
# row[0] is the timestamp/index, skip or store as needed
positions = [None if val == "null" else float(val) for val in row[1:]]
timeline.append(positions)
return timeline
- If there is already an import function, update its logic to use
None if val == "null" else ...
for consistency. - Ensure that all documentation and usage of import/export functions mention that 'null' is the standard representation for missing values.
- If the import function is used elsewhere, update those usages to use the new or updated function.
Hello @bessman, could you please review this PR and suggest changes that might be needed? |
pslab/external/motor.py
Outdated
angles = [] | ||
for key in ["Servo1", "Servo2", "Servo3", "Servo4"]: | ||
value = row.get(key, "").strip().lower() | ||
if value in ("", "null", "none"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can None values actually be represented by all of these (empty string, null, none)? If so, why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, in this case we treat None values as ("", "null", "none") because
The CSV exported by PSLab Mobile explicitly uses the string "null" for missing values
If users manually edit the CSV or add new entries, it's possible for values like an empty string "" or "none" to appear
Let me know your suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the export uses only "null", the import should only accept "null".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bessman I’ve now updated the import logic to only check for "null" as per your suggestion
Fixes #254
Changes
Added
import_timeline_from_csv()
method to theRoboticArm
classAllows importing a timeline (CSV format) generated by the PSLab Mobile app and using it directly in Python.
Added
export_timeline_to_csv()
method to theRoboticArm
classEnables exporting a Python-generated timeline to a timestamped CSV file, which can then be used in the PSLab Mobile app.
Summary by Sourcery
Add CSV import and export capabilities to the RoboticArm and improve timeline execution to handle missing values
New Features:
Enhancements: