Ejemplo n.º 1
0
Archivo: gui.py Proyecto: cilsat/slurp
def test():
    log = lambda message: sys.stdout.write(message)
    writer = Writer('D:/exe/imod/IMOD_USER/pusair-output')

    try:
        log('Reading config... ')
        config.parse()
        if config.config['gradient'] > 1:
            raise ValueError('Maximum gradient is 1')
        log('Done\n')

        p, adj = slurp.prep(
            # fbore=str('D:/exe/imod/IMOD_USER/pusair-input/Boreholes_Dimas.ipf'),
            fbore=str('D:/exe/imod/IMOD_USER/pusair-input/Boreholes_Jakarta.ipf'),
            fscreen=str('data/well_M_z_all.ipf'),
            config=config,
            log=log)

        interpolator = Interpolator(p, adj, writer, log)
        interpolator.interpolate()

        log('\n[DONE]')
    except Exception as e:
        log('\n\n[ERROR] {}'.format(e))
        traceback.print_exc()

    writer.reset()
Ejemplo n.º 2
0
Archivo: gui.py Proyecto: cilsat/slurp
    def run(self):
        log = lambda message: self.emit(SIGNAL('logging(QString)'), message)
        writer = Writer(str(self.text_output))

        try:
            log('Reading config... ')
            config.parse()
            if config.config['gradient'] > 1:
                raise ValueError('Maximum gradient is 1')
            log('Done\n')

            p, adj = slurp.prep(
                fbore=str(self.text_input),
                fscreen=str(self.text_screen),
                config=config,
                log=log)

            interpolator = Interpolator(p, adj, writer, log)
            interpolator.interpolate()

            log('\n[DONE]')
        except Exception as e:
            log('\n\n[ERROR] {}'.format(e))
            traceback.print_exc()

        self.emit(SIGNAL('finish_program()'))
Ejemplo n.º 3
0
    def Interpolate(self, **kwds):  #XXX: refactor so use self.interpolator ?
        """interpolate data (x,z) to generate response function z=f(*x)

        Input:
          maxpts: int, maximum number of points to use from (x,z)
          noise: float, amplitude of gaussian noise to remove duplicate x
          method: string for kind of interpolator
          extrap: if True, extrapolate a bounding box (can reduce # of nans)
          arrays: if True, return a numpy array; otherwise don't return arrays

        Output:
          interpolated response function, where z=f(*x.T)

        NOTE:
          if scipy is not installed, will use np.interp for 1D (non-rbf),
          or mystic's rbf otherwise. default method is 'nearest' for
          1D and 'linear' otherwise. method can be one of ('rbf','linear',
          'nearest','cubic','inverse','gaussian','quintic','thin_plate').
        """
        from interpolator import Interpolator
        args = self.args.copy()
        args.update(kwds)
        maxpts, noise = self.maxpts, self.noise
        ii = Interpolator(self.x, self.z, maxpts=maxpts, noise=noise, **args)
        self.surrogate = ii.Interpolate(**args)
        # build the surrogate
        self.surrogate.__doc__ = self.objective.__doc__
        return self.surrogate
Ejemplo n.º 4
0
 def add_wheel(self, n, distance, angle, radius, calibration = 1.0):
     overall_adjust = calibration / radius
     factor_advance = -sin(angle)
     factor_lateral = cos(angle)
     factor_turn = distance
     row = (factor_advance * overall_adjust,
            factor_lateral * overall_adjust,
            factor_turn * overall_adjust)
     self._wheels[n] = row
     self._forward_matrix = None
     self._back_matrix = None
     interpolator = Interpolator()
     interpolator.smooth_function = smooth_ease
     self._wheel_speeds[n] = interpolator
    def compute(self, rdf):

        cut_beg    = 13.0
        cut_end    = 15.0

        # Removes values with a density of zero, they will get added back later.
        rdf = rdf[nonzero(rdf[:,1])[0], :]
        # Finds the first index where the density is greater than 0.25*rho.
        i0 = nonzero(rdf[:,1] > 0.25)[0][0]

        # Get distance (r) and energy (e).
        r =  rdf[:,0]
        e = -kB*self.temperature*log(rdf[:,1])


        dr_final = (cut_end-self.min_distance)/self.npts
        # Figure out what range we need

        # Compute derivative from splines.
        rr     = arange(cut_end, r[i0], -dr_final)[::-1]

        interp = Interpolator(r,e)
        ff = [-interp.derivative(ri) for ri in rr]
    
        v0 = [5.0, 0.01]
        lj96_err = lambda v: ff - lj96_force(v,rr)
        v = optimize.leastsq(lj96_err, v0)[0]


        # Add more values to short distance to make sure that 
        # LAMMPS run won't fail when pair distance < table min.
        rpad = arange(self.min_distance, rr[0]-0.5*dr_final, dr_final)
        fpad = lj96_force(v, rpad) + ff[0] - lj96_force(v, rr[0])
        rr   = concatenate((rpad, rr))
        ff   = concatenate((fpad, ff))

        # Now we cut off forces smoothly at any point past max_attraction.
        ff[rr>cut_beg] *= 0.5*(1.0+cos(pi*(rr[rr>cut_beg]-cut_beg)/(cut_end-cut_beg)))
        ff[rr>cut_end] = 0.0

        # Compute energy by integrating forces.
        # Integrating backwards reduces noise.
        ee = -simpson_integrate(rr[::-1], ff[::-1])[::-1]
        ee -= ee[-1]

        self.distance.append(rr)
        self.force.append(ff)
        self.energy.append(ee)
