GitLab Repo

amachine.am_causal_state

 1from dataclasses import dataclass, field, replace, asdict
 2from typing import Self, Any
 3from collections import defaultdict
 4
 5@dataclass(frozen=True)
 6class CausalState :
 7
 8    name             : str
 9    classes          : defaultdict[int, set[str]] = field(default_factory=lambda: defaultdict(set))
10    isomorphs        : set[str] = field(default_factory=set)
11    pseudo_isomorphs : set[str] = field(default_factory=set)
12
13    def __post_init__(self):
14        self.classes[0] = set()
15
16    def add_class( self, composition_depth, class_name ) :
17        self.classes[ composition_depth ].add( class_name )
18
19    def add_isomorph( self, isomorph ) :
20        self.isomorphs.add( isomorph )
21
22    def deepcopy(self) -> Self:
23        return replace(
24            self,
25            classes=self.classes.copy(),
26            isomorphs=self.isomorphs.copy(),
27            pseudo_isomorphs=self.pseudo_isomorphs.copy()
28        )
29
30    def modified_deep_copy(self, **changes) -> Self:
31        cpy = self.deepcopy()
32        return replace ( cpy, **changes )
33
34    @classmethod
35    def from_json_dict(cls, data: dict) -> Self:        
36        return cls(
37            name=data["name"],
38            classes={ int( k ) : v for k, v in data.get( "classes", {} ).items() },
39            isomorphs=set( data.get( "isomorphs", [] ) ),
40            pseudo_isomorphs=set( data.get( "pseudo_isomorphs", [] ) )
41        )
42
43    def to_json_dict( self ) -> dict[str, Any]:
44        d = asdict( self )
45        d[ "isomorphs"        ] = sorted( list( self.isomorphs        ) )
46        d[ "pseudo_isomorphs" ] = sorted( list( self.pseudo_isomorphs ) )
47        return d
@dataclass(frozen=True)
class CausalState:
 6@dataclass(frozen=True)
 7class CausalState :
 8
 9    name             : str
10    classes          : defaultdict[int, set[str]] = field(default_factory=lambda: defaultdict(set))
11    isomorphs        : set[str] = field(default_factory=set)
12    pseudo_isomorphs : set[str] = field(default_factory=set)
13
14    def __post_init__(self):
15        self.classes[0] = set()
16
17    def add_class( self, composition_depth, class_name ) :
18        self.classes[ composition_depth ].add( class_name )
19
20    def add_isomorph( self, isomorph ) :
21        self.isomorphs.add( isomorph )
22
23    def deepcopy(self) -> Self:
24        return replace(
25            self,
26            classes=self.classes.copy(),
27            isomorphs=self.isomorphs.copy(),
28            pseudo_isomorphs=self.pseudo_isomorphs.copy()
29        )
30
31    def modified_deep_copy(self, **changes) -> Self:
32        cpy = self.deepcopy()
33        return replace ( cpy, **changes )
34
35    @classmethod
36    def from_json_dict(cls, data: dict) -> Self:        
37        return cls(
38            name=data["name"],
39            classes={ int( k ) : v for k, v in data.get( "classes", {} ).items() },
40            isomorphs=set( data.get( "isomorphs", [] ) ),
41            pseudo_isomorphs=set( data.get( "pseudo_isomorphs", [] ) )
42        )
43
44    def to_json_dict( self ) -> dict[str, Any]:
45        d = asdict( self )
46        d[ "isomorphs"        ] = sorted( list( self.isomorphs        ) )
47        d[ "pseudo_isomorphs" ] = sorted( list( self.pseudo_isomorphs ) )
48        return d
CausalState( name: str, classes: collections.defaultdict[int, set[str]] = <factory>, isomorphs: set[str] = <factory>, pseudo_isomorphs: set[str] = <factory>)
name: str
classes: collections.defaultdict[int, set[str]]
isomorphs: set[str]
pseudo_isomorphs: set[str]
def add_class(self, composition_depth, class_name):
17    def add_class( self, composition_depth, class_name ) :
18        self.classes[ composition_depth ].add( class_name )
def add_isomorph(self, isomorph):
20    def add_isomorph( self, isomorph ) :
21        self.isomorphs.add( isomorph )
def deepcopy(self) -> Self:
23    def deepcopy(self) -> Self:
24        return replace(
25            self,
26            classes=self.classes.copy(),
27            isomorphs=self.isomorphs.copy(),
28            pseudo_isomorphs=self.pseudo_isomorphs.copy()
29        )
def modified_deep_copy(self, **changes) -> Self:
31    def modified_deep_copy(self, **changes) -> Self:
32        cpy = self.deepcopy()
33        return replace ( cpy, **changes )
@classmethod
def from_json_dict(cls, data: dict) -> Self:
35    @classmethod
36    def from_json_dict(cls, data: dict) -> Self:        
37        return cls(
38            name=data["name"],
39            classes={ int( k ) : v for k, v in data.get( "classes", {} ).items() },
40            isomorphs=set( data.get( "isomorphs", [] ) ),
41            pseudo_isomorphs=set( data.get( "pseudo_isomorphs", [] ) )
42        )
def to_json_dict(self) -> dict[str, typing.Any]:
44    def to_json_dict( self ) -> dict[str, Any]:
45        d = asdict( self )
46        d[ "isomorphs"        ] = sorted( list( self.isomorphs        ) )
47        d[ "pseudo_isomorphs" ] = sorted( list( self.pseudo_isomorphs ) )
48        return d