def test_fromFiber_wo_ids(self): """Test construction of a tensor from a fiber without rank ids""" tensor_in = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_in.getRoot() tensor_ref = Tensor.fromFiber(["R1", "R0"], root) tensor = Tensor.fromFiber(fiber=root) self.assertEqual(tensor, tensor_ref)
def test_fromUncompressed_2D_wo_ids(self): """Test construction of a tensor from nested lists without ids""" tensor_in = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_in.getRoot() tensor_ref = Tensor.fromFiber(["R1", "R0"], root) # Manual copy of test_tensor-1.yaml # 0 1 2 3 # t = [ [0, 0, 0, 0], # 0 [100, 101, 102, 0], # 1 [0, 201, 0, 203], # 2 [0, 0, 0, 0], # 3 [400, 0, 402, 0], # 4 [0, 0, 0, 0], # 5 [0, 601, 0, 603] ] # 6 tensor = Tensor.fromUncompressed(["R1", "R0"], t) self.assertEqual(tensor, tensor_ref)
def test_shape_fromFiber_authoritative(self): """Test shape of a tensor from a fiber with authoritative shape""" y1 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") f1 = y1.getRoot() t1 = Tensor.fromFiber(["M", "K"], f1, [100, 200]) with self.subTest(test="All ranks"): self.assertEqual(t1.getRankIds(), ["M", "K"]) self.assertEqual(t1.getShape(), [100, 200]) with self.subTest(test="All ranks specified"): self.assertEqual(t1.getShape(["M", "K"]), [100, 200]) with self.subTest(test="Just rank 'M'"): self.assertEqual(t1.getShape(["M"]), [100]) with self.subTest(test="Just rank 'K'"): self.assertEqual(t1.getShape(["K"]), [200]) with self.subTest(test="Just rank 'M'"): self.assertEqual(t1.getShape("M"), 100) with self.subTest(test="Just rank 'K'"): self.assertEqual(t1.getShape("K"), 200) with self.subTest(test="Check authoritative"): self.assertEqual(t1.getShape(authoritative=True), [100, 200]) self.assertEqual(t1.getShape(["M", "K"], authoritative=True), [100, 200]) self.assertEqual(t1.getShape(["M"], authoritative=True), [100]) self.assertEqual(t1.getShape(["K"], authoritative=True), [200]) self.assertEqual(t1.getShape("M", authoritative=True), 100) self.assertEqual(t1.getShape("K", authoritative=True), 200)
def test_shape_fromFiber(self): """Test shape of a tensor from a fiber without authoritative shape""" y1 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") f1 = y1.getRoot() t1 = Tensor.fromFiber(["M", "K"], f1) with self.subTest(test="All ranks"): self.assertEqual(t1.getRankIds(), ["M", "K"]) self.assertEqual(t1.getShape(), [7, 4]) with self.subTest(test="All ranks specified"): self.assertEqual(t1.getShape(["M", "K"]), [7, 4]) with self.subTest(test="Just rank 'M' as list"): self.assertEqual(t1.getShape(["M"]), [7]) with self.subTest(test="Just rank 'K' as list"): self.assertEqual(t1.getShape(["K"]), [4]) with self.subTest(test="Just rank 'M'"): self.assertEqual(t1.getShape("M"), 7) with self.subTest(test="Just rank 'K'"): self.assertEqual(t1.getShape("K"), 4) with self.subTest(test="Check authoritative"): self.assertIsNone(t1.getShape(authoritative=True)) self.assertIsNone(t1.getShape(["M", "K"], authoritative=True)) self.assertIsNone(t1.getShape(["M"], authoritative=True)) self.assertIsNone(t1.getShape(["K"], authoritative=True)) self.assertIsNone(t1.getShape("M", authoritative=True)) self.assertIsNone(t1.getShape("K", authoritative=True))
def test_fromFiber(self): """Test construction of a tensor from a fiber""" tensor_ref = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_ref.getRoot() tensor = Tensor.fromFiber(["M", "K"], root) self.assertEqual(tensor, tensor_ref)
def test_flattenRanks_f02(self): """ Test flattenRanks - f02 """ t0 = Tensor.fromYAMLfile("./data/tensor_3d-0.yaml") t1 = Tensor.fromYAMLfile("./data/tensor_3d-1.yaml") t2 = Tensor.fromFiber(["A", "B", "C", "D"], Fiber([1, 4], [t0.getRoot(), t1.getRoot()]), name="t2") f13 = t2.flattenRanks(depth=1, levels=2) u13 = f13.unflattenRanks(depth=1, levels=2) self.assertEqual(u13, t2) f04 = t2.flattenRanks(depth=0, levels=3) u04 = f04.unflattenRanks(depth=0, levels=3) self.assertEqual(u04, t2)
def setUp(self): self.input = {} self.input['f'] = Fiber([0, 1, 2], [ Fiber([0, 1, 2], [ Fiber([0, 1, 2, 3], [2, 4, 2, 4]), Fiber([0, 1, 2, 3], [3, 2, 4, 3]), Fiber([0, 1, 3], [3, 2, 1]) ]), Fiber([0, 1, 2], [ Fiber([0, 1, 2, 3], [3, 2, 5, 4]), Fiber([0, 1, 3], [5, 2, 1]), Fiber([0, 1, 2], [1, 1, 5]) ]), Fiber([0, 1, 2], [ Fiber([1, 2], [4, 2]), Fiber([0, 1, 2, 3], [2, 2, 2, 3]), Fiber([0, 1, 2, 3], [2, 4, 3, 1]) ]) ]) self.input['t'] = Tensor.fromFiber(["C", "H", "W"], self.input['f'])
from fibertree import Fiber, Tensor, TensorImage x = [[[1, 2, 8, 20, 0, 0, 11], [1, 0, 0, 11, 0, 0, 33], [0, 0, 0, 0, 0, 0, 0], [1, 1, 8, 12, 0, 0, 44], [1, 3, 0, 13, 0, 0, 42], [0, 0, 4, 14, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[1, 2, 8, 20, 0, 0, 11], [1, 0, 0, 11, 0, 0, 33], [0, 0, 0, 0, 0, 0, 0], [1, 1, 8, 12, 0, 0, 44], [1, 3, 0, 13, 0, 0, 42], [0, 0, 4, 14, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]] f = Fiber.fromUncompressed(x) f.dump("/tmp/tensor-3d.yaml") f.print("Fiber from uncompressed") t1 = Tensor.fromFiber(["X", "Y", "Z"], f) t1.print("Tensor from fiber") t2 = Tensor.fromUncompressed(["X", "Y", "Z"], x) t2.print("Tensor from uncompressed")
def __init__(self, *tensors, animation='movie', style='tree', enable_wait=False): """__init__ """ # # Set up logging # self.logger = logging.getLogger('fibertree.graphics.tensor_canvas') # # Places to collect information about the frames # num_tensors = len(tensors) self.num_tensors = num_tensors self.orig_tensors = [] self.shadow_tensors = [] self.using_spacetimestamp = None self.update_times = [] if enable_wait else None self.waitname_map = {} for t in tensors: # # Build list of orignal tensors, # but convert Fibers into a Tensor with the Fiber as its root # if isinstance(t, Fiber): # TBD: We do not really know if the fiber is mutable... t = Tensor.fromFiber(fiber=t) self.orig_tensors.append(t) # # Create a tensor to hold a shadow tensor that tracks # updates to the tracked tensors at the right time # if t.isMutable(): self.shadow_tensors.append(copy.deepcopy(t)) else: self.shadow_tensors.append(t) # # Create a tensor to hold last update time # of each element of a tensor # if enable_wait: self.update_times.append(Tensor(rank_ids=t.getRankIds())) # # Create a list to hold a record of activity at each timestamp # self.log = [] # # Flag to help addFrame() know if it is adding activity # to an existing frame # self.inframe = False # # Global cycle tracker # self.cycle = 0 # # Reset image highlighting # ImageUtils.resetColors() # # Create desired canvas # # Note: We create the canvas with the shadow tensors, so that # the visualized activity happens in the desired order # if animation == 'movie': self.canvas = MovieCanvas(*self.shadow_tensors, style=style) elif animation == 'spacetime': self.canvas = SpacetimeCanvas(*self.shadow_tensors) elif animation == 'none': self.canvas = NoneCanvas(*self.shadow_tensors, style=style) else: self.logger.warning("TensorCanvas: No animation type: %s", animation)
# print("a - single highlights") i = UncompressedImage(a, {"PE": [(1, 2)]}) i.show() # print("b") b = Tensor.fromUncompressed(["X"], [1, 2, 0, 0, 4]) i = UncompressedImage(b, {"PE": [(1, ), (4, )]}) i.show() # print("c") a_root = a.getRoot() c = Tensor.fromFiber(["X", "Y", "Z"], Fiber([0, 1, 2], [a_root, Fiber([], []), a_root])) i = UncompressedImage(c) i.show() # print("d") d = c.getRoot() print("Original") i = UncompressedImage(d) i.show() # # print("e") # d_flattened = d.flattenRanks() # print("Flattened")