11from math import log
2- from typing import Any , List , Optional , Union
2+ from typing import Any , Optional , Union
33
44import numpy as np
5- import pandas as pd
65from scipy .optimize import curve_fit
76
87from countess import VERSION
@@ -64,7 +63,7 @@ def output_columns(self) -> dict[str, str]:
6463
6564 def prepare (self , source ):
6665 yaxis_prefix = self .columns .get_column_prefix ()
67- suffix_set = set ([ k .removeprefix (yaxis_prefix ) for k in source .columns if k .startswith (yaxis_prefix )])
66+ suffix_set = { k .removeprefix (yaxis_prefix ) for k in source .columns if k .startswith (yaxis_prefix )}
6867
6968 if self .xaxis .is_not_none ():
7069 xaxis_prefix = self .xaxis .get_column_prefix ()
@@ -73,6 +72,8 @@ def prepare(self, source):
7372 self .suffixes = sorted (suffix_set )
7473
7574 def transform (self , data : dict [str , Any ]) -> Optional [dict [str , Any ]]:
75+ assert self .suffixes
76+
7677 if self .xaxis .is_not_none ():
7778 xaxis_prefix = self .xaxis .get_column_prefix ()
7879 x_values = [data .get (xaxis_prefix + s ) for s in self .suffixes ]
@@ -85,17 +86,22 @@ def transform(self, data: dict[str, Any]) -> Optional[dict[str, Any]]:
8586 return None
8687
8788 if self .log :
88- y_values = [log (y + 1 ) for y in y_values ]
89+ y_values = [log (y + 1 ) if y is not None else None for y in y_values ]
8990 if self .normalize :
90- max_y = max (y_values )
91- y_values = [y / max_y for y in y_values ]
91+ max_y = max (y for y in y_values if y is not None )
92+ y_values = [y / max_y if y is not None else None for y in y_values ]
9293
93- x_values , y_values = zip (* [(x , y ) for x , y in zip (x_values , y_values ) if x > 0 or y > 0 ])
94+ x_values , y_values = zip (
95+ * [(x , y ) for x , y in zip (x_values , y_values ) if x is not None and y is not None and (x > 0 or y > 0 )]
96+ )
9497 if len (x_values ) < len (self .suffixes ) / 2 + 1 :
9598 return None
9699
97- s , v = score (x_values , y_values )
98- if self .variance :
99- return {self .output .value : s , self .variance .value : v }
100- else :
101- return {self .output .value : s }
100+ try :
101+ s , v = score (x_values , y_values )
102+ if self .variance :
103+ return {self .output .value : s , self .variance .value : v }
104+ else :
105+ return {self .output .value : s }
106+ except TypeError :
107+ return None
0 commit comments