Ejemplo n.º 6
0
 def get_x_y(data_list):
     interpolator = Interpolator()
     interpolator.set_u(ACTIONS)
     x = []
     y = []
     for data_row in data_list:
         new_q = data_row["reward"]
         if not data_row["done"]:
             new_q += DISCOUNT * np.max(data_row["next_qualities"])
         interpolator.set_q(data_row["qualities"])
         interpolator.update_function(data_row["action"], new_q)
         x.append(data_row["state"])
         y.append(interpolator.get_q())
     return x, y
Ejemplo n.º 7
0
 def _get_interpolator(self):
     single_CB = CompositeCrackBridge(E_m=self.E_m,
                                      reinforcement_lst=[self.reinforcement])
     CB_model = CompositeCrackBridgePostprocessor(model=single_CB)
     return Interpolator(CB_model = CB_model,
                          load_sigma_c_max = self.load_sigma_c_max,
                          load_n_sigma_c = self.load_n_sigma_c,
                          n_w = 80, n_x = 61, n_BC = 6
                          )
Ejemplo n.º 8
0
    def fit(self, **kwds):
        """generate an interpolated model from data

    Input:
        data: a mystic legacydata.dataset (or callable model, y = model(x))
        rnd: bool, if False, treat the model as deterministic [default: True]
        cached: bool, if True, use a mystic.cache [default: False]

    NOTE:
        any additional keyword arguments will be passed to the interpolator

    NOTE:
        if data is a model, interpolator will use model's cached archive
        """
        self.__kwds__.update(kwds)
        cached = self.__kwds__.pop('cached', False)
        archive = data = self.__kwds__.pop('data', None)
        self.rnd = self.__kwds__.pop('rnd', self.rnd) and (
            bool(self.__kwds__.get('noise', True))
        )  # or callable(data) or isinstance(data, type('')))
        if callable(data):  #XXX: is a model, allow this?
            data = sample(data, bounds=None, pts=0)  #XXX: axis? multivalue?
        elif isinstance(data, type('')):  #XXX: is a name, allow this?
            import mystic.cache as mc
            import dataset as ds
            data = ds.from_archive(mc.archive.read(data))
        x = getattr(data, 'coords', getattr(data, 'x', None))
        z = getattr(data, 'values', getattr(data, 'y', None))
        from interpolator import Interpolator
        terp = Interpolator(x, z, **self.__kwds__)
        self.__func__ = terp.Interpolate()  #XXX: ValueError: zero-size
        self.__model__ = _init_axis(terp.model)
        self.__model__.__name__ = self.__name__
        self.__kwds__['data'] = archive
        if cached:  #FIXME: clear the archive??? generate new uid name?
            self.__kwds__['cached'] = True
            self._OUQModel__init_cache()
            if hasattr(self.__model__, '__cache__'):
                c = self.__model__.__cache__()
                c.clear()
        else:
            self.__kwds__['cached'] = False
        return
Ejemplo n.º 9
0
    def __init__(self, window_name="output"):
        self.WIDTH = 250

        self.DISPLAY_STEERING_MIN = -1
        self.DISPLAY_THROTTLE_MIN = -1
        self.DISPLAY_STEERING_MAX = 1
        self.DISPLAY_THROTTLE_MAX = 1

        self.DISPLAY_STEERING_RANGE = self.DISPLAY_STEERING_MAX - self.DISPLAY_STEERING_MIN
        self.DISPLAY_THROTTLE_RANGE = self.DISPLAY_THROTTLE_MAX - self.DISPLAY_THROTTLE_MIN

        self.DISPLAY_STEP = 0.1
        self.DISPLAY_STEERING_SEGMENT_WIDTH = int(
            self.WIDTH * self.DISPLAY_STEP / self.DISPLAY_STEERING_RANGE)
        self.DISPLAY_THROTTLE_SEGMENT_WIDTH = int(
            self.WIDTH * self.DISPLAY_STEP / self.DISPLAY_THROTTLE_RANGE)

        self.HUE_RANGE = 60

        self.img = None
        self.window_name = window_name
        self._clear()
        self.interpolator = Interpolator()
Ejemplo n.º 10
0
 def __init__(self, known_points, known_values, k=1.0):
     Interpolator.__init__(self, known_points, known_values)
     # k: exponent for inverse distance weighing function; k >= 1; larger k yields an interpolated surface with bigger "plateaus" around the sample points
     assert k >= 1.0
     self._k = k
Ejemplo n.º 11
0
                             nargs=5,
                             required=False,
                             metavar=('DATABASE', 'USER', 'PASSWORD', 'HOST',
                                      'PORT'),
                             help='Specifies connection to database')
    file_parser.add_argument('--db',
                             nargs=5,
                             required=False,
                             metavar=('DATABASE', 'USER', 'PASSWORD', 'HOST',
                                      'PORT'),
                             help='Specifies connection to database')

    args = parser.parse_args()

    output, closing = output.get_outputs(args)
    interpolator = Interpolator(output)

    try:
        if args.command == 'mqtt':

            def on_message(client, user_data, msg):
                if msg.topic == STANDARD_TOPIC:
                    message = msg.payload.rstrip()
                    interpolator.add_item(message.split(','))

            def on_connect(client, userdata, flags, rc):
                client.subscribe(STANDARD_TOPIC)

            client = mqtt.Client()

            client.on_connect = on_connect
Ejemplo n.º 12
0
                increment = True
            f.close()
            os.rename(outpath + ".tmp", outpath)
            #interpolator.printTime("Completed dump");
            #self.invalid.append(path);
            if not increment:
                i = i + 1
        self.cleanup()


######################################################
# start Gatekeeper and listen for requests...
######################################################
lockfile = "lockfile"
filename = '../weather_data/all.nc'
ip = Interpolator(filename)
gk = Gatekeeper("../coms", lockfile)
mindelay = 0.1
# seconds
start = time.time()
last = start
ip.printTime("Starting")

