dbbs-lab / bsb-core

The Brain Scaffold Builder
https://bsb.readthedocs.io
GNU General Public License v3.0
21 stars 16 forks source link

Obscure documentation on hooks #856

Closed francesshei closed 2 months ago

francesshei commented 2 months ago

I have a script implemented from a previous v4 that uses a PostProcessingHook class from bsb.postprocessing that has now been removed. It imports the class and then implements the after_connectivity method: how would that work in v>4.0.1? I see an AfterConnectivityHook class but in the same module, but the docs are barely written. Would it be the same to call its postprocess() method?

The snippet I'm trying to re-implement is:

n = from_storage(args.filename)
n.after_connectivity["reticulothalamic_hook"] = FixPostSynaptic(
    "reticular-cells", "thalamocortical-cells", "reticulothalamic"
)
n.run_after_connectivity()

With hook:

class FixPostSynaptic(PostProcessingHook):
    ...
    def after_connectivity(self, scaffold):
        ...
Helveg commented 2 months ago

The class PostProcessingHook has been split into the AfterPlacementHook and AfterConnectivityHook, use these and change the method after_connectivity to postprocess:

@config.dynamic(attr_name="strategy")
class AfterPlacementHook(abc.ABC):
    name: str = config.attr(key=True)

    def queue(self, pool):
        pool.queue(
            lambda scaffold: scaffold.after_placement[self.name].postprocess(),
            submitter=self,
        )

    @abc.abstractmethod
    def postprocess(self):
        pass

@config.dynamic(attr_name="strategy")
class AfterConnectivityHook(abc.ABC):
    name: str = config.attr(key=True)

    def queue(self, pool):
        pool.queue(
            lambda scaffold: scaffold.after_connectivity[self.name].postprocess(),
            submitter=self,
        )

    @abc.abstractmethod
    def postprocess(self):
        pass

and yes to manually run that step you could do this (recommended):

with scaffold.create_job_pool() as pool:
    if pool.is_main():
        pool.schedule([n.after_connectivity["reticulothalamic_hook"]])
    pool.execute()

or directly call it (not recommended, no scheduling of multiple jobs if required, and no parallelism):

n.after_connectivity["reticulothalamic_hook"].postprocess()