示例#1
0
 def test_read_excel(self, tmpdir_factory):
     """Test read excel files file."""
     for index, path in enumerate(DATA_EXCEL_STD):
         isotherm = pygaps.isotherm_from_xl(path=path)
         with open(DATA_JSON_STD[index], 'r') as file:
             isotherm2 = pygaps.isotherm_from_json(file.read())
             assert isotherm.to_dict() == isotherm2.to_dict()
示例#2
0
 def test_read_excel_mic(self):
     """Test reading of micromeritics report files."""
     for path in DATA_EXCEL_MIC:
         isotherm = pygaps.isotherm_from_xl(path=path, fmt='mic')
         json_path = path.replace('.xls', '.json')
         with open(json_path, 'r') as file:
             assert isotherm == pygaps.isotherm_from_json(file.read())
示例#3
0
 def test_read_excel_bel(self):
     """Test reading of bel report files."""
     for path in DATA_EXCEL_BEL:
         isotherm = pygaps.isotherm_from_xl(path=path, fmt='bel')
         json_path = path.replace('.xls', '.json')
         with open(json_path, 'r') as file:
             new_iso = pygaps.isotherm_from_json(file.read())
             assert isotherm == new_iso
示例#4
0
    def test_read_create_excel(self, basic_pointisotherm, tmpdir_factory):
        """Test creation/read of point isotherm excel file."""
        path = tmpdir_factory.mktemp('excel').join('regular.xls').strpath

        pygaps.isotherm_to_xl(basic_pointisotherm, path=path)

        isotherm = pygaps.isotherm_from_xl(path)
        assert isotherm == basic_pointisotherm
    def loadImport(self, path, name, iso_type):
        isotherm = None

        if iso_type == 0:  # bel raw
            isotherm = pygaps.isotherm_from_bel(path)
        elif iso_type == 1:  # bel report
            isotherm = pygaps.isotherm_from_xl(path, fmt='bel')
        elif iso_type == 2:  # mic report
            isotherm = pygaps.isotherm_from_xl(path, fmt='mic')
        elif iso_type == 3:  # qnt report
            # TODO implement
            pass
        else:
            raise Exception(f"Could not determine import type '{iso_type}'.")

        if not isotherm:
            return

        self.add_isotherm(name, isotherm)
    def load(self, path, name, ext):
        """Load isotherm from disk."""

        isotherm = None

        if ext == '.csv':
            isotherm = pygaps.isotherm_from_csv(path)
        elif ext == '.json':
            isotherm = pygaps.isotherm_from_json(path)
        elif ext == '.xls' or ext == '.xlsx':
            isotherm = pygaps.isotherm_from_xl(path)
        elif ext == '.aif':
            isotherm = pygaps.isotherm_from_aif(path)
        else:
            raise Exception(f"Unknown isotherm type '{ext}'.")

        if not isotherm:
            return

        self.add_isotherm(name, isotherm)
示例#7
0
 def test_read_excel_bel(self):
     """Test reading of bel report files."""
     for path in DATA_EXCEL_BEL:
         isotherm = pygaps.isotherm_from_xl(path=path, fmt='bel')
         json_path = str(path).replace('.xls', '.json')
         assert isotherm == pygaps.isotherm_from_json(json_path)
示例#8
0
def main():
    """
    The main entrypoint for the cli.

    Currently the CLI can read any pyGAPS format (JSON, CSV, Excel) and then:

        * print isotherm to output (default if no argument is passed)
        * plot the isotherm using a Matplotlib window (``-p/--plot``)
        * run basic automated characterization tests (``-ch/--characterize a_bet``
          for the BET area for example)
        * attempt to model the isotherm using a requested model or guess the best
          fitting model (``-md/--model guess``) and save the resulting isotherm model
          using the ``-o/--outfile`` path.
        * convert the isotherm to any unit/basis
          (``-cv/--convert pressure_mode=absolute,pressure_unit=bar``) and save the
          resulting isotherm model using the ``-o/--outfile`` path.

    """

    # Create the parser
    prs = argparse.ArgumentParser(
        description='Command-line interface to pyGAPS')

    # Add the arguments
    prs.add_argument(
        'iso',
        metavar='isotherm',
        type=pathlib.Path,
        help='isotherm to display or process',
    )
    prs.add_argument(
        '-p',
        '--plot',
        action='store_true',
        help='plot the isotherm',
    )
    prs.add_argument(
        '-ch',
        '--characterize',
        help='run characterization methods (BET area, PSD, etc).',
        choices=['a_bet', 'a_lang', 'kh'],
    )
    prs.add_argument(
        '-md',
        '--model',
        choices=['guess', 'henry', 'langmuir', 'dslangmuir', 'bet'],
        help='model an isotherm, saved as the file specified at \'-o\'',
    )
    prs.add_argument(
        '-cv',
        '--convert',
        help='convert to another unit/basis, savepath using \'-o\'',
    )
    prs.add_argument(
        '-o',
        '--outfile',
        type=pathlib.Path,
        help='output path for a resulting isotherm',
    )
    prs.add_argument(
        '-v',
        '--verbose',
        action='store_true',
        help='increase verbosity',
    )

    # Execute the parse_args() method
    args = prs.parse_args()

    import pygaps as pg

    # Read the isotherm
    if not args.iso.exists():
        raise FileNotFoundError("Path does not exist.")

    ext = args.iso.suffix
    if ext == '.json':
        iso = pg.isotherm_from_json(args.iso)
    elif ext == '.csv':
        iso = pg.isotherm_from_csv(args.iso)
    elif ext == '.xls':
        iso = pg.isotherm_from_xl(args.iso)
    else:
        from pygaps.utilities.exceptions import ParsingError
        raise ParsingError(f"Cannot read '{ext}' files")

    plot = False
    out_iso = None

    # pass to various characterization functions
    if args.characterize:
        plot = args.verbose
        if args.characterize == 'a_bet':
            res = pg.area_BET(iso, verbose=args.verbose)
            print(res)
        elif args.characterize == 'a_lang':
            res = pg.area_langmuir(iso, verbose=args.verbose)
        elif args.characterize == 'kh':
            res = pg.initial_henry_slope(iso, verbose=args.verbose)

    # model isotherm using `model_iso`
    elif args.model:
        plot = args.verbose
        out_iso = pg.model_iso(iso, model=args.model, verbose=args.verbose)

    # convert an isotherm to a different basis/unit
    elif args.convert:
        convert_args = {
            keyval.split('=')[0]: keyval.split('=')[1]
            for keyval in map(str.strip, args.convert.split(','))
        }
        convert_args['verbose'] = args.verbose
        iso.convert(**convert_args)
        out_iso = iso

    # simply plot the isotherm
    elif args.plot:
        iso.plot()
        plot = True

    # default to printing isotherm to stdout
    else:
        print(iso)

    if out_iso:
        if args.outfile:
            ext = args.outfile.suffix
            if ext == '.json':
                out_iso.to_json(args.outfile)
            elif ext == '.csv':
                out_iso.to_csv(args.outfile)
            elif ext == '.xls':
                out_iso.to_xl(args.outfile)
            else:
                from pygaps.utilities.exceptions import ParsingError
                raise ParsingError(f"Cannot export as '{ext}' files")

        if args.verbose:
            print(out_iso)

    if plot:
        import matplotlib.pyplot as plt
        plt.show()