Skip to content

Commit

Permalink
Fix an issue with numpy vectors as slice arguments (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkanche authored Jan 6, 2025
1 parent b640dae commit ead0055
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 128 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Publish to PyPI

on:
push:
tags: "*"

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Test with tox
run: |
tox
- name: Build docs
run: |
tox -e docs
- run: touch ./docs/_build/html/.nojekyll

- name: GH Pages Deployment
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: gh-pages # The branch the action should deploy to.
folder: ./docs/_build/html
clean: true # Automatically remove deleted files from the deploy branch

- name: Build Project and Publish
run: |
python -m tox -e clean,build
- name: Publish package
uses: pypa/[email protected]
with:
user: __token__
password: ${{ secrets.PYPI_PASSWORD }}
51 changes: 0 additions & 51 deletions .github/workflows/pypi-publish.yml

This file was deleted.

40 changes: 0 additions & 40 deletions .github/workflows/pypi-test.yml

This file was deleted.

32 changes: 32 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Run tests

on:
push:
branches: [master]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]

name: Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "pip"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Test with tox
run: |
tox
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 0.5.1

- Fixed an issue with numpy arrays as slice arguments. Code now uses Biocutils's subset functions to perform these operations.
- Rename GitHub actions for consistency with the rest of the packages.

## Version 0.5.0

- chore: Remove Python 3.8 (EOL)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Project generated with PyScaffold](https://img.shields.io/badge/-PyScaffold-005CA0?logo=pyscaffold)](https://pyscaffold.org/)
[![PyPI-Server](https://img.shields.io/pypi/v/GenomicRanges.svg)](https://pypi.org/project/GenomicRanges/)
![Unit tests](https://github.com/BiocPy/GenomicRanges/actions/workflows/pypi-test.yml/badge.svg)
![Unit tests](https://github.com/BiocPy/GenomicRanges/actions/workflows/run-tests.yml/badge.svg)

# GenomicRanges

Expand Down
10 changes: 5 additions & 5 deletions src/genomicranges/GenomicRanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,11 +953,11 @@ def get_subset(self, subset: Union[str, int, bool, Sequence]) -> "GenomicRanges"

current_class_const = type(self)
return current_class_const(
seqnames=self._seqnames[idx],
ranges=self._ranges[idx],
strand=self._strand[idx],
names=self._names[idx] if self._names is not None else None,
mcols=self._mcols[idx, :],
seqnames=ut.subset_sequence(self._seqnames, idx),
ranges=ut.subset_sequence(self._ranges, idx),
strand=ut.subset_sequence(self._strand, idx),
names=ut.subset_sequence(self._names, idx) if self._names is not None else None,
mcols=ut.subset(self._mcols, idx),
seqinfo=self._seqinfo,
metadata=self._metadata,
)
Expand Down
48 changes: 17 additions & 31 deletions src/genomicranges/GenomicRangesList.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,38 +847,24 @@ def __getitem__(self, args: Union[str, int, tuple, list, slice]) -> Union[Genomi
else:
idx, _ = ut.normalize_subscript(args, len(self), self._names)

if isinstance(idx, list):
if ut.is_list_of_type(idx, bool):
if len(idx) != len(self):
raise ValueError("`indices` is a boolean vector, length should match the size of the data.")

idx = [i for i in range(len(idx)) if idx[i] is True]

new_ranges = [self.ranges[i] for i in idx]
new_range_lengths = [self._range_lengths[i] for i in idx]

new_names = None
if self.names is not None:
new_names = [self.names[i] for i in idx]

new_mcols = None
if self.mcols is not None:
new_mcols = self.mcols[idx, :]

return GenomicRangesList(new_ranges, new_range_lengths, new_names, new_mcols, self._metadata)
elif isinstance(idx, (slice, range)):
if isinstance(idx, range):
idx = slice(idx.start, idx.stop, idx.step)

return GenomicRangesList(
self._ranges[idx],
self._range_lengths[idx],
self._names[idx] if self._names is not None else self._names,
self._mcols[idx, :],
self._metadata,
)
if ut.is_list_of_type(idx, bool):
if len(idx) != len(self):
raise ValueError("`indices` is a boolean vector, length should match the size of the data.")

idx = [i for i in range(len(idx)) if idx[i] is True]

new_ranges = ut.subset_sequence(self._ranges, idx)
new_range_lengths = ut.subset_sequence(self._range_lengths, idx)

new_names = None
if self.names is not None:
new_names = ut.subset_sequence(self.names, idx)

new_mcols = None
if self.mcols is not None:
new_mcols = ut.subset(self.mcols, idx)

raise TypeError("Arguments to subset `GenomicRangesList` is not supported.")
return GenomicRangesList(new_ranges, new_range_lengths, new_names, new_mcols, self._metadata)

#######################################
######>> class initializers <<#########
Expand Down

0 comments on commit ead0055

Please sign in to comment.