Skip to content

Commit

Permalink
feat: update read metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Sep 5, 2023
1 parent a8aef77 commit e0e21b0
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 33 deletions.
10 changes: 5 additions & 5 deletions chem_spectra/lib/composer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def coupling_string(js):


class BaseComposer:
def __init__(self, core, auto_metadata=None):
def __init__(self, core):
self.core = core
self.title = None
self.meta = None
Expand All @@ -43,7 +43,6 @@ def __init__(self, core, auto_metadata=None):
self.refArea = 1
self.refShift = 0
self.prepare_itg_mpy()
self.auto_metadata = auto_metadata

def __header_pk_common(self, category):
return [
Expand Down Expand Up @@ -86,6 +85,7 @@ def __header_auto_metadata(self):
return [
'\n',
TEXT_AUTO_METADATA,
'##$CSAUTOMETADATA=\n'
]

def gen_headers_root(self):
Expand Down Expand Up @@ -294,10 +294,10 @@ def gen_simulation_info(self):

def generate_auto_metadata(self):
content = self.__header_auto_metadata()
if self.auto_metadata is None: return content
if self.core.auto_metadata is None: return content

for key, value in self.auto_metadata.items():
for key, value in self.core.auto_metadata.items():
content.append(
'##{}={}\n'.format(key.upper(), value)
'{}={}\n'.format(key.upper(), value)
)
return content
4 changes: 2 additions & 2 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


class NIComposer(BaseComposer):
def __init__(self, core, auto_metadata=None):
super().__init__(core, auto_metadata)
def __init__(self, core):
super().__init__(core)
self.title = core.fname
self.meta = self.__compose()

Expand Down
11 changes: 8 additions & 3 deletions chem_spectra/lib/converter/bagit/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class BagItBaseConverter:
def __init__(self, target_dir, params=False, fname=''):
self.params = parse_params(params)
if target_dir is None:
self.data, self.images, self.list_csv, self.combined_image, self.auto_metadata = None, None, None, None, None
self.data, self.images, self.list_csv, self.combined_image = None, None, None, None
else:
self.data, self.images, self.list_csv, self.combined_image, self.auto_metadata = self.__read(target_dir, fname)
self.data, self.images, self.list_csv, self.combined_image = self.__read(target_dir, fname)

def __read_metadata(self, target_dir):
metadata_dir_path = os.path.join(target_dir, 'metadata/')
Expand Down Expand Up @@ -61,6 +61,10 @@ def __read(self, target_dir, fname):
for file_name in list_file_names:
jcamp_path = os.path.join(data_dir_path, file_name)
base_cv = JcampBaseConverter(jcamp_path)
metadata = None
if file_name in auto_metadata:
metadata = auto_metadata[file_name]

if base_cv.typ == 'MS':
mscv = JcampMSConverter(base_cv)
mscp = MSComposer(mscv)
Expand All @@ -73,6 +77,7 @@ def __read(self, target_dir, fname):
list_csv.append(tf_csv)
else:
nicv = JcampNIConverter(base_cv)
if nicv.auto_metadata == None: nicv.auto_metadata = metadata
nicp = NIComposer(nicv)
list_composer.append(nicp)
tf_jcamp = nicp.tf_jcamp()
Expand All @@ -84,7 +89,7 @@ def __read(self, target_dir, fname):

combined_image = self.__combine_images(list_composer)

return list_files, list_images, list_csv, combined_image, auto_metadata
return list_files, list_images, list_csv, combined_image

def get_base64_data(self):
if self.data is None:
Expand Down
1 change: 1 addition & 0 deletions chem_spectra/lib/converter/fid/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __set_properties(self):
self.solv_peaks = []
self.is_dept = self.__is_dept()
self.__read_solvent()
self.auto_metadata = None

def __is_em_wave(self):
return self.typ in ['INFRARED', 'RAMAN', 'UVVIS']
Expand Down
13 changes: 13 additions & 0 deletions chem_spectra/lib/converter/jcamp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(self, path, params=False):
self.solv_peaks = []
self.is_dept = self.__is_dept()
self.__read_solvent()
self.auto_metadata = self.__read_auto_metadata()

def __read(self, path):
return ng.jcampdx.read(path, show_all_data=True, read_err='ignore')
Expand Down Expand Up @@ -221,3 +222,15 @@ def __is_dept(self):
return True

return False

def __read_auto_metadata(self):
target = self.dic.get('$CSAUTOMETADATA', [])
if target:
target = [t for t in target[0].split('\n')]
result = {}
for value in target:
splited = value.split('=')
if len(splited) > 1:
result[splited[0]] = splited[1]
return result
return None
1 change: 1 addition & 0 deletions chem_spectra/lib/converter/jcamp/ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, base):
self.fname = base.fname
self.runs, self.spectra, self.auto_scan = self.__read_mz_ml()
self.datatables = self.__set_datatables()
self.auto_metadata = base.auto_metadata

def __set_params(self, params):
exact_mz = params.get('mass', 0) if params else 0
Expand Down
1 change: 1 addition & 0 deletions chem_spectra/lib/converter/jcamp/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def __init__(self, base):
self.__read_integration_from_file()
self.__read_multiplicity_from_file()
self.__read_voltammetry_data_from_file()
self.auto_metadata = base.auto_metadata

