Skip to content

Commit 0ecef41

Browse files
committed
Attempt with images again.
1 parent 3c1eb9a commit 0ecef41

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

benchmarks/lowq2_reconstruction/PRfunctions.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@
1313
# Utility Functions
1414
# =============================================================================
1515

16+
def create_data_uri_from_file(image_path):
17+
"""Read an image file and convert to base64 data URI"""
18+
if not os.path.exists(image_path):
19+
print(f"⚠️ Image file not found: {image_path}")
20+
return None
21+
22+
try:
23+
with open(image_path, 'rb') as f:
24+
image_data = f.read()
25+
26+
# Encode to base64
27+
b64_data = base64.b64encode(image_data).decode('utf-8')
28+
29+
# Determine mime type from extension
30+
ext = Path(image_path).suffix.lower()
31+
mime_type = 'image/png' if ext == '.png' else 'image/jpeg' if ext in ['.jpg', '.jpeg'] else 'image/png'
32+
33+
data_uri = f"data:{mime_type};base64,{b64_data}"
34+
35+
print(f"✅ Created data URI for {image_path} (size: {len(data_uri)} chars)")
36+
return data_uri
37+
except Exception as e:
38+
print(f"❌ Failed to create data URI for {image_path}: {e}")
39+
return None
40+
1641
def parse_repository(repository):
1742
"""Parse repository string into owner and name"""
1843
try:
@@ -154,7 +179,7 @@ def find_line_number_of_change(original_content, old_value):
154179
# GitHub PR Comment Functions
155180
# =============================================================================
156181

157-
def create_pr_suggestion(repo_owner, repo_name, pr_number, calibration_file, xml_file, line_number, suggested_line, head_sha, github_token, artifacts_url=''):
182+
def create_pr_suggestion(repo_owner, repo_name, pr_number, calibration_file, xml_file, line_number, suggested_line, head_sha, github_token, before_images=None, after_images=None, artifacts_url=''):
158183
"""Create a PR comment with proposed changes"""
159184
print(f"Creating PR comment with calibration update for #{pr_number}...")
160185

