Ejemplo n.º 1
0
def get_nilmtk_meters():
    HOUSE_1_APPLIANCES = [
        'fridge freezer', 'washer dryer', 'kettle', 'dish washer', 'microwave'
    ]
    ukdale = nilmtk.DataSet('/data/mine/vadeec/merged/ukdale.h5')
    ukdale.set_window("2013-04-12", "2013-05-12")
    elec = ukdale.buildings[1].elec
    meters = []
    for appliance in HOUSE_1_APPLIANCES:
        meter = elec[appliance]
        meters.append(meter)
    meters = nilmtk.MeterGroup(meters)
    return meters
    'dish washer': 'e545d'
}

APPLIANCES = [
    'fridge freezer',
    'washer dryer',
    'kettle',
    'HTPC',
    'dish washer'
]

meters = []
for appliance in APPLIANCES:
    meter = elec[appliance]
    meters.append(meter)
meters = nilmtk.MeterGroup(meters)

scores = {}

MAINS_APPLIANCES = ['washer dryer', 'HTPC', 'dish washer']
mains = np.load(join(BASE_DIRECTORY, 'e545a/mains.npy'))
mains = pd.DataFrame(mains)

fridge_mains = np.load(join(BASE_DIRECTORY, 'e544a/mains.npy'))
fridge_mains = pd.DataFrame(fridge_mains)

kettle_mains = np.load(join(BASE_DIRECTORY, 'e545b/mains.npy'))
kettle_mains = pd.DataFrame(kettle_mains)


def co_disag():
Ejemplo n.º 3
0
def load_nilmtk_activations( dataset_paths,
                             target_appliance_name,
                             appliance_names,
                             on_power_threshold,
                             min_on_duration,
                             min_off_duration,
                             sample_period,
                             windows,
                             sanity_check=1 ):
    """
    Parameters
    ----------
    windows : dict
        Structure example:
        {
            'UKDALE': {
                'train': {<building_i>: <window>},
                'unseen_activations_of_seen_appliances': {<building_i>: <window>},
                'unseen_appliances': {<building_i>: <window>}
            }
        }

    Returns
    -------
    all_activations : dict
        Structure example:
        {<train | unseen_appliances | unseen_activations_of_seen_appliances>: {
             <appliance>: {
                 <building_name>: [<activations>]
        }}}
        Each activation is a pd.Series with DatetimeIndex and the following
        metadata attributes: building, appliance, fold.
    """
    logger.info("Loading NILMTK activations...")

    if sanity_check:
        # Sanity check
        for dataset in windows:
            check_windows(windows[dataset])
    
    all_activations = {}
    for dataset_name, folds in windows.items():
        # Load dataset
        dataset = nilmtk.DataSet(dataset_paths[dataset_name])
        appliance_aliases = appliance_names[dataset_name][target_appliance_name]
        
        for fold, buildings_and_windows in folds.items():
            logger.info(
                "Loading activations for fold {}.....".format(fold))         
            for building_i, windows_for_building in buildings_and_windows.items():
                #dataset.set_window(*window)
                elec = dataset.buildings[building_i].elec
                building_name = (
                    dataset.metadata['name'] + '_building_{}'.format(building_i))
                
                appliance_meters = []
                for meter in elec.meters:
                    if meter.is_site_meter():
                        continue

                    append_meter = False
                    for a in meter.appliances:
                        if a.type['type'] in appliance_aliases:
                            append_meter = True
                    if append_meter:
                        appliance_meters.append(meter)
                        print(meter.appliances)

                if not appliance_meters:
                    logger.info(
                        "No {} found in {}".format(target_appliance_name, building_name))
                    continue

                #if appliance_meters:
                if len(appliance_meters) > 1:
                    meter = nilmtk.MeterGroup(meters=appliance_meters)
                else:
                    meter = appliance_meters[0]
                logger.info(
                    "Loading {} for {}...".format(target_appliance_name, building_name))

                meter_activations = []
                for window in windows_for_building:
                    if dataset_name == "ECO":
                        dataset.store.window = TimeFrame(start=window[0], end=window[1], tz='GMT')
                    else:
                        dataset.set_window(*window) # does not work for ECO
                    # Get activations_for_fold and process them
                    meter_activations_for_building = meter.get_activations(
                        sample_period=sample_period,
                        min_off_duration=min_off_duration,
                        min_on_duration=min_on_duration,
                        on_power_threshold=on_power_threshold,
                        resample_kwargs={'fill_method': 'ffill', 'how': 'mean', 'limit': 20})
                    #meter_activations_for_building = [activation.astype(np.float32)
                    #                     for activation in meter_activations_for_building]
                    meter_activations.extend(meter_activations_for_building)

                # Attach metadata
                #for activation in meter_activations:
                #    activation._metadata = copy(activation._metadata)
                #    activation._metadata.extend(
                #        ["building", "appliance", "fold"])
                #    activation.building = building_name
                #    activation.appliance = appliance
                #    activation.fold = fold

                # Save
                if meter_activations:
                    all_activations.setdefault(
                        fold, {}).setdefault(
                        target_appliance_name, {})[building_name] = meter_activations
                logger.info(
                    "Loaded {} {} activations from {}."
                    .format(len(meter_activations), target_appliance_name, building_name))

        dataset.store.close()
        
    logger.info("Done loading NILMTK activations.")
    return all_activations
