示例#1
0
    def __init__(self, *args, **kwargs):
        self.baseline_dir = kwargs.pop(
            'baseline_dir',
            # 'tests/baseline_images/matplotlib_{ver}'.format(ver=matplotlib.__version__).replace('.', '_')
            os.path.normpath(os.path.join(TEST_DIR, 'baseline_images')))
        self.result_dir = kwargs.pop(
            'result_dir',
            # 'tests/result_images/matplotlib_{ver}'.format(ver=matplotlib.__version__).replace('.', '_')
            os.path.normpath(os.path.join(TEST_TEMP_DIR,
                                          'image_compare_tests')))
        self.filename = kwargs.pop('filename', None)
        self.extensions = kwargs.pop('extensions', ['png'])
        self.savefig_kwargs = kwargs.pop('savefig_kwargs', {'dpi': 80})
        self.tolerance = kwargs.pop('tolerance', 2)
        self.fig_size = kwargs.pop('fig_size', None)
        self.is_compare_image = self.to_bool(
            os.getenv('MTPY_TEST_COMPARE_IMAGE', False))
        self._logger = MtPyLog().get_mtpy_logger(__name__)
        self._logger.info("Image Comparison Test: {stat}".format(
            stat="ENABLED" if self.is_compare_image else "DISABLED"))
        self.on_fail = kwargs.pop('on_fail', None)
        self.on_compare_fail = kwargs.pop("on_compare_fail", None)
        self.on_empty_image = kwargs.pop("on_empty_image", None)

        ImageCompare._thread_lock.acquire()
        if not os.path.exists(self.result_dir):
            os.mkdir(self.result_dir)
        ImageCompare._thread_lock.release()
示例#2
0
def reset_matplotlib():
    # save some important params
    interactive = matplotlib.rcParams['interactive']
    backend = matplotlib.rcParams['backend']
    # reset
    matplotlib.rcdefaults()  # reset the rcparams to default
    # recover
    matplotlib.rcParams['backend'] = backend
    matplotlib.rcParams['interactive'] = interactive
    logger = MtPyLog().get_mtpy_logger(__name__)
    logger.info("Testing using matplotlib backend {}".format(matplotlib.rcParams['backend']))
示例#3
0
    def __init__(self, edilist=None, mt_objs=None, outdir=None, ptol=0.05):
        """ constructor
        :param edilist: a list of edifiles with full path, for read-only
        :param outdir:  computed result to be stored in outdir
        :param ptol: period tolerance considered as equal, default 0.05 means 5 percent
        this param controls what freqs/periods are grouped together:
        10pct may result more double counting of freq/period data than 5pct.
        eg: E:/Data/MT_Datasets/WenPingJiang_EDI 18528 rows vs 14654 rows
        """

        #self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)  # will be EdiCollection
        self._logger = MtPyLog.get_mtpy_logger(
            __name__)  # __name__ will be  path.to.module OR __main__
        self._logger.setLevel(INFO)

        if edilist is not None:
            self.edifiles = edilist
            self._logger.info("number of edi files in this collection: %s",
                              len(self.edifiles))
        elif mt_objs is not None:
            self.edifiles = [mt_obj.fn for mt_obj in mt_objs]
        assert len(self.edifiles) > 0

        self.num_of_edifiles = len(self.edifiles)  # number of stations
        print("number of stations/edifiles = %s" % self.num_of_edifiles)

        self.ptol = ptol

        if edilist is not None:
            # if edilist is provided, always create MT objects from the list
            self._logger.debug("constructing MT objects from edi files")
            self.mt_obj_list = [mt.MT(edi) for edi in self.edifiles]
        elif mt_objs is not None:
            # use the supplied mt_objs
            self.mt_obj_list = list(mt_objs)
        else:
            self._logger.error("None Edi file set")

        # get all frequencies from all edi files
        self.all_frequencies = None
        self.mt_periods = None
        self.all_unique_periods = self._get_all_periods()

        self.geopdf = self.create_mt_station_gdf()

        self.bound_box_dict = self.get_bounding_box()  # in orginal projection

        # ensure that outdir is created if not exists.
        if outdir is None:
            #raise Exception("Error: OutputDir is not specified!!!")
            pass
        elif not os.path.exists(outdir):
            os.mkdir(outdir)

        self.outdir = outdir

        return
示例#4
0
    def __init__(self, edilist=None, mt_objs=None, outdir=None, ptol=0.05):
        """
        constructor
        """

        #self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)  # will be EdiCollection
        self._logger = MtPyLog.get_mtpy_logger(
            __name__)  # __name__ will be  path.to.module OR __main__
        self._logger.setLevel(INFO)

        if edilist is not None:
            self.edifiles = edilist
            self._logger.info("number of edi files in this collection: %s",
                              len(self.edifiles))
        elif mt_objs is not None:
            self.edifiles = [mt_obj.fn for mt_obj in mt_objs]
        assert len(self.edifiles) > 0

        self.num_of_edifiles = len(self.edifiles)  # number of stations
        print("number of stations/edifiles = %s" % self.num_of_edifiles)

        self.ptol = ptol

        if edilist is not None:
            # if edilist is provided, always create MT objects from the list
            self._logger.debug("constructing MT objects from edi files")
            self.mt_obj_list = [mt.MT(edi) for edi in self.edifiles]
        elif mt_objs is not None:
            # use the supplied mt_objs
            self.mt_obj_list = list(mt_objs)
        else:
            self._logger.error("None Edi file set")

        # get all frequencies from all edi files
        self.all_frequencies = None
        self.mt_periods = None
        self.all_unique_periods = self._get_all_periods()

        self.geopdf = self.create_mt_station_gdf()

        self.bound_box_dict = self.get_bounding_box()  # in orginal projection

        # ensure that outdir is created if not exists.
        if outdir is None:
            #raise Exception("Error: OutputDir is not specified!!!")
            pass
        elif not os.path.exists(outdir):
            os.mkdir(outdir)

        self.outdir = outdir

        return