## Running it indefinitely
try:
    while (True):
        gk.process(ip)
        current = time.time()
        delay = last + mindelay - current
        if (delay > 0):
            time.sleep(delay)
Ejemplo n.º 13
0
class OutputVisualizer:
    def __init__(self, window_name="output"):
        self.WIDTH = 250

        self.DISPLAY_STEERING_MIN = -1
        self.DISPLAY_THROTTLE_MIN = -1
        self.DISPLAY_STEERING_MAX = 1
        self.DISPLAY_THROTTLE_MAX = 1

        self.DISPLAY_STEERING_RANGE = self.DISPLAY_STEERING_MAX - self.DISPLAY_STEERING_MIN
        self.DISPLAY_THROTTLE_RANGE = self.DISPLAY_THROTTLE_MAX - self.DISPLAY_THROTTLE_MIN

        self.DISPLAY_STEP = 0.1
        self.DISPLAY_STEERING_SEGMENT_WIDTH = int(
            self.WIDTH * self.DISPLAY_STEP / self.DISPLAY_STEERING_RANGE)
        self.DISPLAY_THROTTLE_SEGMENT_WIDTH = int(
            self.WIDTH * self.DISPLAY_STEP / self.DISPLAY_THROTTLE_RANGE)

        self.HUE_RANGE = 60

        self.img = None
        self.window_name = window_name
        self._clear()
        self.interpolator = Interpolator()

    def _clear(self):
        self.img = np.zeros((self.WIDTH, self.WIDTH, 3), np.uint8)

    def _iterate(self, output):
        u = output[:, :2]
        q = output[:, 2]
        self.interpolator.set_u(u)
        self.interpolator.set_q(q)
        X = []
        Y = []
        Z = []
        for throttle in np.arange(-1, 1.1, 0.1):
            for steering in np.arange(-1, 1.1, 0.1):
                X.append(throttle)
                Y.append(steering)
                Z.append(
                    self.interpolator.get_quality(
                        np.array([throttle, steering])))
        return X, Y, Z

    def _coord2px(self, value):
        return int(min(max(value, 0), self.WIDTH - 1))

    def _draw_output_2(self, output):
        u = output[:, :2]
        q = output[:, 2]

        x = [action[1] for action in u]
        y = [action[0] for action in u]

        x_0 = min(x)
        y_0 = min(y)

        x_pixels_per_value = self.WIDTH / (max(x) - min(x))
        y_pixels_per_value = self.WIDTH / (max(y) - min(y))

        x_loc = [
            self._coord2px((x_value - x_0) * x_pixels_per_value)
            for x_value in x
        ]
        y_loc = [
            self._coord2px(self.WIDTH - (y_value - y_0) * y_pixels_per_value)
            for y_value in y
        ]

        x_values = sorted(set(x_loc))
        y_values = sorted(set(y_loc))

        x_start = dict(
            zip(x_values, [0] + [
                self._coord2px((x_values[i + 1] + x_values[i]) / 2)
                for i in range(len(x_values) - 1)
            ]))
        y_start = dict(
            zip(y_values, [0] + [
                self._coord2px((y_values[i + 1] + y_values[i]) / 2)
                for i in range(len(y_values) - 1)
            ]))

        x_stop = dict(
            zip(x_values, [
                self._coord2px((x_values[i + 1] + x_values[i]) / 2)
                for i in range(len(x_values) - 1)
            ] + [self.WIDTH - 1]))
        y_stop = dict(
            zip(y_values, [
                self._coord2px((y_values[i + 1] + y_values[i]) / 2)
                for i in range(len(y_values) - 1)
            ] + [self.WIDTH - 1]))

        q_0 = min(q + [0])

        hue_per_value = self.HUE_RANGE / (max(q + [0]) - min(q + [0]))

        for i in range(len(q)):
            cv2.rectangle(self.img, (x_start[x_loc[i]], y_start[y_loc[i]]),
                          (x_stop[x_loc[i]], y_stop[y_loc[i]]),
                          color=tuple(
                              map(
                                  int,
                                  cv2.cvtColor(
                                      np.uint8([[[(q[i] - q_0) * hue_per_value,
                                                  255, 255]]]),
                                      cv2.COLOR_HSV2BGR)[0, 0])),
                          thickness=-1)

            cv2.circle(self.img, (x_loc[i], y_loc[i]),
                       max(int(self.DISPLAY_THROTTLE_SEGMENT_WIDTH / 5), 1),
                       (0, 0, 0), -1)

    def _draw_output(self, output):
        u = output[:, :2]
        q = output[:, 2]
        self.interpolator.set_u(u)
        self.interpolator.set_q(q)

        X = []
        Y = []
        Z = []

        for throttle in np.arange(
                self.DISPLAY_THROTTLE_MIN,
                self.DISPLAY_THROTTLE_MAX + self.DISPLAY_STEP,
                self.DISPLAY_STEP):
            y = (self.WIDTH - self.DISPLAY_THROTTLE_SEGMENT_WIDTH) * (
                0.5 - throttle / self.DISPLAY_THROTTLE_RANGE)
            for steering in np.arange(
                    self.DISPLAY_STEERING_MIN,
                    self.DISPLAY_STEERING_MAX + self.DISPLAY_STEP,
                    self.DISPLAY_STEP):
                x = (self.WIDTH - self.DISPLAY_STEERING_SEGMENT_WIDTH) * (
                    0.5 + steering / self.DISPLAY_STEERING_RANGE)

                X.append(int(x))
                Y.append(int(y))
                Z.append(
                    self.interpolator.get_quality(
                        np.array([throttle, steering])))

        knots = []
        for i in range(len(u)):
            throttle = u[i][0]
            steering = u[i][1]
            y = int(self.WIDTH *
                    (0.5 - throttle / self.DISPLAY_THROTTLE_RANGE))
            x = int(self.WIDTH *
                    (0.5 + steering / self.DISPLAY_STEERING_RANGE))
            knots.append((x, y))

        min_q = min(Z)
        range_q = max(q) - min_q

        q_multiplier = self.HUE_RANGE / range_q
        if math.isnan(q_multiplier) or math.isinf(q_multiplier):
            q_multiplier = 1

        Z = [
            min(max(int(q_multiplier * (z - min_q)), 0), self.HUE_RANGE)
            for z in Z
        ]

        for i in range(len(Z)):
            cv2.rectangle(self.img, (int(min(
                self.WIDTH - 1, X[i])), int(min(self.WIDTH - 1, Y[i]))),
                          (int(
                              min(self.WIDTH - 1,
                                  X[i] + self.DISPLAY_STEERING_SEGMENT_WIDTH)),
                           int(
                               min(self.WIDTH - 1, Y[i] +
                                   self.DISPLAY_THROTTLE_SEGMENT_WIDTH))),
                          color=tuple(
                              map(
                                  int,
                                  cv2.cvtColor(np.uint8([[[Z[i], 255, 255]]]),
                                               cv2.COLOR_HSV2BGR)[0, 0])),
                          thickness=-1)

        for knot in knots:
            cv2.circle(self.img, knot,
                       max(int(self.DISPLAY_THROTTLE_SEGMENT_WIDTH / 5), 1),
                       (0, 0, 0), -1)

    def render(self, output):
        self._clear()
        self._draw_output_2(output)
        cv2.imshow(self.window_name, self.img)
        cv2.waitKey(40)
