# Path: photometry/plots.py
# def plots_interactive(backend=('Qt5Agg', 'MacOSX', 'Qt4Agg', 'Qt5Cairo', 'TkAgg')):
# def plots_noninteractive():
# def plot_image(image, ax=None, scale='log', cmap=None, origin='lower', xlabel=None,
# 	ylabel=None, cbar=None, clabel='Flux ($e^{-}s^{-1}$)', cbar_ticks=None,
# 	cbar_ticklabels=None, cbar_pad=None, cbar_size='5%', title=None,
# 	percentile=95.0, vmin=None, vmax=None, offset_axes=None, color_bad='k', **kwargs):
# def plot_image_fit_residuals(fig, image, fit, residuals=None, percentile=95.0):
# def plot_outline(img, ax=None, threshold=0.5, **kwargs):
# def save_figure(path, fig=None, fmt='png', **kwargs):
# 
# Path: photometry/utilities.py
# def load_settings():
# def load_sector_settings(sector=None):
# def find_ffi_files(rootdir, sector=None, camera=None, ccd=None):
# def _find_tpf_files(rootdir, sector=None, cadence=None):
# def find_tpf_files(rootdir, starid=None, sector=None, camera=None, ccd=None, cadence=None,
# 	findmax=None):
# def find_hdf5_files(rootdir, sector=None, camera=None, ccd=None):
# def find_catalog_files(rootdir, sector=None, camera=None, ccd=None):
# def load_ffi_fits(path, return_header=False, return_uncert=False):
# def to_tuple(inp, default=None):
# def _move_median_central_1d(x, width_points):
# def move_median_central(x, width_points, axis=0):
# def add_proper_motion(ra, dec, pm_ra, pm_dec, bjd, epoch=2000.0):
# def integratedGaussian(x, y, flux, x_0, y_0, sigma=1):
# def mag2flux(mag, zp=20.451):
# def sphere_distance(ra1, dec1, ra2, dec2):
# def radec_to_cartesian(radec):
# def cartesian_to_radec(xyz):
# def rms_timescale(time, flux, timescale=3600/86400):
# def find_nearest(array, value):
# def download_file(url, destination, desc=None, timeout=60,
# 	position_holders=None, position_lock=None, showprogress=None):
# def download_parallel(urls, workers=4, timeout=60, showprogress=None):
# 	def _wrapper(arg):
# 	def __init__(self, *args, **kwargs):
# 	def emit(self, record):
# 	def __init__(self, *args, message_queue, **kwargs):
# 	def emit(self, record):
# 	def __init__(self, logger, level=logging.INFO):
# 	def write(self, message):
# 	def flush(self):
# def sqlite_drop_column(conn, table, col):
# class TqdmLoggingHandler(logging.Handler):
# class ListHandler(logging.Handler):
# class LoggerWriter(object):


import os
import logging
import copy
import numpy as np
import matplotlib as mpl
from bottleneck import nanmedian
from scipy import stats, ndimage
from scipy.optimize import minimize
from statsmodels.nonparametric.kde import KDEUnivariate as KDE
from statsmodels.nonparametric.bandwidths import select_bandwidth
from sklearn.cluster import DBSCAN
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
from ..plots import plt, plot_image, plot_outline, save_figure
from matplotlib.backends.backend_pdf import PdfPages
from ..utilities import mad_to_sigma
							ax1 = fig.add_subplot(121)
							ax1.axvspan(np.min(column_rows)-0.5, np.max(column_rows)+0.5, color='0.7')
							ax1.plot(Ypixel, SumImage[:, c], 'ro-', drawstyle='steps-mid')
							ax1.set_title(title)
							ax1.set_xlabel('Y pixels')
							ax1.set_ylabel('Sum-image counts')
							ax1.set_ylim(0, mask_max)
							ax1.set_xlim(-0.5, NY-0.5)

							ax2 = fig.add_subplot(122)
							plot_image(SumImage, ax=ax2, scale='log')
							ax2.plot(outline_before[u][:,0], outline_before[u][:,1], 'r:')
							# The outline of the mask after saturated columns have been
							# corrected for:
							plot_outline(mask, ax=ax2, c='r')
							ax2.axvline(c, color='r', ls='--')

							pdf.savefig(fig)
							plt.close(fig)

	#==============================================================================
	# Create plots
	#==============================================================================
	if plot_folder is not None:
		# Colors to use for each cluster label:
		cmap_rainbow = copy.copy(plt.get_cmap('gist_rainbow'))
		colors = cmap_rainbow(np.linspace(0, 1, len(unique_labels)))

		# Set up figure to hold subplots:
		aspect = 0.5 if NY/NX > 5 else 0.2

