@@ -748,7 +748,10 @@ def get_axis(self, hint: str | int | Axis) -> Axis:
748748 return wt_kit .from_list_of_objects (self .axes , self .axis_expressions , hint )
749749
750750 def norm_for_each (
751- self , var : str | Variable | int , channel : str | Channel | int = 0 , new_channel : dict = {}
751+ self ,
752+ var : str | Variable | int ,
753+ channel : str | Channel | int = 0 ,
754+ new_channel : dict = {},
752755 ):
753756 """normalize the data for each var slice
754757 var array must at least one trivial dimension (or else norm will return an array of ones)
@@ -767,7 +770,12 @@ def norm_for_each(
767770
768771 Examples
769772 --------
770- TODO
773+ import WrightTools.datasets as ds
774+ import WrightTools as wt
775+
776+ d = wt.open(ds.wt5.v1p0p1_MoS2_TrEE_movie)
777+ d.norm_for_each("d2", "ai0") # equivalent to d.ai0[:] /= d.ai0[:].max(axis=(0,1))[None, None, :]
778+
771779 """
772780 variable = self .get_var (var )
773781 channel = self .get_channel (channel )
@@ -779,15 +787,17 @@ def norm_for_each(
779787 )
780788 # nontrivial = tuple({i for i in range(self.ndim)} - trivial)
781789 trivial = tuple (trivial )
782- norm_vals = np .expand_dims (channel [:].max (axis = trivial ), trivial )
790+ norm_vals = np .expand_dims (np .nanmax (channel [:], axis = trivial ), trivial )
791+ new = (channel [:] - channel .null ) / (norm_vals - channel .null )
783792 if new_channel :
784793 self .create_channel (
785794 new_channel .pop ("name" , f"{ channel .natural_name } _{ variable .natural_name } _norm" ),
786- values = channel [:] / norm_vals ,
795+ values = new ,
787796 ** new_channel ,
788797 )
789798 else :
790- channel [:] /= norm_vals
799+ channel [:] = new
800+ channel .null = 0
791801 return
792802
793803 def moment (self , axis , channel = 0 , moment = 1 , * , resultant = None ):
0 commit comments