resistics.regression module

The regression module provides functions and classes for the following:

  • Preparing gathered data for regression

  • Performing the linear regression

Resistics has built in solvers that use scikit learn models, namely

  • Ordinary least squares

  • RANSAC

  • TheilSen

These will perform well in many scenarios. However, the functionality available in resistics makes it possible to use custom solvers if required.

pydantic model resistics.regression.RegressionInputMetadata[source]

Bases: resistics.common.Metadata

Metadata for regression input data, mainly to track processing history

Show JSON schema
{
   "title": "RegressionInputMetadata",
   "description": "Metadata for regression input data, mainly to track processing history",
   "type": "object",
   "properties": {
      "contributors": {
         "title": "Contributors",
         "type": "object",
         "additionalProperties": {
            "anyOf": [
               {
                  "$ref": "#/definitions/SiteCombinedMetadata"
               },
               {
                  "$ref": "#/definitions/SpectraMetadata"
               }
            ]
         }
      },
      "history": {
         "title": "History",
         "default": {
            "records": []
         },
         "allOf": [
            {
               "$ref": "#/definitions/History"
            }
         ]
      }
   },
   "required": [
      "contributors"
   ],
   "definitions": {
      "ResisticsFile": {
         "title": "ResisticsFile",
         "description": "Required information for writing out a resistics file",
         "type": "object",
         "properties": {
            "created_on_local": {
               "title": "Created On Local",
               "type": "string",
               "format": "date-time"
            },
            "created_on_utc": {
               "title": "Created On Utc",
               "type": "string",
               "format": "date-time"
            },
            "version": {
               "title": "Version",
               "type": "string"
            }
         }
      },
      "Record": {
         "title": "Record",
         "description": "Class to hold a record\n\nA record holds information about a process that was run. It is intended to\ntrack processes applied to data, allowing a process history to be saved\nalong with any datasets.\n\nExamples\n--------\nA simple example of creating a process record\n\n>>> from resistics.common import Record\n>>> messages = [\"message 1\", \"message 2\"]\n>>> record = Record(\n...     creator={\"name\": \"example\", \"parameter1\": 15},\n...     messages=messages,\n...     record_type=\"example\"\n... )\n>>> record.summary()\n{\n    'time_local': '...',\n    'time_utc': '...',\n    'creator': {'name': 'example', 'parameter1': 15},\n    'messages': ['message 1', 'message 2'],\n    'record_type': 'example'\n}",
         "type": "object",
         "properties": {
            "time_local": {
               "title": "Time Local",
               "type": "string",
               "format": "date-time"
            },
            "time_utc": {
               "title": "Time Utc",
               "type": "string",
               "format": "date-time"
            },
            "creator": {
               "title": "Creator",
               "type": "object"
            },
            "messages": {
               "title": "Messages",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "record_type": {
               "title": "Record Type",
               "type": "string"
            }
         },
         "required": [
            "creator",
            "messages",
            "record_type"
         ]
      },
      "History": {
         "title": "History",
         "description": "Class for storing processing history\n\nParameters\n----------\nrecords : List[Record], optional\n    List of records, by default []\n\nExamples\n--------\n>>> from resistics.testing import record_example1, record_example2\n>>> from resistics.common import History\n>>> record1 = record_example1()\n>>> record2 = record_example2()\n>>> history = History(records=[record1, record2])\n>>> history.summary()\n{\n    'records': [\n        {\n            'time_local': '...',\n            'time_utc': '...',\n            'creator': {\n                'name': 'example1',\n                'a': 5,\n                'b': -7.0\n            },\n            'messages': ['Message 1', 'Message 2'],\n            'record_type': 'process'\n        },\n        {\n            'time_local': '...',\n            'time_utc': '...',\n            'creator': {\n                'name': 'example2',\n                'a': 'parzen',\n                'b': -21\n            },\n            'messages': ['Message 5', 'Message 6'],\n            'record_type': 'process'\n        }\n    ]\n}",
         "type": "object",
         "properties": {
            "records": {
               "title": "Records",
               "default": [],
               "type": "array",
               "items": {
                  "$ref": "#/definitions/Record"
               }
            }
         }
      },
      "SiteCombinedMetadata": {
         "title": "SiteCombinedMetadata",
         "description": "Metadata for combined data\n\nCombined metadata stores metadata for measurements that are combined from\na single site.",
         "type": "object",
         "properties": {
            "file_info": {
               "$ref": "#/definitions/ResisticsFile"
            },
            "site_name": {
               "title": "Site Name",
               "type": "string"
            },
            "fs": {
               "title": "Fs",
               "type": "number"
            },
            "system": {
               "title": "System",
               "default": "",
               "type": "string"
            },
            "serial": {
               "title": "Serial",
               "default": "",
               "type": "string"
            },
            "wgs84_latitude": {
               "title": "Wgs84 Latitude",
               "default": -999.0,
               "type": "number"
            },
            "wgs84_longitude": {
               "title": "Wgs84 Longitude",
               "default": -999.0,
               "type": "number"
            },
            "easting": {
               "title": "Easting",
               "default": -999.0,
               "type": "number"
            },
            "northing": {
               "title": "Northing",
               "default": -999.0,
               "type": "number"
            },
            "elevation": {
               "title": "Elevation",
               "default": -999.0,
               "type": "number"
            },
            "measurements": {
               "title": "Measurements",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "chans": {
               "title": "Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "n_evals": {
               "title": "N Evals",
               "type": "integer"
            },
            "eval_freqs": {
               "title": "Eval Freqs",
               "type": "array",
               "items": {
                  "type": "number"
               }
            },
            "histories": {
               "title": "Histories",
               "type": "object",
               "additionalProperties": {
                  "$ref": "#/definitions/History"
               }
            }
         },
         "required": [
            "site_name",
            "fs",
            "chans",
            "n_evals",
            "eval_freqs",
            "histories"
         ]
      },
      "ChanMetadata": {
         "title": "ChanMetadata",
         "description": "Channel metadata",
         "type": "object",
         "properties": {
            "name": {
               "title": "Name",
               "type": "string"
            },
            "data_files": {
               "title": "Data Files",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "chan_type": {
               "title": "Chan Type",
               "type": "string"
            },
            "chan_source": {
               "title": "Chan Source",
               "type": "string"
            },
            "sensor": {
               "title": "Sensor",
               "default": "",
               "type": "string"
            },
            "serial": {
               "title": "Serial",
               "default": "",
               "type": "string"
            },
            "gain1": {
               "title": "Gain1",
               "default": 1,
               "type": "number"
            },
            "gain2": {
               "title": "Gain2",
               "default": 1,
               "type": "number"
            },
            "scaling": {
               "title": "Scaling",
               "default": 1,
               "type": "number"
            },
            "chopper": {
               "title": "Chopper",
               "default": false,
               "type": "boolean"
            },
            "dipole_dist": {
               "title": "Dipole Dist",
               "default": 1,
               "type": "number"
            },
            "sensor_calibration_file": {
               "title": "Sensor Calibration File",
               "default": "",
               "type": "string"
            },
            "instrument_calibration_file": {
               "title": "Instrument Calibration File",
               "default": "",
               "type": "string"
            }
         },
         "required": [
            "name"
         ]
      },
      "SpectraLevelMetadata": {
         "title": "SpectraLevelMetadata",
         "description": "Metadata for spectra of a windowed decimation level",
         "type": "object",
         "properties": {
            "fs": {
               "title": "Fs",
               "type": "number"
            },
            "n_wins": {
               "title": "N Wins",
               "type": "integer"
            },
            "win_size": {
               "title": "Win Size",
               "exclusiveMinimum": 0,
               "type": "integer"
            },
            "olap_size": {
               "title": "Olap Size",
               "exclusiveMinimum": 0,
               "type": "integer"
            },
            "index_offset": {
               "title": "Index Offset",
               "type": "integer"
            },
            "n_freqs": {
               "title": "N Freqs",
               "type": "integer"
            },
            "freqs": {
               "title": "Freqs",
               "type": "array",
               "items": {
                  "type": "number"
               }
            }
         },
         "required": [
            "fs",
            "n_wins",
            "win_size",
            "olap_size",
            "index_offset",
            "n_freqs",
            "freqs"
         ]
      },
      "SpectraMetadata": {
         "title": "SpectraMetadata",
         "description": "Metadata for spectra data",
         "type": "object",
         "properties": {
            "file_info": {
               "$ref": "#/definitions/ResisticsFile"
            },
            "fs": {
               "title": "Fs",
               "type": "array",
               "items": {
                  "type": "number"
               }
            },
            "chans": {
               "title": "Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "n_chans": {
               "title": "N Chans",
               "type": "integer"
            },
            "n_levels": {
               "title": "N Levels",
               "type": "integer"
            },
            "first_time": {
               "title": "First Time",
               "pattern": "%Y-%m-%d %H:%M:%S.%f_%o_%q_%v",
               "examples": [
                  "2021-01-01 00:00:00.000061_035156_250000_000000"
               ]
            },
            "last_time": {
               "title": "Last Time",
               "pattern": "%Y-%m-%d %H:%M:%S.%f_%o_%q_%v",
               "examples": [
                  "2021-01-01 00:00:00.000061_035156_250000_000000"
               ]
            },
            "system": {
               "title": "System",
               "default": "",
               "type": "string"
            },
            "serial": {
               "title": "Serial",
               "default": "",
               "type": "string"
            },
            "wgs84_latitude": {
               "title": "Wgs84 Latitude",
               "default": -999.0,
               "type": "number"
            },
            "wgs84_longitude": {
               "title": "Wgs84 Longitude",
               "default": -999.0,
               "type": "number"
            },
            "easting": {
               "title": "Easting",
               "default": -999.0,
               "type": "number"
            },
            "northing": {
               "title": "Northing",
               "default": -999.0,
               "type": "number"
            },
            "elevation": {
               "title": "Elevation",
               "default": -999.0,
               "type": "number"
            },
            "chans_metadata": {
               "title": "Chans Metadata",
               "type": "object",
               "additionalProperties": {
                  "$ref": "#/definitions/ChanMetadata"
               }
            },
            "levels_metadata": {
               "title": "Levels Metadata",
               "type": "array",
               "items": {
                  "$ref": "#/definitions/SpectraLevelMetadata"
               }
            },
            "ref_time": {
               "title": "Ref Time",
               "pattern": "%Y-%m-%d %H:%M:%S.%f_%o_%q_%v",
               "examples": [
                  "2021-01-01 00:00:00.000061_035156_250000_000000"
               ]
            },
            "history": {
               "title": "History",
               "default": {
                  "records": []
               },
               "allOf": [
                  {
                     "$ref": "#/definitions/History"
                  }
               ]
            }
         },
         "required": [
            "fs",
            "chans",
            "n_levels",
            "first_time",
            "last_time",
            "chans_metadata",
            "levels_metadata",
            "ref_time"
         ]
      }
   }
}

field contributors: Dict[str, Union[resistics.gather.SiteCombinedMetadata, resistics.spectra.SpectraMetadata]] [Required]

Details about the data contributing to the regression input data

field history: resistics.common.History = History(records=[])

The processing history

class resistics.regression.RegressionInputData(metadata: resistics.regression.RegressionInputMetadata, tf: resistics.transfunc.TransferFunction, freqs: List[float], obs: List[Dict[str, numpy.ndarray]], preds: List[numpy.ndarray])[source]

Bases: resistics.common.ResisticsData

Class to hold data that will be input into a solver

The purpose of regression input data is to provision for many different solvers and user written solvers.

The regression input data has the following key attributes:

  • freqs

  • obs

  • preds

The freqs attribute is a 1-D array of evaluation frequencies.

The obs attribute is a dictionary of dictionaries. The parent dictionary has a key of the evaluation frequency index. The secondary dictionary has key of output channel. The values in the secondary dictionary are the observations for that output channel and have 1-D size:

(n_wins x n_cross_chans x 2).

The factor of 2 is because the real and complex parts of each equation are separated into two equations to allow use of solvers that work exclusively on real data.

The preds attribute is a single level dictionary with key of evaluation frequency index and value of the predictors for the evaluation frequency. The predictors have 2-D shape:

(n_wins x n_cross_chans x 2) x (n_input_channels x 2).

The number of windows is multiplied by 2 for the same reason as the observations. The doubling of the input channels is because one is the predictor for the real part of that transfer function component and one is the predictor for the complex part of the transfer function component.

Considering the impedance tensor as an example with:

  • output channels Ex, Ey

  • input channels Hx, Hy

  • cross channels Hx, Hy

The below shows the arrays for the 0 index evaluation frequency:

Observations

  • Ex: [w1_crossHx_RE, w1_crossHx_IM, w1_crossHy_RE, w1_crossHy_IM]

  • Ey: [w1_crossHx_RE, w1_crossHx_IM, w1_crossHy_RE, w1_crossHy_IM]

Predictors Ex

  • w1_crossHx_RE: Zxx_RE Zxx_IM Zxy_RE Zxy_IM

  • w1_crossHx_IM: Zxx_RE Zxx_IM Zxy_RE Zxy_IM

  • w1_crossHy_RE: Zxx_RE Zxx_IM Zxy_RE Zxy_IM

  • w1_crossHy_IM: Zxx_RE Zxx_IM Zxy_RE Zxy_IM

Predictors Ey

  • w1_crossHx_RE: Zyx_RE Zyx_IM Zyy_RE Zyy_IM

  • w1_crossHx_IM: Zyx_RE Zyx_IM Zyy_RE Zyy_IM

  • w1_crossHy_RE: Zyx_RE Zyx_IM Zyy_RE Zyy_IM

  • w1_crossHy_IM: Zyx_RE Zyx_IM Zyy_RE Zyy_IM

Note that the predictors are the same regardless of the output channel, only the observations change.

property n_freqs: int

Get the number of frequencies

get_inputs(freq_idx: int, out_chan: str) Tuple[numpy.ndarray, numpy.ndarray][source]

Get observations and predictions

Parameters
  • freq_idx (int) – The evaluation frequency index

  • out_chan (str) – The output channel

Returns

Observations and predictons

Return type

Tuple[np.ndarray, np.ndarray]

pydantic model resistics.regression.RegressionPreparerSpectra[source]

Bases: resistics.common.ResisticsProcess

Prepare regression data directly from spectra data

This can be useful for running a single measurement

Show JSON schema
{
   "title": "RegressionPreparerSpectra",
   "description": "Prepare regression data directly from spectra data\n\nThis can be useful for running a single measurement",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      }
   }
}

run(tf: resistics.transfunc.TransferFunction, spec_data: resistics.spectra.SpectraData) resistics.regression.RegressionInputData[source]

Construct the linear equation for solving

field name: Optional[str] [Required]
Validated by
  • validate_name

pydantic model resistics.regression.RegressionPreparerGathered[source]

Bases: resistics.common.ResisticsProcess

Regression preparer for gathered data

In nearly all cases, this is the regresson preparer to use. As input, it requires GatheredData.

Show JSON schema
{
   "title": "RegressionPreparerGathered",
   "description": "Regression preparer for gathered data\n\nIn nearly all cases, this is the regresson preparer to use. As input, it\nrequires GatheredData.",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      }
   }
}

