# Path: tkp/db/model.py
# class Image(Base):
#     __tablename__ = 'image'
# 
#     id = Column(Integer, seq_image, primary_key=True,
#                 server_default=seq_image.next_value())
# 
#     dataset_id = Column('dataset', Integer, ForeignKey('dataset.id'), nullable=False, index=True)
#     dataset = relationship('Dataset', backref=backref('images', cascade="delete"))
# 
#     band_id = Column('band', ForeignKey('frequencyband.id'), nullable=False, index=True)
#     band = relationship('Frequencyband', cascade="delete")
# 
#     skyrgn_id = Column('skyrgn', Integer, ForeignKey('skyregion.id'), nullable=False, index=True)
#     skyrgn = relationship('Skyregion', backref=backref('images', cascade="delete"))
# 
#     tau = Column(Integer)
#     stokes = Column(SmallInteger, nullable=False, server_default=text("1"))
#     tau_time = Column(Double)
#     freq_eff = Column(Double, nullable=False)
#     freq_bw = Column(Double)
#     taustart_ts = Column(DateTime, nullable=False, index=True)
#     rb_smaj = Column(Double, nullable=False)
#     rb_smin = Column(Double, nullable=False)
#     rb_pa = Column(Double, nullable=False)
#     deltax = Column(Double, nullable=False)
#     deltay = Column(Double, nullable=False)
#     fwhm_arcsec = Column(Double)
#     fov_degrees = Column(Double)
#     rms_qc = Column(Double, nullable=False)
#     rms_min = Column(Double)
#     rms_max = Column(Double)
#     detection_thresh = Column(Double)
#     analysis_thresh = Column(Double)
#     url = Column(String(1024))
#     node = Column(SmallInteger, nullable=False, server_default=text("1"))
#     nodes = Column(SmallInteger, nullable=False, server_default=text("1"))
# 
#     data = relationship("ImageData", uselist=False, back_populates="image")
# 
# Path: tkp/db/quality.py
# def sync_rejectreasons(session):
# def reject(imageid, reason, comment, session):
# def unreject(imageid, session):
# def isrejected(imageid, session):


import numpy
from tkp.utility import nice_format
from scipy.stats import norm
from sqlalchemy.sql.expression import desc
from tkp.db.model import Image
from tkp.db.quality import reject_reasons
    """
    RMS for quality-control.

    Root mean square value calculated from central region of an image.
    We sigma-clip the input-data in an attempt to exclude source-pixels
    and keep only background-pixels.

    Args:
        data: A numpy array
        rms_est_sigma: sigma value used for clipping
        rms_est_fraction: determines size of subsection, result will be
            1/fth of the image size where f=rms_est_fraction
    returns the rms value of a iterative sigma clipped subsection of an image
    """
    return rms(clip(subregion(data, rms_est_fraction), rms_est_sigma))

def reject_basic_rms(image_id, session, est_sigma=4, rms_max=100., rms_min=0.0):
    """Check if the RMS value of an image lies within a range predetermined
    at the start of a pipeline run.
    
    args:
        image_id (int): database ID of the image we want to check
        session (sqlalchemy.orm.session.Session): the database session
        est_sigma (float): sigma multiplication factor
        rms_max (float): global maximum rms for image quality check
        rms_min (float): global minimum rms for image quality check
    returns:
        bool: None if not rejected, (rejectreason, comment) if rejected
    """


# Path: tkp/db/model.py
# class Image(Base):
#     __tablename__ = 'image'
# 
#     id = Column(Integer, seq_image, primary_key=True,
#                 server_default=seq_image.next_value())
# 
#     dataset_id = Column('dataset', Integer, ForeignKey('dataset.id'), nullable=False, index=True)
#     dataset = relationship('Dataset', backref=backref('images', cascade="delete"))
# 
#     band_id = Column('band', ForeignKey('frequencyband.id'), nullable=False, index=True)
#     band = relationship('Frequencyband', cascade="delete")
# 
#     skyrgn_id = Column('skyrgn', Integer, ForeignKey('skyregion.id'), nullable=False, index=True)
#     skyrgn = relationship('Skyregion', backref=backref('images', cascade="delete"))
# 
#     tau = Column(Integer)
#     stokes = Column(SmallInteger, nullable=False, server_default=text("1"))
#     tau_time = Column(Double)
#     freq_eff = Column(Double, nullable=False)
#     freq_bw = Column(Double)
#     taustart_ts = Column(DateTime, nullable=False, index=True)
#     rb_smaj = Column(Double, nullable=False)
#     rb_smin = Column(Double, nullable=False)
#     rb_pa = Column(Double, nullable=False)
#     deltax = Column(Double, nullable=False)
#     deltay = Column(Double, nullable=False)
#     fwhm_arcsec = Column(Double)
#     fov_degrees = Column(Double)
#     rms_qc = Column(Double, nullable=False)
#     rms_min = Column(Double)
#     rms_max = Column(Double)
#     detection_thresh = Column(Double)
#     analysis_thresh = Column(Double)
#     url = Column(String(1024))
#     node = Column(SmallInteger, nullable=False, server_default=text("1"))
#     nodes = Column(SmallInteger, nullable=False, server_default=text("1"))
# 
#     data = relationship("ImageData", uselist=False, back_populates="image")
# 
# Path: tkp/db/quality.py
# def sync_rejectreasons(session):
# def reject(imageid, reason, comment, session):
# def unreject(imageid, session):
# def isrejected(imageid, session):


# Path: tkp/quality/rms.py
import numpy
from tkp.utility import nice_format
from scipy.stats import norm
from sqlalchemy.sql.expression import desc
from tkp.db.model import Image
from tkp.db.quality import reject_reasons

    """
    RMS for quality-control.

    Root mean square value calculated from central region of an image.
    We sigma-clip the input-data in an attempt to exclude source-pixels
    and keep only background-pixels.

    Args:
        data: A numpy array
        rms_est_sigma: sigma value used for clipping
        rms_est_fraction: determines size of subsection, result will be
            1/fth of the image size where f=rms_est_fraction
    returns the rms value of a iterative sigma clipped subsection of an image
    """
    return rms(clip(subregion(data, rms_est_fraction), rms_est_sigma))

def reject_basic_rms(image_id, session, est_sigma=4, rms_max=100., rms_min=0.0):
    """Check if the RMS value of an image lies within a range predetermined
    at the start of a pipeline run.
    
    args:
        image_id (int): database ID of the image we want to check
        session (sqlalchemy.orm.session.Session): the database session
        est_sigma (float): sigma multiplication factor
        rms_max (float): global maximum rms for image quality check
        rms_min (float): global minimum rms for image quality check
    returns:
        bool: None if not rejected, (rejectreason, comment) if rejected
    """


    image = session.query(Image).filter(Image.id == image_id).one()