Ejemplo n.º 14
0
    def train(self, terminal_state):
        # Start training only if certain number of samples is already saved
        if len(self.replay_memory) < MIN_REPLAY_MEMORY_SIZE:
            return

        # Calculate Prioritized Experience Replay weights
        current_states = np.array([transition[0] for transition in self.replay_memory])
        future_states = np.array([transition[3] for transition in self.replay_memory])
        current_qs = self.model.predict(current_states)
        future_qs = self.target_model.predict(future_states)
        p = np.array([abs((reward + DISCOUNT * np.amax(future_qs[index]) if not done else reward)
                          - current_qs[index][ACTIONS.index(action)])
                      for index, (_, action, reward, _, done) in enumerate(self.replay_memory)])
        p = np.interp(p, (p.min(), p.max()), (0, +1))
        p /= np.sum(p)

        # Get a minibatch of random samples from memory replay table
        minibatch = np.array(self.replay_memory)[np.random.choice(len(self.replay_memory),
                                                                  size=MINIBATCH_SIZE,
                                                                  replace=False,
                                                                  p=p)]  # random.sample(self.replay_memory, MINIBATCH_SIZE)

        # Get current states from minibatch, then query NN model for Q values
        current_states = np.array([transition[0] for transition in minibatch])  # / 255
        current_qs_list = self.model.predict(current_states)

        # Get future states from minibatch, then query NN model for Q values
        # When using target network, query it, otherwise main network should be queried
        new_current_states = np.array([transition[3] for transition in minibatch])  # / 255
        future_target_qs_list = self.target_model.predict(new_current_states)
        future_model_qs_list = self.model.predict(new_current_states)

        x = []
        y = []
        interpolator = Interpolator()

        # Now we need to enumerate our batches
        for index, (current_state, action, reward, new_current_state, done) in enumerate(minibatch):

            # If not a terminal state, get new q from future states, otherwise set it to 0
            # almost like with Q Learning, but we use just part of equation here
            future_model_qs_at_index = future_model_qs_list[index]
            future_target_qs_at_index = future_target_qs_list[index]
            # future_qs = np.reshape(future_model_qs_at_index, OUTPUT_2D_SHAPE)
            if not done:
                max_future_q = future_target_qs_at_index[np.argmax(future_model_qs_at_index)]
                new_q = reward + DISCOUNT * max_future_q
            else:
                new_q = reward

            # Update Q value for given state
            current_qs_list_at_index = current_qs_list[index]
            current_qs = np.reshape(current_qs_list_at_index, OUTPUT_2D_SHAPE)
            current_actions = ACTIONS
            current_qualities = current_qs

            interpolator.set_u(current_actions)
            interpolator.set_q(current_qualities)
            interpolator.update_function(action, new_q)
            # current_qs = np.zeros(OUTPUT_2D_SHAPE)
            # current_qs[:, :2] = interpolator.get_u()
            current_qs = interpolator.get_q()  # [current_actions.index(action)] = [new_q]  #

            # print(current_state)
            # print(current_qs_list)
            # print(action)
            # current_qs[action] = new_q

            # And append to our training data
            x.append(current_state)
            reshaped_current_qs = np.reshape(current_qs, OUTPUT_1D_SHAPE)
            y.append(reshaped_current_qs)

        # print("x:", x)
        # print("y:", y)
        # Fit on all samples as one batch, log only on terminal state
        self.model.fit(np.array(x), np.array(y), batch_size=MINIBATCH_SIZE, verbose=0, shuffle=False,
                       callbacks=[self.tensorboard] if terminal_state else None)
        # Update target network counter every episode
        if terminal_state:
            self.target_update_counter += 1

        # If counter reaches set value, update target network with weights of main network
        if self.target_update_counter > UPDATE_TARGET_EVERY:
            self.target_model.set_weights(self.model.get_weights())
            # a = self.model.get_weights()
            # print(a)
            self.target_update_counter = 0
            self.save_replay_memory()
    def __init__(self, simulate=False):
        # setup motor controller. The PWM controller can control up to 16
        # different devices. We have to add devices, one for each thruster that
        # we can control. The first parameter is the human-friendly name of the
        # device. That is used for logging to the console and/or a database. The
        # next parameter indicates which PWM connector this device is connected
        # to. This is refered to as the PWM channel. The last two values
        # indicate at what time intervals (ticks) the PWM should turn on and
        # off, respectively. We simply start each device at 0 time and control
        # the duration of the pulses by adjusting the off time. Note that we may
        # be able to shuffle on/off times to even out the current draw from the
        # thrusters, but so far, that hasn't been an issue. It's even possible
        # that the PWM controller may do that for us already.
        if simulate is False:
            from pwm_controller import PWMController

            self.motor_controller = PWMController()
            self.motor_controller.add_device("HL", HL, 0, NEUTRAL)
            self.motor_controller.add_device("VL", VL, 0, NEUTRAL)
            self.motor_controller.add_device("VC", VC, 0, NEUTRAL)
            self.motor_controller.add_device("VR", VR, 0, NEUTRAL)
            self.motor_controller.add_device("HR", HR, 0, NEUTRAL)
            self.motor_controller.add_device("LIGHT", LIGHT, 0, FULL_REVERSE)
        else:
            self.motor_controller = None

        # setup the joysticks. We use a 2D vector to represent the x and y
        # values of the joysticks.
        self.j1 = Vector2D()
        self.j2 = Vector2D()

        # create interpolators
        self.horizontal_left = Interpolator()
        self.vertical_left = Interpolator()
        self.vertical_center = Interpolator()
        self.vertical_right = Interpolator()
        self.horizontal_right = Interpolator()

        # setup interpolators from a file or manually
        if os.path.isfile(SETTINGS_FILE):
            with open(SETTINGS_FILE, 'r') as f:
                self.set_settings(json.load(f), False)
        else:
            # Set the sensitivity to be applied to each thruster. 0 indicates a
            # linear response which is the default when no sensitivity is applied. 1
            # indicates full sensitivity. Values between 0 and 1 can be used to
            # increase and to decrease the overall sensitivity. Increasing sensivity
            # dampens lower values and amplifies larger values giving more precision
            # at lower power levels.
            self.sensitivity = 0.7

            # We use a cubic to apply sensitivity. If you find that full sensitivity
            # (dampening) does not give you fine enough control, you can increase\
            # the degree of the polynomial used for dampening. Note that this must
            # be a positive odd number. Any other values will cause unexpected
            # results.
            self.power = 3

            # setup the various interpolators for each thruster. Each item we add
            # to the interpolator consists of two values: an angle in degrees and a
            # thrust value. An interpolator works by returning a value for any given
            # input value. More specifically in this case, we will give each
            # interpolator an angle and it will return a thrust value for that
            # angle. Since we have only given the interpolator values for very
            # specific angles, it will have to determine values for angles we have
            # not provided. It does this using linear interpolation.
            self.horizontal_left.addIndexValue(0.0, -1.0)
            self.horizontal_left.addIndexValue(90.0, 1.0)
            self.horizontal_left.addIndexValue(180.0, 1.0)
            self.horizontal_left.addIndexValue(270.0, -1.0)
            self.horizontal_left.addIndexValue(360.0, -1.0)

            self.vertical_left.addIndexValue(0.0, 1.0)
            self.vertical_left.addIndexValue(90.0, -1.0)
            self.vertical_left.addIndexValue(180.0, -1.0)
            self.vertical_left.addIndexValue(270.0, 1.0)
            self.vertical_left.addIndexValue(360.0, 1.0)

            self.vertical_center.addIndexValue(0.0, 0.0)
            self.vertical_center.addIndexValue(90.0, 1.0)
            self.vertical_center.addIndexValue(180.0, 0.0)
            self.vertical_center.addIndexValue(270.0, -1.0)
            self.vertical_center.addIndexValue(360.0, 0.0)

            self.vertical_right.addIndexValue(0.0, -1.0)
            self.vertical_right.addIndexValue(90.0, -1.0)
            self.vertical_right.addIndexValue(180.0, 1.0)
            self.vertical_right.addIndexValue(270.0, 1.0)
            self.vertical_right.addIndexValue(360.0, -1.0)

            self.horizontal_right.addIndexValue(0.0, 1.0)
            self.horizontal_right.addIndexValue(90.0, 1.0)
            self.horizontal_right.addIndexValue(180.0, -1.0)
            self.horizontal_right.addIndexValue(270.0, -1.0)
            self.horizontal_right.addIndexValue(360.0, 1.0)

        # setup ascent/descent controllers
        self.ascent = -1.0
        self.descent = -1.0

        # setup light
        self.light = 0.0