run(tf: resistics.transfunc.TransferFunction, gathered_data: resistics.gather.GatheredData) resistics.regression.RegressionInputData[source]

Create the RegressionInputData

Parameters
Returns

Data that can be used as input into a solver

Return type

RegressionInputData

field name: Optional[str] [Required]
Validated by
  • validate_name

pydantic model resistics.regression.Solution[source]

Bases: resistics.common.WriteableMetadata

Class to hold a transfer function solution

Examples

>>> from resistics.testing import solution_mt
>>> solution = solution_mt()
>>> print(solution.tf.to_string())
| Ex | = | Ex_Hx Ex_Hy | | Hx |
| Ey |   | Ey_Hx Ey_Hy | | Hy |
>>> solution.n_freqs
5
>>> solution.freqs
[10.0, 20.0, 30.0, 40.0, 50.0]
>>> solution.periods.tolist()
[0.1, 0.05, 0.03333333333333333, 0.025, 0.02]
>>> solution.components["ExHx"]
Component(real=[1.0, 1.0, 2.0, 2.0, 3.0], imag=[5.0, 5.0, 4.0, 4.0, 3.0])
>>> solution.components["ExHy"]
Component(real=[1.0, 2.0, 3.0, 4.0, 5.0], imag=[-5.0, -4.0, -3.0, -2.0, -1.0])