Ejemplo n.º 4
0
def load_data_from_nilmtk_datasets(windows, dataset_paths, appliances, target_appliance_name, sample_period):
    data = {}
    data_good_sections = {}

    logger.info("Loading NILMTK data...")

    for dataset_name, folds in windows.items():
        # Load dataset
        dataset = nilmtk.DataSet(dataset_paths[dataset_name])

        for fold, buildings_and_windows in folds.items():
            for building_i, windows_for_building in buildings_and_windows.items():
                dataset.set_window(None, None)
                elec = dataset.buildings[building_i].elec

                building_name = (
                    dataset.metadata['name'] +
                    '_building_{}'.format(building_i))
                logger.info(
                    "Loading data for {}...".format(building_name))
                mains_meter = elec.mains()
                good_sections = get_effective_good_sections(mains_meter)

                appliance_aliases = appliances[dataset_name][target_appliance_name]
                appliance_meters = []
                for meter in elec.meters:
                    if meter.is_site_meter():
                        continue

                    if len(meter.appliances) == 1:
                        appliancetype = meter.appliances[0].type['type']
                        if appliancetype in appliance_aliases:
                            appliance_meters.append(meter)
                    else:
                        append_meter = False
                        for a in meter.appliances:
                            if a.type['type'] in appliance_aliases:
                                append_meter = True
                        if append_meter:
                            appliance_meters.append(meter)
                            print(meter.appliances)

                if not appliance_meters:
                    logger.info(
                        "No {} found in {}".format(target_appliance_name, building_name))
                    continue

                if len(appliance_meters) > 1:
                    appliance_metergroup = nilmtk.MeterGroup(meters=appliance_meters)
                else:
                    appliance_metergroup = appliance_meters[0]
                data_good_sections.setdefault(fold, {})[building_name] = good_sections

                def load_data(meter):
                    df = meter.power_series_all_data(
                        sample_period=sample_period
                        )
                    if df is not None:
                        return df.astype(np.float32).dropna()
                    else:
                        return None

                dfs = []
                for window in windows_for_building:
                    if dataset_name == "ECO":
                        dataset.store.window = TimeFrame(start=window[0], end=window[1], tz='GMT')
                    else:
                        if window is None:
                            ipdb.set_trace() # Something has gone wrong...see what happend!
                        dataset.set_window(*window) # does not work for ECO
                    #ipdb.set_trace()
                    mains_data = load_data(mains_meter)
                    appliance_data = load_data(appliance_metergroup)
                    if (mains_data is None) or (appliance_data is None):
                        continue
                    df = pd.DataFrame(
                        {'mains': mains_data, 'target': appliance_data},
                        dtype=np.float32).dropna()
                    del mains_data
                    del appliance_data
                    if not df.empty:
                        dfs.append(df)

                df = pd.concat(dfs, axis=0)
                dfs = []
                for gs in good_sections:
                    dfslice = gs.slice(df)
                    if not dfslice.empty:
                        dfs.append(dfslice)
                df = pd.concat(dfs, axis=0)

                if not df.empty:
                    data.setdefault(fold, {})[building_name] = df

                logger.info(
                    "Loaded data from building {} for fold {}"
                    " from {} to {}."
                    .format(building_name, fold, df.index[0], df.index[-1]))

        dataset.store.close()

    logger.info("Done loading NILMTK data.")
    return data, data_good_sections