Ejemplo n.º 1
0
class MeasureFrame(LabelFrame):
    def __init__(self, master, tracker, text="Measuring", *args, **kwargs):
        LabelFrame.__init__(self, master, text=text, *args, **kwargs)
        self.tracker = tracker

        self.config_frame = NamedEntryFrame(self, (OBS_INTERVAL,
                                                   NUM_SAMPLES,
                                                   NUM_OBSS),
                                            parsers={OBS_INTERVAL: float,
                                                     NUM_SAMPLES: int,
                                                     NUM_OBSS: int})
        self.config_frame.grid()

        self.save_frame = LabelFrame(self, text="Saving")
        self.dest_selector = FileSelectionFrame(self.save_frame,
                                                ask_mode="save")
        self.dest_selector.grid(row=0, column=0, columnspan=2)
        self.save_button = Button(self.save_frame, text="Save",
                                  command=bg_caller(self.save))
        self.save_button.grid(row=1, column=0)
        self.appending_var = BooleanVar()
        self.append_checkbutton = Checkbutton(self.save_frame, text="Append",
                                              variable=self.appending_var)
        self.append_checkbutton.grid(row=1, column=1)
        self.save_frame.grid()

    def measure(self, only_accurate=True):
        try:
            interval = self.config_frame.get(OBS_INTERVAL)
            samples = self.config_frame.get(NUM_SAMPLES)
            num_obss = self.config_frame.get(NUM_OBSS)
        except ValueError:
            logger.error("Could not parse input fields.")
        data = self.tracker.measure(observation_interval=interval,
                                    samples_per_observation=samples,
                                    number_of_observations=num_obss)
        if only_accurate:
            accurate_data = [point for point in data
                             if point.status == point.DATA_ACCURATE]
            num_invalid = len(data) - len(accurate_data)
            if num_invalid > 0:
                logger.warning("Hiding {} inaccurate data points."
                               .format(num_invalid))
            return accurate_data
        else:
            return data

    def save(self, only_accurate=True):
        dest = self.dest_selector.path_var.get()
        if not dest:
            logger.error("Must select a destination file.")
            return

        data = self.measure(only_accurate=only_accurate)
        w = csv.writer(open(dest, 'a' if self.appending_var.get() else 'w'))
        for point in data:
            w.writerow((point.time, point.position.r,
                        point.position.theta, point.position.phi))

        logger.info("Saved measurements into {!r}".format(dest))
Ejemplo n.º 2
0
    def __init__(self, master, tracker, text="Repositioning", **kwargs):
        LabelFrame.__init__(self, master, text=text, **kwargs)
        self.tracker = tracker

        self.source_selector = FileSelectionFrame(self, initial_dir=nodes_dir,
                                                  ask_mode="open",
                                                  text="Source")
        self.source_selector.grid(row=0, column=0)
        self.dest_selector = FileSelectionFrame(self, initial_dir=nodes_dir,
                                                ask_mode="save", text="Dest")
        self.dest_selector.grid(row=0, column=1)

        # The names and positions we gather:
        self.name_fields = []
        self.posn_labels = []
        self.training_info = []

        for i in range(3):
            # Why three? Well, we need three training points to figure
            # out where all the other points are. Proof: take your
            # fist and move it around (it's a rigid structure, like
            # the arrangement of retroreflectors), and think about how
            # many points (e.g. knuckles) you'd need to know the
            # positions of to figure out the positions of all your
            # other knuckles.  (It's three. If you disagree, please
            # think about it more until you agree, because I am
            # absolutely confident.)
            f = LabelFrame(self, text="(Name/position) ({})".format(i))
            self.name_fields.append(Entry(f))
            self.name_fields[-1].grid(row=0, column=0)
            self.posn_labels.append(Label(f, text="<None>"))
            self.posn_labels[-1].grid(row=0, column=1)
            b = Button(f, text="Set",
                       command=bg_caller(deglobalize_args(self.set_position,
                                                          i)))
            b.grid(row=0, column=2)
            f.grid(row=1+i, column=0, columnspan=2)
            self.training_info.append(None)

        Button(self, text="Compute/Save", command=self.save).grid(row=4,
                                                                  column=0)