@@ -196,9 +221,31 @@ def create_pr_suggestion(repo_owner, repo_name, pr_number, calibration_file, xml
196221
197222
Please update the calibration URL in `{xml_file}` at line {line_number}."""
198223

199-
# Add artifacts link if provided
224+
# Convert image file paths to data URIs and embed
225+
if before_images:
226+
comment_body += "\n\n---\n\n### 📊 Before Calibration Update\n\n"
227+
for img_path in before_images:
228+
data_uri = create_data_uri_from_file(img_path)
229+
if data_uri:
230+
comment_body += f"![Before Image]({data_uri})\n\n"
231+
232+
if after_images:
233+
comment_body += "\n\n---\n\n### ✨ After Calibration Update\n\n"
234+
for img_path in after_images:
235+
data_uri = create_data_uri_from_file(img_path)
236+
if data_uri:
237+
comment_body += f"![After Image]({data_uri})\n\n"
238+
239+
# Add artifacts URL link if provided
200240
if artifacts_url:
201-
comment_body += f"\n\n---\n\n### 📊 Review Results\n\nPlease review the artifacts here: {artifacts_url}"
241+
comment_body += f"\n\n---\n\n📁 For comprehensive results and full-resolution artifacts, please visit: [{artifacts_url}]({artifacts_url})\n\n"
242+
243+
# Check total comment size
244+
comment_size = len(comment_body)
245+
print(f"Total comment size: {comment_size} characters")
246+
if comment_size > 65536:
247+
print(f"⚠️ WARNING: Comment exceeds GitHub's 65KB limit by {comment_size - 65536} characters!")
248+
print(" Consider further compressing images or reducing resolution.")
202249

203250
# Create or update comment via GitHub REST API (no gh CLI)
204251
if existing_comment_id:

benchmarks/lowq2_reconstruction/config.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,22 @@ upload_onnx:lowq2_reconstruction:
103103
- echo "Updating PR with lowq2 reconstruction results"
104104
- echo "ONNX file will be available at $ONNX_UPLOAD_URL"
105105
- |
106-
# Build artifact folder URL for review
106+
# Compress images for data URI embedding in GitHub comment
107+
BEFORE_IMG="results/lowq2_reconstruction/test_local/test_energy_theta_phi_resolution_local.png"
108+
AFTER_IMG="results/lowq2_reconstruction/retrained_local/retrained_energy_theta_phi_resolution_local.png"
109+
BEFORE_COMPRESSED="results/lowq2_reconstruction/test_local/compressed_test_energy_theta_phi.png"
110+
AFTER_COMPRESSED="results/lowq2_reconstruction/retrained_local/compressed_retrained_energy_theta_phi.png"
111+
112+
echo "Compressing images for data URI embedding..."
113+
magick "$BEFORE_IMG" -resize 800x600 -strip -quality 20 -colors 8 "$BEFORE_COMPRESSED"
114+
magick "$AFTER_IMG" -resize 800x600 -strip -quality 20 -colors 8 "$AFTER_COMPRESSED"
115+
116+
echo "Before compressed: $(ls -lh $BEFORE_COMPRESSED | awk '{print $5}')"
117+
echo "After compressed: $(ls -lh $AFTER_COMPRESSED | awk '{print $5}')"
118+
119+
# Build artifacts URL
107120
ARTIFACTS_URL="${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/browse/results/lowq2_reconstruction/"
108-
echo "Artifacts URL: $ARTIFACTS_URL"
121+
echo "Artifacts will be available at: $ARTIFACTS_URL"
109122
- |
110123
python benchmarks/lowq2_reconstruction/makePRSuggestion.py \
111124
--pr 213 \
@@ -114,5 +127,7 @@ upload_onnx:lowq2_reconstruction:
114127
--calibrationFile calibrations/onnx/Low-Q2_Steering_Reconstruction.onnx \
115128
--xml benchmarks/lowq2_reconstruction/calibrations.xml \
116129
--repository eic/detector_benchmarks \
130+
--beforeImages "$BEFORE_COMPRESSED" \
131+
--afterImages "$AFTER_COMPRESSED" \
117132
--artifactsURL "$ARTIFACTS_URL"
118133
- echo "Updating GitHub status for lowq2 reconstruction"

benchmarks/lowq2_reconstruction/makePRSuggestion.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
parser.add_argument('--calibrationFile', type=str, default='calibrations/onnx/Low-Q2_Steering_Reconstruction.onnx', help='Path to the local calibration file')
1515
parser.add_argument('--xml', type=str, default='compact/calibrations.xml', help='Path to the XML configuration file')
1616
parser.add_argument('--repository', type=str, default='eic/epic', help='GitHub repository (owner/name)')
17-
parser.add_argument('--artifactsURL', type=str, default='', help='URL to job artifacts for review')
17+
parser.add_argument('--beforeImages', type=str, nargs='*', default=[], help='List of before image file paths')
18+
parser.add_argument('--afterImages', type=str, nargs='*', default=[], help='List of after image file paths')
19+
parser.add_argument('--artifactsURL', type=str, default='', help='URL to job artifacts for additional review')
1820

1921

2022
args = parser.parse_args()
@@ -25,6 +27,8 @@
2527
calibration_file = args.calibrationFile
2628
xml_file = args.xml
2729
repository = args.repository
30+
before_images = args.beforeImages
31+
after_images = args.afterImages
2832
artifacts_url = args.artifactsURL
2933

3034
# =============================================================================
@@ -63,7 +67,7 @@
6367
print(f" Suggested change: {suggested_line.strip()}")
6468

6569
# Create the PR comment with proposed changes
66-
response = create_pr_suggestion(repo_owner, repo_name, pr_number, calibration_file, xml_file, line_number, suggested_line, pr_info['head']['sha'], github_token, artifacts_url)
70+
response = create_pr_suggestion(repo_owner, repo_name, pr_number, calibration_file, xml_file, line_number, suggested_line, pr_info['head']['sha'], github_token, before_images, after_images, artifacts_url)
6771

6872
if response:
6973
print("🎉 PR comment created successfully!")

0 commit comments

Comments
 (0)