Skip to content

Commit

Permalink
Merge pull request #40 from brisvag/develop
Browse files Browse the repository at this point in the history
Bugfixes, automatic flattening of datacrates
  • Loading branch information
brisvag authored Nov 24, 2020
2 parents 4e89d27 + 9599ab3 commit fe45364
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Install Python 3
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.8
- name: Install requirements
run: |
python -m pip install --upgrade pip
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/test_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Test PR

on:
pull_request:
branches: [ project_stuff ]
branches: [ develop ]

jobs:
test:
Expand All @@ -12,13 +12,11 @@ jobs:
- name: Install Python 3
uses: actions/setup-python@v1
with:
python-version: 3.7
python-version: 3.8
- name: Install requirements
run: |
python -m pip install --upgrade pip
pip install pytest
pip install pandas
pip install numpy
pip install -r requirements.txt
- name: Run tests
run: pytest
27 changes: 23 additions & 4 deletions peepingtom/core/datablocks/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List
from collections.abc import Iterable

from ...utils.containers import AttributedList

Expand Down Expand Up @@ -27,8 +28,9 @@ def updated(self):
def __newlike__(self, *args, **kwargs):
# this makes sure that operators get the right output in case
# _merge or _stack return notimplemented
if args[0] is NotImplemented:
return NotImplemented
if args:
if args[0] is NotImplemented:
return NotImplemented
cls = type(self)
return cls(parent=self.parent, *args, **kwargs)

Expand Down Expand Up @@ -218,7 +220,7 @@ def __setattr__(self, name, value):
Extend the functionality of __setattr__ to automatically add datablocks to the
'blocks' attribute of a 'MultiBlock' when set
"""
if isinstance(value, BaseBlock):
if isinstance(value, DataBlock):
self._add_block(value)
super().__setattr__(name, value)

Expand All @@ -239,7 +241,7 @@ def _add_block(self, block: DataBlock):
This is particularly useful when extending the functionality of an existing
MultiBlock object by inheritance
"""
self.blocks.append(block)
self._blocks.append(block)

@staticmethod
def _merge_data(multiblocks):
Expand Down Expand Up @@ -286,6 +288,23 @@ class DataCrate(AttributedList):
"""
A container for DataBlock objects which exist within the same n-dimensional reference space
"""
def __init__(self, iterable=()):
# recursively unpack the iterable into datablocks only
def unpack(iterable):
datablocks = []
for item in iterable:
if not isinstance(item, BaseBlock):
if isinstance(item, Iterable):
datablocks.extend(unpack(item))
else:
raise TypeError(f'DataCrate can only hold BaseBlocks, not {type(item)}')
else:
datablocks.append(item)
return datablocks

items = unpack(iterable)
super().__init__(items)

def __and__(self, other):
if isinstance(other, DataCrate):
return DataCrate(self + other)
Expand Down
5 changes: 3 additions & 2 deletions peepingtom/utils/helpers/dataframe_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def df_to_xyz(df: pd.DataFrame, mode: str):
if not columns_in_df(coord_columns[mode], df):
raise DataFrameError("Could not get coordinates from DataFrame")

positions = df[coord_columns[mode]].to_numpy() + df.get(shift_columns[mode], 0).to_numpy()
positions = df[coord_columns[mode]].to_numpy()
if columns_in_df(df, shift_columns[mode]):
positions += df[shift_columns[mode]].to_numpy()

return positions

Expand All @@ -63,7 +65,6 @@ def df_to_euler_angles(df: pd.DataFrame, mode: str):
}
_check_mode(mode)
if not columns_in_df(angle_columns[mode], df):
print(angle_columns[mode], df.columns)
raise DataFrameError("Could not get euler angles from DataFrame")
euler_angles = df[angle_columns[mode]]
return euler_angles.to_numpy()
Expand Down
5 changes: 4 additions & 1 deletion peepingtom/visualisation/depictors/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ def init_layers(self, point_kwargs={}, vector_kwargs={}):
unit_z_rotated_order_xyz = self.datablock.orientations.oriented_vectors('z').reshape((-1, 3))
unit_z_rotated_order_zyx = unit_z_rotated_order_xyz[:, ::-1]
# attach appropriate higher dimensions indeces to vectors
# TODO: make more general
if self.datablock.positions.ndim > 3:
unit_z_rotated_order_zyx = np.concatenate([positions[:, :-3], unit_z_rotated_order_zyx], axis=1)
padded = np.zeros_like(self.datablock.positions.data)
padded[:, -3:] = unit_z_rotated_order_zyx
unit_z_rotated_order_zyx = padded

napari_vectors = np.stack([positions, unit_z_rotated_order_zyx], axis=1)
v_layer = self.make_vectors_layer(napari_vectors,
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
napari>=0.4.0
napari[all]>=0.4.0
numpy>=1.19.4
pandas>=1.1.4
scipy>=1.5.4
seaborn>=0.11.0
dynamotable>=0.2.2
eulerangles>=0.1
dynamotable>=0.2.3
eulerangles>=0.1.1
mrcfile>=1.1.2
starfile>=0.3.2
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ classifiers =
[options]
zip_safe = False
packages = find:
python_requires = >= 3.7
python_requires = >= 3.8
include_package_data = True
install_requires =
napari>=0.4.0
Expand Down

0 comments on commit fe45364

Please sign in to comment.