To get the components as an array, either get_component or subscripting be used

>>> solution["ExHy"]
array([1.-5.j, 2.-4.j, 3.-3.j, 4.-2.j, 5.-1.j])
>>> solution["ab"]
Traceback (most recent call last):
...
ValueError: Component ab not found in ['ExHx', 'ExHy', 'EyHx', 'EyHy']

It is also possible to get the tensor values at a particular evaluation frequency

>>> solution.get_tensor(2)
array([[ 2.+4.j,  3.-3.j],
       [-3.+3.j, -2.-4.j]])

Show JSON schema
{
   "title": "Solution",
   "description": "Class to hold a transfer function solution\n\nExamples\n--------\n>>> from resistics.testing import solution_mt\n>>> solution = solution_mt()\n>>> print(solution.tf.to_string())\n| Ex | = | Ex_Hx Ex_Hy | | Hx |\n| Ey |   | Ey_Hx Ey_Hy | | Hy |\n>>> solution.n_freqs\n5\n>>> solution.freqs\n[10.0, 20.0, 30.0, 40.0, 50.0]\n>>> solution.periods.tolist()\n[0.1, 0.05, 0.03333333333333333, 0.025, 0.02]\n>>> solution.components[\"ExHx\"]\nComponent(real=[1.0, 1.0, 2.0, 2.0, 3.0], imag=[5.0, 5.0, 4.0, 4.0, 3.0])\n>>> solution.components[\"ExHy\"]\nComponent(real=[1.0, 2.0, 3.0, 4.0, 5.0], imag=[-5.0, -4.0, -3.0, -2.0, -1.0])\n\nTo get the components as an array, either get_component or subscripting\nbe used\n\n>>> solution[\"ExHy\"]\narray([1.-5.j, 2.-4.j, 3.-3.j, 4.-2.j, 5.-1.j])\n>>> solution[\"ab\"]\nTraceback (most recent call last):\n...\nValueError: Component ab not found in ['ExHx', 'ExHy', 'EyHx', 'EyHy']\n\nIt is also possible to get the tensor values at a particular evaluation\nfrequency\n\n>>> solution.get_tensor(2)\narray([[ 2.+4.j,  3.-3.j],\n       [-3.+3.j, -2.-4.j]])",
   "type": "object",
   "properties": {
      "file_info": {
         "$ref": "#/definitions/ResisticsFile"
      },
      "tf": {
         "$ref": "#/definitions/TransferFunction"
      },
      "freqs": {
         "title": "Freqs",
         "type": "array",
         "items": {
            "type": "number"
         }
      },
      "components": {
         "title": "Components",
         "type": "object",
         "additionalProperties": {
            "$ref": "#/definitions/Component"
         }
      },
      "history": {
         "$ref": "#/definitions/History"
      },
      "contributors": {
         "title": "Contributors",
         "type": "object",
         "additionalProperties": {
            "anyOf": [
               {
                  "$ref": "#/definitions/SiteCombinedMetadata"
               },
               {
                  "$ref": "#/definitions/SpectraMetadata"
               }
            ]
         }
      }
   },
   "required": [
      "tf",
      "freqs",
      "components",
      "history",
      "contributors"
   ],
   "definitions": {
      "ResisticsFile": {
         "title": "ResisticsFile",
         "description": "Required information for writing out a resistics file",
         "type": "object",
         "properties": {
            "created_on_local": {
               "title": "Created On Local",
               "type": "string",
               "format": "date-time"
            },
            "created_on_utc": {
               "title": "Created On Utc",
               "type": "string",
               "format": "date-time"
            },
            "version": {
               "title": "Version",
               "type": "string"
            }
         }
      },
      "TransferFunction": {
         "title": "TransferFunction",
         "description": "Define a generic transfer function\n\nThis class is a describes generic transfer function, including:\n\n- The output channels for the transfer function\n- The input channels for the transfer function\n- The cross channels for the transfer function\n\nThe cross channels are the channels that will be used to calculate out the\ncross powers for the regression.\n\nThis generic parent class has no implemented plotting function. However,\nchild classes may have a plotting function as different transfer functions\nmay need different types of plots.\n\n.. note::\n\n    Users interested in writing a custom transfer function should inherit\n    from this generic Transfer function\n\nSee Also\n--------\nImpandanceTensor : Transfer function for the MT impedance tensor\nTipper : Transfer function for the MT tipper\n\nExamples\n--------\nA generic example\n\n>>> tf = TransferFunction(variation=\"example\", out_chans=[\"bye\", \"see you\", \"ciao\"], in_chans=[\"hello\", \"hi_there\"])\n>>> print(tf.to_string())\n| bye      |   | bye_hello         bye_hi_there      | | hello    |\n| see you  | = | see you_hello     see you_hi_there  | | hi_there |\n| ciao     |   | ciao_hello        ciao_hi_there     |\n\nCombining the impedance tensor and the tipper into one TransferFunction\n\n>>> tf = TransferFunction(variation=\"combined\", out_chans=[\"Ex\", \"Ey\"], in_chans=[\"Hx\", \"Hy\", \"Hz\"])\n>>> print(tf.to_string())\n| Ex |   | Ex_Hx Ex_Hy Ex_Hz | | Hx |\n| Ey | = | Ey_Hx Ey_Hy Ey_Hz | | Hy |\n                               | Hz |",
         "type": "object",
         "properties": {
            "name": {
               "title": "Name",
               "type": "string"
            },
            "variation": {
               "title": "Variation",
               "default": "generic",
               "maxLength": 16,
               "type": "string"
            },
            "out_chans": {
               "title": "Out Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "in_chans": {
               "title": "In Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "cross_chans": {
               "title": "Cross Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "n_out": {
               "title": "N Out",
               "type": "integer"
            },
            "n_in": {
               "title": "N In",
               "type": "integer"
            },
            "n_cross": {
               "title": "N Cross",
               "type": "integer"
            }
         },
         "required": [
            "out_chans",
            "in_chans"
         ]
      },
      "Component": {
         "title": "Component",
         "description": "Data class for a single component in a Transfer function\n\nExample\n-------\n>>> from resistics.transfunc import Component\n>>> component = Component(real=[1, 2, 3, 4, 5], imag=[-5, -4, -3, -2 , -1])\n>>> component.get_value(0)\n(1-5j)\n>>> component.to_numpy()\narray([1.-5.j, 2.-4.j, 3.-3.j, 4.-2.j, 5.-1.j])",
         "type": "object",
         "properties": {
            "real": {
               "title": "Real",
               "type": "array",
               "items": {
                  "type": "number"
               }
            },
            "imag": {
               "title": "Imag",
               "type": "array",
               "items": {
                  "type": "number"
               }
            }
         },
         "required": [
            "real",
            "imag"
         ]
      },
      "Record": {
         "title": "Record",
         "description": "Class to hold a record\n\nA record holds information about a process that was run. It is intended to\ntrack processes applied to data, allowing a process history to be saved\nalong with any datasets.\n\nExamples\n--------\nA simple example of creating a process record\n\n>>> from resistics.common import Record\n>>> messages = [\"message 1\", \"message 2\"]\n>>> record = Record(\n...     creator={\"name\": \"example\", \"parameter1\": 15},\n...     messages=messages,\n...     record_type=\"example\"\n... )\n>>> record.summary()\n{\n    'time_local': '...',\n    'time_utc': '...',\n    'creator': {'name': 'example', 'parameter1': 15},\n    'messages': ['message 1', 'message 2'],\n    'record_type': 'example'\n}",
         "type": "object",
         "properties": {
            "time_local": {
               "title": "Time Local",
               "type": "string",
               "format": "date-time"
            },
            "time_utc": {
               "title": "Time Utc",
               "type": "string",
               "format": "date-time"
            },
            "creator": {
               "title": "Creator",
               "type": "object"
            },
            "messages": {
               "title": "Messages",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "record_type": {
               "title": "Record Type",
               "type": "string"
            }
         },
         "required": [
            "creator",
            "messages",
            "record_type"
         ]
      },
      "History": {
         "title": "History",
         "description": "Class for storing processing history\n\nParameters\n----------\nrecords : List[Record], optional\n    List of records, by default []\n\nExamples\n--------\n>>> from resistics.testing import record_example1, record_example2\n>>> from resistics.common import History\n>>> record1 = record_example1()\n>>> record2 = record_example2()\n>>> history = History(records=[record1, record2])\n>>> history.summary()\n{\n    'records': [\n        {\n            'time_local': '...',\n            'time_utc': '...',\n            'creator': {\n                'name': 'example1',\n                'a': 5,\n                'b': -7.0\n            },\n            'messages': ['Message 1', 'Message 2'],\n            'record_type': 'process'\n        },\n        {\n            'time_local': '...',\n            'time_utc': '...',\n            'creator': {\n                'name': 'example2',\n                'a': 'parzen',\n                'b': -21\n            },\n            'messages': ['Message 5', 'Message 6'],\n            'record_type': 'process'\n        }\n    ]\n}",
         "type": "object",
         "properties": {
            "records": {
               "title": "Records",
               "default": [],
               "type": "array",
               "items": {
                  "$ref": "#/definitions/Record"
               }
            }
         }
      },
      "SiteCombinedMetadata": {
         "title": "SiteCombinedMetadata",
         "description": "Metadata for combined data\n\nCombined metadata stores metadata for measurements that are combined from\na single site.",
         "type": "object",
         "properties": {
            "file_info": {
               "$ref": "#/definitions/ResisticsFile"
            },
            "site_name": {
               "title": "Site Name",
               "type": "string"
            },
            "fs": {
               "title": "Fs",
               "type": "number"
            },
            "system": {
               "title": "System",
               "default": "",
               "type": "string"
            },
            "serial": {
               "title": "Serial",
               "default": "",
               "type": "string"
            },
            "wgs84_latitude": {
               "title": "Wgs84 Latitude",
               "default": -999.0,
               "type": "number"
            },
            "wgs84_longitude": {
               "title": "Wgs84 Longitude",
               "default": -999.0,
               "type": "number"
            },
            "easting": {
               "title": "Easting",
               "default": -999.0,
               "type": "number"
            },
            "northing": {
               "title": "Northing",
               "default": -999.0,
               "type": "number"
            },
            "elevation": {
               "title": "Elevation",
               "default": -999.0,
               "type": "number"
            },
            "measurements": {
               "title": "Measurements",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "chans": {
               "title": "Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "n_evals": {
               "title": "N Evals",
               "type": "integer"
            },
            "eval_freqs": {
               "title": "Eval Freqs",
               "type": "array",
               "items": {
                  "type": "number"
               }
            },
            "histories": {
               "title": "Histories",
               "type": "object",
               "additionalProperties": {
                  "$ref": "#/definitions/History"
               }
            }
         },
         "required": [
            "site_name",
            "fs",
            "chans",
            "n_evals",
            "eval_freqs",
            "histories"
         ]
      },
      "ChanMetadata": {
         "title": "ChanMetadata",
         "description": "Channel metadata",
         "type": "object",
         "properties": {
            "name": {
               "title": "Name",
               "type": "string"
            },
            "data_files": {
               "title": "Data Files",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "chan_type": {
               "title": "Chan Type",
               "type": "string"
            },
            "chan_source": {
               "title": "Chan Source",
               "type": "string"
            },
            "sensor": {
               "title": "Sensor",
               "default": "",
               "type": "string"
            },
            "serial": {
               "title": "Serial",
               "default": "",
               "type": "string"
            },
            "gain1": {
               "title": "Gain1",
               "default": 1,
               "type": "number"
            },
            "gain2": {
               "title": "Gain2",
               "default": 1,
               "type": "number"
            },
            "scaling": {
               "title": "Scaling",
               "default": 1,
               "type": "number"
            },
            "chopper": {
               "title": "Chopper",
               "default": false,
               "type": "boolean"
            },
            "dipole_dist": {
               "title": "Dipole Dist",
               "default": 1,
               "type": "number"
            },
            "sensor_calibration_file": {
               "title": "Sensor Calibration File",
               "default": "",
               "type": "string"
            },
            "instrument_calibration_file": {
               "title": "Instrument Calibration File",
               "default": "",
               "type": "string"
            }
         },
         "required": [
            "name"
         ]
      },
      "SpectraLevelMetadata": {
         "title": "SpectraLevelMetadata",
         "description": "Metadata for spectra of a windowed decimation level",
         "type": "object",
         "properties": {
            "fs": {
               "title": "Fs",
               "type": "number"
            },
            "n_wins": {
               "title": "N Wins",
               "type": "integer"
            },
            "win_size": {
               "title": "Win Size",
               "exclusiveMinimum": 0,
               "type": "integer"
            },
            "olap_size": {
               "title": "Olap Size",
               "exclusiveMinimum": 0,
               "type": "integer"
            },
            "index_offset": {
               "title": "Index Offset",
               "type": "integer"
            },
            "n_freqs": {
               "title": "N Freqs",
               "type": "integer"
            },
            "freqs": {
               "title": "Freqs",
               "type": "array",
               "items": {
                  "type": "number"
               }
            }
         },
         "required": [
            "fs",
            "n_wins",
            "win_size",
            "olap_size",
            "index_offset",
            "n_freqs",
            "freqs"
         ]
      },
      "SpectraMetadata": {
         "title": "SpectraMetadata",
         "description": "Metadata for spectra data",
         "type": "object",
         "properties": {
            "file_info": {
               "$ref": "#/definitions/ResisticsFile"
            },
            "fs": {
               "title": "Fs",
               "type": "array",
               "items": {
                  "type": "number"
               }
            },
            "chans": {
               "title": "Chans",
               "type": "array",
               "items": {
                  "type": "string"
               }
            },
            "n_chans": {
               "title": "N Chans",
               "type": "integer"
            },
            "n_levels": {
               "title": "N Levels",
               "type": "integer"
            },
            "first_time": {
               "title": "First Time",
               "pattern": "%Y-%m-%d %H:%M:%S.%f_%o_%q_%v",
               "examples": [
                  "2021-01-01 00:00:00.000061_035156_250000_000000"
               ]
            },
            "last_time": {
               "title": "Last Time",
               "pattern": "%Y-%m-%d %H:%M:%S.%f_%o_%q_%v",
               "examples": [
                  "2021-01-01 00:00:00.000061_035156_250000_000000"
               ]
            },
            "system": {
               "title": "System",
               "default": "",
               "type": "string"
            },
            "serial": {
               "title": "Serial",
               "default": "",
               "type": "string"
            },
            "wgs84_latitude": {
               "title": "Wgs84 Latitude",
               "default": -999.0,
               "type": "number"
            },
            "wgs84_longitude": {
               "title": "Wgs84 Longitude",
               "default": -999.0,
               "type": "number"
            },
            "easting": {
               "title": "Easting",
               "default": -999.0,
               "type": "number"
            },
            "northing": {
               "title": "Northing",
               "default": -999.0,
               "type": "number"
            },
            "elevation": {
               "title": "Elevation",
               "default": -999.0,
               "type": "number"
            },
            "chans_metadata": {
               "title": "Chans Metadata",
               "type": "object",
               "additionalProperties": {
                  "$ref": "#/definitions/ChanMetadata"
               }
            },
            "levels_metadata": {
               "title": "Levels Metadata",
               "type": "array",
               "items": {
                  "$ref": "#/definitions/SpectraLevelMetadata"
               }
            },
            "ref_time": {
               "title": "Ref Time",
               "pattern": "%Y-%m-%d %H:%M:%S.%f_%o_%q_%v",
               "examples": [
                  "2021-01-01 00:00:00.000061_035156_250000_000000"
               ]
            },
            "history": {
               "title": "History",
               "default": {
                  "records": []
               },
               "allOf": [
                  {
                     "$ref": "#/definitions/History"
                  }
               ]
            }
         },
         "required": [
            "fs",
            "chans",
            "n_levels",
            "first_time",
            "last_time",
            "chans_metadata",
            "levels_metadata",
            "ref_time"
         ]
      }
   }
}

field tf: resistics.transfunc.TransferFunction [Required]

The transfer function that was solved

field freqs: List[float] [Required]

The evaluation frequencies

field components: Dict[str, resistics.transfunc.Component] [Required]

The solution

field history: resistics.common.History [Required]

The processing history

field contributors: Dict[str, Union[resistics.gather.SiteCombinedMetadata, resistics.spectra.SpectraMetadata]] [Required]

The contributors to the solution with their respective details

property n_freqs

Get the number of evaluation frequencies

property periods: numpy.ndarray

Get the periods

get_component(key: str) numpy.ndarray[source]

Get the solution for a single component for all the evaluation frequencies

Parameters

key (str) – The component key

Returns

The component data in an array

Return type

np.ndarray

Raises

ValueError – If the component does not exist in the solution

get_tensor(eval_idx: int) numpy.ndarray[source]

Get the tensor at a single evaluation frequency

Parameters

eval_idx (int) – The index of the evaluation frequency

Returns

The tensor as a numpy array

Return type

np.ndarray

pydantic model resistics.regression.Solver[source]

Bases: resistics.common.ResisticsProcess

General resistics solver

Show JSON schema
{
   "title": "Solver",
   "description": "General resistics solver",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      }
   }
}

run(regression_input: resistics.regression.RegressionInputData) resistics.regression.Solution[source]

Every solver should have a run method

field name: Optional[str] [Required]
Validated by
  • validate_name

pydantic model resistics.regression.SolverScikit[source]

Bases: resistics.regression.Solver

Base class for Scikit learn solvers

Show JSON schema
{
   "title": "SolverScikit",
   "description": "Base class for Scikit learn solvers",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "fit_intercept": {
         "title": "Fit Intercept",
         "default": false,
         "type": "boolean"
      },
      "normalize": {
         "title": "Normalize",
         "default": false,
         "type": "boolean"
      }
   }
}

