diff --git a/src/caosadvancedtools/scifolder/result_table_cfood.py b/src/caosadvancedtools/scifolder/result_table_cfood.py new file mode 100644 index 0000000000000000000000000000000000000000..edd65b309f0ae927949e3e7b6a3d0b3aa91d5f65 --- /dev/null +++ b/src/caosadvancedtools/scifolder/result_table_cfood.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# encoding: utf-8 +# +# Copyright (C) 2019 Henrik tom Wörden +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +import caosdb as db +import pandas as pd +from caosadvancedtools.cfood import (AbstractFileCFood, assure_has_description, + assure_has_parent, assure_has_property, + assure_object_is_in_list, get_entity) +from caosadvancedtools.read_md_header import get_header + +from ..cfood import assure_property_is, fileguide +from .experiment_cfood import ExperimentCFood +from .generic_pattern import date_pattern, date_suffix_pattern, project_pattern +from .utils import parse_responsibles, reference_records_corresponding_to_files +from .withreadme import DATAMODEL as dm +from .withreadme import RESULTS, REVISIONOF, SCRIPTS, WithREADME, get_glob + + +# TODO similarities with TableCrawler +class ResultTableCFood(AbstractFileCFood): + + # win_paths can be used to define fields that will contain windows style + # path instead of the default unix ones. Possible fields are: + # ["results", "revisionOf"] + win_paths = [] + table_re = r"result_table_(?P<recordtype>.*).csv$" + property_name_re = re.compile(r"^(?P<pname>.+?)\s*(\[\s?(?P<unit>.*?)\s?\] *)?$") + + @staticmethod + def name_beautifier(x): return x + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.table = pd.read_csv(fileguide.access(self.crawled_path)) + + @staticmethod + def get_re(): + return (".*/ExperimentalData/"+project_pattern + date_pattern + + date_suffix_pattern + ResultTableCFood.table_re) + + def create_identifiables(self): + self.recs = [] + self.experiment, self.project = ( + ExperimentCFood.create_identifiable_experiment(self.match)) + + for idx, row in self.table.iterrows(): + rec = db.Record() + rec.add_parent(self.match.group("recordtype")) + + for col in self.table.columns[:2]: + match = ResultTableCFood.match(col) + + if match.group("unit"): + rec.add_property(match.group("pname"), row.loc[col], unit=match.group("unit")) + else: + rec.add_property(match.group("pname"), row.loc[col]) + self.identifiables.append(rec) + self.recs.append(rec) + + self.identifiables.extend([self.experiment, self.project]) + + def update_identifiables(self): + for ii, (idx, row) in enumerate(self.table.iterrows()): + for col in row.index: + assure_property_is(self.recs[ii], col, row.loc[col], to_be_updated=self.to_be_updated) + assure_property_is(self.experiment, self.match.group("recordtype"), + self.recs, to_be_updated=self.to_be_updated) diff --git a/unittests/test.csv b/unittests/test.csv new file mode 100644 index 0000000000000000000000000000000000000000..f8ec95d33f7185960abfaa63284effc531bd8573 --- /dev/null +++ b/unittests/test.csv @@ -0,0 +1,3 @@ +demperature [°C] ,depth [m] +234.4,3.0 +344.6,5.1 diff --git a/unittests/test_result_table_cfood.py b/unittests/test_result_table_cfood.py new file mode 100644 index 0000000000000000000000000000000000000000..3341a2394cc9ef15ae172bb8992445d87c60d063 --- /dev/null +++ b/unittests/test_result_table_cfood.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +# encoding: utf-8 +# +# ** header v3.0 +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2018 Research Group Biomedical Physics, +# Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +# ** end header +# + +""" +test module for ResultTableCFood +""" + + +import os +import re +import unittest + +import caosdb as db +from caosadvancedtools.scifolder.result_table_cfood import ResultTableCFood + + +class CFoodTest(unittest.TestCase): + def test_re(self): + self.assertIsNotNone(re.match(ResultTableCFood.table_re, "result_table_Hallo.csv")) + self.assertEqual(re.match(ResultTableCFood.table_re, "result_table_Hallo.csv").group("recordtype"), + "Hallo") + self.assertIsNotNone(re.match(ResultTableCFood.table_re, + "result_table_Cool RecordType.csv")) + self.assertEqual(re.match(ResultTableCFood.table_re, "result_table_Cool RecordType.csv").group("recordtype"), + "Cool RecordType") + self.assertIsNone(re.match(ResultTableCFood.table_re, "result_tableCool RecordType.csv")) + + self.assertIsNotNone(re.match(ResultTableCFood.property_name_re, + "temperature [C]")) + self.assertEqual(re.match(ResultTableCFood.property_name_re, + "temperature [C]").group("pname"), + "temperature") + self.assertEqual(re.match(ResultTableCFood.property_name_re, + "temperature [C]").group("unit"), "C") + self.assertEqual(re.match(ResultTableCFood.property_name_re, + "temperature [ C ]").group("unit"), "C") + self.assertEqual(re.match(ResultTableCFood.property_name_re, + "temperature").group("pname"), "temperature") + + def test_ident(self): + rtc = ResultTableCFood(os.path.join(os.path.dirname(__file__), "test.csv")) + rtc.match = re.match(ResultTableCFood.get_re(), + "/ExperimentalData/2010_TestProject/2019-02-03_something/result_table_RT.csv") + rtc.create_identifiables() + rtc.update_identifiables()