示例#5
0
    def __init__(self, parent, file_handler, selected_files):
        """

        :param parent:
        :type parent: StartQt4
        :param file_handler:
        :type file_handler: FileHandler
        :param selected_files:
        :type selected_files: set
        """
        QWidget.__init__(self, parent)
        self._parent = parent
        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)
        self.file_handler = file_handler
        self.selected_stations = selected_files
        self._current_plot = None
        self.ui = Ui_PlotOption()
        self.ui.setupUi(self)

        # hide cancel button
        self.ui.pushButton_cancel.hide()

        # populate dropdown menu
        self.plotOptions = []

        # print VisualizationBase.__subclasses__()

        for child in VisualizationBase.__subclasses__():
            name = child.plot_name()
            if name not in self.plotOptions:
                self.plotOptions.append(child)
                self.ui.comboBoxSelect_Plot.addItem(name)
            else:
                raise Exception("Duplicated Plot Name: %s in class %s" % (name, child.__name__))

        # busy overlay
        self._busy_overlay = BusyOverlay(self)
        self._busy_overlay.hide()

        # connect signals
        self.ui.comboBoxSelect_Plot.currentIndexChanged.connect(self._selection_changed)
        self.ui.pushButton_plot.clicked.connect(self._create_plot)
        self.ui.pushButton_cancel.clicked.connect(self._cancel_plot)

        if VisualizationBase.__subclasses__():
            self.ui.comboBoxSelect_Plot.setEnabled(True)
            self.ui.comboBoxSelect_Plot.setCurrentIndex(0)
        else:
            self.ui.comboBoxSelect_Plot.setEnabled(False)
示例#6
0
    def __init__(self, parent, edi_list, select_period_kwargs, data_kwargs, mesh_kwargs, topo_args, covariance_kwargs,
                 show=False):
        QtCore.QThread.__init__(self, parent)
        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)

        self._edi_list = edi_list
        self._select_period_kwargs = select_period_kwargs
        self._data_kwargs = data_kwargs
        self._mesh_kwagrs = mesh_kwargs
        self._topo_args = topo_args
        self._covariance_kwargs = covariance_kwargs

        self.output_dir = self._data_kwargs['save_path']

        self.show = show
示例#7
0
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)
        self._file_handler = FileHandler()
        self._is_file_dialog_opened = False

        self.ui = Ui_SmartMT_MainWindow()

        # init gui
        self.ui.setupUi(self)
        self.setup_menu()

        # export dialogs
        self._export_dialog = ExportDialog(self)
        self._export_dialog_modem = ExportDialogModEm(self)

        # set station viewer
        self._station_viewer = StationViewer(self,
                                             file_handler=self._file_handler)
        self._station_viewer.ui.pushButton_plot.clicked.connect(
            self.plot_selected_station)
        self._station_viewer.selection_changed.connect(
            self._selected_station_changed)
        self.ui.stackedWidget.addWidget(self._station_viewer)

        # set plot option widget
        self._plot_option = PlotOption(self, self._file_handler,
                                       self._station_viewer.selected_stations)
        self._plot_option.ui.pushButton_back.clicked.connect(
            lambda x: self.ui.stackedWidget.setCurrentWidget(self.
                                                             _station_viewer))
        self._station_viewer.selection_changed.connect(
            self._plot_option.data_changed)
        self.ui.stackedWidget.addWidget(self._plot_option)

        self.ui.stackedWidget.setCurrentWidget(self._station_viewer)

        self._subwindow_counter = 0

        self._station_summary = None

        self._progress_bar = ProgressBar(title='Loading files...')
        self.subwindows = {}
        # enable export if the activated subwindow is a image window
        self.ui.mdiArea.subWindowActivated.connect(self._subwindow_activated)
        self.setWindowState(QtCore.Qt.WindowMaximized)
示例#8
0
    def __init__(self, parent):
        """
        set up ui and attributes here
        this function sets up the empty gui area for adding gui components that defines information necessary
        for visualization
        :param parent:
        """
        QtCore.QThread.__init__(self, parent)
        self._parent = parent
        self._mt_objs = None
        self._fig = None
        self._plotting_object = None
        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)
        self._parameter_ui = PlotParameter(self._parent)

        # add plot common setting gui
        # self._common_ui = CommonSettings(self._parameter_ui)
        # apply default common settings
        self.default_common_settings()
