From 8eab560bd3e8a2d63c22022df523136e19925d15 Mon Sep 17 00:00:00 2001
From: fsdavid <daviti1@mail.com>
Date: Wed, 18 May 2022 12:20:50 +0200
Subject: [PATCH] Add typing on connectivity component

---
 src/atlasComponents/sapi/sapi.service.ts      |  8 ++---
 src/atlasComponents/sapi/type.ts              |  6 ++++
 .../connectivityBrowser.component.ts          | 25 ++++++++------
 .../connectivity/hasConnectivity.directive.ts | 34 +++++++++++++------
 4 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/src/atlasComponents/sapi/sapi.service.ts b/src/atlasComponents/sapi/sapi.service.ts
index 3ce34e05f..198b9e304 100644
--- a/src/atlasComponents/sapi/sapi.service.ts
+++ b/src/atlasComponents/sapi/sapi.service.ts
@@ -1,9 +1,9 @@
 import { Injectable } from "@angular/core";
 import { HttpClient } from '@angular/common/http';
-import { map, shareReplay, tap } from "rxjs/operators";
+import { map, shareReplay } from "rxjs/operators";
 import { SAPIAtlas, SAPISpace } from './core'
 import {
-  SapiAtlasModel,
+  SapiAtlasModel, SapiModalityModel,
   SapiParcellationModel,
   SapiQueryPriorityArg,
   SapiRegionalFeatureModel,
@@ -90,8 +90,8 @@ export class SAPI{
     return reg.getFeatures(spaceId)
   }
 
-  getModalities() {
-    return this.http.get(`${SAPI.bsEndpoint}/modalities`)
+  getModalities(): Observable<SapiModalityModel[]> {
+    return this.http.get<SapiModalityModel[]>(`${SAPI.bsEndpoint}/modalities`)
   }
 
   httpGet<T>(url: string, params?: Record<string, string>, sapiParam?: SapiQueryPriorityArg){
diff --git a/src/atlasComponents/sapi/type.ts b/src/atlasComponents/sapi/type.ts
index 687a916f5..905c0dee7 100644
--- a/src/atlasComponents/sapi/type.ts
+++ b/src/atlasComponents/sapi/type.ts
@@ -7,6 +7,11 @@ export type IdName = {
   name: string
 }
 
+export type SapiModalityModel = {
+  name: string
+  types: string[]
+}
+
 type Point = [number, number, number]
 
 export type BoundingBoxConcept = [Point, Point]
@@ -30,6 +35,7 @@ export type SapiVolumeModel = components["schemas"]["VolumeModel"]
 export type SapiDatasetModel = components["schemas"]["DatasetJsonModel"]
 export type SpyNpArrayDataModel = components["schemas"]["NpArrayDataModel"]
 export type SapiIeegSessionModel = components["schemas"]["IEEGSessionModel"]
+
 /**
  * utility types
  */
diff --git a/src/atlasComponents/sapiViews/features/connectivity/connectivityBrowser/connectivityBrowser.component.ts b/src/atlasComponents/sapiViews/features/connectivity/connectivityBrowser/connectivityBrowser.component.ts
index df1f74668..a54cb97de 100644
--- a/src/atlasComponents/sapiViews/features/connectivity/connectivityBrowser/connectivityBrowser.component.ts
+++ b/src/atlasComponents/sapiViews/features/connectivity/connectivityBrowser/connectivityBrowser.component.ts
@@ -5,7 +5,7 @@ import {catchError, take} from "rxjs/operators";
 import {SAPI, SapiAtlasModel, SapiParcellationModel, SapiRegionModel} from "src/atlasComponents/sapi";
 import { atlasAppearance, atlasSelection } from "src/state";
 import {PARSE_TYPEDARRAY} from "src/atlasComponents/sapi/sapi.service";
-import {SapiParcellationFeatureMatrixModel} from "src/atlasComponents/sapi/type";
+import {SapiModalityModel, SapiParcellationFeatureMatrixModel} from "src/atlasComponents/sapi/type";
 import { of } from "rxjs";
 import {CustomLayer} from "src/state/atlasAppearance";
 
@@ -53,7 +53,7 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
 
     }
 
-    @Input() types: any[] = []
+    @Input() types: SapiModalityModel[] = []
 
     private _defaultProfile
     @Input() set defaultProfile(val: any) {
@@ -84,14 +84,12 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
       this.regionName = newRegionName
     }
 
-    public atlasId: any
     public regionName: string
     public regionHemisphere: string = null
     public selectedDataset: any
     public connectionsString: string
-    public logConnectionsString: string
-    public pureConnections: any
-    public connectedAreas: BehaviorSubject<any[]> = new BehaviorSubject([])
+    public pureConnections: { [key: string]: number }
+    public connectedAreas: BehaviorSubject<ConnectedArea[]> = new BehaviorSubject([])
     public noConnectivityForRegion: boolean
     private subscriptions: Subscription[] = []
     public allRegions = []
@@ -112,7 +110,7 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
     @ViewChild('fullConnectivityGrid') public fullConnectivityGridElement: ElementRef<any>
 
     constructor(
-        private store$: Store<any>,
+        private store$: Store,
         private sapi: SAPI,
         private changeDetectionRef: ChangeDetectorRef,
     ) {}
@@ -265,7 +263,7 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
     }
 
     
-    changeLog(checked) {
+    changeLog(checked: boolean) {
       this.logChecked = checked
       this.connectedAreas.next(this.formatConnections(this.pureConnections))
       this.connectivityComponentElement.nativeElement.toggleShowLog()
@@ -281,7 +279,7 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
       )
     }
 
-    getRegionWithName(region) {
+    getRegionWithName(region: string) {
       return this.allRegions.find(r => r.name === region)
     }
 
@@ -299,7 +297,7 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
       this.subscriptions.forEach(s => s.unsubscribe())
     }
 
-    private formatConnections = (areas) => {
+    private formatConnections(areas: { [key: string]: number }) {
       const cleanedObj = Object.keys(areas)
         .map(key => ({name: key, numberOfConnections: areas[key]}))
         .filter(f => f.numberOfConnections > 0)
@@ -326,3 +324,10 @@ export class ConnectivityBrowserComponent implements AfterViewInit, OnDestroy {
     private colormap_blue = x => x < 0.3? this.clamp(4.0 * x + 0.5) : this.clamp(-4.0 * x + 2.5)
 
 }
+
+export type ConnectedArea = {
+    color: {r: number, g: number, b: number}
+    name: string
+    numberOfConnections: number
+}
+
diff --git a/src/atlasComponents/sapiViews/features/connectivity/hasConnectivity.directive.ts b/src/atlasComponents/sapiViews/features/connectivity/hasConnectivity.directive.ts
index 75b5edaad..e0dbdcbde 100644
--- a/src/atlasComponents/sapiViews/features/connectivity/hasConnectivity.directive.ts
+++ b/src/atlasComponents/sapiViews/features/connectivity/hasConnectivity.directive.ts
@@ -3,7 +3,12 @@ import {from, of, Subscription} from "rxjs";
 import {map, switchMap, take} from "rxjs/operators";
 import {HttpClient} from "@angular/common/http";
 import {PARSE_TYPEDARRAY, SAPI} from "src/atlasComponents/sapi/sapi.service";
-import {SapiAtlasModel, SapiParcellationFeatureMatrixModel, SapiParcellationModel} from "src/atlasComponents/sapi/type";
+import {
+    SapiAtlasModel, SapiModalityModel,
+    SapiParcellationFeatureMatrixModel, SapiParcellationFeatureModel,
+    SapiParcellationModel,
+    SapiRegionModel
+} from "src/atlasComponents/sapi/type";
 
 @Directive({
   selector: '[sxplr-sapiviews-features-connectivity-check]',
@@ -20,10 +25,10 @@ export class HasConnectivity implements OnDestroy {
     @Input('sxplr-sapiviews-features-connectivity-check-parcellation')
     parcellation: SapiParcellationModel
 
-    private _region: any
+    private _region: SapiRegionModel
 
     @Input()
-    set region(val) {
+    set region(val: SapiRegionModel) {
       this._region = val
       if (val) {
         if (!this.connectivityModalities.length) {
@@ -44,15 +49,15 @@ export class HasConnectivity implements OnDestroy {
     public hasConnectivity = false
     public connectivityNumber = 0
 
-    private connectivityModalities: any[] = []
+    private connectivityModalities: SapiModalityModel[] = []
     private waitForModalities = false
-    public defaultProfile: any
-    public availableModalities: string[] = []
+    public defaultProfile: DefaultProfile
+    public availableModalities: SapiModalityModel[] = []
 
     constructor(private httpClient: HttpClient,
                 private sapi: SAPI) {
       this.sapi.getModalities()
-        .pipe(map((mod: any[]) => mod.filter((m: any) => m.types && m.types.find(t => t.includes('siibra/features/connectivity')))))
+        .pipe(map((mod: SapiModalityModel[]) => mod.filter((m: SapiModalityModel) => m.types && m.types.find(t => t.includes('siibra/features/connectivity')))))
         .subscribe(modalities => {
           this.connectivityModalities = modalities
           if (this.waitForModalities) {
@@ -63,7 +68,6 @@ export class HasConnectivity implements OnDestroy {
     }
 
     private checkConnectivity() {
-      this.defaultProfile = {}
       if (this.region.name) {
         this.connectivityModalities.forEach(m => {
           const type = m.types[0]
@@ -72,7 +76,8 @@ export class HasConnectivity implements OnDestroy {
             .getFeatures({type, page: 1, size: 1})
             .pipe(
               take(1),
-              switchMap((res: any) => {
+              // ToDo remove any type when `SapiParcellationFeatureModel` will be fixed
+              switchMap((res: SapiParcellationFeatureModel[] | any) => {
                 if (res && res.items) {
 
                   this.availableModalities.push(m)
@@ -80,7 +85,7 @@ export class HasConnectivity implements OnDestroy {
 
                   if (firstDataset) {
                     this.hasConnectivity = true
-                    if (!(this.defaultProfile && this.defaultProfile.length)) {
+                    if (!(this.defaultProfile)) {
                       return this.sapi.getParcellation(this.atlas["@id"], this.parcellation["@id"])
                         .getFeatureInstance(firstDataset['@id'])
                         .pipe(switchMap(inst => {
@@ -91,6 +96,7 @@ export class HasConnectivity implements OnDestroy {
                               matrix: inst,
                               numberOfDatasets: res.total
                             }
+
                             const matrixData = inst as SapiParcellationFeatureMatrixModel
                             this.regionIndex = (matrixData.columns as Array<string>).findIndex(md => md === this.region.name)
                             return from(this.sapi.processNpArrayData<PARSE_TYPEDARRAY.RAW_ARRAY>(matrixData.matrix, PARSE_TYPEDARRAY.RAW_ARRAY))
@@ -118,5 +124,11 @@ export class HasConnectivity implements OnDestroy {
     ngOnDestroy(){
       while (this.subscriptions.length > 0) this.subscriptions.pop().unsubscribe()
     }
-
 }
+
+type DefaultProfile = {
+    type: string
+    selectedDataset: SapiParcellationFeatureModel
+    matrix: SapiParcellationFeatureModel
+    numberOfDatasets: number
+}
\ No newline at end of file
-- 
GitLab