Ejemplo n.º 16
0
  def test___init__(self):
    ''' Just run the linear and quadratic interpolations '''

    fseries, signalseries, errorseries = self.ServeInput()
    interpolator = Interpolator(fseries, signalseries, running_regr_type = 'linear', windowlength=0)
    y_interpolated = interpolator.y(fseries)
    e_interpolated = interpolator.e(fseries)
    self.assertAlmostEqual(numpy.sum(signalseries-y_interpolated), 0.0, places=10)
    self.assertAlmostEqual(numpy.sum(e_interpolated), 0.0, places=10)

    fseries, signalseries, errorseries = self.ServeInput()
    interpolator = Interpolator(fseries, signalseries, running_regr_type = 'linear', windowlength=5)
    y_interpolated = interpolator.y(fseries)
    e_interpolated = interpolator.e(fseries)

    fseries, signalseries, errorseries = self.ServeInput(create_errorseries=True)
    interpolator = Interpolator(fseries, signalseries, running_regr_type = 'linear', windowlength=5)
    y_interpolated = interpolator.y(fseries)
    e_interpolated = interpolator.e(fseries)

    fseries, signalseries, errorseries = self.ServeInput()
    interpolator = Interpolator(fseries, signalseries, running_regr_type = 'quadratic', windowlength=5)
    y_interpolated = interpolator.y(fseries)
    e_interpolated = interpolator.e(fseries)

    fseries, signalseries, errorseries = self.ServeInput(create_errorseries=True)
    interpolator = Interpolator(fseries, signalseries, running_regr_type = 'quadratic', windowlength=5)
    y_interpolated = interpolator.y(fseries)
    e_interpolated = interpolator.e(fseries)