field fit_intercept: bool = False

Flag for adding an intercept term

field normalize: bool = False

Flag for normalizing, only used if fit_intercept is True

pydantic model resistics.regression.SolverScikitOLS[source]

Bases: resistics.regression.SolverScikit

Ordinary least squares solver

This is simply a wrapper around the scikit learn least squares regression https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

Show JSON schema
{
   "title": "SolverScikitOLS",
   "description": "Ordinary least squares solver\n\nThis is simply a wrapper around the scikit learn least squares regression\nhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "fit_intercept": {
         "title": "Fit Intercept",
         "default": false,
         "type": "boolean"
      },
      "normalize": {
         "title": "Normalize",
         "default": false,
         "type": "boolean"
      },
      "n_jobs": {
         "title": "N Jobs",
         "default": -2,
         "type": "integer"
      }
   }
}

field n_jobs: int = -2

Number of jobs to run

run(regression_input: resistics.regression.RegressionInputData) resistics.regression.Solution[source]

Run ordinary least squares regression on the RegressionInputData

pydantic model resistics.regression.SolverScikitHuber[source]

Bases: resistics.regression.SolverScikit

Scikit Huber solver

This is simply a wrapper around the scikit learn Huber Regressor. For more information, please see https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.HuberRegressor.html

Show JSON schema
{
   "title": "SolverScikitHuber",
   "description": "Scikit Huber solver\n\nThis is simply a wrapper around the scikit learn Huber Regressor. For\nmore information, please see\nhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.HuberRegressor.html",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "fit_intercept": {
         "title": "Fit Intercept",
         "default": false,
         "type": "boolean"
      },
      "normalize": {
         "title": "Normalize",
         "default": false,
         "type": "boolean"
      },
      "epsilon": {
         "title": "Epsilon",
         "default": 1,
         "type": "number"
      }
   }
}