示例#9
0
    def __init__(self, grid_dimensions=None, **kwargs):
        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)

        self.grid_dimensions = grid_dimensions
        self.smoothing_east = 0.3
        self.smoothing_north = 0.3
        self.smoothing_z = 0.3
        self.smoothing_num = 1

        self.exception_list = []
        self.mask_arr = None

        self.save_path = os.getcwd()
        self.cov_fn_basename = 'covariance.cov'

        self.cov_fn = None

        self._header_str = '\n'.join([
            '+{0}+'.format('-' * 77),
            '| This file defines model covariance for a recursive autoregression scheme.   |',
            '| The model space may be divided into distinct areas using integer masks.     |',
            '| Mask 0 is reserved for air; mask 9 is reserved for ocean. Smoothing between |',
            '| air, ocean and the rest of the model is turned off automatically. You can   |',
            '| also define exceptions to override smoothing between any two model areas.   |',
            '| To turn off smoothing set it to zero.  This header is 16 lines long.        |',
            '| 1. Grid dimensions excluding air layers (Nx, Ny, NzEarth)                   |',
            '| 2. Smoothing in the X direction (NzEarth real values)                       |',
            '| 3. Smoothing in the Y direction (NzEarth real values)                       |',
            '| 4. Vertical smoothing (1 real value)                                        |',
            '| 5. Number of times the smoothing should be applied (1 integer >= 0)         |',
            '| 6. Number of exceptions (1 integer >= 0)                                    |',
            '| 7. Exceptions in the for e.g. 2 3 0. (to turn off smoothing between 3 & 4)  |',
            '| 8. Two integer layer indices and Nx x Ny block of masks, repeated as needed.|',
            '+{0}+'.format('-' * 77)
        ])

        for key in list(kwargs.keys()):
            if hasattr(self, key):
                setattr(self, key, kwargs[key])
            else:
                self._logger.warn(
                    "Argument {}={} is not supportted thus not been set.".
                    format(key, kwargs[key]))
示例#10
0
    def __init__(self, **kwargs):
        """
        Initialise the object
        :param kwargs: keyword-value pairs
        """
        super(PlotResPhaseMaps, self).__init__(**kwargs)

        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)

        fn_list = kwargs.pop('fn_list', [])

        if(len(fn_list)==0): raise NameError('File list is empty.')

        # ----set attributes for the class-------------------------
        self.mt_list = mtpl.get_mtlist(fn_list=fn_list)

        # read in map scale
        self.mapscale = kwargs.pop('mapscale', 'deg')
        if self.mapscale == 'km':
            self.dscale = 1000.
        elif self.mapscale == 'm':
            self.dscale = 1.
        # end if

        self.plot_title = kwargs.pop('plot_title', None)
        self.fig_dpi = kwargs.pop('fig_dpi', 100)

        self.fig_size = kwargs.pop('fig_size', [8, 6])

        self.font_size = kwargs.pop('font_size', 7)

        self.plot_yn = kwargs.pop('plot_yn', 'y')
        self.save_fn = kwargs.pop('save_fn', "/c/tmp")

        # By this stage all keyword arguments meant to be set as class properties will have
        # been processed. Popping all class properties that still exist in kwargs
        self.kwargs = kwargs
        for key in vars(self):
            self.kwargs.pop(key, None)

        self.axesList = []
示例#11
0
class gdal_data_check(object):
    _has_checked = False
    _gdal_data_found = False
    _logger = MtPyLog.get_mtpy_logger(__name__)

    def __init__(self, func):
        """
        this decorator should only be used for the function that requres gdal and gdal-data
        to function correctly.

        the decorator will check if the GDAL_DATA is set and the path in GDAL_DATA is exist.
        If GDAL_DATA is not set, then try to use external program "gdal-config --datadir" to
        findout where the data files are installed.

        If failed to find the data file, then ImportError will be raised.

        :param func: function to be decorated
        """
        self._func = func
        if not self._has_checked:
            self._gdal_data_found = self._check_gdal_data()
            self._has_checked = True
        if not self._gdal_data_found:
            raise ImportError("GDAL_DATA environment variable not set. Please see "
                              "https://trac.osgeo.org/gdal/wiki/FAQInstallationAndBuilding#HowtosetGDAL_DATAvariable for "
                              "more information.")

    def __call__(self, *args, **kwargs):  # pragma: no cover
        return self._func(*args, **kwargs)

    def _check_gdal_data(self):
        if 'GDAL_DATA' not in os.environ:
            # gdal data not defined, try to define
            from subprocess import Popen, PIPE
            self._logger.warn("GDAL_DATA environment variable is not set")
            try:
                # try to find out gdal_data path using gdal-config
                self._logger.info("Trying to find gdal-data path ...")
                process = Popen(['gdal-config', '--datadir'], stdout=PIPE)
                (output, err) = process.communicate()
                exit_code = process.wait()
                output = output.strip()
                if exit_code == 0 and os.path.exists(output):
                    os.environ['GDAL_DATA'] = output
                    self._logger.info("Found gdal-data path: {}".format(output))
                    return True
                else:
                    self._logger.error(
                        "\tCannot find gdal-data path. Please find the gdal-data path of your installation and set it to "
                        "\"GDAL_DATA\" environment variable. Please see "
                        "https://trac.osgeo.org/gdal/wiki/FAQInstallationAndBuilding#HowtosetGDAL_DATAvariable for "
                        "more information.")
                    return False
            except Exception:
                return False
        else:
            if os.path.exists(os.environ['GDAL_DATA']):
                self._logger.info("GDAL_DATA is set to: {}".format(os.environ['GDAL_DATA']))
                return True
            else:
                self._logger.error("GDAL_DATA is set to: {}, but the path does not exist.".format(os.environ['GDAL_DATA']))
                return False