def __thres(self):
dt = self.datatype
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/edit/edit_meta_13C-DEPT135
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/edit/edit_meta_1H
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/edit/edit_meta_IR
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/result/im/im_meta_13C-DEPT135
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ $$ === CHEMSPECTRA PEAK TABLE EDIT ===


$$ === CHEMSPECTRA AUTO METADATA ===
##END=
##$CSAUTOMETADATA=
##END=
1 change: 1 addition & 0 deletions tests/fixtures/result/im/im_meta_1H
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/im/im_meta_IR
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/meta_13C-CPD
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/meta_13C-DEPT135
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ $$ === CHEMSPECTRA PEAK TABLE EDIT ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=
1 change: 1 addition & 0 deletions tests/fixtures/result/meta_1H
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/meta_IR
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/meta_SVS-790A_13C
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/result/ps/ps_meta_IR
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ $$ === CHEMSPECTRA PEAK TABLE AUTO ===


$$ === CHEMSPECTRA AUTO METADATA ===
##$CSAUTOMETADATA=
##END=

##END=
Expand Down
7 changes: 5 additions & 2 deletions tests/lib/composer/test_base_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ def test_base_composer_generate_auto_metadata_headers(jcamp_file_1h):
assert headers == [
'\n',
'$$ === CHEMSPECTRA AUTO METADATA ===\n',
'##$CSAUTOMETADATA=\n',
]

def test_base_composer_generate_auto_metadata(jcamp_file_1h):
base_converter = JcampBaseConverter(jcamp_file_1h)
metadata = {"just a string": "just a string value"}
composer = BaseComposer(core=base_converter, auto_metadata=metadata)
base_converter.auto_metadata = metadata
composer = BaseComposer(core=base_converter)
auto_metadata = composer.generate_auto_metadata()
assert auto_metadata == [
'\n',
'$$ === CHEMSPECTRA AUTO METADATA ===\n',
'##JUST A STRING=just a string value\n'
'##$CSAUTOMETADATA=\n',
'JUST A STRING=just a string value\n'
]
11 changes: 7 additions & 4 deletions tests/lib/composer/test_ni_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ def test_init_ni_composer_success(jcamp_file_1h):

def test_init_ni_composer_success_with_auto_metadata(jcamp_file_1h, auto_metadata_dic):
base_converter = JcampBaseConverter(jcamp_file_1h)
base_converter.auto_metadata = auto_metadata_dic
ni_converter = JcampNIConverter(base=base_converter)
ni_composer = NIComposer(core=ni_converter, auto_metadata=auto_metadata_dic)
ni_composer = NIComposer(core=ni_converter)

assert ni_composer is not None
assert ni_composer.auto_metadata == auto_metadata_dic
assert ni_composer.core.auto_metadata == auto_metadata_dic

def test_composer_auto_metadata(jcamp_file_1h, auto_metadata_dic):
base_converter = JcampBaseConverter(jcamp_file_1h)
base_converter.auto_metadata = auto_metadata_dic
ni_converter = JcampNIConverter(base=base_converter)
ni_composer = NIComposer(core=ni_converter, auto_metadata=auto_metadata_dic)
ni_composer = NIComposer(core=ni_converter)

assert ni_composer is not None
assert '$$ === CHEMSPECTRA AUTO METADATA ===\n' in ni_composer.meta
assert '##TEST_KEY=just a string value\n' in ni_composer.meta
assert '##$CSAUTOMETADATA=\n' in ni_composer.meta
assert 'TEST_KEY=just a string value\n' in ni_composer.meta

def test_ni_composer_header(jcamp_file_1h):
base_converter = JcampBaseConverter(jcamp_file_1h)
Expand Down
16 changes: 0 additions & 16 deletions tests/lib/converter/bagit/test_bagit_base_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_bagit_converter_failed():
assert converter.images is None
assert converter.list_csv is None
assert converter.combined_image is None
assert converter.auto_metadata is None

def test_bagit_convert_to_jcamp():
with tempfile.TemporaryDirectory() as td:
Expand Down Expand Up @@ -118,8 +117,6 @@ def test_get_base64_data_succeed():
converter = BagItConveter(td)
list_base64 = converter.get_base64_data()
assert len(list_base64) == 3
for base64Str in list_base64:
assert base64Str.endswith("=")

def test_get_combined_image_failed():
converter = BagItConveter(None)
Expand All @@ -134,16 +131,3 @@ def test_get_combined_image():
converter = BagItConveter(td)
combined_image = converter.combined_image
assertFileType(combined_image, 'image/png')

def test_get_metadata():
with open(aif_auto_metadata_path, 'r') as f:
expected_metadata = json.loads(f.read())

with tempfile.TemporaryDirectory() as td:
with zipfile.ZipFile(aif_layout_path, 'r') as z:
z.extractall(td)

converter = BagItConveter(td)
auto_metadata = converter.auto_metadata
assert auto_metadata is not None
assert auto_metadata == expected_metadata

0 comments on commit e0e21b0

Please sign in to comment.