#!/usr/bin/env python
# coding: utf-8

import pandas as pd
import numpy as np
import scIB
import warnings
warnings.filterwarnings('ignore')
import argparse
from functools import reduce

if __name__=='__main__':
    """
    Scale LISI scores in the output of merge_metrics.py
    """
    
    parser = argparse.ArgumentParser(description='Scale LISI scores')

    parser.add_argument('-i', '--input', required=True, help='Merged metrics file in csv file format')
    parser.add_argument('-o', '--output', required=True, help='Merged and LISI-scaled metrics file in csv file format')
    
    args = parser.parse_args()
    
    #get input file
    file = args.input
    res = pd.read_csv(file, index_col=0)
    #get scenario info
    scenario= pd.Series([idx.split('/')[0] for idx in res.index], dtype='category')
    
    #scale by scenario
    for scen in scenario.cat.categories:
        scen_idx = res.index[scenario == scen]
        res_tmp = res.loc[scen_idx]
        #select columns with LISI score and scale (assuming scale=True in metrics call)
        #original cLISI in [0,1] with 1 good and 0 bad
        #scale to 1 good and 0 bad
        max_cl = np.max([1,res_tmp['cLISI'].max()]) #take the max of 2 and observed score
        min_cl = np.min([0,res_tmp['cLISI'].min()]) #take the min of 1 and observed score
        res.loc[scen_idx,'cLISI'] = (res.loc[scen_idx,'cLISI']-min_cl)/(max_cl-min_cl) 
        #original iLISI in [0,max] with 0 bad and max good
        max_il = np.max([1,res_tmp['iLISI'].max()])
        min_il = np.min([0,res_tmp['iLISI'].min()])
        #scale to 1 good and 0 bad
        res.loc[scen_idx,'iLISI']=(res.loc[scen_idx,'iLISI']-min_il)/(max_il-min_il)
    
    results = res #do not transpose the file
    #write output file
    results.to_csv(args.output)

@@ -1149,34 +1149,24 @@
    max_cl = 
-np.max(%5B1,
 res_tmp%5B'cLI
@@ -1175,18 +1175,16 @@
 '%5D.max()
-%5D)
  #take t
@@ -1197,14 +1197,8 @@
  of 
-2 and 
 obse
@@ -1221,34 +1221,24 @@
    min_cl = 
-np.min(%5B0,
 res_tmp%5B'cLI
@@ -1247,18 +1247,16 @@
 '%5D.min()
-%5D)
  #take t
@@ -1269,14 +1269,8 @@
  of 
-1 and 
 obse
@@ -1271,32 +1271,228 @@
 f observed score
+%0A        intval_clen = max_cl-min_cl%0A        #divide by 1 if all scores are the same, otherwise divide by max-min%0A        divisor = %5B1 if intval_clen %3C 1e-10 else intval_clen%5D%0A        #scale cLISI
 %0A        res.loc
@@ -1551,24 +1551,15 @@
 cl)/
-(max_cl-min_cl) 
+divisor
 %0A   
@@ -1635,18 +1635,8 @@
 l = 
-np.max(%5B1,
 res_
@@ -1653,18 +1653,16 @@
 '%5D.max()
-%5D)
 %0A       
@@ -1675,18 +1675,8 @@
 l = 
-np.min(%5B0,
 res_
@@ -1697,45 +1697,205 @@
 in()
-%5D)%0A        #scale to 1 good and 0 bad
+%0A        intval_ilen = max_il-min_il%0A        #divide by 1 if all scores are the same, otherwise divide by max-min%0A        divisori = %5B1 if intval_ilen %3C 1e-10 else intval_ilen%5D%0A        #scale iLISI
 %0A   
@@ -1964,23 +1964,16 @@
 il)/
-(max_il-min_il)
+divisori
 %0A   

Scale iLISI and cLISI with min/max