Ejemplo n.º 17
0
                plt.plot(timestamps, data[i][3], 'b')
                plt.xlabel('frame time (msec)')
                plt.ylabel('acceleromenter value')
                plt.legend(['x', 'y', 'z'])
                plt.show()
                visualized += 1
            
    if (visualize_interpolated):
        timestamps = np.arange(train_frame_start,
            train_frame_end + train_sparseness, train_sparseness)

        visualize_count = 3
        visualized = 0
        for i in xrange(0, len(labels)):
            if (not labels[i] and visualized < visualize_count):
                cutted_data = Interpolator.cut_data([data[i]],
                    test_frame_start, test_frame_end, test_sparseness)
                interpolated_data = Interpolator.interpolate(
                    cutted_data, train_frame_start,
                    train_frame_end, train_sparseness,
                    interpolation_degree)
                plt.figure(figsize=(20, 10))
                plt.plot(timestamps, data[i][2], '--b')
                plt.plot(timestamps, interpolated_data[0][2], '-r')
                plt.xlabel('frame time (msec)')
                plt.ylabel('acceleromenter value')
                plt.legend(['source data', 'interpolated data'])
                plt.show()
                visualized += 1
        
    if (training_enabled):
        train_dataset_size = int(0.8 * len(data))
class ThrusterController:

    def __init__(self, simulate=False):
        # setup motor controller. The PWM controller can control up to 16
        # different devices. We have to add devices, one for each thruster that
        # we can control. The first parameter is the human-friendly name of the
        # device. That is used for logging to the console and/or a database. The
        # next parameter indicates which PWM connector this device is connected
        # to. This is refered to as the PWM channel. The last two values
        # indicate at what time intervals (ticks) the PWM should turn on and
        # off, respectively. We simply start each device at 0 time and control
        # the duration of the pulses by adjusting the off time. Note that we may
        # be able to shuffle on/off times to even out the current draw from the
        # thrusters, but so far, that hasn't been an issue. It's even possible
        # that the PWM controller may do that for us already.
        if simulate is False:
            from pwm_controller import PWMController

            self.motor_controller = PWMController()
            self.motor_controller.add_device("HL", HL, 0, NEUTRAL)
            self.motor_controller.add_device("VL", VL, 0, NEUTRAL)
            self.motor_controller.add_device("VC", VC, 0, NEUTRAL)
            self.motor_controller.add_device("VR", VR, 0, NEUTRAL)
            self.motor_controller.add_device("HR", HR, 0, NEUTRAL)
            self.motor_controller.add_device("LIGHT", LIGHT, 0, FULL_REVERSE)
        else:
            self.motor_controller = None

        # setup the joysticks. We use a 2D vector to represent the x and y
        # values of the joysticks.
        self.j1 = Vector2D()
        self.j2 = Vector2D()

        # create interpolators
        self.horizontal_left = Interpolator()
        self.vertical_left = Interpolator()
        self.vertical_center = Interpolator()
        self.vertical_right = Interpolator()
        self.horizontal_right = Interpolator()

        # setup interpolators from a file or manually
        if os.path.isfile(SETTINGS_FILE):
            with open(SETTINGS_FILE, 'r') as f:
                self.set_settings(json.load(f), False)
        else:
            # Set the sensitivity to be applied to each thruster. 0 indicates a
            # linear response which is the default when no sensitivity is applied. 1
            # indicates full sensitivity. Values between 0 and 1 can be used to
            # increase and to decrease the overall sensitivity. Increasing sensivity
            # dampens lower values and amplifies larger values giving more precision
            # at lower power levels.
            self.sensitivity = 0.7

            # We use a cubic to apply sensitivity. If you find that full sensitivity
            # (dampening) does not give you fine enough control, you can increase\
            # the degree of the polynomial used for dampening. Note that this must
            # be a positive odd number. Any other values will cause unexpected
            # results.
            self.power = 3

            # setup the various interpolators for each thruster. Each item we add
            # to the interpolator consists of two values: an angle in degrees and a
            # thrust value. An interpolator works by returning a value for any given
            # input value. More specifically in this case, we will give each
            # interpolator an angle and it will return a thrust value for that
            # angle. Since we have only given the interpolator values for very
            # specific angles, it will have to determine values for angles we have
            # not provided. It does this using linear interpolation.
            self.horizontal_left.addIndexValue(0.0, -1.0)
            self.horizontal_left.addIndexValue(90.0, 1.0)
            self.horizontal_left.addIndexValue(180.0, 1.0)
            self.horizontal_left.addIndexValue(270.0, -1.0)
            self.horizontal_left.addIndexValue(360.0, -1.0)

            self.vertical_left.addIndexValue(0.0, 1.0)
            self.vertical_left.addIndexValue(90.0, -1.0)
            self.vertical_left.addIndexValue(180.0, -1.0)
            self.vertical_left.addIndexValue(270.0, 1.0)
            self.vertical_left.addIndexValue(360.0, 1.0)

            self.vertical_center.addIndexValue(0.0, 0.0)
            self.vertical_center.addIndexValue(90.0, 1.0)
            self.vertical_center.addIndexValue(180.0, 0.0)
            self.vertical_center.addIndexValue(270.0, -1.0)
            self.vertical_center.addIndexValue(360.0, 0.0)

            self.vertical_right.addIndexValue(0.0, -1.0)
            self.vertical_right.addIndexValue(90.0, -1.0)
            self.vertical_right.addIndexValue(180.0, 1.0)
            self.vertical_right.addIndexValue(270.0, 1.0)
            self.vertical_right.addIndexValue(360.0, -1.0)

            self.horizontal_right.addIndexValue(0.0, 1.0)
            self.horizontal_right.addIndexValue(90.0, 1.0)
            self.horizontal_right.addIndexValue(180.0, -1.0)
            self.horizontal_right.addIndexValue(270.0, -1.0)
            self.horizontal_right.addIndexValue(360.0, 1.0)

        # setup ascent/descent controllers
        self.ascent = -1.0
        self.descent = -1.0

        # setup light
        self.light = 0.0

    def __del__(self):
        '''
        When an instance of this class gets destroyed, we need to make sure that
        we turn off all motors. Otherwise, we could end up in a situation where
        the vehicle could have thrusters running when we don't have scripts
        running to control it.
        '''
        self.set_motor(HL, 0.0)
        self.set_motor(VL, 0.0)
        self.set_motor(VC, 0.0)
        self.set_motor(VL, 0.0)
        self.set_motor(HR, 0.0)

    def update_axis(self, axis, value):
        '''
        This is the main method of this class. It is responsible for taking an
        controller input value (referred to as an axis value) and then
        converting that into the appropriate thrust values for the appropriate
        thrusters associated with that axis.

        For the two joysticks, we convert the joystick position into an angle.
        We know which thrusters each joystick controls, so we feed the
        calculated angle into the thruster interpolators for that joystick. This
        gives us the new thruster value for each thruster, which we then apply
        to the PWM controller devices for those thrusters.

        Note that the angle of the joystick does not give us all of the
        information that we need. If the joystick is close to the center
        position, then we don't need to apply as much thrust. If it is pushed
        all the way to the edge, then we nee 100% thrust. So, we treat the
        center as 0% and the edge as 100%. The values we get back from the
        interpolators are 100% values, so we simply apply the joystick
        percentage to the interpolator value to find the actual thrust value we
        need to use.

        Things get a bit more complicated for the vertical thrusters because it
        is possible that we will be pitiching or rolling the vehicle while
        simultaneously trying to move the vehicle directly up or down. If we
        pitch or roll the vehicle only, then the process is exactly as we
        described above. However, if are pithing and/or rolling AND moveing the
        vehicle vertically, we need to combine the two operations into one set
        of thruster values. We have to first determine the values for pitch and
        roll, then we increase or decrease all thruster values equally in the up
        or down direction. However it is possible that we will not be able to
        increase/decrease all thrusters by the same amount since we are already
        applying thrust for pitch and roll. This means we need to make sure our
        values do not go outside the closed intervale [-1,1]. This means that as
        we pitch or roll harder, the vehical will flattern out as we apply
        vertical thrust.
        '''

        # We need to keep track of which thrusters need updating. We use the
        # following flags for that purpose
        update_horizontal_thrusters = False
        update_vertical_thrusters = False

        # Round the incoming value to the specified precision to reduce input
        # noise
        value = round(value, PRECISION)

        # Update the appropriate joystick vector based on which controller axis
        # has changed. Note that we make sure the value is different from what
        # we have already to prevent unnecessary updates. Recall that the
        # controller may send values whose differences are smaller than our
        # precision. This means we will get an update from the controller, but
        # we decided to ignore it since it won't result in a significant change
        # to our thrusters.
        if axis == JL_H:
            if self.j1.x != value:
                self.j1.x = value
                update_horizontal_thrusters = True
        elif axis == JL_V:
            if self.j1.y != value:
                self.j1.y = value
                update_horizontal_thrusters = True
        elif axis == JR_H:
            if self.j2.x != value:
                self.j2.x = value
                update_vertical_thrusters = True
        elif axis == JR_V:
            if self.j2.y != value:
                self.j2.y = value
                update_vertical_thrusters = True
        elif axis == AL:
            if self.descent != value:
                self.descent = value
                update_vertical_thrusters = True
        elif axis == AR:
            if self.ascent != value:
                self.ascent = value
                update_vertical_thrusters = True
        else:
            pass
            # print("unknown axis ", event.axis)

        # updating horizontal thrusters is easy: find current angle, convert
        # angle to thruster values, apply values
        if update_horizontal_thrusters:
            left_value = self.horizontal_left.valueAtIndex(self.j1.angle)
            right_value = self.horizontal_right.valueAtIndex(self.j1.angle)
            power = min(1.0, self.j1.length)
            self.set_motor(HL, left_value * power)
            self.set_motor(HR, right_value * power)

        # updating vertical thrusters is trickier. We do the same as above, but
        # then post-process the values if we are applying vertical up/down
        # thrust. As mentioned above, we have to be careful to stay within our
        # [-1,1] interval.
        if update_vertical_thrusters:
            power = min(1.0, self.j2.length)
            back_value = self.vertical_center.valueAtIndex(self.j2.angle) * power
            front_left_value = self.vertical_left.valueAtIndex(self.j2.angle) * power
            front_right_value = self.vertical_right.valueAtIndex(self.j2.angle) * power
            if self.ascent != -1.0:
                percent = (1.0 + self.ascent) / 2.0
                max_thrust = max(back_value, front_left_value, front_right_value)
                max_adjust = (1.0 - max_thrust) * percent
                # back_value += max_adjust
                front_left_value += max_adjust
                front_right_value += max_adjust
            elif self.descent != -1.0:
                percent = (1.0 + self.descent) / 2.0
                min_thrust = min(back_value, front_left_value, front_right_value)
                max_adjust = (min_thrust - -1.0) * percent
                # back_value -= max_adjust
                front_left_value -= max_adjust
                front_right_value -= max_adjust
            self.set_motor(VC, back_value)
            self.set_motor(VL, front_left_value)
            self.set_motor(VR, front_right_value)

    def update_button(self, button, value):
        if button == UP:
            self.light = min(1.0, self.light + LIGHT_STEP)
        elif button == DOWN:
            self.light = max(0.0, self.light - LIGHT_STEP)
        elif button == RESET:
            self.light = 0.0

        light_value = map_range(self.light, 0.0, 1.0, -1.0, 1.0)
        print("button %s, light = %s, light_value = %s" % (button, self.light, light_value))
        self.set_motor(LIGHT, light_value)

    def set_motor(self, motor_number, value):
        if self.motor_controller is not None:
            motor = self.motor_controller.devices[motor_number]
            value = self.apply_sensitivity(value)
            pwm_value = int(map_range(value, -1.0, 1.0, FULL_REVERSE, FULL_FORWARD))

            # print("setting motor {0} to {1}".format(motor_number, pwm_value))
            motor.off = pwm_value

    def apply_sensitivity(self, value):
        return self.sensitivity * value**self.power + (1.0 - self.sensitivity) * value

    def get_settings(self):
        return {
            'version': 1,
            'sensitivity': {
                'strength': self.sensitivity,
                'power': self.power
            },
            'thrusters': [
                self.horizontal_left.to_array(),
                self.vertical_left.to_array(),
                self.vertical_center.to_array(),
                self.vertical_right.to_array(),
                self.horizontal_right.to_array()
            ]
        }

    def set_settings(self, data, save=True):
        if data['version'] == 1:
            # save settings for future loading
            if save:
                if data['name'] == "":
                    filename = SETTINGS_FILE
                else:
                    filename = os.path.join("settings", data['name'] + ".json")

                with open(filename, 'w') as out:
                    out.write(json.dumps(data, indent=2))

            # update current settings
            self.sensitivity = float(data['sensitivity']['strength'])
            self.power = float(data['sensitivity']['power'])
            self.horizontal_left.from_array(data['thrusters'][0])
            self.vertical_left.from_array(data['thrusters'][1])
            self.vertical_center.from_array(data['thrusters'][2])
            self.vertical_right.from_array(data['thrusters'][3])
            self.horizontal_right.from_array(data['thrusters'][4])
        else:
            print("Unsupported data version number '{}'".format(data['version']))
