Example #1
0
    def test_csv_modelisotherm(self, basic_modelisotherm, tmpdir_factory):
        """Test creation of the ModelIsotherm CSV."""

        path = tmpdir_factory.mktemp('csv').join('isotherm.csv').strpath

        pygaps.isotherm_to_csv(basic_modelisotherm, path)

        isotherm = pygaps.isotherm_from_csv(path)

        assert isotherm.to_dict() == basic_modelisotherm.to_dict()
Example #2
0
    def test_csv_pointisotherm(self, basic_pointisotherm, tmpdir_factory):
        """Tests creation of the regular csv file"""

        path = tmpdir_factory.mktemp('csv').join('isotherm.csv').strpath

        pygaps.isotherm_to_csv(basic_pointisotherm, path)

        isotherm = pygaps.isotherm_from_csv(path)

        assert isotherm == basic_pointisotherm
    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)
Example #4
0
    def test_csv_isotherm_checks(self):
        """Sanity checks for the CSV parser."""

        # basic from string
        text = "material,test\nadsorbate,test\ntemperature,303\n"
        assert pygaps.isotherm_from_csv(text)

        # empty value
        text = "material,test\nadsorbate,test\ntemperature,303\nnew,"
        assert pygaps.isotherm_from_csv(text).new is None

        # multiple value errors
        text = "material,test\nadsorbate,test\ntemperature,303\nnew,1,2"
        with pytest.raises(ParsingError):
            pygaps.isotherm_from_csv(text)

        # bool in csv
        text = "material,test\nadsorbate,test\ntemperature,303\nnew,True"
        assert pygaps.isotherm_from_csv(text).new is True

        # list in csv
        text = "material,test\nadsorbate,test\ntemperature,303\nnew,[1 2 3]"
        assert pygaps.isotherm_from_csv(text).new == [1, 2, 3]
Example #5
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()