Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
savitakartik committed Oct 7, 2024
2 parents 0c7c292 + 61d35d2 commit ecca2c7
Show file tree
Hide file tree
Showing 26 changed files with 1,751 additions and 1,365 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/post.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: Post screenshots

on:
pull_request_target:

jobs:
post:
name: Python
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
SCREENSHOTS_BRANCH: "pr-screenshots"
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'

- name: Install deps
run: pip install -r requirements.txt

- name: Create Screenshots
run: |
msp simulate --length 1000 --recombination-rate 0.01 --mutation-rate 0.01 100 out.trees
python -m tsbrowse preprocess out.trees
python -m tsbrowse screenshot out.tsbrowse mutations
python -m tsbrowse screenshot out.tsbrowse edges
python -m tsbrowse screenshot out.tsbrowse nodes
- name: Commit Screenshots
run: |
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
pr_number="${{ github.event.pull_request.number }}"
screenshot_dir="pr_${pr_number}"
if ! git ls-remote --exit-code --heads origin $SCREENSHOTS_BRANCH; then
git checkout --orphan $SCREENSHOTS_BRANCH
git rm -rf .
git commit --allow-empty -m "Initial commit for screenshots branch"
git push origin $SCREENSHOTS_BRANCH
else
git fetch origin $SCREENSHOTS_BRANCH
git checkout $SCREENSHOTS_BRANCH
fi
mkdir -p $screenshot_dir
mv *.png $screenshot_dir/
git add $screenshot_dir
git commit -m "Update screenshots for PR #${pr_number}"
git push origin $SCREENSHOTS_BRANCH
- name: Update or Post Comment
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs').promises;
const prNumber = context.payload.pull_request.number;
const screenshotDir = `pr_${prNumber}`;
const pngFiles = (await fs.readdir(screenshotDir)).filter(file => file.endsWith('.png'));
let commentBody = '## Automated Screenshots\n\n';
commentBody += 'These screenshots are automatically updated as the PR changes.\n\n';
commentBody += '<details><summary>Click to view screenshots</summary>\n\n';
for (const file of pngFiles) {
const imageUrl = `https://raw.githubusercontent.com/${context.repo.owner}/${context.repo.repo}/${process.env.SCREENSHOTS_BRANCH}/${screenshotDir}/${file}`;
commentBody += `### ${file}\n\n![${file}](${imageUrl})\n\n`;
}
commentBody += '</details>';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' && comment.body.includes('## Automated Screenshots')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody,
});
console.log('Updated existing comment');
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentBody,
});
console.log('Created new comment');
}
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
shell: bash
steps:
- name: Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.10.0
uses: styfle/cancel-workflow-action@0.11.0
with:
access_token: ${{ github.token }}

Expand All @@ -50,7 +50,7 @@ jobs:

- name: Tests with numba
run: coverage run --source=tsbrowse -m pytest -x tests

- name: Tests with coverage (no numba)
run: TSBROWSE_DISABLE_NUMBA=1 coverage run --source=tsbrowse -m pytest -x tests

Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ It is particularly useful to help evaluate ARGs that have been inferred using to
[KwARG](https://github.com/a-ignatieva/kwarg),
[Threads](https://pypi.org/project/threads-arg/), etc.

To view a tskit tree sequence or tszip file first pre-process it:

`python -m tsbrowse preprocess /path/to/trees-file`

This will write a `.tsbrowse` file

To launch the app use:

`python -m tsbrowse /path/to/trees-file`
`python -m tsbrowse serve /path/to/tsbrowse-file`

Or to generate a PNG of a specific page use, e.g:

`python -m tsbrowse screenshot /path/to/tsbrowse-file mutations`

On WSL, it may be necessary to disable Numba's CUDA support:

`NUMBA_DISABLE_CUDA=1 python -m tsbrowse /path/to/trees-file`
`NUMBA_DISABLE_CUDA=1 python -m tsbrowse serve /path/to/tsbrowse-file`

## Installation

Expand Down
70 changes: 70 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os

import tszip
from click.testing import CliRunner
from PIL import Image

from . import test_preprocess
from tsbrowse import __main__ as main


def test_preprocess_cli(tmpdir):
tszip_path = os.path.join(tmpdir, "test_input.tszip")
default_output_path = os.path.join(tmpdir, "test_input.tsbrowse")
custom_output_path = os.path.join(tmpdir, "custom_input.tsbrowse")

ts = test_preprocess.single_tree_example_ts()
tszip.compress(ts, tszip_path)

runner = CliRunner()
result = runner.invoke(main.cli, ["preprocess", tszip_path])
assert result.exit_code == 0
assert os.path.exists(default_output_path)
tszip.load(default_output_path).tables.assert_equals(ts.tables)

result = runner.invoke(
main.cli, ["preprocess", tszip_path, "--output", custom_output_path]
)
assert result.exit_code == 0
assert os.path.exists(custom_output_path)
tszip.load(custom_output_path).tables.assert_equals(ts.tables)


def test_screenshot_cli(tmpdir):
tszip_path = os.path.join(tmpdir, "test_input.tszip")
tsbrowse_path = os.path.join(tmpdir, "test_input.tsbrowse")
ts = test_preprocess.single_tree_example_ts()
tszip.compress(ts, tszip_path)
runner = CliRunner()
result = runner.invoke(main.cli, ["preprocess", tszip_path])
assert result.exit_code == 0

# Test with default output
result = runner.invoke(main.cli, ["screenshot", tsbrowse_path, "overview"])
assert result.exit_code == 0
default_output = os.path.join(tmpdir, "test_input_overview.png")
assert os.path.exists(default_output)
with Image.open(default_output) as img:
width, height = img.size
assert width == 1560
assert height == 296

# Test with path
custom_output = os.path.join(tmpdir, "custom_screenshot.png")
result = runner.invoke(
main.cli,
[
"screenshot",
tsbrowse_path,
"overview",
"--output",
custom_output,
],
)
assert result.exit_code == 0
assert os.path.exists(custom_output)

# Test with invalid page
result = runner.invoke(main.cli, ["screenshot", tsbrowse_path, "InvalidPage"])
assert result.exit_code != 0
assert "Invalid value" in result.output
Loading

0 comments on commit ecca2c7

Please sign in to comment.