Skip to content

Commit

Permalink
feat: process DSC layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Jun 12, 2024
1 parent 5a0e33d commit 9360ca3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 34 deletions.
2 changes: 2 additions & 0 deletions chem_spectra/controller/helper/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def extract_params(request):
list_file_names = request.form.getlist('list_file_names[]')
data_type_mapping = request.form.get('data_type_mapping', default='')
detector = request.form.get('detector', default=None)
dsc_meta_data = request.form.get('dsc_meta_data', default=None)

params = {
'peaks_str': request.form.get('peaks_str', default=None),
Expand All @@ -131,6 +132,7 @@ def extract_params(request):
'axesUnits': axesUnits,
'data_type_mapping': data_type_mapping,
'detector': detector,
'dsc_meta_data': dsc_meta_data,
}
has_params = (
params.get('peaks_str') or
Expand Down
74 changes: 43 additions & 31 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ def __gen_header_sec(self):
result.append(key_str)
return result

def __gen_header_user_input_meta_data(self):
if self.core.is_dsc:
dsc_meta_data = self.core.params.get('dsc_meta_data', None)
melting_point, tg_value = '', ''
if dsc_meta_data is not None:
melting_point = dsc_meta_data.get('meltingPoint', '')
tg_value = dsc_meta_data.get('tg', '')
else:
melting_point_arr = self.core.dic.get('MELTINGPOINT', [''])
tg_value_arr = self.core.dic.get('TG', [''])
melting_point = melting_point_arr[0]
tg_value = tg_value_arr[0]
return [
f'##MELTINGPOINT={melting_point}\n',
f'##TG={tg_value}\n'
]
return []

def __get_xy_of_peak(self, peak):
if peak is None:
return '', ''
Expand Down Expand Up @@ -213,6 +231,7 @@ def __compose(self):
meta.extend(self.__gen_headers_spectrum_orig())
if self.core.is_sec:
meta.extend(self.__gen_header_sec())
meta.extend(self.__gen_header_user_input_meta_data())
meta.extend(self.gen_spectrum_orig())
meta.extend(self.__gen_headers_im())
meta.extend(self.__gen_headers_integration())
Expand Down Expand Up @@ -242,35 +261,12 @@ def __compose(self):
return meta

def __plt_nbins(self):
typ = self.core.typ
if 'NMR' == typ:
return 20
elif 'INFRARED' == typ:
return 20
elif 'RAMAN' == typ:
return 20
elif 'UVVIS' == typ:
return 20
elif 'THERMOGRAVIMETRIC ANALYSIS' == typ:
return 20
elif 'MS' == typ:
return 20
return 20

def __fakto(self):
typ = self.core.typ
if 'NMR' == typ:
return 1
elif 'MS' == typ:
return 1
elif 'INFRARED' == typ:
if 'INFRARED' == typ:
return -1
elif 'RAMAN' == typ:
return 1
elif 'UVVIS' == typ:
return 1
elif 'THERMOGRAVIMETRIC ANALYSIS' == typ:
return 1
return 1

def tf_img(self):
Expand Down Expand Up @@ -534,16 +530,32 @@ def tf_img(self):


def __generate_info_box(self, plotlib):
if not self.core.is_sec:
if not (self.core.is_sec or self.core.is_dsc):
return
core_dic = self.core.dic
sec_data_key = ['MN', 'MW', 'MP', 'D']
result = []
for key in sec_data_key:
dic_value = core_dic.get(key, [])
key_str = f"{key}={dic_value[0]}" if len(dic_value) > 0 else None
if key_str is not None:
result.append(key_str)
if self.core.is_sec:
sec_data_key = ['MN', 'MW', 'MP', 'D']
for key in sec_data_key:
dic_value = core_dic.get(key, [])
key_str = f"{key}={dic_value[0]}" if len(dic_value) > 0 else None
if key_str is not None:
result.append(key_str)
else:
dsc_meta_data = self.core.params.get('dsc_meta_data', None)
melting_point, tg_value = '', ''
if dsc_meta_data is not None:
melting_point = dsc_meta_data.get('meltingPoint', '')
tg_value = dsc_meta_data.get('tg', '')
else:
melting_point_arr = self.core.dic.get('MELTINGPOINT', [''])
tg_value_arr = self.core.dic.get('TG', [''])
melting_point = melting_point_arr[0]
tg_value = tg_value_arr[0]

melting_point_str = f"MELTING POINT={melting_point}"
tg_str = f"TG={tg_value}"
result.extend([melting_point_str, tg_str])

info_str = '\n'.join(result)
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
Expand Down
4 changes: 4 additions & 0 deletions chem_spectra/lib/converter/jcamp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, path, params=False):
self.is_emissions = self.__is_emissions()
self.is_dls_acf = self.__is_dls_acf()
self.is_dls_intensity = self.__is_dls_intensity()
self.is_dsc = self.__is_dsc()
self.non_nmr = self.__non_nmr()
self.ncl = self.__ncl()
self.simu_peaks = self.__read_simu_peaks()
Expand Down Expand Up @@ -156,6 +157,9 @@ def __is_dls_acf(self):

def __is_dls_intensity(self):
return self.typ in ['DLS intensity']

def __is_dsc(self):
return self.typ in ['DIFFERENTIAL SCANNING CALORIMETRY']

