Skip to content
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

union methods for Datasets should be reworked #30

Open
NicolaDonelli opened this issue Apr 15, 2022 · 0 comments
Open

union methods for Datasets should be reworked #30

NicolaDonelli opened this issue Apr 15, 2022 · 0 comments

Comments

@NicolaDonelli
Copy link
Contributor

NicolaDonelli commented Apr 15, 2022

I noticed that the method cgnal.core.data.model.ml.Dataset.union takes a Dataset as input returns a LazyDataset while cgnal.core.data.model.ml.PandasDataset.union takes a Dataset as input returns either a Dataset or a PandasDataset (and this translates to PandasTimeIndexedDataset making it return either a Dataset or a PandasTimeIndexedDataset).

Since I cannot see any advantage in being able to handle types of dataset different from self while performing union (if one wants to unite different types of dataset should just cast the latter to the type of the former...) and I do not like very much that especially the union of two objects of the same type do not return the same object type, I'd propose these fixes:

TDataset = TypeVar('TDataset', bound='Dataset')

class Dataset(
    _IterableUtils[SampleTypes, "CachedDataset", "LazyDataset"],
    Generic[FeatType, LabType],
    ABC,
):
...
    @abstractclass
    def def union(self: TDataset, other: TDataset) -> TDataset: ...

class CachedDataset(_CachedIterable[SampleTypes], DillSerialization, Dataset):
...
    def union(self, other: CachedDataset) -> CachedDataset: 
        return CachedDataset([x for x in self.items] + [x for x in other.items])

class LazyDataset(_LazyIterable[Sample], Dataset):
...
    def union(self, other: LazyDataset) -> LazyDataset: 
        def __generator__():
            for sample in self:
                yield sample
            for sample in other:
                yield sample
        return LazyDataset(IterGenerator(__generator__))


class PandasDataset(Dataset[FeatType, LabType], DillSerialization):
...
    def union(self:TPandasDataset, other: TPandasDataset) -> TPandasDataset: 
        features = pd.concat([self.features, other.features])
        labels = (
            pd.concat([self.labels, other.labels])
            if not (self.labels is None and other.labels is None)
            else None
        )
        return self.createObject(features, labels)

@CGnal/datascientists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant