Пример #1
0
 def load(self, config: OrderedDict) -> "IUHistory":
     """Load config data"""
     if config is None:
         config = {}
     self._history_span = timedelta(days=config.get(CONF_HISTORY_SPAN, 7))
     self._history_refresh = timedelta(seconds=config.get(CONF_HISTORY_REFRESH, 120))
     return self
Пример #2
0
 def _make_closure(self, kwargs: OrderedDict, optimizer: Optimizer) -> Closure:
     """Build a closure object that captures the given arguments and runs the `training_step` function and
     optionally other functions such as `backward` and `zero_grad`."""
     opt_idx = kwargs.get("optimizer_idx", 0)
     step_fn = self._make_step_fn(kwargs)
     backward_fn = self._make_backward_fn(optimizer, opt_idx)
     zero_grad_fn = self._make_zero_grad_fn(kwargs.get("batch_idx", 0), opt_idx, optimizer)
     return Closure(step_fn=step_fn, backward_fn=backward_fn, zero_grad_fn=zero_grad_fn)
Пример #3
0
 def load(self, config: OrderedDict):
     """Load config data for the controller"""
     self.clear()
     self._is_enabled = config.get(CONF_ENABLED, True)
     self._name = config.get(CONF_NAME, f"Controller {self._controller_index + 1}")
     self._switch_entity_id = config.get(CONF_ENTITY_ID)
     self._preamble = wash_td(config.get(CONF_PREAMBLE))
     self._postamble = wash_td(config.get(CONF_POSTAMBLE))
     self._dirty = True
     return self
Пример #4
0
    def load(self, config: OrderedDict):
        """ Load zone data from the configuration"""
        self.clear()

        self._is_enabled = config.get(CONF_ENABLED, True)
        self._name = config.get(CONF_NAME, f"Zone {self.zone_index + 1}")
        self._switch_entity_id = config.get(CONF_ENTITY_ID)
        self._adjustment.load(config)
        self._dirty = True
        return self
Пример #5
0
def main(argv):
    sawGitpException = False
    try:
        checkOtherInstancesWhenStarting()
        cmds = OrderedDict(
            diffdev=gitpTop_DiffMain,
            diffprev=gitpTop_DiffPrev,
            copytoaltrepo=gitpTop_CopyToAltRepo,
            slowcopytoaltrepo=gitpTop_SlowCopyToAltRepo,
            pack=gitpTop_Pack,
            apply=gitpTop_Apply_ApplyAndCheck,
            cdalt=gitpTop_CdAltRepo,
            recent=gitpTop_ViewRecent,
            runprettier=gitpTop_RunCommitHook,
            updatebasis=gitpTop_UpdateBasis,
            resethardall=gitpTop_ResetHardAll,
            seepacketdiff=gitpTop_ShowPacketDiff,
            seepacketinfo=lambda: gitpTop_ShowDescription(False),
            seepacketinfoverbose=lambda: gitpTop_ShowDescription(True),
        )

        infoText = OrderedDict(
            diffdev=' (shows diff against main branch, aka "ddd")',
            diffprev=' (shows diff against prev commit on branch)',
            copytoaltrepo=' (copy files to the other repo)',
        )

        root = argv[1] if len(argv) > 1 else ''
        cmd = argv[2] if len(argv) > 2 else ''
        os.chdir(root)
        fn = cmds.get(cmd)
        if fn:
            fn()
        else:
            help = "gitp, a tool to store & back up the changes you're not ready to push to a server.\n"
            help += "Ben Fisher, 2021\n\n"
            help += 'Syntax:\n'
            for k in cmds:
                info = infoText.get(k, '')
                help += f'gitp {k}{info}\n'
            trace(help)
    except GitPacketException as e:
        # we'll raise GitPacketException when stopping the script intentionally.
        sys.stderr.write(str(e) + '\n')
        sawGitpException = True

    # having a GitPacketException currently still counts as a clean exit.
    markCleanExitWhenEnding()
    if sawGitpException:
        sys.exit(1)
Пример #6
0
    def load(self, config: OrderedDict):
        """Load config data for the system"""
        self.clear()

        global SYSTEM_GRANULARITY
        SYSTEM_GRANULARITY = config.get(CONF_GRANULARITY, DEFAULT_GRANULATITY)
        if CONF_TESTING in config:
            test_config = config[CONF_TESTING]
            self._testing = test_config.get(CONF_ENABLED, False)
            self._test_speed = test_config.get(CONF_SPEED, DEFAULT_TEST_SPEED)
            for test in test_config[CONF_TIMES]:
                start = wash_dt(dt.as_utc(test[CONF_START]))
                end = wash_dt(dt.as_utc(test[CONF_END]))
                if CONF_NAME in test[CONF_NAME]:
                    name = test[CONF_NAME]
                else:
                    name = None
                self._test_times.append({"name": name, "start": start, "end": end})

        for ci, controller in enumerate(config[CONF_CONTROLLERS]):
            Ctrl = self.find_add(self, ci).load(controller)

            for zi, zone in enumerate(controller[CONF_ZONES]):
                Zn = Ctrl.find_add(self, Ctrl, zi).load(zone)

                for si, schedule in enumerate(zone[CONF_SCHEDULES]):
                    Zn.find_add(self, Ctrl, Zn, si).load(schedule)

        self._dirty = True
        self._muster_required = True
        return self
Пример #7
0
def infer_model_type(checkpoint_weights: OrderedDict,
                     attention_mechanism: bool) -> (str, bool):
    """
    Function to infer the model type using the weights matrix.
    We first try to use the "model_type" key added by our retrain process.
    If this fails, we infer it using our knowledge of the layers' names.
    For example, BPEmb model uses an embedding network, thus, if `embedding_network.model.weight_ih_l0` is present,
    we can say that it is such a type; otherwise, it is a FastText model.
    Finally, to handle the attention model, we use a similar approach but using the
    `decoder.linear_attention_mechanism_encoder_outputs.weight` layer name to deduct the presence of
    attention mechanism.

    Args:
        checkpoint_weights (OrderedDict): The weights matrix.
        attention_mechanism (bool): Either or not the model uses an attention mechanism or not.

    Return:
        A tuple where the first element is the model_type name and the second element is the attention_mechanism flag.

    """
    inferred_model_type = checkpoint_weights.get("model_type")
    if inferred_model_type is not None:
        model_type = inferred_model_type
    else:
        if "embedding_network.model.weight_ih_l0" in checkpoint_weights.keys():
            model_type = "bpemb"
        else:
            model_type = "fasttext"

    if "decoder.linear_attention_mechanism_encoder_outputs.weight" in checkpoint_weights.keys(
    ):
        attention_mechanism = True

    return model_type, attention_mechanism
Пример #8
0
def check_netapp_api_snapvault(
    item: str,
    params: Mapping[str, Any],
    section: SectionSingleInstance,
) -> CheckResult:
    snapvault = section.get(item)
    if not snapvault:
        return

    for key in ["source-system", "destination-system", "policy", "status", "state"]:
        if key in snapvault:
            yield Result(state=State.OK, summary="%s: %s" % (key.title(), snapvault[key]))

    if 'lag-time' not in snapvault:
        return

    lag_time = int(snapvault['lag-time'])

    policy_lag_time = OrderedDict(params.get('policy_lag_time', []))
    levels = policy_lag_time.get(snapvault.get('policy'))

    if not levels:
        levels = params.get('lag_time', (None, None))

    yield from check_levels(
        value=lag_time,
        levels_upper=levels,
        render_func=render.timespan,
        label='Lag time',
    )
Пример #9
0
    def advance(self, kwargs: OrderedDict) -> None:  # type: ignore[override]
        """Runs the train step together with optimization (if necessary) on the current batch split.

        Args:
            kwargs: the kwargs passed down to the hooks.
        """
        # replace the batch with the split batch
        self.split_idx, kwargs["batch"] = self._remaining_splits.pop(0)

        self.trainer._logger_connector.on_train_split_start(self.split_idx)

        outputs: Optional[Union[_OPTIMIZER_LOOP_OUTPUTS_TYPE,
                                _MANUAL_LOOP_OUTPUTS_TYPE]] = None  # for mypy
        # choose which loop will run the optimization
        if self.trainer.lightning_module.automatic_optimization:
            optimizers = _get_active_optimizers(
                self.trainer.optimizers, self.trainer.optimizer_frequencies,
                kwargs.get("batch_idx", 0))
            outputs = self.optimizer_loop.run(optimizers, kwargs)
        else:
            outputs = self.manual_loop.run(kwargs)
        if outputs:
            # automatic: can be empty if all optimizers skip their batches
            # manual: #9052 added support for raising `StopIteration` in the `training_step`. If that happens,
            # then `advance` doesn't finish and an empty dict is returned
            self._outputs.append(outputs)
Пример #10
0
 def validate(self, data: OrderedDict) -> OrderedDict:
     """Check that either policy, group or user is set."""
     count = sum([
         bool(data.get("policy", None)),
         bool(data.get("group", None)),
         bool(data.get("user", None)),
     ])
     invalid = count > 1
     empty = count < 1
     if invalid:
         raise ValidationError(
             "Only one of 'policy', 'group' or 'user' can be set.")
     if empty:
         raise ValidationError(
             "One of 'policy', 'group' or 'user' must be set.")
     return data
Пример #11
0
    def _run_optimization(self, kwargs: OrderedDict, optimizer: torch.optim.Optimizer) -> ClosureResult:
        """Runs closure (train step + backward) together with optimization if necessary.

        Args:
            kwargs: the kwargs passed down to the hooks.
            optimizer: the current optimizer
        """
        opt_idx = kwargs.get("optimizer_idx", 0)

        # toggle model params
        self._run_optimization_start(opt_idx, optimizer)

        closure = self._make_closure(kwargs, optimizer)

        if (
            # when the strategy handles accumulation, we want to always call the optimizer step
            not self.trainer.strategy.handles_gradient_accumulation
            and self.trainer.fit_loop._should_accumulate()
        ):
            # For gradient accumulation

            # -------------------
            # calculate loss (train step + train step end)
            # -------------------
            # automatic_optimization=True: perform ddp sync only when performing optimizer_step
            with _block_parallel_sync_behavior(self.trainer.strategy, block=True):
                closure()

        # ------------------------------
        # BACKWARD PASS
        # ------------------------------
        # gradient update with accumulated gradients
        else:
            # the `batch_idx` is optional with inter-batch parallelism
            self._optimizer_step(optimizer, opt_idx, kwargs.get("batch_idx", 0), closure)

        result = closure.consume_result()

        if result.loss is not None:
            # if no result, user decided to skip optimization
            # otherwise update running loss + reset accumulated loss
            # TODO: find proper way to handle updating running loss
            self.trainer.fit_loop.epoch_loop.batch_loop._update_running_loss(result.loss)

        # untoggle model params
        self._run_optimization_end(opt_idx)
        return result
Пример #12
0
    def load(self, config: OrderedDict):
        """Load schedule data from config"""
        self.clear()

        self._time = config[CONF_TIME]
        self._duration = wash_td(config[CONF_DURATION])
        self._name = config.get(CONF_NAME, f"Schedule {self.schedule_index + 1}")
        if CONF_WEEKDAY in config:
            self._weekdays = []
            for i in config[CONF_WEEKDAY]:
                self._weekdays.append(WEEKDAYS.index(i))
        else:
            self._weekdays = None

        if CONF_MONTH in config:
            self._months = []
            for i in config[CONF_MONTH]:
                self._months.append(MONTHS.index(i) + 1)
        else:
            self._months = None

        self._days = config.get(CONF_DAY, None)

        return self
class ScopedSymbolTable:
    def __init__(self, scope_name, scope_level, enclosing_scope=None):
        self._symbols = OrderedDict()
        self.scope_name = scope_name
        self.scope_level = scope_level
        self.enclosing_scope = enclosing_scope

    def _init_builtins(self):
        self.insert(BuiltinTypeSymbol('INTEGER'))
        self.insert(BuiltinTypeSymbol('REAL'))

    def __str__(self):
        h1 = 'SCOPE (SCOPED SYMBOL TABLE)'
        lines = ['\n', h1, '=' * len(h1)]
        for header_name, header_value in (('Scope name', self.scope_name),
                                          ('Scope level', self.scope_level),
                                          ('Enclosing scope',
                                           self.enclosing_scope.scope_name
                                           if self.enclosing_scope else None)):
            lines.append('%-15s: %s' % (header_name, header_value))
        h2 = 'Scope (Scoped symbol table) contents'
        lines.extend([h2, '-' * len(h2)])
        lines.extend(
            ('%7s: %r' % (key, value)) for key, value in self._symbols.items())
        lines.append('\n')
        s = '\n'.join(lines)
        return s

    __repr__ = __str__

    def insert(self, symbol):
        print('Insert: %s' % symbol.name)
        self._symbols[symbol.name] = symbol

    def lookup(self, name):
        print('Lookup: %s. (Scope name: %s)' % (name, self.scope_name))
        # 'symbol' is either an instance of the Symbol class or None
        symbol = self._symbols.get(name)

        if symbol is not None:
            return symbol

        # recursivamente va hacia arriba en la cadena buscando el nombre.
        if self.enclosing_scope is not None:
            return self.enclosing_scope.lookup(name)
Пример #14
0
 def update(self, instance: CourseInstance,
            validated_data: OrderedDict) -> CourseInstance:
     instance.url = validated_data.get('url', instance.url)
     instance.instance_name = validated_data.get('instance_name',
                                                 instance.instance_name)
     instance.language = validated_data.get('language', instance.language)
     instance.starting_time = validated_data.get('starting_time',
                                                 instance.starting_time)
     instance.ending_time = validated_data.get('ending_time',
                                               instance.ending_time)
     instance.visible_to_students = validated_data.get(
         'visible_to_students', instance.visible_to_students)
     instance.configure_url = validated_data.get('configure_url',
                                                 instance.configure_url)
     instance.save()
     self.set_teachers(instance, self.teachers)
     return instance
Пример #15
0
class LRUCache:
    def __init__(self, capacity):
        self.cache = OrderedDict()
        self.capacity = capacity

    def get(self, key):
        if key in self.cache:
            self.cache.move_to_end(key)

        return self.cache.get(key, -1)

    def put(self, key, value):
        if key in self.cache:
            self.cache.move_to_end(key)
        else:
            if len(self.cache) >= self.capacity:
                self.cache.popitem(last=False)

        self.cache[key] = value
Пример #16
0
    def contains_many(self, relations: OrderedDict, skip: List = []) -> bool:
        """Walk down all sub_relations by __ name and check if any are *Many"""
        walk = ''
        sub_relations = self.name.split('__')
        i = 0
        for sub_relation in sub_relations:
            # Walk down by concatenation
            walk += sub_relation

            # Skip these sub_relations if desired
            if len(skip) > i and skip[i] == sub_relation:
                walk += '__'
                i += 1
                continue

            # Check relation type for *Many
            relation_type = type(relations.get(walk))
            if relation_type == HasMany or relation_type == BelongsToMany or relation_type == MorphMany or relation_type == MorphToMany:
                return True

            # Walk down
            walk += '__'
            i += 1
        return False
Пример #17
0
    def from_ordered_dict(cls, chat_id: str, query_info: OrderedDict) -> ChatModel:
        formatted_members = list(query_info['members'].keys())
        formatted_messages = query_info.get('messages', None)

        return ChatModel(chat_id, query_info['chat_name'], formatted_members, formatted_messages)
Пример #18
0
 def from_ordered_dict(cls, query_info: OrderedDict) -> MessageModel:
     return MessageModel(query_info['uid'], query_info['message'],
                         query_info['author'], query_info['time'],
                         query_info['chat'],
                         query_info.get('is_editted', False))
Пример #19
0
def main():
    EAR_THRESH = 0.25
    EAR_CONSECUTIVE_FRAMES = 42

    COUNTER = 0
    # count = 0
    global ALARM_ON
    # ALARM_ON = False

    print("Preparing the detectors:")
    download_detector()
    print("Loading modules:")

    start = datetime.datetime.now()
    P = "shape_predictor_68_face_landmarks.dat"
    print("Loading facial landmark predictor...")

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(P)
    after_model_load = datetime.datetime.now()

    # For dlib's 68-point facial detector:
    FACIAL_LANDMARKS = OrderedDict([
        ("jaw", list(range(0, 17))),
        ("right_eyebrow", list(range(17, 22))),
        ("left_eyebrow", list(range(22, 27))),
        ("nose", list(range(27, 36))),
        ("right_eye", list(range(36, 42))),
        ("left_eye", list(range(42, 48))),
        ("mouth", list(range(48, 68))),
    ])

    path = "videos/"
    dir_name = []
    for file in os.listdir(path):
        if os.path.isdir(os.path.join(path, file)):
            dir_name.append(file)

    for person in dir_name:
        personpath = f"{path}/{person}/"
        state = []
        for status in os.listdir(personpath):
            if os.path.isdir(os.path.join(personpath, status)):
                state.append(status)

        for stat in state:
            filepath = f"{personpath}/{stat}"

            for filename in os.listdir(filepath):
                # using 0 for external camera input
                # cap = cv2.VideoCapture(0)
                if os.path.isfile(os.path.join(filepath, filename)):
                    cap = cv2.VideoCapture(os.path.join(filepath, filename))

                    if cap.isOpened():
                        CHECK, frame = cap.read()
                    else:
                        CHECK = False

                    time_stamp = True

                    ear_list = []
                    mar_list = []

                    ear_start_time = time.time()
                    while CHECK:
                        _, frame = cap.read()

                        if _:
                            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                            before_face = datetime.datetime.now()
                            faces = detector(gray)
                            after_face = datetime.datetime.now()

                            for (i, face) in enumerate(faces):
                                x1 = face.left()
                                x2 = face.right()
                                y1 = face.top()
                                y2 = face.bottom()

                                # draw the face bounding box
                                cv2.rectangle(frame, (x1, y1), (x2, y2),
                                              (0, 255, 0), 2)

                                # show the face number
                                cv2.putText(
                                    frame,
                                    "Face #{}".format(i + 1),
                                    (x1 - 10, y1 - 10),
                                    cv2.FONT_HERSHEY_SIMPLEX,
                                    0.5,
                                    (0, 255, 0),
                                    2,
                                )

                                before_landmarks = time.time()
                                landmarks = predictor(gray, face)
                                after_landmarks = time.time()

                                # calculating the facial landmamrks
                                landmark_keys = [
                                    "right_eye", "left_eye", "mouth"
                                ]
                                required_landmarks = []
                                for key in landmark_keys:
                                    required_landmarks.extend(
                                        FACIAL_LANDMARKS.get(key))

                                # drawing the facial landmarks in the video
                                for n in required_landmarks:
                                    x = landmarks.part(n).x
                                    y = landmarks.part(n).y
                                    cv2.circle(frame, (x, y), 3, (0, 0, 255),
                                               -1)

                                calculated_mar = mouth_aspect_ratio(
                                    FACIAL_LANDMARKS["mouth"], landmarks)

                                left_EAR = eye_aspect_ratio(
                                    FACIAL_LANDMARKS["left_eye"], landmarks)
                                right_EAR = eye_aspect_ratio(
                                    FACIAL_LANDMARKS["right_eye"], landmarks)

                                ear_both_eyes = (left_EAR + right_EAR) / 2

                                # count += 1

                                if (time.time() - ear_start_time) >= 1:
                                    ear_list.append(round(ear_both_eyes, 2))
                                    mar_list.append(round(calculated_mar, 2))
                                    # print("4 sec")
                                    ear_start_time = time.time()

                                    # ear_time = time.time()
                                    # count = 0

                                if ear_both_eyes < EAR_THRESH:
                                    COUNTER += 1

                                    if COUNTER >= EAR_CONSECUTIVE_FRAMES:
                                        '''
                                        if not ALARM_ON:
                                            ALARM_ON = True

                                            # creating new thread to play the alarm in background
                                            audio_thread = threading.Thread(
                                                target=raise_alarm
                                            )
                                            audio_thread.start()
										'''
                                        cv2.putText(
                                            frame,
                                            "Drowsiness Alert!",
                                            (10, 30),
                                            cv2.FONT_HERSHEY_SIMPLEX,
                                            0.5,
                                            (0, 255, 0),
                                            2,
                                        )
                                    # print("Drowsiness detected!")
                                else:
                                    COUNTER = 0
                                    ALARM_ON = False

                                cv2.putText(
                                    frame,
                                    "MAR: {:.2f}".format(calculated_mar),
                                    (300, 400),
                                    cv2.FONT_HERSHEY_SIMPLEX,
                                    0.5,
                                    (0, 0, 255),
                                    2,
                                )

                                cv2.putText(
                                    frame,
                                    "EAR: {:.2f}".format(ear_both_eyes),
                                    (300, 30),
                                    cv2.FONT_HERSHEY_SIMPLEX,
                                    0.5,
                                    (0, 0, 255),
                                    2,
                                )
                        else:
                            print("End of video")
                            break
                        cv2.namedWindow("Capturing")
                        cv2.imshow("Capturing", frame)

                        if time_stamp:
                            logger("---{} seconds---".format(
                                round((time.time() - start_time), 2)))
                            logger("Model load: " +
                                   str(after_model_load - start))
                            logger("Face detection: " +
                                   str(after_face - before_face))
                            if len(faces) > 0:
                                logger("Landmark detection: " +
                                       str(after_landmarks - before_landmarks))
                            time_stamp = False

                        key = cv2.waitKey(1)

                        # Use q to close the detection
                        if key == ord("q"):
                            print("Ending the capture")
                            break

                    # print(ear_list)
                    save_ear(ear_list, mar_list, stat, person)

                    cv2.destroyAllWindows()
                    cap.release()