示例#12
0
class ImageCompare(object):
    """
    - to enable the image comparison tests in the code, add @ImageCompare(**kwargs)
    to the function that generates image with matplotlib, by default, the new image
    will be saved and check if the image is an empty image. to
    - enable the image comparison tests against the baseline image stored, set environment variable
    MTPY_TEST_COMPARE_IMAGE = True
    """
    def __init__(self, *args, **kwargs):
        self.baseline_dir = kwargs.pop(
            'baseline_dir',
            # 'tests/baseline_images/matplotlib_{ver}'.format(ver=matplotlib.__version__).replace('.', '_')
            os.path.normpath(os.path.join(TEST_DIR, 'baseline_images')))
        self.result_dir = kwargs.pop(
            'result_dir',
            # 'tests/result_images/matplotlib_{ver}'.format(ver=matplotlib.__version__).replace('.', '_')
            os.path.normpath(os.path.join(TEST_TEMP_DIR,
                                          'image_compare_tests')))
        self.filename = kwargs.pop('filename', None)
        self.extensions = kwargs.pop('extensions', ['png'])
        self.savefig_kwargs = kwargs.pop('savefig_kwargs', {'dpi': 80})
        self.tolerance = kwargs.pop('tolerance', 2)
        self.fig_size = kwargs.pop('fig_size', None)
        self.is_compare_image = self.to_bool(
            os.getenv('MTPY_TEST_COMPARE_IMAGE', False))
        self._logger = MtPyLog().get_mtpy_logger(__name__)
        self._logger.info("Image Comparison Test: {stat}".format(
            stat="ENABLED" if self.is_compare_image else "DISABLED"))
        self.on_fail = kwargs.pop('on_fail', None)
        self.on_compare_fail = kwargs.pop("on_compare_fail", None)
        self.on_empty_image = kwargs.pop("on_empty_image", None)

        ImageCompare._thread_lock.acquire()
        if not os.path.exists(self.result_dir):
            os.mkdir(self.result_dir)
        ImageCompare._thread_lock.release()

    def __call__(self, original):
        import matplotlib.pyplot as plt
        from matplotlib.testing.compare import compare_images

        if self.filename is None:
            filename = original.__name__
        else:
            filename = self.filename

        filename = filename.replace('[', '_').replace(']',
                                                      '_').replace('/', '_')
        filename = filename.strip(' _')

        test_suite_name = original.__module__.split('.')[-1]

        @functools.wraps(original)
        def new_test_func(*args, **kwargs):
            if inspect.ismethod(original):
                result = original.__func__(*args, **kwargs)
            else:
                result = original(*args, **kwargs)
            for baseline_image, test_image, baseline_rcparams, test_rcparams in self._get_baseline_result_pairs(
                    test_suite_name, filename, self.extensions):
                # save image
                fig = plt.gcf()
                if fig is not None:
                    if self.fig_size is not None:
                        fig.set_size_inches(self.fig_size)
                        fig.set_tight_layout(True)
                    fig.savefig(test_image, **self.savefig_kwargs)
                    # save rcParams
                    with open(test_rcparams, "w") as rcfile:
                        from pprint import pprint
                        rc = matplotlib.rcParams.copy()
                        rc.pop("datapath")  # hide datapath
                        pprint(rc, rcfile)
                    import pytest
                    if self.is_compare_image and os.path.exists(
                            baseline_image):
                        msg = compare_images(baseline_image,
                                             test_image,
                                             tol=self.tolerance)
                        if msg is not None:
                            msg += "\n"
                            msg += self.compare_rcParam(
                                baseline_rcparams, test_rcparams)
                            # print image in base64
                            # print("====================")
                            # print("Expected Image:")
                            # self._print_image_base64(baseline_image)
                            # print("Actual Image:")
                            # self._print_image_base64(test_image)
                            # print("====================")
                            self.print_image_testing_note(file=sys.stderr)
                            if self.on_compare_fail is not None:
                                self.on_compare_fail()
                            if self.on_fail is not None:
                                self.on_fail()
                            pytest.fail(msg, pytrace=False)
                        else:
                            # clearup the image as they are the same with the baseline
                            os.remove(test_image)
                            os.remove(test_rcparams)
                            if not os.listdir(os.path.dirname(test_image)):
                                os.rmdir(os.path.dirname(test_image))
                    else:
                        # checking if the created image is empty
                        verify(test_image)
                        actual_image = _png.read_png_int(test_image)
                        actual_image = actual_image[:, :, :
                                                    3]  # remove the alpha channel (if exists)
                        import numpy as np
                        if np.any(actual_image):
                            self.print_image_testing_note(file=sys.stderr)
                            if self.is_compare_image:
                                pytest.skip(
                                    "Image file not found for comparison test "
                                    "(This is expected for new tests.)\nGenerated Image: "
                                    "\n\t{test}".format(test=test_image))
                            else:
                                self._logger.info(
                                    "\nGenerated Image: {test}".format(
                                        test=test_image))
                        else:
                            # empty image created
                            if self.on_empty_image is not None:
                                self.on_empty_image()
                            if self.on_fail is not None:
                                self.on_fail()
                            pytest.fail(
                                "Image file not found for comparison test "
                                "(This is expected for new tests.),"
                                " but the new image created is empty.")
            return result

        return new_test_func

    def _get_baseline_result_pairs(self, test_suite_name, fname, extensions):
        if test_suite_name is None:
            baseline = self.baseline_dir
            result = self.result_dir
        else:
            baseline = os.path.join(self.baseline_dir, test_suite_name)
            result = os.path.join(self.result_dir, test_suite_name)
            if not os.path.exists(baseline):
                os.makedirs(baseline)
            if not os.path.exists(result):
                os.makedirs(result)

        for ext in extensions:
            name = '{fname}.{ext}'.format(fname=fname, ext=ext)
            rc_name = '{fname}_rcParams.txt'.format(fname=fname)
            yield os.path.normpath(
                os.path.join(baseline, name)), os.path.normpath(
                    os.path.join(result, name)), os.path.normpath(
                        os.path.join(baseline, rc_name)), os.path.normpath(
                            os.path.join(result, rc_name))

    @staticmethod
    def _print_image_base64(image_file_name):
        with open(image_file_name, "rb") as image_file:
            image_data = image_file.read()
            print(
                "<img src=\"data:image/{};base64,{}\" style=\"display:block; max-width:800px; width: auto; height: "
                "auto;\" />".format(
                    os.path.splitext(image_file_name)[1].strip(" ."),
                    image_data.encode("base64")))

    @staticmethod
    def print_image_testing_note(file=sys.stdout):
        print("====================", file=file)
        print("matplotlib Version: " + matplotlib.__version__, file=file)
        print(
            "NOTE: The test result may be different in different versions of matplotlib.",
            file=file)
        print("====================", file=file)

    @staticmethod
    def compare_rcParam(baseline_rcparams, test_rcparams):
        # check if the rcParams are different
        msg = "Comparing matplotlib.rcParam:\n"
        if os.path.isfile(baseline_rcparams):
            with open(baseline_rcparams, "r") as fbaserc:
                with open(test_rcparams, "r") as ftestrc:
                    import difflib
                    lines = [
                        line
                        for line in difflib.unified_diff(fbaserc.readlines(),
                                                         ftestrc.readlines(),
                                                         fromfile="baseline",
                                                         tofile="test",
                                                         n=0)
                    ]
                    if lines:
                        msg += "  Found differences:\n    " + "    ".join(
                            lines)
                    else:
                        msg += " NO differences found."
        else:
            msg += "  Baseline rcParams file not found."
        return msg

    def to_bool(self, param):
        if isinstance(param, str) and param:
            param = param.lower()
            if param in ('true', '1', 't'):
                return True
            elif param in ('false', 'f', '0'):
                return False
        else:
            return bool(param)
示例#13
0
from tests import TEST_DIR, make_temp_dir, TEST_TEMP_DIR

if os.name == "posix" and 'DISPLAY' not in os.environ:
    print("MATPLOTLIB: No Display found, using non-interactive svg backend",
          file=sys.stderr)
    matplotlib.use('svg')
    import matplotlib.pyplot as plt
    MTPY_TEST_HAS_DISPLAY = False
else:
    # matplotlib.use('svg')
    import matplotlib.pyplot as plt
    MTPY_TEST_HAS_DISPLAY = True
    plt.ion()

MtPyLog.get_mtpy_logger(__name__).info(
    "Testing using matplotlib backend {}".format(
        matplotlib.rcParams['backend']))


def reset_matplotlib():
    # save some important params
    interactive = matplotlib.rcParams['interactive']
    backend = matplotlib.rcParams['backend']
    # reset
    matplotlib.rcdefaults()  # reset the rcparams to default
    # recover
    matplotlib.rcParams['backend'] = backend
    matplotlib.rcParams['interactive'] = interactive
    logger = MtPyLog().get_mtpy_logger(__name__)
    logger.info("Testing using matplotlib backend {}".format(
        matplotlib.rcParams['backend']))
示例#14
0
import matplotlib.pyplot as plt
import numpy as np

import mtpy.core.mt as mt
from mtpy.imaging.penetration import get_index, load_edi_files, Depth3D
from mtpy.utils.decorator import deprecated
from mtpy.utils.mtpylog import MtPyLog

# mpl.rcParams['lines.linewidth'] = 2
# mpl.rcParams['lines.color'] = 'r'

# mpl.rcParams['figure.figsize'] = [20, 10]

# get a logger object for this module, using the utility class MtPyLog to
# config the logger
_logger = MtPyLog.get_mtpy_logger(__name__)

# logger =
# MtPyLog(path2configfile='logging.yml').get_mtpy_logger(__name__) #
# specific


