def PackedListC(codec : Codec) -> Codec: enc, dec = listC(bytesC) def encode(f : FileOut, xs : PackedList) -> None: enc(f, xs.blocks) def decode(f : FileIn) -> PackedList: xs : PackedList = PackedList(codec) xs.blocks = dec(f) return xs return Codec(encode, decode)
def test_complicated(xs): tupleC(listC(strC), intC, listC(tupleC(intC, strC))).test(xs)
def test_list_str(xs): listC(strC).test(xs)
def test_list_int(xs): listC(intC).test(xs)
tupleC, namedtupleC, setC, frozensetC from util.codec_progress import CodecProgress, listCP, oneCP log = logging.getLogger(__name__) class Row(NamedTuple): tuple_size : int garp_menu_tuples : Set[FrozenSet[FrozenSet[int]]] # set of tuples of menus (= alt sets) RowC = namedtupleC(Row, intC, setC(frozensetC(frozensetC(intC)))) class Subject(NamedTuple): name : str rows : List[Row] SubjectC = namedtupleC(Subject, strC, listC(RowC)) class MenuRowNode(util.tree_model.Node): def __init__(self, parent_node, row: int, alternatives : List[str], xs : FrozenSet[FrozenSet[int]]) -> None: util.tree_model.Node.__init__( self, parent_node, row, fields=( '', '', '{' + ','.join( '{' + ','.join(alternatives[i] for i in sorted(menu)) + '}' for menu in sorted(xs) ) + '}', ), )
def csv_alt(self, index: Optional[int]) -> Optional[str]: if index is None: return None else: return self.alternatives[index] def pack(self) -> PackedSubject: return PackedSubject(SubjectC.encode_to_memory(self)) @staticmethod def unpack(packed: PackedSubject) -> 'Subject': return SubjectC.decode_from_memory(packed) SubjectC = namedtupleC(Subject, strC, listC(strC), listC(ChoiceRowC)) DatasetHeaderC = tupleC(strC, listC(strC)) class Dataset: def __init__(self, name: str, alternatives: Sequence[str]) -> None: self.name = name self.alternatives = list(alternatives) def __str__(self): return self.label_name() def label_type(self): return self.__class__.__name__
garp : int sarp : int ViolationsC = namedtupleC(Violations, intC, intC) class Subject(NamedTuple): subject_name : str violations : List[Tuple[int, Violations]] # cycle length, counts warp_strict : int warp_nonstrict : int hm_garp : BoundEstimate hm_sarp : BoundEstimate hm_warp_strict : BoundEstimate hm_warp_nonstrict : BoundEstimate SubjectC = namedtupleC(Subject, strC, listC(tupleC(intC, ViolationsC)), intC, intC, BoundEstimateC, BoundEstimateC, BoundEstimateC, BoundEstimateC, ) class ViolationsNode(util.tree_model.Node): def __init__(self, parent_node, row, len_viol : Tuple[int, Violations]) -> None: cycle_length, violations = len_viol util.tree_model.Node.__init__( self, parent_node, row, fields=( '', cycle_length, violations.garp, violations.sarp, '-',
sarp: int garp_binary_menus: int sarp_binary_menus: int RowC = namedtupleC(Row, intC, intC, intC, intC, intC) class SubjectRaw(NamedTuple): name: str rows: List[Row] warp_pairs: int warp_all: int SubjectRawC = namedtupleC(SubjectRaw, strC, listC(RowC), intC, intC) class Subject(NamedTuple): raw: SubjectRaw total_garp: int total_sarp: int total_garp_binary_menus: int total_sarp_binary_menus: int @staticmethod def from_raw(raw: SubjectRaw) -> 'Subject': return Subject( raw, total_garp=sum(r.garp for r in raw.rows),
Issue = Union[RepeatedMenu, ChoiceNotInMenu, ] IssueC = enumC('Issue', { RepeatedMenu: (setC(intC), ), ChoiceNotInMenu: (setC(intC), intC), }) class Subject(NamedTuple): name: str issues: List[Issue] SubjectC = namedtupleC(Subject, strC, listC(IssueC)) class IssueNode(util.tree_model.Node): def __init__(self, parent_node, row: int, alternatives: List[str], issue: Issue) -> None: if isinstance(issue, RepeatedMenu): fields = ( 'repeated menu', ','.join(alternatives[i] for i in sorted(issue.menu)), '', ) elif isinstance(issue, ChoiceNotInMenu): fields = ( 'choice not in menu', ','.join(alternatives[i] for i in sorted(issue.menu)),
if self.lower_bound != self.upper_bound: return f'{self.lower_bound-1} < N ≤ {self.upper_bound}' else: return self.upper_bound PenaltyC = namedtupleC(Penalty, intC, intC) class Request(NamedTuple): # type: ignore subjects: List[dataset.PackedSubject] models: Sequence[model.Model] disable_parallelism: bool RequestC = namedtupleC(Request, listC(dataset.PackedSubjectC), listC(ModelC), boolC) InstanceRepr = NewType('InstanceRepr', bytes) InstanceReprC = bytesC class InstanceInfo(NamedTuple): # type: ignore model: ModelRepr penalty: Penalty instance: InstanceRepr InstanceInfoC = namedtupleC(InstanceInfo, ModelC, PenaltyC, InstanceReprC)
class Subject(NamedTuple): subject_name: str violations: List[Tuple[int, Violations]] # cycle length, counts warp_strict: int warp_nonstrict: int hm_garp: BoundEstimate hm_sarp: BoundEstimate hm_warp_strict: BoundEstimate hm_warp_nonstrict: BoundEstimate SubjectC = namedtupleC( Subject, strC, listC(tupleC(intC, ViolationsC)), intC, intC, BoundEstimateC, BoundEstimateC, BoundEstimateC, BoundEstimateC, ) class ViolationsNode(util.tree_model.Node): def __init__(self, parent_node, row, len_viol: Tuple[int, Violations]) -> None: cycle_length, violations = len_viol util.tree_model.Node.__init__( self,
GenChoicesC = enumC('GenChoices', { Instance: (bytesC, ), Uniform: (boolC, boolC), }) class Request(NamedTuple): name: str alternatives: List[str] gen_menus: GenMenus gen_choices: GenChoices preserve_deferrals: bool RequestC = namedtupleC(Request, strC, listC(strC), GenMenusC, GenChoicesC, boolC) class Response(NamedTuple): subject_packed: PackedSubject observation_count: int ResponseC = namedtupleC(Response, PackedSubjectC, intC) def run(core: Core, request: Request) -> Response: return cast( Response, core.call('simulation', RequestC, ResponseC, request),