field epsilon: float = 1

The smaller the epsilon, the more robust it is to outliers.

run(regression_input: resistics.regression.RegressionInputData) resistics.regression.Solution[source]

Run Huber Regressor regression on the RegressionInputData

pydantic model resistics.regression.SolverScikitTheilSen[source]

Bases: resistics.regression.SolverScikit

Scikit Theil Sen solver

This is simply a wrapper around the scikit learn Theil Sen Regressor. For more information, please see https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.TheilSenRegressor.html

Show JSON schema
{
   "title": "SolverScikitTheilSen",
   "description": "Scikit Theil Sen solver\n\nThis is simply a wrapper around the scikit learn Theil Sen Regressor. For\nmore information, please see\nhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.TheilSenRegressor.html",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "fit_intercept": {
         "title": "Fit Intercept",
         "default": false,
         "type": "boolean"
      },
      "normalize": {
         "title": "Normalize",
         "default": false,
         "type": "boolean"
      },
      "n_jobs": {
         "title": "N Jobs",
         "default": -2,
         "type": "integer"
      },
      "max_subpopulation": {
         "title": "Max Subpopulation",
         "default": 2000,
         "type": "integer"
      },
      "n_subsamples": {
         "title": "N Subsamples",
         "type": "number"
      }
   }
}

