cantata 0.2.2

Convert SONATA package neuro science simulations into Arbor simulations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
use crate::{
    err::{Context, Result},
    fit::Attribute,
    raw,
    sup::{find_component, Components},
    Map,
};
use anyhow::{anyhow, bail};
use hdf5_metno as hdf5;
use serde::{Deserialize, Serialize};
use std::{fs::File, str::FromStr};

/// Model types currently known to SONATA
///
/// * single_compartment
///
///   A single cylindrical compartment is created with length equal to diameter,
///   thus the same effective area as that of a sphere of the same diameter. The
///   diameter is defined by an additional expected dynamics_param `D`, which
///   defaults to 1 um. Further, the passive mechanism is inserted and the
///   additional mechanisms named in the `model_template` required attribute.
///
/// * point_neuron
///
///   The actual model type is defined by the `model_template` required
///   attribute, eg an NMODL file for NRN and for NEST/PyNN, model_template will
///   provide the name of a built-in model.
///
/// * virtual
///
///   Placeholder neuron, which is not otherwise simulated, but can a the source
///   of spikes.
///
/// * biophysical
///
///   A compartmental neuron. The attribute morphology must be defined, either
///   via the node or node_type.
///
///  In addition, any number of Key:Value pairs may be listed
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "model_type")]
pub enum ModelType {
    #[serde(rename = "biophysical")]
    Biophysical {
        model_template: String,
        #[serde(flatten)]
        attributes: Map<String, Attribute>,
    },
    #[serde(rename = "single_compartment")]
    Single {
        model_template: String,
        #[serde(flatten)]
        attributes: Map<String, Attribute>,
    },
    #[serde(rename = "point_neuron")]
    Point {
        model_template: String,
        #[serde(flatten)]
        attributes: Map<String, Attribute>,
    },
    #[serde(rename = "virtual")]
    Virtual {
        #[serde(flatten)]
        attributes: Map<String, Attribute>,
    },
}

/// Types are defined in CSV files with one named column for each attribute.
/// Separator is a single space.
/// Non scalar attributes may be given provided values are quoted and their
/// components are separated by spaces.
/// Node type columns will be assigned to node attributes indexed by the type id.
/// Columns are:
/// - node_type_id: required; defines the node_type_id of this row.
/// - population: required in either this, or the nodes.h5; defines the population.
///               Multiple populations may define the same node_type_id,
/// - model_type: required, and may be defined only in the node_types.csv
/// - required columns may also appear in the instance H5; defined by the population
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NodeType {
    /// unique -- within population -- id of this type. Used in instances to
    /// reference this type.
    #[serde(rename = "node_type_id")]
    pub type_id: u64,
    /// population using this type. Might be given by instance file, this, or
    /// both
    #[serde(rename = "pop_name")]
    pub population: Option<String>,
    /// holds the actual parameter data
    #[serde(flatten)]
    pub model_type: ModelType,
    /// type-wide dynamics, pulled from the model-type
    #[serde(default)]
    pub dynamics: Map<String, f64>,
}

impl NodeType {
    fn clean_up(&mut self) {
        match &mut self.model_type {
            ModelType::Biophysical { attributes, .. }
            | ModelType::Single { attributes, .. }
            | ModelType::Point { attributes, .. }
            | ModelType::Virtual { attributes } => {
                attributes.retain(|_, v| !matches!(v, Attribute::String(s) if s == "NULL"));
            }
        }
    }

    pub fn attributes(&self) -> &Map<String, Attribute> {
        match &self.model_type {
            ModelType::Biophysical { attributes, .. }
            | ModelType::Single { attributes, .. }
            | ModelType::Point { attributes, .. }
            | ModelType::Virtual { attributes } => attributes,
        }
    }
}

#[derive(Debug)]
pub struct ParameterGroup {
    pub id: u64,
    pub dynamics: Map<String, Vec<f64>>,
    pub custom: Map<String, Vec<f64>>,
}

/// Populations are stored HDF5 files, and have an associated node types file to
/// define node_type_ids and assign attributes common across a population.
/// NOTE: node_types file may be shared by multiple population files.
///
/// Node groups are represented as HDF5 groups (with population as parent)
/// containing a dataset for each parameter of length equal to the number of
/// nodes in the group.
///
/// If an attribute is defined in both the node types and the node instances, the
/// latter overrides the former.
///
/// We have the following layout for the node instance HDF5 file:
///
/// Path                                 Type                      Required
/// =================================    ======================    ============
/// /nodes                               Group
///     * /<population_name>             Group
///         * /node_type_id              Dataset{N_total_nodes}    X
///         * /node_id                   Dataset{N_total_nodes}    X
///         * /node_group_id             Dataset{N_total_nodes}    X
///         * /node_group_index          Dataset{N_total_nodes}    X
///         * /<group_id>                Group                     one per unique group_id, at least one
///             * /dynamics_params       Group                     X (may be empty, though)
///                 * /<param>           Dataset {M_nodes}
///             * /<custom_attribute>    Dataset {M_nodes}
///
/// Notes:
/// * For each unique entry in node_group_id we expect one <group_id> group
///   under the population
#[derive(Debug)]
pub struct NodePopulation {
    pub name: String,
    pub size: usize,
    pub type_ids: Vec<u64>,
    pub node_ids: Vec<u64>,
    pub group_ids: Vec<u64>,
    pub group_indices: Vec<usize>,
    pub groups: Vec<ParameterGroup>,
}

