<commit_before>import unittest
if not  hasattr(unittest.TestCase, 'assertIsInstance'):
    import unittest2 as unittest
import tkp.database as tkpdb
import tkp.database.utils.transients as dbt
import tkp.database.utils as dbutils
from tkp.testutil import db_subs
from tkp.testutil.decorators import requires_database, duration


class TestTransientBasics(unittest.TestCase):
    @requires_database()
    def setUp(self):
        self.database = tkpdb.DataBase()
    def tearDown(self):
        self.database.close()

    def test_single_band_transient_search(self):
        """test_single_band_transient_search

            Test simplest functional case - only source is a transient source.
            This makes it simple to verify the database properties.

        """
        # We have to add a dataset, some images all with some measurements
        # After insertion, and source association, we run the transient search.
        dataset = tkpdb.DataSet(database=self.database,
                                data={'description':"Trans:"
                                        + self._testMethodName})
        n_images = 4
        im_params = db_subs.example_dbimage_datasets(n_images)

        TransientSource = db_subs.MockSource(
             ex=db_subs.example_extractedsource_tuple(),
             lightcurve=[
                 db_subs.MockLCPoint(index=2, peak=20e-2, flux=20e-2, sigma=200),
                 db_subs.MockLCPoint(index=3, peak=1e-2, flux=1e-2, sigma=10),
                    ]
                 )

        measurements = TransientSource.synthesise_measurements(
                                               n_images,
                                               include_non_detections=True)

        images = []
        for idx in range(len(im_params)):
            image = tkpdb.Image(dataset=dataset, data=im_params[idx])
            images.append(image)
            if measurements[idx] is not None:
                image.insert_extracted_sources([ measurements[idx] ])
            image.associate_extracted_sources(deRuiter_r=3.7)
            freq_bands = dataset.frequency_bands()
            self.assertEqual(len(freq_bands), 1)
            transients = dbt.transient_search(
                                            eta_lim=1,
                                            V_lim=0.1,
                                            probability_threshold=0.7,
                                            minpoints=1,
                                            image_id=image.id)

        # Check the number of detected transients
        self.assertEqual(len(transients), 1)

        # Check that the bands for the images are the same as the transient's band
        freq_bands = dataset.frequency_bands()
        self.assertEqual(len(freq_bands), 1)
        for tr in transients:
            self.assertEqual(freq_bands[0], tr.band)

        runcats = dataset.runcat_entries()
        self.assertEqual(len(runcats), 1)
        for tr in transients:
            self.assertEqual(runcats[0]['runcat'], tr.runcat)

        # Check that the trigger xtrsrc happened in the third image
        query = """\
        select taustart_ts
          from extractedsource x
              ,image i
         where x.image = i.id
           and x.id = (select trigger_xtrsrc
                         from transient t
                             ,runningcatalog r
                        where t.runcat = r.id
                          and r.dataset = %s
                      )
        """
        self.database.cursor.execute(query, (dataset.id,))
        taustart_ts = zip(*self.database.cursor.fetchall())[0]<commit_msg>Match transients_test to Transient class tweaks<commit_after>import unittest
if not  hasattr(unittest.TestCase, 'assertIsInstance'):
    import unittest2 as unittest
import tkp.database as tkpdb
import tkp.database.utils.transients as dbt
import tkp.database.utils as dbutils
from tkp.testutil import db_subs
from tkp.testutil.decorators import requires_database, duration


class TestTransientBasics(unittest.TestCase):
    @requires_database()
    def setUp(self):
        self.database = tkpdb.DataBase()
    def tearDown(self):
        self.database.close()

    def test_single_band_transient_search(self):
        """test_single_band_transient_search

            Test simplest functional case - only source is a transient source.
            This makes it simple to verify the database properties.

        """
        # We have to add a dataset, some images all with some measurements
        # After insertion, and source association, we run the transient search.
        dataset = tkpdb.DataSet(database=self.database,
                                data={'description':"Trans:"
                                        + self._testMethodName})
        n_images = 4
        im_params = db_subs.example_dbimage_datasets(n_images)

        TransientSource = db_subs.MockSource(
             ex=db_subs.example_extractedsource_tuple(),
             lightcurve=[
                 db_subs.MockLCPoint(index=2, peak=20e-2, flux=20e-2, sigma=200),
                 db_subs.MockLCPoint(index=3, peak=1e-2, flux=1e-2, sigma=10),
                    ]
                 )

        measurements = TransientSource.synthesise_measurements(
                                               n_images,
                                               include_non_detections=True)

        images = []
        for idx in range(len(im_params)):
            image = tkpdb.Image(dataset=dataset, data=im_params[idx])
            images.append(image)
            if measurements[idx] is not None:
                image.insert_extracted_sources([ measurements[idx] ])
            image.associate_extracted_sources(deRuiter_r=3.7)
            freq_bands = dataset.frequency_bands()
            self.assertEqual(len(freq_bands), 1)
            transients = dbt.transient_search(
                                            eta_lim=1,
                                            V_lim=0.1,
                                            probability_threshold=0.7,
                                            minpoints=1,
                                            image_id=image.id)

        # Check the number of detected transients
        self.assertEqual(len(transients), 1)

        # Check that the bands for the images are the same as the transient's band
        freq_bands = dataset.frequency_bands()
        self.assertEqual(len(freq_bands), 1)
        for tr in transients:
            self.assertEqual(freq_bands[0], tr.band)

        runcats = dataset.runcat_entries()
        self.assertEqual(len(runcats), 1)
        for tr in transients:
            self.assertEqual(runcats[0]['runcat'], tr.runcatid)

        # Check that the trigger xtrsrc happened in the third image
        query = """\
        select taustart_ts
          from extractedsource x
              ,image i
         where x.image = i.id
           and x.id = (select trigger_xtrsrc
                         from transient t
                             ,runningcatalog r
                        where t.runcat = r.id
                          and r.dataset = %s
                      )
        """
        self.database.cursor.execute(query, (dataset.id,))
        taustart_ts = zip(*self.database.cursor.fetchall())[0]
{'fraction_non_alphanumeric': 0.07698607698607698, 'fraction_numerical': 0.009282009282009281, 'mean_word_length': 1.6585631349782293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1772864', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1856, 'n_words': 442}