# This is the major function to be maintained!!!
# use the Zcompotent=[det, zxy, zyx]
def plot_latlon_depth_profile(edi_dir,
                              period,
                              zcomponent='det',
                              showfig=True,
                              savefig=True,
                              savepath=None,
                              fig_dpi=400,
"""
import os
import sys
import csv
import glob

import numpy as np
import mtpy.core.mt as mt
import mtpy.imaging.mtplottools as mtplottools
from mtpy.utils.decorator import deprecated
from mtpy.utils.matplotlib_utils import gen_hist_bins

from logging import DEBUG, INFO, ERROR, WARNING
from mtpy.utils.mtpylog import MtPyLog

_logger = MtPyLog.get_mtpy_logger(
    __name__)  # __name__ will be  path.to.module OR __main__
_logger.setLevel(INFO)


def get_resistivity_from_edi_file(edifile, rholist=['det']):
    """Compute the resistivity values of an edi file
    :param edifile: input edifile
    :param rholist: flag the method to compute penetration depth: det zxy zyx
    :return: a tuple:(station_lat, statoin_lon, periods_list, pendepth_list)
    """
    _logger.debug("processing the edi file %s", edifile)

    mt_obj = mt.MT(edifile)
    zeta = mt_obj.Z  # the attribute Z represent the impedance tensor 2X2 matrix
    freqs = zeta.freq  # frequencies
示例#16
0
# define mtpy release version below
# see https://packaging.python.org/guides/single-sourcing-package-version/
__version__ = "1.1.3"

# load mtpy default logging config
from mtpy.utils.mtpylog import MtPyLog

MtPyLog.load_configure()
示例#17
0
TEST_DIR = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
TEST_TEMP_DIR = os.path.normpath(os.path.join(TEST_DIR, "temp"))

if not os.path.isdir(TEST_TEMP_DIR):
    os.mkdir(TEST_TEMP_DIR)


def make_temp_dir(dir_name, base_dir=TEST_TEMP_DIR):
    _temp_dir = os.path.normpath(os.path.join(base_dir, dir_name))
    if os.path.isdir(_temp_dir):
        shutil.rmtree(_temp_dir)
    os.mkdir(_temp_dir)
    return _temp_dir


EDI_DATA_DIR = os.path.normpath(
    os.path.join(TEST_MTPY_ROOT, 'examples/data/edi_files'))
EDI_DATA_DIR2 = os.path.normpath(
    os.path.join(TEST_MTPY_ROOT, 'examples/data/edi_files_2'))
AUS_TOPO_FILE = os.path.normpath(
    os.path.join(TEST_MTPY_ROOT, 'examples/data/AussieContinent_etopo1.asc'))
SAMPLE_DIR = os.path.normpath(
    os.path.join(
        TEST_MTPY_ROOT,
        'examples/model_files'))  # r'E:\Githubz\mtpy\examples\model_files'
M2D_DIR = os.path.normpath(
    os.path.join(TEST_MTPY_ROOT, 'examples/data/mare2dem'))

# set test logging configure
MtPyLog.load_configure(os.path.join(TEST_DIR, "logging.yml"))
示例#18
0
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.axes_grid1 import make_axes_locatable
from shapely.geometry import Point, Polygon, LineString, LinearRing

from mtpy.core.edi_collection import EdiCollection
from mtpy.utils.mtpy_decorator import deprecated
from mtpy.utils.mtpylog import MtPyLog
from mtpy.utils.edi_folders import recursive_glob

mpl.rcParams['lines.linewidth'] = 2
# mpl.rcParams['lines.color'] = 'r'
mpl.rcParams['figure.figsize'] = [10, 6]

_logger = MtPyLog.get_mtpy_logger(__name__)  # logger inside this file/module
_logger.setLevel(logging.DEBUG)  # set your logger level


class ShapefilesCreator(EdiCollection):
    """ Extend the EdiCollection parent class,
    create phase tensor and tipper shapefiles for a list of edifiles

    :param edifile_list: [path2edi,...]
    :param outdir: path2output dir, where the shp file will be written.
    :param orig_crs = {'init': 'epsg:4283'}  # GDA94
    """

    def __init__(self, edifile_list, outdir, epsg_code=4326):
        """
        loop through a list of edi files, create required shapefiles
示例#19
0
 def __init__(self):
     self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)
     self._data = None
     self._fig = None
示例#20
0
 def __init__(self):
     self._station_dict = dict()
     self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)
     self._file_dict = dict()
     self._group_dict = dict()
     self._file_to_groups = dict()
示例#21
0
                        # remove the window
                        del self._main_ui.subwindows[title]
                    else:
                        subwindow.hide()

    @deprecated(
        "This is an dummy action that should be used only when the required function hasn't been implemented"
    )
    def dummy_action(self, *args, **kwargs):
        pass


if __name__ == "__main__":
    if __debug__:
        MtPyLog.load_configure(
            os.path.join(os.path.abspath("tests"),
                         "logging.yml"))  # debug only
        # handle uncaught exceptions to log as since PYQT5.5 will not display any uncaught exceptions
        # ref: http://pyqt.sourceforge.net/Docs/PyQt5/incompatibilities.html#unhandled-python-exceptions
        logger = MtPyLog.get_mtpy_logger(__name__)
        sys.excepthook = lambda exc_type, exc_value, exc_trace: logger.error(
            "Uncaught exception", exc_info=(exc_type, exc_value, exc_trace))

    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    smartMT = StartGUI()
    smartMT.show()

    # hack to fix the "python has stopped working" error,
    # the possible cause is the QtGui4.dll crashes, need to test it on linux environment
