@@ -1165,19 +1165,34 @@ def calc_core_bytes(self):
11651165
11661166 data_bytes = []
11671167
1168+ # Allow use of custom labels
1169+ label_table = ann_label_table
1170+ if self .custom_labels is not None :
1171+ label_table = pd .concat (
1172+ [label_table , self .custom_labels ], ignore_index = True
1173+ )
1174+
1175+ # Generate typecodes from annotation label table
1176+ typecodes = {
1177+ label_table .iloc [i ]["symbol" ]: label_table .iloc [i ]["label_store" ]
1178+ for i in range (len (label_table ))
1179+ }
1180+
11681181 # Iterate across all fields one index at a time
11691182 for i in range (len (sampdiff )):
11701183
11711184 # Process the samp (difference) and sym items
11721185 data_bytes .append (
1173- field2bytes ("samptype" , [sampdiff [i ], self .symbol [i ]], self .custom_labels )
1186+ field2bytes (
1187+ "samptype" , [sampdiff [i ], self .symbol [i ]], typecodes
1188+ )
11741189 )
11751190
11761191 # Process the extra optional fields
11771192 for field in extra_write_fields :
11781193 value = getattr (compact_annotation , field )[i ]
11791194 if value is not None :
1180- data_bytes .append (field2bytes (field , value , self . custom_labels ))
1195+ data_bytes .append (field2bytes (field , value , typecodes ))
11811196
11821197 # Flatten and convert to correct format
11831198 data_bytes = np .array (
@@ -1600,7 +1615,7 @@ def compact_carry_field(full_field):
16001615 return compact_field
16011616
16021617
1603- def field2bytes (field , value , custom_labels = None ):
1618+ def field2bytes (field , value , typecodes ):
16041619 """
16051620 Convert an annotation field into bytes to write.
16061621
@@ -1610,6 +1625,8 @@ def field2bytes(field, value, custom_labels=None):
16101625 The annotation field of the value to be converted to bytes.
16111626 value : list
16121627 The value to be converted to bytes.
1628+ typecodes : dict
1629+ The mapping between each annotation label an its corresponding typecode.
16131630
16141631 Returns
16151632 -------
@@ -1619,18 +1636,10 @@ def field2bytes(field, value, custom_labels=None):
16191636 """
16201637 data_bytes = []
16211638
1622- # allow use of custom labels
1623- label_table = ann_label_table
1624- if custom_labels is not None :
1625- label_table = pd .concat ([label_table , custom_labels ], ignore_index = True )
1626-
16271639 # samp and sym bytes come together
16281640 if field == "samptype" :
16291641 # Numerical value encoding annotation symbol
1630- typecode = label_table .loc [
1631- label_table ["symbol" ] == value [1 ], "label_store"
1632- ].values [0 ]
1633-
1642+ typecode = typecodes [value [1 ]]
16341643 # sample difference
16351644 sd = value [0 ]
16361645
0 commit comments