首页 > 解决方案 > KeyError:使用 fcswrite 库时出现“'FCS 格式'”

问题描述

我正在尝试使用 fcs write 创建一个新的 .fcs 文件,但是出现错误。我不确定这个错误对我的代码意味着什么,因为我没有使用错误源自的密钥。

我的代码

import os
import FlowCytometryTools
from FlowCytometryTools import FCMeasurement
import fcswrite

# ---- Set dataDirectory equal to the directory that contains the files you wish to remove PHI ---
dataDirectory = r'C:\Users\hghas\OneDrive\Desktop\Demonstration'
os.chdir(dataDirectory)

for i, file in enumerate(os.listdir()):
    # Loading file
    sample = FCMeasurement(ID=f'Test Sample #{i+1}', datafile=file)
    # Removing PHI
    sample.meta['@SAMPLEID1'] = f"Study #{i + 1}"
    mrn, hNumber = sample.meta['@SAMPLEID3'].split()
    sample.meta['@SAMPLEID3'] = hNumber
    sample.meta['@SAMPLEID4'] = None
    temp = sample.get_meta()
    temp['@SAMPLEID3']="Hello"
    #sample.set_meta(temp)
    print(sample.meta['@SAMPLEID3'])
    measurementData = sample.get_data()
    channelNames = list(sample.channel_names)
    fcswrite.write_fcs(r'C:\Users\hghas\OneDrive\Desktop\Demonstration\test.fcs', channelNames, measurementData, text_kw_pr=temp)
    # Renaming the files
    newName = "{} {} {}{}".format(sample.meta['@SAMPLEID1'], sample.meta['@SAMPLEID2'], sample.meta['@SAMPLEID3'], ".fcs")
    os.rename(file, newName)

错误

Traceback (most recent call last):
  File "C:\Users\hghas\PycharmProjects\PHI_Removal_Sunnybrook\main.py", line 29, in <module>
    fcswrite.write_fcs(r'C:\Users\hghas\OneDrive\Desktop\Demonstration\test.fcs', channelNames, measurementData, text_kw_pr=temp)
  File "C:\Users\hghas\anaconda3\envs\testing\lib\site-packages\fcswrite\fcswrite.py", line 172, in write_fcs
    TEXT = TEXT.format(data_start_byte=data_start_byte,
KeyError: "'FCS format'

来自 fcswrite 的导致错误的代码块

    text_padding = 47  # for visual separation and safety
    data_start_byte = header_size + len(TEXT) + text_padding
    data_end_byte = data_start_byte + len(DATA) - 1
    TEXT = TEXT.format(data_start_byte=data_start_byte,
                       data_end_byte=data_end_byte)

标签: pythonkeyerror

解决方案


推荐阅读