Beispiel #1
0
def test_dbstore(tmp_db_store, data_files, random_samples):
    from wwb_scanner.file_handlers import BaseImporter
    from wwb_scanner.scan_objects import Spectrum
    from wwb_scanner.utils.dbstore import db_store

    spec = {}
    for fkey, d in data_files.items():
        for skey, fn in d.items():
            spectrum = BaseImporter.import_file(fn)
            spectrum.save_to_dbstore()
            assert spectrum.eid is not None
            spec[spectrum.eid] = spectrum

    rs = 1.024e6
    nsamp = 256
    step_size = 0.5e6
    freq_range = [572e6, 636e6]

    spectrum = Spectrum()
    fc = freq_range[0]
    while True:
        freqs, sig, Pxx = random_samples(n=nsamp, rs=rs, fc=fc)
        spectrum.add_sample_set(frequency=freqs, iq=Pxx)
        if spectrum.sample_data['frequency'].max() >= freq_range[1] / 1e6:
            break
        fc += step_size

    spectrum.save_to_dbstore()
    assert spectrum.eid is not None
    spec[spectrum.eid] = spectrum

    for eid, spectrum in spec.items():
        db_spectrum = Spectrum.from_dbstore(eid=spectrum.eid)
        for attr in Spectrum._serialize_attrs:
            assert getattr(spectrum, attr) == getattr(db_spectrum, attr)
        assert np.array_equal(spectrum.sample_data, db_spectrum.sample_data)

    db_data = db_store.get_all_scans()

    assert set(db_data.keys()) == set(spec.keys())
Beispiel #2
0
class ScannerBase(JSONMixin):
    WINDOW_TYPES = WINDOW_TYPES
    def __init__(self, **kwargs):
        self._running = threading.Event()
        self._stopped = threading.Event()
        self._current_freq = None
        self._progress = 0.
        ckwargs = kwargs.get('config')
        if not ckwargs:
            ckwargs = db_store.get_scan_config()
        if not ckwargs:
            ckwargs = {}
        self.config = ScanConfig(ckwargs)
        self.device_config = self.config.device
        self.sampling_config = self.config.sampling
        if 'spectrum' in kwargs:
            self.spectrum = Spectrum.from_json(kwargs['spectrum'])
        else:
            self.spectrum = Spectrum()
        self.spectrum.scan_config = self.config
        if not kwargs.get('__from_json__'):
            self.sample_collection = SampleCollection(scanner=self)
    @property
    def current_freq(self):
        return self._current_freq
    @current_freq.setter
    def current_freq(self, value):
        self._current_freq = value
        if value is not None:
            f_min, f_max = self.config.scan_range
            self.progress = (value - f_min) / (f_max - f_min)
        self.on_current_freq(value)
    def on_current_freq(self, value):
        print 'scanning %s' % (value)
    @property
    def progress(self):
        return self._progress
    @progress.setter
    def progress(self, value):
        if value == self._progress:
            return
        self._progress = value
        self.on_progress(value)
    def on_progress(self, value):
        print '%s%%' % (int(value * 100))
    def build_sample_sets(self):
        freq,  end_freq = self.config.scan_range
        sample_collection = self.sample_collection
        while freq <= end_freq:
            sample_set = sample_collection.build_sample_set(mhz_to_hz(freq))
            freq += self.step_size
    def run_scan(self):
        self.build_sample_sets()
        running = self._running
        running.set()
        self.sample_collection.scan_all_freqs()
        self.sample_collection.stopped.wait()
        if running.is_set():
            self.save_to_dbstore()
        running.clear()
        self._stopped.set()
    def stop_scan(self):
        self._running.clear()
        self.sample_collection.cancel()
        self._stopped.wait()
    def scan_freq(self, freq):
        pass
    def save_to_dbstore(self):
        self.spectrum.save_to_dbstore()
    def _serialize(self):
        d = dict(
            config=self.config._serialize(),
            spectrum=self.spectrum._serialize(),
            sample_collection=self.sample_collection._serialize(),
        )
        return d
    def _deserialize(self, **kwargs):
        data = kwargs.get('sample_collection')
        self.sample_collection = SampleCollection.from_json(data, scanner=self)
Beispiel #3
0
class ScannerBase(JSONMixin):
    WINDOW_TYPES = WINDOW_TYPES
    def __init__(self, **kwargs):
        self._running = threading.Event()
        self._stopped = threading.Event()
        self._current_freq = None
        self._progress = 0.
        ckwargs = kwargs.get('config')
        if not ckwargs:
            ckwargs = db_store.get_scan_config()
        if not ckwargs:
            ckwargs = {}
        self.config = ScanConfig(ckwargs)
        self.device_config = self.config.device
        self.sampling_config = self.config.sampling
        if 'spectrum' in kwargs:
            self.spectrum = Spectrum.from_json(kwargs['spectrum'])
        else:
            self.spectrum = Spectrum()
        self.spectrum.scan_config = self.config
        if not kwargs.get('__from_json__'):
            self.sample_collection = SampleCollection(scanner=self)
    @property
    def current_freq(self):
        return self._current_freq
    @current_freq.setter
    def current_freq(self, value):
        self._current_freq = value
        if value is not None:
            f_min, f_max = self.config.scan_range
            self.progress = (value - f_min) / (f_max - f_min)
        self.on_current_freq(value)
    def on_current_freq(self, value):
        print('scanning %s' % (value))
    @property
    def progress(self):
        return self._progress
    @progress.setter
    def progress(self, value):
        if value == self._progress:
            return
        self._progress = value
        self.on_progress(value)
    def on_progress(self, value):
        print('%s%%' % (int(value * 100)))
    def build_sample_sets(self):
        freq,  end_freq = self.config.scan_range
        sample_collection = self.sample_collection
        while freq <= end_freq:
            sample_set = sample_collection.build_sample_set(mhz_to_hz(freq))
            freq += self.step_size
    def run_scan(self):
        self.build_sample_sets()
        running = self._running
        running.set()
        self.sample_collection.scan_all_freqs()
        self.sample_collection.stopped.wait()
        if running.is_set():
            self.save_to_dbstore()
        running.clear()
        self._stopped.set()
    def stop_scan(self):
        self._running.clear()
        self.sample_collection.cancel()
        self._stopped.wait()
    def scan_freq(self, freq):
        pass
    def save_to_dbstore(self):
        self.spectrum.save_to_dbstore()
    def _serialize(self):
        d = dict(
            config=self.config._serialize(),
            spectrum=self.spectrum._serialize(),
            sample_collection=self.sample_collection._serialize(),
        )
        return d
    def _deserialize(self, **kwargs):
        data = kwargs.get('sample_collection')
        self.sample_collection = SampleCollection.from_json(data, scanner=self)