Skip to content

TileSet & Tiles

TileSet

rgrow.TileSet

Source code in rgrow-python/rgrow/__init__.py
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@attrs.define(auto_attribs=True)
class TileSet:
    tiles: List[Tile] = attrs.field(factory=list)
    bonds: List[Bond] = attrs.field(
        factory=list, converter=_conv_bond_list, on_setattr=attrs.setters.convert
    )
    glues: List[tuple[int | str, int | str, float]] = attrs.field(factory=list)
    cover_strands: Optional[List[CoverStrand]] = None
    gse: Optional[float] = None
    gmc: Optional[float] = None
    alpha: Optional[float] = None
    threshold: Optional[float] = None
    seed: Optional[tuple[int, int, int | str] | list[tuple[int, int, int | str]]] = None
    size: Optional[int | tuple[int, int]] = None
    tau: Optional[float] = None
    smax: Optional[int] = None
    update_rate: Optional[int] = None
    kf: Optional[float] = None
    fission: Optional[str] = None
    block: Optional[int] = None
    chunk_handling: Optional[str] = None
    chunk_size: Optional[str] = None
    canvas_type: Optional[str] = None
    tracking: Optional[str] = None
    hdoubletiles: Optional[List[Tuple[str | int, str | int]]] = None
    vdoubletiles: Optional[List[Tuple[str | int, str | int]]] = None
    model: Optional[str] = None

    def _to_rg_tileset(self) -> _TileSet:
        kwargs = {
            k: getattr(self, k)
            for k in [
                "gse",
                "gmc",
                "alpha",
                "threshold",
                "seed",
                "size",
                "tau",
                "smax",
                "update_rate",
                "kf",
                "fission",
                "block",
                "chunk_handling",
                "chunk_size",
                "canvas_type",
                "tracking",
                "cover_strands",
                "hdoubletiles",
                "vdoubletiles",
                "model",
            ]
            if getattr(self, k) is not None
        }

        return _TileSet(tiles=self.tiles, bonds=self.bonds, glues=self.glues, **kwargs)

    def create_system(self) -> "System":
        return self._to_rg_tileset().create_system()

    def create_state_empty(self) -> State:
        return self._to_rg_tileset().create_state_empty()

    def create_system_and_state(self) -> tuple[System, State]:
        return self._to_rg_tileset().create_system_and_state()

    def create_state(self, system: System | None = None) -> State:
        if system is None:
            system = self.create_system()
        return self._to_rg_tileset().create_state(system=system)

    def run_window(self) -> State:
        return self._to_rg_tileset().run_window()

    @classmethod
    def from_dict(cls, d: dict[str, Any]) -> "TileSet":
        if "tiles" in d:
            d["tiles"] = [Tile(**x) for x in d["tiles"]]
        if "bonds" in d:
            d["bonds"] = [Bond(**x) for x in d["bonds"]]
        if "options" in d:
            d.update(d.pop("options"))
        if "Gse" in d:
            d["gse"] = d.pop("Gse")
        if "Gmc" in d:
            d["gmc"] = d.pop("Gmc")
        # remove unknown keys, and warn
        for k in list(d.keys()):
            if k not in cls.__dict__:
                print(f"Warning: unknown key {k!r} in tileset")
                del d[k]
        return cls(**d)

    def run_rbffs(
        self,
        n_trials: int = 1000,
        n_trajectories: int = 1000,
        target_size: int = 100,
        canvas_size: tuple[int, int] = (32, 32),
        subseq_bound: EvolveBounds = EvolveBounds(for_time=1e7),
        size_step: int = 1,
        parallel: bool = False,
        num_workers: int | None = None,
        **kwargs: Any,
    ) -> RBFFSResult:
        return self._to_rg_tileset().run_rbffs(
            n_trials=n_trials,
            n_trajectories=n_trajectories,
            target_size=target_size,
            canvas_size=canvas_size,
            subseq_bound=subseq_bound,
            size_step=size_step,
            parallel=parallel,
            num_workers=num_workers,
            **kwargs,
        )

    def run_ffs(
        self,
        constant_variance: bool = True,
        var_per_mean2: float = 0.01,
        min_configs: int = 100,
        max_configs: int = 100000,
        early_cutoff: bool = True,
        cutoff_probability: float = 0.99,
        cutoff_number: int = 4,
        min_cutoff_size: int = 30,
        init_bound: EvolveBounds = EvolveBounds(for_time=1e7),
        subseq_bound: EvolveBounds = EvolveBounds(for_time=1e7),
        start_size: int = 3,
        size_step: int = 1,
        keep_configs: bool = False,
        min_nuc_rate: float | None = None,
        canvas_size: tuple[int, int] = (64, 64),
        target_size: int = 100,
        config: FFSRunConfig | None = None,  # FIXME
        **kwargs: Any,
    ) -> FFSRunResult:
        return self._to_rg_tileset().run_ffs(
            constant_variance=constant_variance,
            var_per_mean2=var_per_mean2,
            min_configs=min_configs,
            max_configs=max_configs,
            early_cutoff=early_cutoff,
            cutoff_probability=cutoff_probability,
            cutoff_number=cutoff_number,
            min_cutoff_size=min_cutoff_size,
            init_bound=init_bound,
            subseq_bound=subseq_bound,
            start_size=start_size,
            size_step=size_step,
            keep_configs=keep_configs,
            min_nuc_rate=min_nuc_rate,
            canvas_size=canvas_size,
            target_size=target_size,
            **kwargs,
        )

Tile

rgrow.Tile

Source code in rgrow-python/rgrow/__init__.py
206
207
208
209
210
211
212
@attr.define(auto_attribs=True)
class Tile:
    edges: List[int | str]
    name: Optional[str] = None
    stoic: Optional[float] = 1.0
    color: Optional[str] = None
    shape: Optional[str | rgr.TileShape] = None

Bond

rgrow.Bond

Source code in rgrow-python/rgrow/__init__.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
@attrs.define(auto_attribs=True)
class Bond:
    name: str
    strength: float

    @staticmethod
    def _conv(a: "Bond | Any", *args: Any, **kwargs: Any) -> "Bond":
        if isinstance(a, Bond):
            return a
        elif isinstance(a, dict):
            return Bond(**a)
        elif isinstance(a, tuple):
            return Bond(*a)
        else:
            return Bond(a, *args, **kwargs)  # type: ignore