def disag_rectangles(net, mains, max_target_power, on_power_threshold,
                     overlap_threshold=0.5):
    rectangles = disaggregate_start_stop_end(
        mains, net, std=INPUT_STATS['std'], stride=STRIDE,
        max_target_power=max_target_power)
    rectangles_matrix = rectangles_to_matrix(rectangles[0], max_target_power)
    disag_vector = rectangles_matrix_to_vector(
        rectangles_matrix,
        min_on_power=on_power_threshold,
        overlap_threshold=overlap_threshold
    )
    return disag_vector
# Unstandardise for plotting
targets *= MAX_TARGET_POWER
mains_unstandardised = (mains * input_stats['std']) + input_stats['mean']
mains_unstandardised *= mains_source.max_input_power

# disag
STRIDE = 16
logger.info("Starting disag...")
rectangles = disaggregate_start_stop_end(mains,
                                         net,
                                         stride=STRIDE,
                                         max_target_power=MAX_TARGET_POWER)
rectangles_matrix = rectangles_to_matrix(rectangles[0], MAX_TARGET_POWER)
disag_vector = rectangles_matrix_to_vector(rectangles_matrix,
                                           min_on_power=500,
                                           overlap_threshold=0.30)

# save data to disk
logger.info("Saving data to disk...")
np.save('mains', mains_unstandardised)
np.save('targets', targets)
np.save('disag_vector', disag_vector)
save_rectangles(rectangles)

# plot
logger.info("Plotting...")
fig, axes = plt.subplots(4, 1, sharex=True)
alpha = STRIDE / seq_length
plot_disaggregate_start_stop_end(rectangles, ax=axes[0], alpha=alpha)
axes[0].set_title('Network output')
mains = pad(mains.flatten())
targets = pad(targets.flatten())
logger.info("Done preparing synthetic mains data!")

# Unstandardise for plotting
targets *= MAX_TARGET_POWER
mains_unstandardised = (mains * input_stats['std']) + input_stats['mean']
mains_unstandardised *= mains_source.max_input_power

# disag
STRIDE = 16
logger.info("Starting disag...")
rectangles = disaggregate_start_stop_end(
    mains, net, stride=STRIDE, max_target_power=MAX_TARGET_POWER)
rectangles_matrix = rectangles_to_matrix(rectangles[0], MAX_TARGET_POWER)
disag_vector = rectangles_matrix_to_vector(
    rectangles_matrix, min_on_power=100, overlap_threshold=0.40)

# save data to disk
logger.info("Saving data to disk...")
np.save('mains', mains_unstandardised)
np.save('targets', targets)
np.save('disag_vector', disag_vector)
save_rectangles(rectangles)

# plot
logger.info("Plotting...")
fig, axes = plt.subplots(4, 1, sharex=True)
alpha = STRIDE / seq_length
plot_disaggregate_start_stop_end(rectangles, ax=axes[0], alpha=alpha)
axes[0].set_title('Network output')