field n_jobs: int = -2

Number of jobs to run

field max_subpopulation: int = 2000

Maximum population. Reduce this if the process is taking a long time

field n_subsamples: Optional[float] = None

Number of rows to use for each solution

run(regression_input: resistics.regression.RegressionInputData) resistics.regression.Solution[source]

Run TheilSen regression on the RegressionInputData

pydantic model resistics.regression.SolverScikitRANSAC[source]

Bases: resistics.regression.SolverScikit

Run a RANSAC solver with LinearRegression as Base Estimator

This is a wrapper around the scikit learn RANSAC regressor. More information can be found here https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RANSACRegressor.html

Show JSON schema
{
   "title": "SolverScikitRANSAC",
   "description": "Run a RANSAC solver with LinearRegression as Base Estimator\n\nThis is a wrapper around the scikit learn RANSAC regressor. More information\ncan be found here\nhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RANSACRegressor.html",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "fit_intercept": {
         "title": "Fit Intercept",
         "default": false,
         "type": "boolean"
      },
      "normalize": {
         "title": "Normalize",
         "default": false,
         "type": "boolean"
      },
      "min_samples": {
         "title": "Min Samples",
         "default": 0.8,
         "type": "number"
      },
      "max_trials": {
         "title": "Max Trials",
         "default": 20,
         "type": "integer"
      }
   }
}

