Exemple #1
0
 def testWithAnonymousArrayIndices(self):
     a = proto_diff.ProtoPath(("observation", "actions"))
     b = proto_diff.ProtoPath(("observation", "actions", 1))
     c = proto_diff.ProtoPath(("observation", "actions", 2))
     self.assertEqual(str(a), "observation.actions")
     self.assertEqual(str(b.with_anonymous_array_indices()),
                      "observation.actions[*]")
     self.assertEqual(b.with_anonymous_array_indices(),
                      c.with_anonymous_array_indices())
Exemple #2
0
    def testGetField(self):
        proto = sc_pb.ResponseObservation(observation=sc_pb.Observation(
            game_loop=1, alerts=[sc_pb.AlertError]))

        game_loop = proto_diff.ProtoPath(("observation", "game_loop"))
        alert = proto_diff.ProtoPath(("observation", "alerts", 0))
        self.assertEqual(game_loop.get_field(proto), 1)
        self.assertEqual(alert.get_field(proto), sc_pb.AlertError)
        self.assertEqual(
            proto_diff.ProtoPath(game_loop.path[:-1]).get_field(proto),
            sc_pb.Observation(game_loop=1, alerts=[sc_pb.AlertError]))
    def testFilteredIn(self):
        a = sc_pb.ResponseObservation(observation=sc_pb.Observation(
            feature_layer_data=spatial_pb2.ObservationFeatureLayer(
                renders=spatial_pb2.FeatureLayers(
                    height_map=common_pb2.ImageData(
                        bits_per_pixel=32,
                        size=common_pb2.Size2DI(x=4, y=4),
                        data=np.array([[0, 0, 0, 0], [1, 0, 1, 0],
                                       [0, 0, 0, 1], [1, 1, 1, 1]],
                                      dtype=np.int32).tobytes())))))
        b = sc_pb.ResponseObservation(observation=sc_pb.Observation(
            feature_layer_data=spatial_pb2.ObservationFeatureLayer(
                renders=spatial_pb2.FeatureLayers(
                    height_map=common_pb2.ImageData(
                        bits_per_pixel=32,
                        size=common_pb2.Size2DI(x=4, y=4),
                        data=np.array([[0, 0, 0, 0], [0, 1, 1, 0],
                                       [0, 0, 0, 1], [1, 1, 1, 0]],
                                      dtype=np.int32).tobytes())))))

        result = image_differencer.image_differencer(path=proto_diff.ProtoPath(
            ("observation", "feature_layer_data", "renders", "height_map",
             "data")),
                                                     proto_a=a,
                                                     proto_b=b)

        self.assertEqual(
            result,
            "3 element(s) changed - [1][0]: 1 -> 0; [1][1]: 0 -> 1; [3][3]: 1 -> 0"
        )
Exemple #4
0
def image_differencer(path, proto_a, proto_b):
  """proto_diff differencer for PySC2 image data."""
  if path[-1] == "data" and len(path) >= 2:
    image_data_path = proto_diff.ProtoPath(path[:-1])
    image_data_a = image_data_path.get_field(proto_a)
    if isinstance(image_data_a, common_pb2.ImageData):
      image_data_b = image_data_path.get_field(proto_b)
      image_a = features.Feature.unpack_layer(image_data_a)
      image_b = features.Feature.unpack_layer(image_data_b)
      return np_util.summarize_array_diffs(image_a, image_b)

  return None
Exemple #5
0
    def testOrdering(self):
        self.assertLess(
            proto_diff.ProtoPath(("observation", "actions", 1, "game_loop")),
            proto_diff.ProtoPath(("observation", "actions", 1, "target")))

        self.assertLess(
            proto_diff.ProtoPath(("observation", "actions", 1)),
            proto_diff.ProtoPath(("observation", "actions", 1, "target")))

        self.assertGreater(proto_diff.ProtoPath(("observation", "actions", 1)),
                           proto_diff.ProtoPath(("observation", )))
 def testFilteredOut(self):
     result = image_differencer.image_differencer(path=proto_diff.ProtoPath(
         ("observation", "actions", 1)),
                                                  proto_a=None,
                                                  proto_b=None)
     self.assertIsNone(result)
Exemple #7
0
 def testIndexing(self):
     path = proto_diff.ProtoPath(("observation", "actions", 1))
     self.assertEqual(path[0], "observation")
     self.assertEqual(path[1], "actions")
     self.assertEqual(path[-2], "actions")
     self.assertEqual(path[-1], 1)
Exemple #8
0
 def testNotEqual(self):
     a = proto_diff.ProtoPath(("observation", "actions", 1))
     b = proto_diff.ProtoPath(("observation", "actions", 2))
     self.assertNotEqual(a, b)
     self.assertNotEqual(hash(a), hash(b))
Exemple #9
0
 def testStringRepr(self):
     self.assertEqual(
         str(proto_diff.ProtoPath(("observation", "actions", 1, "target"))),
         "observation.actions[1].target")
Exemple #10
0
 def testCreationFromGenerator(self):
     self.assertEqual(str(proto_diff.ProtoPath(a for a in "abc")), "a.b.c")
Exemple #11
0
 def testCreationFromList(self):
     self.assertEqual(str(proto_diff.ProtoPath(["observation", "actions"])),
                      "observation.actions")
Exemple #12
0
 def testCreationFromTuple(self):
     self.assertEqual(str(proto_diff.ProtoPath(("observation", "actions"))),
                      "observation.actions")