示例#22
0
    def __init__(self, parent=None):
        QWizard.__init__(self, parent)
        self.ui = Ui_Wizard_esport_modem()
        self.ui.setupUi(self)

        self._logger = MtPyLog.get_mtpy_logger(self.__class__.__name__)
        # setup gui
        # self.setWindowTitle("ModEM input file generator")

        # add math label
        self._math_label_sign_impedances = MathTextLabel(
            self,
            self._math_label_sign_text.format(
                "+" if self.ui.radioButton_impedance_sign_plus.isChecked() else "-"
            )
        )
        self.ui.horizontalLayout_sign_impedance.addWidget(self._math_label_sign_impedances)
        self._math_label_sign_vertical = MathTextLabel(
            self,
            self._math_label_sign_text.format(
                "+" if self.ui.radioButton_vertical_sign_plus.isChecked() else "-"
            )
        )
        self.ui.horizontalLayout_sign_vertical.addWidget(self._math_label_sign_vertical)

        # add math formulae of each error type
        self._math_label_elbert = MathTextLabel(
            self,
            "$E_{egbert}=e_Z\\times |Z_{xy}\\times Z_{yx}|^\\frac{1}{2}$",
        )
        self.ui.verticalLayout_error_types.addWidget(self._math_label_elbert)

        self._math_label_mean = MathTextLabel(self, "$E_{mean} = e_Z\\times mean(|Z_{xy}, Z_{yx}|)$")
        self._math_label_mean.setHidden(True)
        self.ui.verticalLayout_error_types.addWidget(self._math_label_mean)

        self._math_label_eigen = MathTextLabel(self, "$E_{eigen} = e_Z\\times eigenvalues(Z)$")
        self._math_label_eigen.setHidden(True)
        self.ui.verticalLayout_error_types.addWidget(self._math_label_eigen)

        self._math_label_median = MathTextLabel(self, "$E_{median} = e_Z\\times median(|Z_{xx},Z_{xy},Z_{yx},Z_{yy}|)$")
        self._math_label_median.setHidden(True)
        self.ui.verticalLayout_error_types.addWidget(self._math_label_median)

        self._math_label_error_type_note = MathTextLabel(self, "where $E$ is the error and $e$ is the error value (z).")
        self.ui.verticalLayout_error_types.addWidget(self._math_label_error_type_note)

        # add period selection
        self._period_select_ui = FrequencySelection(
            self.ui.wizardPage_period,
            show_period=True,
            show_frequency=False,
            allow_range_select=True,
            select_multiple=True
        )
        self._period_select_ui.ui.checkBox_show_existing.setChecked(True)
        self._period_select_ui.setEnabled(False)
        self._period_select_ui.setHidden(True)
        self.ui.wizardPage_period.layout().addWidget(self._period_select_ui)

        self._period_select_from_file_ui = FrequencySelectionFromFile(self.ui.wizardPage_period)
        self._period_select_from_file_ui.setEnabled(False)
        self._period_select_from_file_ui.setHidden(True)
        self.ui.wizardPage_period.layout().addWidget(self._period_select_from_file_ui)

        # add rotation
        self._rotation_ui = Rotation(self.ui.wizardPage_data)
        self._rotation_ui.setTitle('Data Rotation Angle')
        self.ui.horizontalLayout_data.addWidget(self._rotation_ui)

        self._mesh_rotation_ui = Rotation(self.ui.wizardPage_mesh)
        self._mesh_rotation_ui.setTitle('Mesh Rotation Angle')
        self.ui.gridLayout_mesh.addWidget(self._mesh_rotation_ui)

        # hide error percents
        self._component_error_type_z_changed()

        # hide bottom in vertical mesh as it is not used in mesh gen
        self.ui.doubleSpinBox_bottom.hide()
        self.ui.label_bottom.hide()

        # epsg
        self.ui.comboBox_epsg.addItems(
            [str(epsg) for epsg in sorted(epsg_dict.keys())]
        )

        # set validators
        self._double_validator = QDoubleValidator(-np.inf, np.inf, 1000)
        self._double_validator.setNotation(QDoubleValidator.ScientificNotation)
        self.ui.lineEdit_resistivity_init.setValidator(self._double_validator)
        self.ui.lineEdit_resistivity_air.setValidator(self._double_validator)
        self.ui.lineEdit_resistivity_sea.setValidator(self._double_validator)

        self._file_validator = FileValidator()
        self.ui.comboBox_topography_file.lineEdit().setValidator(self._file_validator)
        self.ui.comboBox_topography_file.lineEdit().setMaxLength(256)

        self._dir_validator = DirectoryValidator()
        self.ui.comboBox_directory.lineEdit().setValidator(self._dir_validator)

        # setup directory and dir dialog
        self._dir_dialog = QFileDialog(self)
        # self._dir_dialog.setDirectory(os.path.expanduser("~"))
        self.ui.comboBox_directory.addItem(os.path.expanduser("~"))
        self._update_full_output()

        # set maximum
        # self.ui.spinBox_cell_num_ew.setMaximum(0xFFFFFFFF)
        # self.ui.spinBox_cell_num_ns.setMaximum(0xFFFFFFFF)

        # tooltip for error types
        for index, tooltip in enumerate(self._error_type_z_tool_tip):
            self.ui.comboBox_error_type_z.setItemData(index, tooltip, QtCore.Qt.ToolTipRole)
            self.ui.comboBox_error_type_zxx.setItemData(index, tooltip, QtCore.Qt.ToolTipRole)
            self.ui.comboBox_error_type_zxy.setItemData(index, tooltip, QtCore.Qt.ToolTipRole)
            self.ui.comboBox_error_type_zyx.setItemData(index, tooltip, QtCore.Qt.ToolTipRole)
            self.ui.comboBox_error_type_zyy.setItemData(index, tooltip, QtCore.Qt.ToolTipRole)

        # hide parts
        self.ui.groupBox_component_error_types.setHidden(True)
        self.ui.label_component_error_types.setHidden(True)

        # connect signals
        self.ui.radioButton_impedance_sign_plus.toggled.connect(
            lambda is_checked: self._math_label_sign_impedances.set_math_text(
                self._math_label_sign_text.format("+" if is_checked else "-"))
        )
        self.ui.radioButton_vertical_sign_plus.toggled.connect(
            lambda is_checked: self._math_label_sign_vertical.set_math_text(
                self._math_label_sign_text.format("+" if is_checked else "-")
            )
        )

        self.ui.radioButton_impedance_full.toggled.connect(self._impedance_full_toggled)
        self.ui.radioButton_impedance_off_diagonal.toggled.connect(self._impedance_off_diagonal_toggled)
        self.ui.radioButton_impedance_none.toggled.connect(self._impedance_none_toggled)
        self.ui.radioButton_vertical_full.toggled.connect(self._vertical_full_toggled)

        self.ui.comboBox_error_type_z.currentIndexChanged.connect(self._error_type_z_changed)
        for component in self._impedance_components:
            combobox = getattr(self.ui, 'comboBox_error_type_{}'.format(component))
            checkbox = getattr(self.ui, 'checkBox_{}'.format(component))
            combobox.currentIndexChanged.connect(self._component_error_type_z_changed)
            checkbox.toggled.connect(self._error_component_checkbox_toggled(combobox))

        self.ui.comboBox_output_name.currentIndexChanged.connect(self._update_full_output)
        self.ui.comboBox_output_name.lineEdit().editingFinished.connect(self._output_name_changed)
        self.ui.comboBox_output_name.editTextChanged.connect(self._update_full_output)
        self.ui.comboBox_directory.currentIndexChanged.connect(self._update_full_output)
        self.ui.comboBox_directory.lineEdit().editingFinished.connect(self._output_dir_changed)
        self.ui.comboBox_directory.editTextChanged.connect(self._update_full_output)
        self.ui.pushButton_browse.clicked.connect(self._browse)

        # self.ui.doubleSpinBox_target_depth.valueChanged.connect(self._target_depth_changed)
        self.ui.doubleSpinBox_target_depth.lineEdit().editingFinished.connect(self._target_depth_changed)
        self.ui.doubleSpinBox_bottom.lineEdit().editingFinished.connect(self._bottom_changed)
        # self.ui.doubleSpinBox_bottom.valueChanged.connect(self._bottom_changed)

        # self.ui.comboBox_topography_file.currentIndexChanged.connect()
        self.ui.comboBox_topography_file.lineEdit().editingFinished.connect(
            self._topography_file_changed
        )
        self.ui.pushButton_browse_topography_file.clicked.connect(self._browse_topography_file)

        self.ui.pushButton_test.clicked.connect(self._test_button_clicked)

        self.ui.checkBox_cell_num_ew.stateChanged.connect(
            lambda p_int: self.ui.spinBox_cell_num_ew.setEnabled(p_int != 0)
        )
        self.ui.checkBox_cell_num_ns.stateChanged.connect(
            lambda p_int: self.ui.spinBox_cell_num_ns.setEnabled(p_int != 0)
        )

        self.ui.checkBox_component_error_types.setEnabled(False)  # disabled due to missing implementations todo implement this option in modem.Data
        self.ui.checkBox_component_error_types.toggled.connect(self._component_error_type_toggled)
        self.ui.radioButton_select_period_percent.toggled.connect(
            lambda checked: self.ui.doubleSpinBox_select_period_percent.setEnabled(checked))
        self.ui.radioButton_select_period.toggled.connect(
            lambda checked: self._period_select_ui.setEnabled(checked))
        self.ui.radioButton_select_period.toggled.connect(
            lambda checked: self._period_select_ui.setHidden(not checked))
        self.ui.radioButton_select_by_file.toggled.connect(
            lambda checked: self._period_select_from_file_ui.setEnabled(checked))
        self.ui.radioButton_select_by_file.toggled.connect(
            lambda checked: self._period_select_from_file_ui.setHidden(not checked))

        # register fields
        self.ui.wizardPage_output.registerField('output_path*', self.ui.lineEdit_full_output)
        self.ui.wizardPage_topography.registerField('topography_file*', self.ui.comboBox_topography_file)
        self.ui.wizardPage_topography.registerField('sea_resistivity',
                                                    self.ui.lineEdit_resistivity_sea)
        self.ui.wizardPage_topography.registerField('air_resistivity',
                                                    self.ui.lineEdit_resistivity_air)

        # attribute
        self._mt_objs = None

        self._progress_bar = ProgressBar()