# Path: photometry/plots.py
# def plots_interactive(backend=('Qt5Agg', 'MacOSX', 'Qt4Agg', 'Qt5Cairo', 'TkAgg')):
# def plots_noninteractive():
# def plot_image(image, ax=None, scale='log', cmap=None, origin='lower', xlabel=None,
# 	ylabel=None, cbar=None, clabel='Flux ($e^{-}s^{-1}$)', cbar_ticks=None,
# 	cbar_ticklabels=None, cbar_pad=None, cbar_size='5%', title=None,
# 	percentile=95.0, vmin=None, vmax=None, offset_axes=None, color_bad='k', **kwargs):
# def plot_image_fit_residuals(fig, image, fit, residuals=None, percentile=95.0):
# def plot_outline(img, ax=None, threshold=0.5, **kwargs):
# def save_figure(path, fig=None, fmt='png', **kwargs):
# 
# Path: photometry/utilities.py
# def load_settings():
# def load_sector_settings(sector=None):
# def find_ffi_files(rootdir, sector=None, camera=None, ccd=None):
# def _find_tpf_files(rootdir, sector=None, cadence=None):
# def find_tpf_files(rootdir, starid=None, sector=None, camera=None, ccd=None, cadence=None,
# 	findmax=None):
# def find_hdf5_files(rootdir, sector=None, camera=None, ccd=None):
# def find_catalog_files(rootdir, sector=None, camera=None, ccd=None):
# def load_ffi_fits(path, return_header=False, return_uncert=False):
# def to_tuple(inp, default=None):
# def _move_median_central_1d(x, width_points):
# def move_median_central(x, width_points, axis=0):
# def add_proper_motion(ra, dec, pm_ra, pm_dec, bjd, epoch=2000.0):
# def integratedGaussian(x, y, flux, x_0, y_0, sigma=1):
# def mag2flux(mag, zp=20.451):
# def sphere_distance(ra1, dec1, ra2, dec2):
# def radec_to_cartesian(radec):
# def cartesian_to_radec(xyz):
# def rms_timescale(time, flux, timescale=3600/86400):
# def find_nearest(array, value):
# def download_file(url, destination, desc=None, timeout=60,
# 	position_holders=None, position_lock=None, showprogress=None):
# def download_parallel(urls, workers=4, timeout=60, showprogress=None):
# 	def _wrapper(arg):
# 	def __init__(self, *args, **kwargs):
# 	def emit(self, record):
# 	def __init__(self, *args, message_queue, **kwargs):
# 	def emit(self, record):
# 	def __init__(self, logger, level=logging.INFO):
# 	def write(self, message):
# 	def flush(self):
# def sqlite_drop_column(conn, table, col):
# class TqdmLoggingHandler(logging.Handler):
# class ListHandler(logging.Handler):
# class LoggerWriter(object):


# Path: photometry/AperturePhotometry/k2p2v2.py
import os
import logging
import copy
import numpy as np
import matplotlib as mpl
from bottleneck import nanmedian
from scipy import stats, ndimage
from scipy.optimize import minimize
from statsmodels.nonparametric.kde import KDEUnivariate as KDE
from statsmodels.nonparametric.bandwidths import select_bandwidth
from sklearn.cluster import DBSCAN
from skimage.feature import peak_local_max
from skimage.segmentation import watershed
from ..plots import plt, plot_image, plot_outline, save_figure
from matplotlib.backends.backend_pdf import PdfPages
from ..utilities import mad_to_sigma

							ax1 = fig.add_subplot(121)
							ax1.axvspan(np.min(column_rows)-0.5, np.max(column_rows)+0.5, color='0.7')
							ax1.plot(Ypixel, SumImage[:, c], 'ro-', drawstyle='steps-mid')
							ax1.set_title(title)
							ax1.set_xlabel('Y pixels')
							ax1.set_ylabel('Sum-image counts')
							ax1.set_ylim(0, mask_max)
							ax1.set_xlim(-0.5, NY-0.5)

							ax2 = fig.add_subplot(122)
							plot_image(SumImage, ax=ax2, scale='log')
							ax2.plot(outline_before[u][:,0], outline_before[u][:,1], 'r:')
							# The outline of the mask after saturated columns have been
							# corrected for:
							plot_outline(mask, ax=ax2, c='r')
							ax2.axvline(c, color='r', ls='--')

							pdf.savefig(fig)
							plt.close(fig)

	#==============================================================================
	# Create plots
	#==============================================================================
	if plot_folder is not None:
		# Colors to use for each cluster label:
		cmap_rainbow = copy.copy(plt.get_cmap('gist_rainbow'))
		colors = cmap_rainbow(np.linspace(0, 1, len(unique_labels)))

		# Set up figure to hold subplots:
		aspect = 0.5 if NY/NX > 5 else 0.2

		fig0 = plt.figure(figsize=(2*plt.figaspect(aspect)))