def test_overwrite_gaf(self): temp = tempfile.TemporaryDirectory() # Create a GAF gaf = GAF() gaf.add_argument(1, strength=1, payload=Payload('1', PayloadType.STRING)) gaf.add_argument(2, strength=2, payload=Payload('2', PayloadType.STRING)) gaf.add_conclusion(3, 0.5, 1) gaf.add_relation(1, 2, BipolarFramework.ATTACK) gaf.add_relation(2, 3, BipolarFramework.SUPPORT) # Initialise a writer writer = Writer(temp.name, 'muddle') self.assertTrue( os.path.exists(os.path.join(temp.name, 'muddle', 'model'))) # Write the gaf writer.write_gaf(gaf) # Write the gaf under a different name writer.write_gaf(gaf, 'peter') # Write the gaf under the same name with self.assertRaises(UserWarning): writer.write_gaf(gaf, 'peter') # Wipe the temp directory temp.cleanup()
def test_invalid_types(self): with self.assertRaises(AssertionError): p = Payload('hello', PayloadType.IMAGE) with self.assertRaises(AssertionError): p = Payload('hello', PayloadType.IMAGE_PAIR) with self.assertRaises(AssertionError): p = Payload(123, PayloadType.STRING)
def test_serialise(self): gaf = GAF() gaf.add_argument(1, strength=1, payload=Payload('1', PayloadType.STRING)) gaf.add_argument(2, strength=2, payload=Payload('2', PayloadType.STRING)) gaf.add_conclusion(3, 0.5, 1) gaf.add_relation(1, 2, BipolarFramework.ATTACK) gaf.add_relation(2, 3, BipolarFramework.SUPPORT) decoder = json.decoder.JSONDecoder() with open(os.path.join(_resource_dir, 'basic_gaf.json'), 'r') as f: self.assertEqual(decoder.decode(gaf.serialise()), decoder.decode(f.read())) f.close()
def test_add_argument(self): gaf = GAF() gaf.add_argument(1, strength=2, payload=Payload('test', PayloadType.STRING)) gaf.add_argument(2) self.assertEqual( gaf.arguments(), { 1: { 'strength': 2, 'payload': Payload('test', PayloadType.STRING), 'type': NodeType.ARGUMENT }, 2: { 'strength': None, 'payload': None, 'type': NodeType.ARGUMENT } })
def test_create_image_pair_payload(self): temp = tempfile.TemporaryDirectory() gaf = GAF() img = Image.open(os.path.join(_resource_dir, 'test.png')).convert('RGB') _ = gaf._create_payload(Payload((img, img), PayloadType.IMAGE_PAIR), temp.name, '') generated_payloads = glob.glob(os.path.join(temp.name, '*.jpg')) self.assertEqual(len(generated_payloads), 2) temp.cleanup()
def test_create_image_payload(self): temp = tempfile.TemporaryDirectory() gaf = GAF() img = Image.open(os.path.join(_resource_dir, 'test.png')).convert('RGB') save_path = gaf._create_payload(Payload(img, PayloadType.IMAGE), temp.name, '') generated_payloads = glob.glob(os.path.join(temp.name, '*.jpg')) self.assertEqual(len(generated_payloads), 1) self.assertEqual(os.path.basename(save_path), os.path.basename(generated_payloads[0])) temp.cleanup()
def test_serialise_2(self): gaf = GAF() gaf.add_input('picture of carrot', payload=Payload('picture', PayloadType.STRING)) gaf.add_argument('orange', strength=1, payload=Payload('orange body', PayloadType.STRING)) gaf.add_argument('green', strength=2, payload=Payload('green leaves', PayloadType.STRING)) gaf.add_conclusion('carrot', 0.5, 1) gaf.add_relation('picture of carrot', 'orange', BipolarFramework.ATTACK) gaf.add_relation('picture of carrot', 'green', BipolarFramework.SUPPORT) gaf.add_relation('green', 'carrot', BipolarFramework.ATTACK) gaf.add_relation('orange', 'carrot', BipolarFramework.SUPPORT) decoder = json.decoder.JSONDecoder() with open(os.path.join(_resource_dir, 'carrot_gaf.json'), 'r') as f: self.assertEqual(decoder.decode(gaf.serialise()), decoder.decode(f.read())) f.close()
from argflow.gaf import GAF, Payload, PayloadType from argflow.gaf.frameworks import BipolarFramework from argflow.gaf.mappers import InfluenceMapper, CharacterisationMapper, StrengthMapper from argflow.portal import Writer from argflow.chi import Chi FIRST = range(1, 100) SECOND = range(101, 200) THIRD = range(201, 300) if __name__ == '__main__': gaf = GAF() # Add start and finish gaf.add_input(0, payload=Payload('Input', PayloadType.STRING)) gaf.add_conclusion(-1, 0.8, 'True', payload=Payload('Conclusion', PayloadType.STRING)) # First layer for i in FIRST: gaf.add_argument(i, strength=random.random(), payload=Payload(str(i), PayloadType.STRING)) relation = BipolarFramework.SUPPORT if random.random( ) < 0.8 else BipolarFramework.ATTACK # Connect to input gaf.add_relation(0, i, relation) # Second layer for i in SECOND:
def generate(self, x, node, model): return Payload(f'Feature {node["idx"]}', PayloadType.STRING)
def generate(self, x, node, model): return Payload('Feature', PayloadType.STRING)
def test_different_class(self): self.assertNotEqual(Payload('oioi everyone', PayloadType.STRING), 'asdf')
def test_not_equal(self): self.assertNotEqual(Payload('oioi everyone', PayloadType.STRING), Payload('hello world', PayloadType.STRING))
def test_equal(self): self.assertEqual(Payload('hello world', PayloadType.STRING), Payload('hello world', PayloadType.STRING))
def generate(self, x, node, model): return Payload(node["value"] if "value" in node else "node", PayloadType.STRING)
import random from argflow.gaf import GAF, Payload, PayloadType from argflow.gaf.frameworks import BipolarFramework from argflow.gaf.mappers import InfluenceMapper, CharacterisationMapper, StrengthMapper from argflow.portal import Writer from argflow.chi import Chi FIRST = range(1, 4000) if __name__ == '__main__': gaf = GAF() # Add start and finish gaf.add_input(0, payload=Payload('Input', PayloadType.STRING)) gaf.add_conclusion(-1, 0.8, 'True', payload=Payload('Conclusion', PayloadType.STRING)) # First layer for i in FIRST: gaf.add_argument(i, strength=random.random(), payload=Payload( str(i), PayloadType.STRING)) relation = BipolarFramework.SUPPORT if random.random() < 0.8 else BipolarFramework.ATTACK # Connect to input gaf.add_relation(0, i, relation) gaf.add_relation(i, -1, relation) # Write explanation summaries = Writer('../portal/examples', 'Stress Tests') summaries.write_gaf(gaf)
def generate(self, x, node, model): gc = GradCAM() am = ActMax() img_gc = gc.generate(x, node, model).content img_am = am.generate(x, node, model).content return Payload((img_gc, img_am), PayloadType.IMAGE_PAIR)