Skip to content

Commit 8044a14

Browse files
committed
Define grading strategies...
...and add some entry-level stuff
1 parent 5309c13 commit 8044a14

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

src/classy_blocks/grading/autograding/grader.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import abc
2+
from typing import get_args
23

3-
from classy_blocks.grading.autograding.params import ChopParams, SimpleChopParams
4+
from classy_blocks.grading.autograding.params import ChopParams, SimpleChopParams, SimpleHighReChopParams
5+
from classy_blocks.grading.autograding.probe import Probe
6+
from classy_blocks.items.block import Block
47
from classy_blocks.mesh import Mesh
8+
from classy_blocks.types import AxisType
59

610

711
class GraderBase(abc.ABC):
812
def __init__(self, mesh: Mesh, params: ChopParams):
913
self.mesh = mesh
1014
self.params = params
1115

16+
self.probe = Probe(self.mesh)
17+
1218
@abc.abstractmethod
1319
def grade(self) -> None:
1420
pass
@@ -25,3 +31,18 @@ def grade(self):
2531
for block in self.mesh.blocks:
2632
for axis in block.axes:
2733
axis.chops = chops
34+
35+
36+
class SimpleHighReGrader(GraderBase):
37+
def __init__(self, mesh: Mesh, params: SimpleHighReChopParams):
38+
super().__init__(mesh, params)
39+
40+
def grade_block(self, block: Block) -> None:
41+
raise NotImplementedError("WIP!")
42+
43+
def grade_axis(self, axis: AxisType) -> None:
44+
raise NotImplementedError("WIP!")
45+
46+
def grade(self):
47+
for axis in get_args(AxisType):
48+
self.grade_axis(axis)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
import functools
2+
from typing import Set, get_args
3+
4+
from classy_blocks.items.block import Block
5+
from classy_blocks.items.wires.axis import Axis
16
from classy_blocks.mesh import Mesh
7+
from classy_blocks.types import AxisType
8+
9+
10+
@functools.lru_cache(maxsize=3000) # that's for 1000 blocks
11+
def _get_block_from_axis(mesh: Mesh, axis: Axis) -> Block:
12+
for block in mesh.blocks:
13+
for index in get_args(AxisType):
14+
if block.axes[index] == axis:
15+
return block
16+
17+
raise RuntimeError("Block for Axis not found!")
218

319

420
class Probe:
521
"""Examines the mesh and gathers required data for auto chopping"""
622

723
def __init__(self, mesh: Mesh):
824
self.mesh = mesh
25+
26+
def _get_block_from_axis(self, axis: Axis) -> Block:
27+
return _get_block_from_axis(self.mesh, axis)
28+
29+
def get_blocks_on_layer(self, block: Block, axis: AxisType) -> Set[Block]:
30+
"""Returns all blocks on the same 'layer' as the one in arguments"""
31+
# blocks to be returned
32+
blocks: Set[Block] = set()
33+
# blocks not to check again
34+
traversed: Set[Block] = set()
35+
36+
def check(blk: Block):
37+
if blk not in traversed:
38+
traversed.add(blk)
39+
40+
for neighbour_axis in blk.axes[axis].neighbours:
41+
neighbour_block = self._get_block_from_axis(neighbour_axis)
42+
blocks.add(neighbour_block)
43+
44+
check(self._get_block_from_axis(neighbour_axis))
45+
46+
check(block)
47+
48+
return blocks

src/classy_blocks/items/wires/axis.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ def is_simple(self) -> bool:
121121

122122
def __str__(self):
123123
return f"Axis {self.index} (" + "|".join(str(wire) for wire in self.wires.wires) + ")"
124+
125+
def __hash__(self):
126+
return id(self)

tests/test_autograde.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
3+
from parameterized import parameterized
4+
5+
from classy_blocks.construct.flat.sketches.grid import Grid
6+
from classy_blocks.construct.stack import ExtrudedStack
7+
from classy_blocks.grading.autograding.probe import Probe
8+
from classy_blocks.mesh import Mesh
9+
10+
11+
class AutogradeTestsBase(unittest.TestCase):
12+
def setUp(self):
13+
self.mesh = Mesh()
14+
15+
# create a simple 3x3 grid for easy navigation
16+
base = Grid([0, 0, 0], [1, 1, 0], 3, 3)
17+
self.stack = ExtrudedStack(base, 1, 3)
18+
19+
self.mesh.add(self.stack)
20+
self.mesh.assemble()
21+
22+
23+
class ProbeTests(AutogradeTestsBase):
24+
@parameterized.expand(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 2)))
25+
def test_get_blocks_on_layer(self, block, axis):
26+
probe = Probe(self.mesh)
27+
28+
blocks = probe.get_blocks_on_layer(self.mesh.blocks[block], axis)
29+
self.assertEqual(len(blocks), 9)

0 commit comments

Comments
 (0)