def __ncl(self):
try:
Expand Down
3 changes: 2 additions & 1 deletion chem_spectra/lib/converter/jcamp/data_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"SORPTION-DESORPTION MEASUREMENT": ["SORPTION-DESORPTION MEASUREMENT"],
"Emissions": ["Emissions", "EMISSIONS", "FLUORESCENCE SPECTRUM", "FL SPECTRUM"],
"DLS ACF": ["DLS ACF"],
"DLS intensity": ["DLS INTENSITY", "DLS intensity"]
"DLS intensity": ["DLS INTENSITY", "DLS intensity"],
"DIFFERENTIAL SCANNING CALORIMETRY": ["DIFFERENTIAL SCANNING CALORIMETRY"]
}
}
3 changes: 2 additions & 1 deletion chem_spectra/lib/converter/jcamp/data_type.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"SORPTION-DESORPTION MEASUREMENT": ["SORPTION-DESORPTION MEASUREMENT"],
"Emissions": ["Emissions", "EMISSIONS", "FLUORESCENCE SPECTRUM", "FL SPECTRUM"],
"DLS ACF": ["DLS ACF"],
"DLS intensity": ["DLS INTENSITY", "DLS intensity"]
"DLS intensity": ["DLS INTENSITY", "DLS intensity"],
"DIFFERENTIAL SCANNING CALORIMETRY": ["DIFFERENTIAL SCANNING CALORIMETRY"]
}
}
4 changes: 3 additions & 1 deletion chem_spectra/lib/converter/jcamp/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, base):
self.is_emissions = base.is_emissions if hasattr(base, 'is_emissions') else False
self.is_dls_acf = base.is_dls_acf if hasattr(base, 'is_dls_acf') else False
self.is_dls_intensity = base.is_dls_intensity if hasattr(base, 'is_dls_intensity') else False
self.is_dsc = base.is_dsc if hasattr(base, 'is_dsc') else False
self.non_nmr = base.non_nmr
self.ncl = base.ncl
self.is_dept = base.is_dept
Expand Down Expand Up @@ -98,7 +99,8 @@ def __thres(self):
"CYCLIC VOLTAMMETRY": THRESHOLD_XRD,
"SORPTION-DESORPTION MEASUREMENT": THRESHOLD_XRD,
"DLS intensity": THRESHOLD_XRD,
"Emissions": THRESHOLD_EMISSION
"Emissions": THRESHOLD_EMISSION,
"DIFFERENTIAL SCANNING CALORIMETRY": THRESHOLD_TGA,
}

if self.params.get('user_data_type_mapping'):
Expand Down
4 changes: 4 additions & 0 deletions chem_spectra/lib/converter/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def parse_params(params):
'jcamp_idx': 0,
'axesUnits': None,
'detector': None,
'dsc_meta_data': None,
}

select_x = params.get('select_x', None)
Expand Down Expand Up @@ -68,6 +69,8 @@ def parse_params(params):
user_data_type_mapping = params.get('data_type_mapping')
detector = params.get('detector')
detector = json.loads(detector) if detector else None
dsc_meta_data = params.get('dsc_meta_data')
dsc_meta_data = json.loads(dsc_meta_data) if dsc_meta_data else None
if (cyclicvolta is not None):
spectraList = cyclicvolta['spectraList']
if (len(spectraList) > 0):
Expand Down Expand Up @@ -101,6 +104,7 @@ def parse_params(params):
'axesUnits': axesUnits,
'user_data_type_mapping': user_data_type_mapping,
'detector': detector,
'dsc_meta_data': dsc_meta_data,
}


Expand Down
Binary file added tests/fixtures/source/bagit/dsc/dsc.zip
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/lib/converter/bagit/test_bagit_base_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
emissions_layout_path = target_dir + 'emissions/emissions.zip'
dls_acf_layout_path = target_dir + 'dls_acf/dls_acf.zip'
dls_intensity_layout_path = target_dir + 'dls_intensity/dls_intensity.zip'
dsc_layout_path = target_dir + 'dsc/dsc.zip'

def assertFileType(file, mimeStr):
assert mimetypes.guess_type(file.name)[0] == mimeStr
Expand Down Expand Up @@ -144,3 +145,12 @@ def test_bagit_has_one_file_no_combined_image():

converter = BagItConveter(td)
assert converter.combined_image is None

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

converter = BagItConveter(td)
jcamp = converter.data[0]
assertJcampContent(jcamp, '##DATA TYPE=DIFFERENTIAL SCANNING CALORIMETRY')
6 changes: 6 additions & 0 deletions tests/lib/converter/test_share.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def expected_default_params():
'jcamp_idx': 0,
'axesUnits': None,
'detector': None,
'dsc_meta_data': None,
}

def test_parse_params_without_params(expected_default_params):
Expand Down Expand Up @@ -222,6 +223,11 @@ def test_parse_params_detector():
def test_parse_solvent():
#TODO: need to be updated
assert 1==1

def test_parse_dsc_meta_data():
params = {'dsc_meta_data': '{"meltingPoint": "1.0", "tg": "1.0"}'}
parsed_data = parse_params(params)
assert parsed_data['dsc_meta_data'] == {"meltingPoint": "1.0", "tg": "1.0"}

def test_reduce_pts_when_does_not_have_any_x():
array_data = []
Expand Down

0 comments on commit 9360ca3

Please sign in to comment.