Ejemplo n.º 19
0
        exit(1)
    # Start frame
    batch_count = (cag['frame_count'] - start_frame) // cag['batch_size']
    if (cag['frame_count'] - 1) % cag['batch_size']:
        batch_count += 1

    # Setup
    sys.path.append(f"{os.path.abspath(cag['algorithm'])}")
    from interpolator import Interpolator

    # Interpolate
    interpolator = Interpolator(
        cag['model_path'],
        cag['sf'],
        int(cag['height']),
        int(cag['width']),
        batch_size=cag['batch_size'],
        net_name=cag['net_name'],
        channel=3  # DAIN
    )
    save = data_writer(cag['output_type'])
    ori_frames = []
    batch = [None] * (cag['batch_size'])
    batch.append(video.read()[1])
    interpolator.batch[0] = interpolator.ndarray2tensor([batch[-1]])[0]
    timer = 0
    start_time = time.time()
    try:
        for i in range(batch_count):
            batch[0] = batch[-1]
            batch[1:] = [video.read() for _ in range(cag['batch_size'])]
Ejemplo n.º 20
0
    pp = pd.concat((p, sp))
    pp.sort_values(['x','y'], inplace=True)
    # log(pp.head().to_string())
    # log('\n')
    # log(pp.tail().to_string())
    log('Pre-processing... ')
    pp.drop(pp[pp.r < config.config['min_height']].index, inplace=True)
    pp.drop(pp[pp.r > config.config['max_height']].index, inplace=True)
    rh_min = 1.6*config.config['cellsize']
    pp.loc[pp.rh < rh_min, 'rh'] = rh_min
    adj = get_groupies(pp, grad=config.config['gradient'])
    log('Done\n')
    return pp, adj

