def set(self, prop, value):
     if _.index_of(self.props(), prop) != -1:
         self.attributes[prop] = value
         self.onChange({prop: value})
         return self
     else:
         raise ValueError("Invalid model property passed: %s" % property)
Exemple #2
0
    def _new_slice(self, times, values, key):
        """
        slicing functionality for timeseries
        """
        try:
            start, stop, step = key.start, key.stop, key.step
            if all(x is None for x in [start, stop, step]):
                # [:] slice, return everything
                return times, values
        except AttributeError:
            start, stop, step = key, False, None

        if start is not None and start < times[
                0] and self.first_val is not False:
            # add default beginning value to front of list
            times = [start] + times
            values = [self.first_val] + values

        start_idx = index_of(start, times, begin=True)
        if stop is False:
            # slice only wants one value
            if self.interpolate:
                return start, self._interpolate(start, times, values)
            return start, values[start_idx]

        times, values = times[start_idx:], values[start_idx:]
        slice_times, slice_values = [x for x in times], [x for x in values]
        if start > slice_times[0]:
            # reset first time in slice_times
            slice_times[0] = start

        if step:
            slice_times, slice_values = pad_lists(step,
                                                  slice_times,
                                                  slice_values,
                                                  keep_dist=True)

        stop_idx = index_of(stop, slice_times)
        if not stop or stop > slice_times[stop_idx]:
            # hack to include the last value if stop is past the end of list
            stop_idx += 1

        if self.interpolate:
            return slice_times[:stop_idx], self._interpolate(
                slice_times[:stop_idx], times, values)

        return slice_times[:stop_idx], slice_values[:stop_idx]
Exemple #3
0
 async def execute_command(self, command, message, prefix):
     command_list = self.get_commands_as_strings()
     if command in command_list:
         await self.get_commands()[utils.index_of(command,
                                                  command_list)](message,
                                                                 prefix)
     else:
         await self.not_found(message)
Exemple #4
0
def plot(loss_list, y_pred, batchX, batchY):
    plt.subplot(2, 3, 1)
    plt.cla()
    plt.plot(loss_list)

    print('batchX = ', batchX)
    # print('batchY = ', batchY)
    cut_idx = index_of([1, 1, 1, 1, 1], batchX)
    print('batchX cut = ', batchX[cut_idx:])
    print('batchY cut = ', batchY[cut_idx:])

    y_preds = np.array([(1 if out[0] < 0.5 else 0) for out in y_pred])
    print('y_preds = ', y_preds)
Exemple #5
0
 def get_vote_option(self, payload, poll_info):
     return utils.index_of(str(payload.emoji), utils.emojis)
Exemple #6
0
 def __setitem__(self, time, value):
     if time in self._times:
         time_index = index_of(time, self._times)
         self._values[time_index] = value
     else:
         raise Exception('time not in timeseries')
Exemple #7
0
    def _data_to_HDU(self, _data, header):
        """
        Method that converts structured numpy.ndarray with data and instance of
        ``PyFits.Header`` class to the instance of ``PyFits.GroupsHDU`` class.

        :param _data:
            Numpy.ndarray with dtype = [('uvw', '<f8', (3,)),
                                       ('time', '<f8'),
                                       ('baseline', 'int'),
                                       ('hands', 'complex', (nif, nstokes,)),
                                       ('weights', '<f8', (nif, nstokes,))]

        :param header:
            Instance of ``PyFits.Header`` class

        :return:
            Instance of ``PyFits.GroupsHDU`` class
        """

        # Constructing array (3, N, #stokes, #if,)
        temp = np.vstack((_data['hands'].real[np.newaxis, :],
                          _data['hands'].imag[np.newaxis, :],
                          _data['weights'][np.newaxis, :]))

        # FIXME: PZEROi has different i for same key in different FITS-files!
        # Construct corresponding arrays of parameter values
        _data_copy = _data.copy()
        _data_copy['uvw'][:, 0] = (_data_copy['uvw'][:, 0] +
                                   self.hdu.header['PZERO1']) *\
            self.hdu.header['PSCAL1']
        _data_copy['uvw'][:, 1] = (_data_copy['uvw'][:, 1] +
                                   self.hdu.header['PZERO2']) *\
            self.hdu.header['PSCAL2']
        _data_copy['uvw'][:, 2] = (_data_copy['uvw'][:, 2] +
                                   self.hdu.header['PZERO3']) *\
            self.hdu.header['PSCAL3']
        _data_copy['time'] = (_data_copy['time'] +
                              self.hdu.header['PZERO4']) *\
            self.hdu.header['PSCAL4']
        _data_copy['baseline'] = (_data_copy['baseline'] +
                                  self.hdu.header['PZERO6']) *\
            self.hdu.header['PSCAL6']

        # Now roll axis 0 (real,imag,weight) to 3rd position
        # (3, N, #if, #stokes) => (N, #if, #stokes, 3)
        temp = np.rollaxis(temp, 0, 4)

        # First, add dimensions:
        for i in range(self.ndim_ones):
            temp = np.expand_dims(temp, axis=4)
            # Now temp has shape (N, #if, #stokes, 3, 1, 1, 1)

        # Change dimensions to pyfits.hdu.data['DATA'] dimensions
        temp = change_shape(temp, self.data_of__data, {
            key: self.data_of_data[key][0]
            for key in self.data_of_data.keys()
        })
        # => (N, 1, 1, #if, 1, #stokes, 3) as in 'DATA' part of pyfits recarray

        # Write regular array data (``temp``) and corresponding parameters to
        # instances of pyfits.GroupsHDU
        imdata = temp

        # Use parameter values of saving data to find indexes of this
        # parameters in the original data entry of HDU
        if len(_data) < len(self.hdu.data):

            original_data = _to_one_ndarray(self.hdu.data, 'UU---SIN',
                                            'VV---SIN', 'WW---SIN', 'DATE',
                                            'BASELINE')
            saving_data = np.dstack(
                (np.array(np.hsplit(_data_copy['uvw'], 3)).T,
                 _data_copy['time'], _data_copy['baseline']))
            saving_data = np.squeeze(saving_data)
            # TODO: this is funnest workaround:)
            par_indxs = np.hstack(
                index_of(saving_data.sum(axis=1), original_data.sum(axis=1)))
        elif len(_data) > len(self.hdu.data):
            raise Exception('There must be equal or less visibilities to\
                            save!')
        else:
            print "Saving uvdata - number of groups hasn't changed..."
            par_indxs = np.arange(len(self.hdu.data))

        parnames = self.hdu.data.parnames
        pardata = list()
        for name in parnames:
            par = self.hdu.data[name][par_indxs]
            par = (par - self.hdu.header['PZERO' + str(self.par_dict[name])]) /\
                self.hdu.header['PSCAL' + str(self.par_dict[name])]
            pardata.append(par)

        # If two parameters for one value (like ``DATE``)
        for name in parnames:
            if parnames.count(name) == 2:
                indx_to_zero = parnames.index(name) + 1
                break
                # then zero array for second parameter with the same name
            # TODO: use dtype from ``BITPIX`` keyword
        pardata[indx_to_zero] = np.zeros(len(par_indxs), dtype=float)

        a = pf.GroupData(imdata,
                         parnames=parnames,
                         pardata=pardata,
                         bitpix=-32)
        b = pf.GroupsHDU(a)
        # PyFits updates header using given data (``GCOUNT``)
        b.header = self.hdu.header

        return b