How does 3d slicer calculate the segmentation volume of dicom image?

hello,I have a project that requires a lot of calculations about the segmentation volume of dicom image.Firstly I tried to use python’s simpleitk library to calculate it,My code looks roughly like this:
file = sitk.ReadImage(f’{path}{file}')
spacing = file.GetSpacing()
file = sitk.GetArrayFromImage(file)
file=np.array(file)
file = file.astype(int)
#print(spacing)
sum=file.sum()
#print(sum)
v=sum*spacing[0]*spacing[1]*spacing[2]/1000
print(v)
But the volume calculated in this way is not the same as the volume calculated by slicer,Where is my code going wrong? Or is there where I can see the source code for slicer to do this?thanks

Segment statistics volume in Slicer can compute volume from binary labelmap and closed surface representations. Probably you are interested in the binary labelmap representation, from that the volume for each segment is computed by multiplying the number of voxels in the segment by the voxel size.

The error in your code is that the sum of label values is used instead count the number of non-zero voxels (or number of voxels that have a certain label value). I would also recommend to include the unit name in the variable names (e.g., spacing_mm, volume_mm3 or volume_cm3) to make the code more readable, safer, and easier to explain magic numbers, such as the 1000. Using file for so many unrelated this is quite confusing, too.

thanks for you answer