Beispiel #1
0
    def disaggregate(
        self, building, disagg_features=[Measurement('power', 'active')],
            environmental=None):
        """Disaggregate the test data according to the model learnt previously
        Performs 1D FHMM disaggregation        
        """
        test_mains = building.utility.electric.get_dataframe_of_mains(
            measurement=disagg_features[0])

        # Array of learnt states
        learnt_states_array = []

        # Break down test_data into chunks and disaggregate separately on them
        for start, end in contiguous_blocks(test_mains.index):
            length = test_mains[start:end].values.size
            temp = test_mains[start:end].values.reshape(length, 1)
            learnt_states_array.append(self.model.predict(temp))

        # Model
        means = OrderedDict()
        for appliance in self.individual:
            means[appliance] = self.individual[appliance].means_
        means_copy = deepcopy(means)
        for appliance in means:
            means_copy[appliance] = means[
                appliance].astype(int).flatten().tolist()
            means_copy[appliance].sort()

        decoded_power_array = []
        decoded_states_array = []
        for learnt_states in learnt_states_array:
            [decoded_states, decoded_power] = decode_hmm(
                len(learnt_states), means_copy, means_copy.keys(), learnt_states)
            decoded_states_array.append(decoded_states)
            decoded_power_array.append(decoded_power)

        # Combining to make a DataFrame with correct index, based on the start,
        # end time and the frequency of the data
        dfs_list = []
        count = 0
        cont_blocks = contiguous_blocks(test_mains.index)
        for i, (start, end) in enumerate(contiguous_blocks(test_mains.index)):
            index = pd.DatetimeIndex(start=start, end=end,
                                     freq=self.freq)

            df = pd.DataFrame(decoded_power_array[i], index=index)
            dfs_list.append(df)

        self.predictions = pd.concat(dfs_list).sort_index()
Beispiel #2
0
    def train(self, building, aggregate='mains', submetered='appliances',
              disagg_features=[Measurement('power', 'active')],
              environmental=None):
        """Train using 1d FHMM. Places the learnt model in `model` attribute
        """

         # Get a dataframe of appliances; Since the algorithm is 1D, we need
        # only the first Measurement
        train_appliances = building.utility.electric.get_dataframe_of_appliances(
            measurement=disagg_features[0])

        train_mains = building.utility.electric.get_dataframe_of_mains(
            measurement=disagg_features[0])

        # Setting frequency
        self.freq = str(int(get_sample_period(train_mains.index))) + 's'

        learnt_model = OrderedDict()
        for appliance in train_appliances:
            print(appliance)
            learnt_model[appliance] = hmm.GaussianHMM(
                2, "full")

            # Data to fit
            X = []

            # Breaking data into contiguous blocks
            for start, end in contiguous_blocks(train_mains.index):
                #print(start, end)
                length = train_appliances[appliance][start:end].values.size
                # print(length)
                # Ignore small sequences
                if length > 50:
                    temp = train_appliances[appliance][
                        start:end].values.reshape(length, 1)
                    X.append(temp)
            # print(X)
            # Fit
            learnt_model[appliance].fit(X)

        # Combining to make a AFHMM
        new_learnt_models = OrderedDict()
        for appliance in learnt_model:
            startprob, means, covars, transmat = sort_learnt_parameters(
                learnt_model[appliance].startprob_, learnt_model[appliance].means_, learnt_model[appliance].covars_, learnt_model[appliance].transmat_)
            new_learnt_models[appliance] = hmm.GaussianHMM(
                startprob.size, "full", startprob, transmat)
            new_learnt_models[appliance].means_ = means
            new_learnt_models[appliance].covars_ = covars

        learnt_model_combined = create_combined_hmm(new_learnt_models)
        self.individual = new_learnt_models
        self.model = learnt_model_combined