#[derive(Debug)]
pub struct NodeList {
    pub types: Vec<NodeType>,
    pub populations: Vec<NodePopulation>,
}

impl NodeList {
    fn new(nodes: &raw::Nodes) -> Result<Self> {
        let path = &nodes.types;
        let path = std::path::PathBuf::from_str(&nodes.types)
            .map_err(anyhow::Error::from)
            .and_then(|p| p.canonicalize().map_err(anyhow::Error::from))
            .with_context(|| format!("Resolving node types {path}"))?;
        let rd = File::open(&path).with_context(|| format!("Opening {path:?}"))?;
        let mut tys = csv::ReaderBuilder::new()
            .delimiter(b' ')
            .from_reader(rd)
            .deserialize()
            .map(|it| it.map_err(anyhow::Error::from))
            .collect::<Result<Vec<NodeType>>>()
            .with_context(|| format!("Parsing node types {path:?}"))?;
        tys.iter_mut().for_each(|ty| ty.clean_up());
        let path = &nodes.nodes;
        let path = std::path::PathBuf::from_str(&nodes.nodes)
            .map_err(anyhow::Error::from)
            .and_then(|p| p.canonicalize().map_err(anyhow::Error::from))
            .with_context(|| format!("Resolving node instances {path}"))?;
        let node_instance_file = hdf5::file::File::open(&path)?;
        let nodes = node_instance_file.group("nodes")?;
        let populations = nodes
            .groups()
            .with_context(|| "Opening population list".to_string())?;

        let mut pops = Vec::new();
        for population in &populations {
            let name = population.name().rsplit_once('/').unwrap().1.to_string();
            let type_ids = get_dataset::<u64>(population, "node_type_id")?;
            let size = type_ids.len();
            let node_ids = get_dataset::<u64>(population, "node_id")
                .unwrap_or_else(|_| (0..size as u64).collect::<Vec<_>>());
            if size != node_ids.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #node_ids"
                )
            }
            let group_ids = get_dataset::<u64>(population, "node_group_id")?;
            if size != group_ids.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #group_id"
                )
            }
            let group_indices = get_dataset::<usize>(population, "node_group_index")?;
            if size != group_indices.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #group_index"
                )
            }
            let mut groups = Vec::new();
            // NOTE We assume here that group ids are contiguous; yet, that is
            // said nowhere.
            let mut group_id = 0;
            loop {
                if let Ok(group) = population.group(&format!("{group_id}")) {
                    let mut dynamics = Map::new();
                    let mut custom = Map::new();
                    if let Ok(dynamics_params) = group.group("dynamics_params") {
                        for param in dynamics_params.datasets()?.iter() {
                            let values = param.read_1d::<f64>()?.to_vec();
                            let name = param.name().rsplit_once('/').unwrap().1.to_string();
                            dynamics.insert(name, values);
                        }
                    }
                    for param in group.datasets()?.iter() {
                        let values = param.read_1d::<f64>()?.to_vec();
                        let name = param.name().rsplit_once('/').unwrap().1.to_string();
                        custom.insert(name, values);
                    }

                    groups.push(ParameterGroup {
                        id: group_id as u64,
                        dynamics,
                        custom,
                    });
                } else {
                    break;
                }
                group_id += 1;
            }
            pops.push(NodePopulation {
                name,
                size,
                type_ids,
                node_ids,
                group_ids,
                group_indices,
                groups,
            })
        }
        Ok(Self {
            types: tys,
            populations: pops,
        })
    }
}

/// types are defined in a CSV file of named columns; separator is a single space.
/// - edge_type_id; required
/// - population; required either in CSV or H5; handles populations defining the same edge_type_id
/// - any number of additional columns may freely be added.
#[derive(Debug, Serialize, Deserialize)]
struct EdgeType {
    #[serde(rename = "edge_type_id")]
    pub type_id: u64,
    #[serde(rename = "pop_name")]
    pub population: Option<String>,
    #[serde(flatten)]
    pub attributes: Map<String, Attribute>,
    #[serde(default)]
    pub dynamics: Map<String, f64>,
}

impl EdgeType {
    fn clean_up(&mut self) {
        self.attributes
            .retain(|_, v| !matches!(v, Attribute::String(s) if s == "NULL"));
    }
}

/// Populations are stored HDF5 files, and have an associated edge types file to
/// define edge_type_ids and assign attributes common across a population.
/// NOTE: edge_types file may be shared by multiple population files.
///
/// Edge groups are represented as HDF5 groups (with population as parent)
/// containing a dataset for each parameter of length equal to the number of
/// edges in the group.
///
/// Edge populations are defined as groups and stored as sparse table via the
/// `source_node_id` and `target_node_id` datasets. These datasets have an
/// associated attribute "node_population" that specifies the node population
/// for resolving the node_ids of the source or target.
///
/// If an attribute is defined in both the edge types and the edge instances, the
/// latter overrides the former.
///
/// We have the following layout for the edge instance HDF5 file:
///
/// Path                                 Type                      Required
/// =================================    ======================    ============
/// /edges                               Group
///     * /<population_name>             Group
///         * /edge_type_id              Dataset{N_total_edges}    X
///         * /edge_id                   Dataset{N_total_edges}    X
///         * /edge_group_id             Dataset{N_total_edges}    X
///         * /edge_group_index          Dataset{N_total_edges}    X
///         * /source_node_id            Dataset{N_total_edges}    X
///             * /population_name       Attribute                 X
///         * /target_node_id            Dataset{N_total_edges}    X
///             * /population_name       Attribute                 X
///         * /<group_id>                Group
///             * /dynamics_params       Group
///                 * /<param>           Dataset {M_edges}
///             * /<custom_attribute>    Dataset {M_edges}
///
/// Notes:
/// * For each unique entry in edge_group_id we expect one <group_id> group
///   under the population
#[derive(Debug)]
struct EdgePopulation {
    name: String,
    size: usize,
    type_ids: Vec<u64>,
    group_ids: Vec<u64>,
    source_ids: Vec<u64>,
    source_pop: String,
    target_ids: Vec<u64>,
    target_pop: String,
    group_indices: Vec<usize>,
    groups: Vec<ParameterGroup>,
}

#[derive(Debug)]
pub struct EdgeList {
    types: Vec<EdgeType>,
    populations: Vec<EdgePopulation>,
}

fn get_dataset<T: hdf5::H5Type + Clone>(g: &hdf5::Group, nm: &str) -> Result<Vec<T>> {
    Ok(g.dataset(nm)
        .with_context(|| format!("Group {} has no dataset {nm}", g.name()))?
        .read_1d::<T>()?
        .to_vec())
}

impl EdgeList {
    fn new(edges: &raw::Edges) -> Result<Self> {
        let path = &edges.types;
        let path = std::path::PathBuf::from_str(&edges.types)
            .map_err(anyhow::Error::from)
            .and_then(|p| p.canonicalize().map_err(anyhow::Error::from))
            .with_context(|| format!("Resolving edge types {path}"))?;
        let rd = File::open(&path).with_context(|| format!("Opening {path:?}"))?;
        let mut tys = csv::ReaderBuilder::new()
            .delimiter(b' ')
            .from_reader(rd)
            .deserialize()
            .map(|it| it.map_err(anyhow::Error::from))
            .collect::<Result<Vec<EdgeType>>>()
            .with_context(|| format!("Parsing edge types {path:?}"))?;
        tys.iter_mut().for_each(|nt| nt.clean_up());
        let path = &edges.edges;
        let path = std::path::PathBuf::from_str(&edges.edges)
            .map_err(anyhow::Error::from)
            .and_then(|p| p.canonicalize().map_err(anyhow::Error::from))
            .with_context(|| format!("Resolving edge instances {path}"))?;
        let edge_instance_file = hdf5::file::File::open(&path)?;
        let edges = edge_instance_file.group("edges")?;
        let populations = edges
            .groups()
            .with_context(|| "Opening population list".to_string())?;

        let mut pops = Vec::new();
        for population in &populations {
            let name = population.name().rsplit_once('/').unwrap().1.to_string();
            let type_ids = get_dataset(population, "edge_type_id")?;
            let size = type_ids.len();
            let edge_ids = get_dataset::<u64>(population, "edge_id")
                .unwrap_or_else(|_| (0..size as u64).collect::<Vec<_>>());
            if size != edge_ids.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #edge_ids"
                )
            }
            let group_ids = get_dataset::<u64>(population, "edge_group_id")?;
            if size != group_ids.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #group_id"
                )
            }
            let group_indices = get_dataset::<usize>(population, "edge_group_index")?;
            if size != group_indices.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #group_index"
                )
            }
            let sources = population
                .dataset("source_node_id")
                .with_context(|| format!("Extracting source indices from population {name}"))?;
            let source_ids = sources.read_1d::<u64>()?.to_vec();
            if size != source_ids.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #source_ids"
                )
            }
            let source_pop = sources
                .attr("node_population")
                .with_context(|| {
                    format!("Extracting source population from population {name}; not found")
                })?
                .read_scalar::<hdf5::types::VarLenUnicode>()
                .with_context(|| {
                    format!("Extracting source population from population {name}; not a string")
                })?
                .as_str()
                .to_string();
            let targets = population
                .dataset("target_node_id")
                .with_context(|| format!("Extracting target indices from population {name}"))?;

            let target_ids = targets.read_1d::<u64>()?.to_vec();
            let target_pop = targets
                .attr("node_population")
                .with_context(|| {
                    format!("Extracting target population from population {name}; not found")
                })?
                .read_scalar::<hdf5::types::VarLenUnicode>()
                .with_context(|| {
                    format!("Extracting target population from population {name}; not a string")
                })?
                .as_str()
                .to_string();
            if size != target_ids.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #target_ids"
                )
            }

            let group_indices = get_dataset::<usize>(population, "edge_group_index")?;
            if size != group_indices.len() {
                anyhow::bail!(
                    "Population {name} in {path:?} has mismatched #type_ids ./. #group_index"
                )
            }
            let mut groups = Vec::new();
            let mut group_id = 0;
            loop {
                if let Ok(group) = population.group(&format!("{group_id}")) {
                    let mut dynamics = Map::new();
                    let mut custom = Map::new();
                    if let Ok(dynamics_params) = group.group("dynamics_params") {
                        for param in dynamics_params.datasets()?.iter() {
                            let values = param.read_1d::<f64>()?.to_vec();
                            let name = param.name().rsplit_once('/').unwrap().1.to_string();
                            dynamics.insert(name, values);
                        }
                    }
                    for param in group.datasets()?.iter() {
                        let values = param.read_1d::<f64>()?.to_vec();
                        let name = param.name().rsplit_once('/').unwrap().1.to_string();
                        custom.insert(name, values);
                    }

                    groups.push(ParameterGroup {
                        id: group_id,
                        dynamics,
                        custom,
                    });
                } else {
                    break;
                }
                group_id += 1;
            }
            pops.push(EdgePopulation {
                name,
                size,
                type_ids,
                group_ids,
                source_ids,
                target_ids,
                source_pop,
                target_pop,
                groups,
                group_indices,
            })
        }
        Ok(Self {
            types: tys,
            populations: pops,
        })
    }
}

#[derive(Debug, Clone)]
pub struct Edge {
    pub src_gid: u64,
    pub source: (u64, f64),
    pub target: (u64, f64),
    pub mech: Option<String>,
    pub delay: f64,
    pub weight: f64,
    pub dynamics: Map<String, f64>,
}

/// Reified node, containing all information we currently have
#[derive(Debug)]
pub struct Node {
    /// globally (!) unique id
    pub gid: usize,
    /// owning population
    pub pop: String,
    /// id within the population
    pub node_id: u64,
    /// id of containing group.
    pub group_id: u64,
    /// index within the group
    pub group_index: usize,
    /// id of node type in population used to instantiate.
    pub node_type_id: u64,
    /// node type used to instantiate
    pub node_type: NodeType,
    /// connections terminating here.
    pub incoming_edges: Vec<Edge>,
    /// Dynamics parameters extracted from the population.
    pub dynamics: Map<String, f64>,
    /// Custom parameters extracted from the population.
    pub custom: Map<String, f64>,
}

/// Bookeeping: index into top-level structure, ie population `pop` is stored at
/// node_lists[pop.list_index].populations[pop.pop_index]. The cells in this
/// population have identifiers in the range [start, start + size)
#[derive(Debug)]
pub struct PopId {
    /// Index into the containing {node, edge}_list
    pub list_index: usize,
    /// Index into the containing population list
    pub pop_index: usize,
    /// GID of first cell in this population
    pub start: usize,
    /// Number of cells in this population
    pub size: usize,
}

impl PopId {
    fn new_nodes(lid: usize, pid: usize, start: usize, pop: &NodePopulation) -> Result<Self> {
        Ok(PopId {
            list_index: lid,
            pop_index: pid,
            start,
            size: pop.size,
        })
    }

    fn new_edges(lid: usize, pid: usize, start: usize, pop: &EdgePopulation) -> Result<Self> {
        Ok(PopId {
            list_index: lid,
            pop_index: pid,
            start,
            size: pop.size,
        })
    }
}

#[derive(Debug)]
pub struct GlobalProperties {
    pub celsius: f64,
    pub v_init: f64,
}

#[derive(Debug)]
pub struct Simulation {
    /// runtime
    pub tfinal: f64,
    /// timestep
    pub dt: f64,
    /// raw-ish node data
    pub node_lists: Vec<NodeList>,
    /// raw-ish edge data
    pub edge_lists: Vec<EdgeList>,
    /// GID from (population, id)
    pub node_population_to_gid: Map<(usize, u64), u64>,
    /// GID to (population, id)
    pub gid_to_node_population: Vec<(usize, u64)>,
    /// node population list, to avoid copying strings and storing the indices
    /// into node_list and population.
    pub node_populations: Vec<PopId>,
    /// reverse mapping Name -> Id
    pub node_population_ids: Map<String, usize>,
    /// Number of total cells
    pub size: usize,
    /// reportable variables per population id
    pub reports: Map<u64, Vec<Probe>>,
    /// current clamps
    pub iclamps: Map<u64, Vec<IClamp>>,
    /// CV discretization
    pub cv_policy: CVPolicy,
    /// threshold for spike generation
    pub spike_threshold: f64,
    /// virtual cell spikes
    pub virtual_spikes: Map<String, Map<u64, Vec<f64>>>,
    /// cable cell global properties
    pub global_properties: Option<GlobalProperties>,
    /// components for delayed/lazy lookup
    pub components: Components,
}

#[derive(Debug, Clone)]
pub enum CVPolicy {
    Default,
    MaxExtent(f64),
}

#[derive(Debug, Clone)]
pub enum Probe {
    CableVoltage(Vec<String>),
    CableIntConc(String, Vec<String>),
    CableExtConc(String, Vec<String>),
    CableState(String, Vec<String>),
    Lif,
}

#[allow(non_snake_case)]
#[derive(Debug, Clone)]
pub struct IClamp {
    pub amplitude_nA: f64,
    pub delay_ms: f64,
    pub duration_ms: f64,
    pub tag: usize,
    pub location: String,
}

fn read_virtual_spikes(
    components: &Components,
    inputs: &raw::Inputs,
) -> Result<Map<String, Map<u64, Vec<f64>>>> {
    let mut res: Map<String, Map<u64, Vec<f64>>> = Map::new();
    for input in inputs.values() {
        if let raw::Input::Spikes {
            module,
            input_file,
            node_set,
        } = input
        {
            if module != "sonata" {
                bail!("Unknown module '{module}' for Input::Spikes");
            }
            if let raw::NodeSet::Name(node_set_name) = node_set {
                let data = res.entry(node_set_name.to_string()).or_default();
                for ifn in input_file {
                    let ifn = find_component(ifn, components)?;
                    let spikes = hdf5::file::File::open(&ifn)?
                        .group("spikes")?
                        .group(node_set_name)?;
                    let nodes = get_dataset::<u32>(&spikes, "node_ids")?;
                    let times = get_dataset::<f64>(&spikes, "timestamps")?;
                    if times.len() != nodes.len() {
                        bail!("Virtual spike data for {node_set_name} has different lengths on timestamps and node_ids");
                    }
                    for (id, ts) in nodes.iter().zip(times.iter()) {
                        data.entry(*id as u64).or_default().push(*ts);
                    }
                }
            } else {
                bail!("NodeSet on spikes must be a named reference, is {node_set:?}");
            }
        }
    }
    Ok(res)
}

impl Simulation {
    pub fn new(sim: &raw::Simulation) -> Result<Self> {
        let mut node_lists = sim
            .network
            .nodes
            .iter()
            .map(NodeList::new)
            .collect::<Result<Vec<_>>>()?;
        let mut edge_lists = sim
            .network
            .edges
            .iter()
            .map(EdgeList::new)
            .collect::<Result<Vec<_>>>()?;

        let mut start = 0;
        let mut edge_populations = Vec::new();
        for (lid, edge_list) in edge_lists.iter_mut().enumerate() {
            for ty in edge_list.types.iter_mut() {
                if let Some(Attribute::String(name)) = ty.attributes.get("dynamics_params") {
                    let fname = find_component(name, &sim.components)?;
                    let fdata = std::fs::File::open(fname)?;
                    let fdata: Map<String, serde_json::Value> = serde_json::from_reader(&fdata)
                        .with_context(|| format!("Parsing JSON from {name}"))?;
                    let mut param = fdata
                        .into_iter()
                        .filter_map(|(k, v)| v.as_f64().map(|v| (k.to_string(), v)))
                        .collect();
                    ty.dynamics.append(&mut param);
                }
            }
            for (pid, population) in edge_list.populations.iter().enumerate() {
                edge_populations.push(PopId::new_edges(lid, pid, start, population)?);
                start += population.size;
            }
        }
        let mut gid = 0;
        let mut start = 0;
        let mut node_population_to_gid = Map::new();
        let mut node_population_ids = Map::new();
        let mut gid_to_node_population = Vec::new();
        let mut node_populations = Vec::new();
        for (lid, node_list) in node_lists.iter_mut().enumerate() {
            for ty in node_list.types.iter_mut() {
                match &ty.model_type {
                    ModelType::Biophysical { attributes, .. }
                    | ModelType::Single { attributes, .. }
                    | ModelType::Point { attributes, .. }
                    | ModelType::Virtual { attributes, .. } => {
                        if let Some(Attribute::String(name)) = attributes.get("dynamics_params") {
                            let fname = find_component(name, &sim.components)?;
                            let fdata = std::fs::File::open(fname)?;
                            let fdata =
                                serde_json::from_reader::<_, Map<String, serde_json::Value>>(
                                    &fdata,
                                )
                                .with_context(|| format!("Parsing JSON from {name}"))?
                                .into_iter()
                                .filter_map(|(k, v)| v.as_f64().map(|v| (k.to_string(), v)));
                            ty.dynamics.extend(fdata);
                        }
                    }
                }
            }
            for (pid, population) in node_list.populations.iter().enumerate() {
                let pop_idx = node_populations.len();
                node_populations.push(PopId::new_nodes(lid, pid, start, population)?);
                node_population_ids.insert(population.name.to_string(), pop_idx);
                for nid in &population.node_ids {
                    node_population_to_gid.insert((pop_idx, *nid), gid);
                    gid_to_node_population.push((pop_idx, *nid));
                    gid += 1;
                }
                start += population.size;
            }
        }

        let cv_policy = if let Some(d) = sim.run.dl {
            CVPolicy::MaxExtent(d)
        } else {
            CVPolicy::Default
        };

        let virtual_spikes = read_virtual_spikes(&sim.components, &sim.inputs)?;

        let global_properties =
            if let raw::Conditions::Detailled { celsius, v_init } = sim.conditions {
                Some(GlobalProperties { celsius, v_init })
            } else {
                None
            };

        let mut res = Self {
            tfinal: sim.run.tstop,
            dt: sim.run.dt,
            node_lists,
            edge_lists,
            gid_to_node_population,
            node_population_to_gid,
            node_populations,
            node_population_ids,
            reports: Default::default(),
            iclamps: Default::default(),
            cv_policy,
            virtual_spikes,
            global_properties,
            components: sim.components.clone(),
            size: start,
            spike_threshold: sim.run.spike_threshold.expect("No spike threshold?"),
        };

        res.read_reports(&sim.reports)?;
        res.read_iclamps(&sim.inputs)?;

        Ok(res)
    }

    fn read_reports(&mut self, reports: &Map<String, raw::Report>) -> Result<()> {
        for raw::Report {
            cells,
            variable_name,
            sections,
            enabled,
            ..
        } in reports.values()
        {
            if !enabled {
                continue;
            }
            let gids = self.resolve_nodeset(cells)?;
            let probe = if !sections.is_empty() {
                let sections = sections.clone();
                if variable_name == "v" {
                    Probe::CableVoltage(sections)
                } else if variable_name.ends_with('i') {
                    eprintln!("Interpreting {variable_name} as internal ion concentration.");
                    Probe::CableIntConc(variable_name.clone(), sections)
                } else if variable_name.ends_with('o') {
                    eprintln!("Interpreting {variable_name} as external ion concentration.");
                    Probe::CableExtConc(variable_name.clone(), sections)
                } else {
                    Probe::CableState(variable_name.clone(), sections)
                }
            } else if variable_name == "v" {
                Probe::Lif
            } else {
                bail!("No clue how to probe {variable_name} on LIF cells.");
            };
            for gid in gids {
                self.reports.entry(gid).or_default().push(probe.clone());
            }
        }
        Ok(())
    }

    fn read_iclamps(&mut self, inputs: &Map<String, raw::Input>) -> Result<()> {
        let mut tag = 0;
        for input in inputs.values() {
            if let raw::Input::CurrentClamp {
                amp,
                delay,
                duration,
                node_set,
                enabled,
                ..
            } = input
            {
                if !enabled {
                    continue;
                }
                assert!(amp.len() == delay.len());
                assert!(amp.len() == duration.len());
                let set = node_set
                    .as_ref()
                    .ok_or_else(|| anyhow!("No nodeset given."))?;
                let gids = self.resolve_nodeset(set)?;
                // TODO find a better way here, there might be `electrode_file` present...
                let location = String::from("(location 0 0.5)");
                eprintln!("Using placeholder current clamp location {location} for {input:?}.");
                for gid in gids.into_iter() {
                    for ix in 0..amp.len() {
                        let ic = IClamp {
                            amplitude_nA: amp[ix],
                            delay_ms: delay[ix],
                            duration_ms: duration[ix],
                            tag,
                            location: location.clone(),
                        };
                        self.iclamps.entry(gid).or_default().push(ic);
                        tag += 1;
                    }
                }
            }
        }
        Ok(())
    }

    fn resolve_nodeset(&self, cells: &raw::NodeSet) -> Result<Vec<u64>> {
        let res = match cells {
            raw::NodeSet::Name(nm) => {
                let pid = self
                    .node_population_ids
                    .get(nm)
                    .ok_or(anyhow!("Unkown population <{nm}>"))?;
                let PopId { start, size, .. } = self.node_populations[*pid];
                (start..start + size).map(|i| i as u64).collect()
            }
            raw::NodeSet::Basic { population, rules } => {
                let mut res = Vec::new();
                let pop = population
                    .as_ref()
                    .ok_or_else(|| anyhow!("Basic NodeSet {cells:?} has no population."))?;
                let pid = self
                    .node_population_ids
                    .get(pop)
                    .ok_or_else(|| anyhow!("Basic NodeSet refers to unknown population {pop}."))?;
                let PopId { start, size, .. } = &self.node_populations[*pid];
                for gid in *start..*start + *size {
                    let Node {
                        dynamics, custom, ..
                    } = self.reify_node(gid)?;
                    let mut pred = true;
                    for (k, vs) in rules.iter() {
                        // TODO Can we match on anything else?
                        if let Some(u) = dynamics.get(k).or(custom.get(k)) {
                            match vs {
                                serde_json::Value::Number(v) => {
                                    let v = v.as_f64().ok_or_else(|| anyhow!("Basic NodeSet {cells:?} has non-float number for param {k}."))?;
                                    pred &= v == *u;
                                }
                                serde_json::Value::Array(vs) => {
                                    // Array filters are true if any value matches
                                    let mut found = false;
                                    for v in vs {
                                        let v = v.as_f64().ok_or_else(|| anyhow!("Basic NodeSet {cells:?} has non-float number for param {k}."))?;
                                        found |= v == *u;
                                    }
                                    pred &= found;
                                }
                                _ => bail!("Cannot match on a {vs:?} in BasicNodeSet, must be list or value.")
                            };
                        } else {
                            // TODO What happens if we do not match the parameter name here?
                            eprintln!("Note: paramter {k} was not found in node {gid}. Currently we reject such nodes. If you consider this a bug, please report it as such.")
                        }
                    }
                    if pred {
                        res.push(gid as u64);
                    }
                }
                res
            }
            raw::NodeSet::Ids(vs) => vs.clone(),
            raw::NodeSet::Compound(nss) => {
                let mut res = Vec::new();
                for ns in nss {
                    res.append(&mut self.resolve_nodeset(ns)?);
                }
                res
            }
        };
        Ok(res)
    }

    fn reify_edges(&self, target_population: &str, target_id: u64) -> Result<Vec<Edge>> {
        let mut incoming_edges = Vec::new();
        for edge_list in &self.edge_lists {
            for edge_population in edge_list
                .populations
                .iter()
                .filter(|p| p.target_pop == target_population)
            {
                for (edge_index, _) in edge_population
                    .target_ids
                    .iter()
                    .enumerate()
                    .filter(|it| *it.1 == target_id)
                {
                    let edge_index_error = || {
                        anyhow!(
                            "Index {} overruns size {} of population {}",
                            edge_index,
                            edge_population.size,
                            edge_population.name
                        )
                    };

                    let type_id = *edge_population
                        .type_ids
                        .get(edge_index)
                        .ok_or_else(edge_index_error)?;
                    let src_id = edge_population
                        .source_ids
                        .get(edge_index)
                        .ok_or_else(edge_index_error)?;
                    let src_pop = &edge_population.source_pop;
                    let src_idx = self
                        .node_population_ids
                        .get(src_pop)
                        .ok_or_else(|| anyhow!("Unknown population <{src_pop}>"))?;
                    let src_gid = *self
                        .node_population_to_gid
                        .get(&(*src_idx, *src_id))
                        .unwrap();
                    let edge_group_id = *edge_population
                        .group_ids
                        .get(edge_index)
                        .ok_or_else(edge_index_error)?;
                    let ty = edge_list
                        .types
                        .iter()
                        .find(|ty| ty.type_id == type_id)
                        .ok_or_else(|| {
                            anyhow!(
                                "Couldn't find edge type {type_id} in population {}",
                                edge_population.name
                            )
                        })?;
                    let edge_group = edge_population
                        .groups
                        .iter()
                        .find(|g| g.id == edge_group_id)
                        .ok_or_else(|| {
                            anyhow!(
                                "Couldn't find group id {edge_group_id} edge population {}",
                                edge_population.name
                            )
                        })?;
                    // index into group for this edge
                    let group_index = *edge_population
                        .group_indices
                        .get(edge_index)
                        .ok_or_else(edge_index_error)?;

                    let delay = if let Some(ds) = edge_group.custom.get("delay") {
                        ds[group_index]
                    } else if let Some(Attribute::Float(d)) = ty.attributes.get("delay") {
                        *d
                    } else {
                        bail!(
                            "Edge {type_id} in population {} has no delay",
                            edge_population.name
                        )
                    };
                    let weight = if let Some(ds) = edge_group.custom.get("syn_weight") {
                        ds[group_index]
                    } else if let Some(Attribute::Float(d)) = ty.attributes.get("syn_weight") {
                        *d
                    } else {
                        bail!(
                            "Edge {type_id} in population {} has no weight",
                            edge_population.name
                        )
                    };

                    let mech = if let Some(s) = ty.attributes.get("model_template") {
                        if let Attribute::String(s) = s {
                            Some(s.to_string())
                        } else {
                            bail!(
                                "Edge type {type_id} in population {} has non-string model",
                                edge_population.name
                            );
                        }
                    } else {
                        None
                    };

                    let tgt_pos = if let Some(ds) = edge_group.custom.get("afferent_swc_pos") {
                        ds[group_index]
                    } else if let Some(s) = ty.attributes.get("afferent_swc_pos") {
                        if let Attribute::Float(s) = s {
                            *s
                        } else {
                            bail!(
                                    "Edge type {type_id} in population {} has non-numeric segment position",
                                    edge_population.name
                                );
                        }
                    } else {
                        0.5 // default to centering
                    };

                    let tgt_id = if let Some(ds) = edge_group.custom.get("afferent_swc_id") {
                        ds[group_index]
                    } else if let Some(s) = ty.attributes.get("afferent_swc_id") {
                        if let Attribute::Float(s) = s {
                            *s
                        } else {
                            bail!(
                                    "Edge type {type_id} in population {} has non-numeric segment position",
                                    edge_population.name
                                );
                        }
                    } else {
                        0.0 // default to first (=soma?)
                    } as u64;

                    let src_pos = if let Some(ds) = edge_group.custom.get("efferent_swc_pos") {
                        ds[group_index]
                    } else if let Some(s) = ty.attributes.get("efferent_swc_pos") {
                        if let Attribute::Float(s) = s {
                            *s
                        } else {
                            bail!(
                                    "Edge type {type_id} in population {} has non-numeric segment position",
                                    edge_population.name
                                );
                        }
                    } else {
                        0.5 // default to centering
                    };

                    let src_id = if let Some(id) = edge_group.custom.get("efferent_swc_id") {
                        bail!("[UNSUPPORTED] Edge type {type_id} in population {} has efferent id {id:?}", edge_population.name);
                        // ds[group_index]
                    } else if let Some(id) = ty.attributes.get("efferent_swc_id") {
                        bail!("[UNSUPPORTED] Edge type {type_id} in population {} has efferent SWC id {id:?}", edge_population.name);
                        // if let Attribute::Float(s) = s {
                        // *s
                        // } else {
                        // bail!(
                        // "Edge type {type_id} in population {} has non-numeric segment position",
                        // edge_population.name
                        // );
                        // }
                    } else {
                        0.0 // default to first (=soma?)
                    } as u64;

                    // Construct dynamics parameters by merging the type level defaults with the group level overrides.
                    let mut dynamics = ty.dynamics.clone();
                    for (k, vs) in &edge_group.dynamics {
                        let v = vs[group_index];
                        dynamics.insert(k.to_string(), v);
                    }

                    incoming_edges.push(Edge {
                        src_gid,
                        source: (src_id, src_pos),
                        target: (tgt_id, tgt_pos),
                        mech,
                        delay,
                        weight,
                        dynamics,
                    });
                }
            }
        }
        Ok(incoming_edges)
    }

