dbbs-lab / bsb-core

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

Over-placement of cells #879

Closed francesshei closed 1 week ago

francesshei commented 2 weeks ago

bsb.placement.RandomPlacement places too many cells when using a single strategy for multiple partitions. This configuration snippet:

  "placement": {
    "tc_placement": {
      "strategy": "bsb.placement.RandomPlacement",
      "cell_types": ["thalamocortical-cells"],
      "partitions": ["VA", "VL", "VM"]
    },
    "rt_placement": {
      "strategy": "bsb.placement.RandomPlacement",
      "cell_types": ["reticular-cells"],
      "partitions": ["mRT"]
    }

places the right number of reticular-cells in the mRT partition, but places too many thalamocortical-cells in the scaffold. All partitions are of nrrd type, and both cell types have a single density value (no per-voxel density).

The expected number of cells ((n_voxels * (voxel_side ** 3)) * density) is much lower than the cells placed in each partition.

drodarie commented 2 weeks ago

Hi @francesshei, sorry for the delay of the reply. I would need more info to help you.

Do I get it right that you are using an atlas (probably the allen mouse atlas) with separated nrrd files containing the densities of each cell type within the atlas volume? What is the difference in number that you obtained for thalamocortical-cells? BTW, you should make sure that your nrrd densities files are in cell . um^-3. Also, the number of cells placed within each voxel is rounded randomly. So if each voxel has a density of 1.1, each voxel will contain 1 cell and have 10% of chance to contain a second one.

May I see your complete configuration, at least to see how you defined the regions, partitions, and cell types?

francesshei commented 2 weeks ago

Hey @drodarie, no worries!

So the NRRD are only used to define the voxels belonging to each partition. I'm using local files instead of just plugging AMBA regions because I defined a thalamic parcellation that is more detailed than the one reported in the CCFv3.

Densities are indeed in cell . um^-3 units, and are assigned as scalar values to the two cell types I'm filling the scaffold with.

Here's the relevant configuration code:

{
  "name": "Thalamic scaffold",
  "storage": {
    "engine": "hdf5",
    "root": "voxel_valvm_mrt.hdf5"
  },
  "network": {
    "x": 13200.0,
    "y": 8000.0,
    "z": 11400.0
  },
  "regions": {
    "thalamus":{
      "children": ["VL", "VA", "VM", "mRT"]
    }
  },
  "partitions": {
    "VL":{
      "type": "nrrd",
      "source": "data/ish/val_subpartitions.nrrd",
      "mask_value": 1.0,
      "voxel_size": 25.0
    },
    "VA":{
      "type": "nrrd",
      "source": "data/ish/val_subpartitions.nrrd",
      "mask_value": 2.0,
      "voxel_size": 25.0
    },
    "VM":{
      "type": "nrrd",
      "source": "data/atlas/halved_vm.nrrd",
      "mask_value": 1,
      "voxel_size": 25.0
    },
    "mRT":{
      "type": "nrrd",
      "source": "data/atlas/motor_rt.nrrd",
      "mask_value": 1,
      "voxel_size": 25.0
    }
  },
  "cell_types": {
    "thalamocortical-cells": {
      "spatial": {
          "radius": 10.0,
          "density": 5.05e-5
        },
      "plotting": {
        "display_name": "Thalamocortical cells",
        "color": "#E62314",
        "opacity": 0.5
      }
    },
    "reticular-cells": {
      "spatial": {
        "radius": 10.0,
        "density": 5.6e-5
      },
      "plotting": {
        "display_name": "Reticular cells",
        "color": "#1e2b61",
        "opacity": 0.5
      }
    }
  },
  "placement": {
    "val_placement": {
      "strategy": "bsb.placement.RandomPlacement",
      "cell_types": ["thalamocortical-cells"],
      "partitions": ["VA", "VL", "VM"]
    },
    "rt_placement": {
      "strategy": "bsb.placement.RandomPlacement",
      "cell_types": ["reticular-cells"],
      "partitions": ["mRT"]
    }
  },

I can share the local files if it's not reproducible otherwise, but the values are some thousand cells wrong. These are the placement results from 4 instances of my thalamic scaffold:

VA:  0.185 mm^3 | 5.05 * 10-5 cells . um^-3 | 13622 +- 55 cells (expected: ~ 9342 cells)
VL: 0.224 mm^3 | 5.05 * 10-5  cells . um^-3 | 15992 +- 51 cells (expected: ~ 11312 cells)
VM: 0.475 mm^3 | 5.05 * 10-5  cells . um^-3 |  27065 +- 18 cells  (expected: ~ 23.987 cells)
francesshei commented 2 weeks ago

As a side note, the issue does not have high priority for me anymore: I solved it using a FixedPosition strategy modified to take as input a numpy array of positions downloaded from the Blue Brain Cell Atlas. So placement is now working fine for me, although there still might be a bug in the default RandomPlacement strategy.

drodarie commented 2 weeks ago

After some investigations, I think I found the bug. Some chunks are dealt twice by the placement strategy because they appear in multiple partitions. This results in placing too many cells.