Commit bbf9a6db authored by hazrmard's avatar hazrmard
Browse files

contiguous_sequences() for LSTM input

parent 3bc6bd3f
Loading
Loading
Loading
Loading
+55 −0
Changes for src/models.py: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,8 +7,10 @@ from os import cpu_count
from typing import Iterable, Tuple

import numpy as np
import pandas as pd
from sklearn import clone
from sklearn.neural_network import MLPRegressor
import torch.nn as nn



@@ -37,3 +39,56 @@ def fit_composite_model(estimator: MLPRegressor,
    estimators = [clone(estimator) for _ in data]
    with Pool(min(len(data), cpu_count())) as pool:
        return pool.starmap(_fit, zip(estimators, data))



def contiguous_sequences(index: Iterable[pd.datetime], interval: pd.Timedelta) ->\
    Iterable[Iterable[pd.datetime]]:
    """
    Breaks up a `DatetimeIndex` or a list of timestamps into a list of contiguous
    sequences.

    Args:
    * `index`: An index/list of timestamps in chronoligical order,
    * `interval`: a `Timedelta` object specifying the uniform intervals to determine
    contiguous indices.

    Returns:
    * A list of lists of `pd.datetime` objects.
    """
    indices = []
    j, k = 0, 1
    while k < len(index):           # for each subsequence
        seq = [index[j]]
        indices.append(seq)
        while k < len(index):       # for each element in subsequence
            diff = index[k] - index[j]
            if diff == interval:    # exact interval, add to subsequence
                seq.append(index[k])
                k += 1
                j += 1
            elif diff < interval:   # interval too small, look ahead
                k += 1
            else:                   # new subsequence
                j = k
                k += 1
                break
    return indices



class TorchEstimator:
    """
    Wraps a `torch.nn.Module` instance with a scikit-learn `Estimator` API.
    """

    def __init__(self, module: nn.Module):
        self.module = module


    def fit(self, X, y):
        pass


    def predict(self, X, y):
        pass
 No newline at end of file