    pub fn reify_node(&self, gid: usize) -> Result<Node> {
        let (node_pop_idx, node_id) = self
            .gid_to_node_population
            .get(gid)
            .ok_or_else(|| anyhow!("Unknown gid {gid}, must be in [0, {})", self.size))?;
        let node_pop_id = self.node_populations.get(*node_pop_idx).expect("");
        let node_list = &self.node_lists[node_pop_id.list_index];
        let node_population = &node_list.populations[node_pop_id.pop_index];
        let node_index = gid - node_pop_id.start;

        // store pre-built error for later
        let node_index_error = || {
            anyhow!(
                "Index {} overruns size {} of population {}",
                node_index,
                node_pop_id.size,
                node_population.name
            )
        };
        let node_group_id = *node_population
            .group_ids
            .get(node_index)
            .ok_or_else(node_index_error)?;
        let node_type_id = *node_population
            .type_ids
            .get(node_index)
            .ok_or_else(node_index_error)?;
        let group_index = *node_population
            .group_indices
            .get(node_index)
            .ok_or_else(node_index_error)?;
        let node_type = node_list
            .types
            .iter()
            .find(|ty| ty.type_id == node_type_id)
            .ok_or_else(|| {
                anyhow!(
                    "Couldn't find node type {node_type_id} in population {}",
                    node_population.name
                )
            })?
            .clone();
        let group = node_population
            .groups
            .iter()
            .find(|g| g.id == node_group_id)
            .ok_or_else(|| {
                anyhow!(
                    "Couldn't find group id {node_group_id} node population {}",
                    node_population.name
                )
            })?;

        let mut dynamics = node_type.dynamics.clone();
        for (k, vs) in &group.dynamics {
            dynamics.insert(k.to_string(), vs[group_index]);
        }

        let mut custom = Map::new();
        for (k, v) in node_type.attributes() {
            if let Attribute::Float(v) = v {
                custom.insert(k.to_string(), *v);
            }
        }
        for (k, vs) in group.custom.iter() {
            custom.insert(k.to_string(), vs[group_index]);
        }

        let incoming_edges = self.reify_edges(&node_population.name, *node_id)?;

        Ok(Node {
            gid,
            pop: node_population.name.clone(),
            group_id: node_group_id,
            node_id: node_index as u64,
            group_index,
            node_type_id,
            node_type,
            incoming_edges,
            dynamics,
            custom,
        })
    }
}