field min_samples: float = 0.8

Minimum number of samples in each solution as a proportion of total

field max_trials: int = 20

The maximum number of trials to run

run(regression_input: resistics.regression.RegressionInputData) resistics.regression.Solution[source]

Run RANSAC regression on the RegressionInputData

pydantic model resistics.regression.SolverScikitWLS[source]

Bases: resistics.regression.SolverScikitOLS

Weighted least squares solver

Warning

This is homespun and is currently only experimental

This is simply a wrapper around the scikit learn least squares regression using the sample_weight option https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

Show JSON schema
{
   "title": "SolverScikitWLS",
   "description": "Weighted least squares solver\n\n.. warning::\n\n    This is homespun and is currently only experimental\n\nThis is simply a wrapper around the scikit learn least squares regression\nusing the sample_weight option\nhttps://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "fit_intercept": {
         "title": "Fit Intercept",
         "default": false,
         "type": "boolean"
      },
      "normalize": {
         "title": "Normalize",
         "default": false,
         "type": "boolean"
      },
      "n_jobs": {
         "title": "N Jobs",
         "default": -2,
         "type": "integer"
      },
      "n_iter": {
         "title": "N Iter",
         "default": 50,
         "type": "integer"
      }
   }
}

field n_jobs: int = -2

Number of jobs to run

field n_iter: int = 50

Number of iterations before quitting if residual is not low enough

bisquare(r: numpy.ndarray, k: float = 4.685) numpy.ndarray[source]

Bisquare location weights

Parameters
  • r (np.ndarray) – Residuals

  • k (float, None) – Tuning parameter. If None, a standard value will be used.

Returns

weights – The robust weights

Return type

np.ndarray

huber(r: numpy.ndarray, k: float = 1.345) numpy.ndarray[source]

Huber location weights

Parameters
  • r (np.ndarray) – Residuals

  • k (float) – Tuning parameter. If None, a standard value will be used.

Returns

weights – The robust weights

Return type

np.ndarray

trimmed_mean(r: numpy.ndarray, k: float = 2) numpy.ndarray[source]

Trimmed mean location weights

Parameters
  • r (np.ndarray) – Residuals

  • k (float) – Tuning parameter. If None, a standard value will be used.

Returns

weights – The robust weights

Return type

np.ndarray