Ejemplo n.º 3
0
    def __init__(self, master, tracker, text="Measuring", *args, **kwargs):
        LabelFrame.__init__(self, master, text=text, *args, **kwargs)
        self.tracker = tracker

        self.config_frame = NamedEntryFrame(self, (OBS_INTERVAL,
                                                   NUM_SAMPLES,
                                                   NUM_OBSS),
                                            parsers={OBS_INTERVAL: float,
                                                     NUM_SAMPLES: int,
                                                     NUM_OBSS: int})
        self.config_frame.grid()

        self.save_frame = LabelFrame(self, text="Saving")
        self.dest_selector = FileSelectionFrame(self.save_frame,
                                                ask_mode="save")
        self.dest_selector.grid(row=0, column=0, columnspan=2)
        self.save_button = Button(self.save_frame, text="Save",
                                  command=bg_caller(self.save))
        self.save_button.grid(row=1, column=0)
        self.appending_var = BooleanVar()
        self.append_checkbutton = Checkbutton(self.save_frame, text="Append",
                                              variable=self.appending_var)
        self.append_checkbutton.grid(row=1, column=1)
        self.save_frame.grid()
Ejemplo n.º 4
0
class RepositioningFrame(LabelFrame):
    """Helps the user deal with the tracker being repositioned.
    
    Asks for a source file (CSV file containing name/x/y/z for many nodes)
    and a destination file (which will be the same). Also asks for the names
    of three points, whose positions are determined by querying a Tracker.
    Plugs the names/(original data)/positions into nodes.recomputing,
    which will determine the new position of the tracker and adjust the
    original file's x/y/z triplets accordingly, dumping the new values
    into the destination file.
    """
    def __init__(self, master, tracker, text="Repositioning", **kwargs):
        LabelFrame.__init__(self, master, text=text, **kwargs)
        self.tracker = tracker

        self.source_selector = FileSelectionFrame(self, initial_dir=nodes_dir,
                                                  ask_mode="open",
                                                  text="Source")
        self.source_selector.grid(row=0, column=0)
        self.dest_selector = FileSelectionFrame(self, initial_dir=nodes_dir,
                                                ask_mode="save", text="Dest")
        self.dest_selector.grid(row=0, column=1)

        # The names and positions we gather:
        self.name_fields = []
        self.posn_labels = []
        self.training_info = []

        for i in range(3):
            # Why three? Well, we need three training points to figure
            # out where all the other points are. Proof: take your
            # fist and move it around (it's a rigid structure, like
            # the arrangement of retroreflectors), and think about how
            # many points (e.g. knuckles) you'd need to know the
            # positions of to figure out the positions of all your
            # other knuckles.  (It's three. If you disagree, please
            # think about it more until you agree, because I am
            # absolutely confident.)
            f = LabelFrame(self, text="(Name/position) ({})".format(i))
            self.name_fields.append(Entry(f))
            self.name_fields[-1].grid(row=0, column=0)
            self.posn_labels.append(Label(f, text="<None>"))
            self.posn_labels[-1].grid(row=0, column=1)
            b = Button(f, text="Set",
                       command=bg_caller(deglobalize_args(self.set_position,
                                                          i)))
            b.grid(row=0, column=2)
            f.grid(row=1+i, column=0, columnspan=2)
            self.training_info.append(None)

        Button(self, text="Compute/Save", command=self.save).grid(row=4,
                                                                  column=0)

    def set_position(self, i):
        """Stores the Tracker's current (r,theta,phi)."""
        data = self.tracker.measure()[0]
        if data.status != data.DATA_ACCURATE:
            logger.error("Data taken were inaccurate.")
            return
        
        self.training_info[i] = data.position
        self.posn_labels[i].configure(text=str(self.training_info[i]))

    def save(self):
        """Recomputes the x/y/z CSV file based on our (name,r,theta,phi)s."""
        if None in self.training_info:
            logger.error("Can't recompute -- need 3 training points")
            return

        source_path = self.source_selector.path_var.get()
        dest_path = self.dest_selector.path_var.get()
        if not (source_path and dest_path):
            logger.error("Can't recompute -- need source and dest files")
            return

        try:
            old_data = nodes.load(source_path)
        except IOError:
            logger.error("{!r} no longer exists".format(source_path))
            return

        training_data = {self.name_fields[i].get(): self.training_info[i]
                         for i in range(3)}

        try:
            new_data = nodes.recompute(old_data, training_data)
        except ValueError as e:
            logger.error("Error recomputing: {}".format(e.message))
            return

        nodes.save(new_data, dest_path)
        logger.info("Wrote recomputed data to {!r}".format(dest_path))