if __name__ == "__main__":
    from interpolator import Interpolator
    from writer import Writer
    import config

    config.parse()
    if len(sys.argv) > 1:
        df, adj = prep(fbore=sys.argv[1], fscreen=sys.argv[2], config=config)
        w = Writer(sys.argv[3])
        i = Interpolator(df, adj, w)
        i.interpolate()
    else:
        df, adj = prep(config=config)
        i = Interpolator(df, adj)
        i.interpolate()

Ejemplo n.º 21
0
 frame_count = int(cap.get(7))
 frame_count_len = len(str(frame_count))
 # Setup
 if 'Interpolator' not in locals():
     if args['empty_cache']:
         os.environ['CUDA_EMPTY_CACHE'] = '1'
     if args['model_path'] == 'default':  # 模型路径
         model_path = f"{args['algorithm']}/{model_path[args['algorithm']]}"
     # Model checking
     if not os.path.exists(model_path):
         print(f"Model {model_path} doesn't exist, exiting")
         exit(1)
     sys.path.append(f"{os.path.abspath(args['algorithm'])}")
     from interpolator import Interpolator
 # Interpolate
 interpolator = Interpolator(model_path, sf, int(cap.get(4)), int(cap.get(3)), batch_size=1,
                             save_which=1, net_name='DAIN_slowmotion', channel=3)
 timer = 0
 loss_fn = mse()
 start_time = time.time()
 batch = [cap.read()[1]]
 batch_count = frame_count//sf-1
 for i in range(batch_count):
     gt = []
     for f in [cap.read() for _ in range(sf-1)]:
         if f[0]:
             gt.append(f[1])
         else:
             break
     f = cap.read()
     if f[0]:
         del batch[:-1]