def test_TopHat(): test_dataset = cp.CSDM( dependent_variables=[cp.as_dependent_variable(np.ones(500))], dimensions=[cp.LinearDimension(500, "1 s")], ) processor = sp.SignalProcessor() processor.operations = [ sp.apodization.TopHat(rising_edge="100 s", falling_edge="400 s") ] rise_and_fall_dataset = processor.apply_operations(test_dataset.copy()) rise_and_fall_should_be = np.zeros(500) rise_and_fall_should_be[100:400] = 1 assert np.allclose(rise_and_fall_dataset.y[0].components, rise_and_fall_should_be) processor.operations = [sp.apodization.TopHat(rising_edge="100 s")] rise_only_dataset = processor.apply_operations(test_dataset.copy()) rise_only_should_be = np.zeros(500) rise_only_should_be[100:] = 1 assert np.allclose(rise_only_dataset.y[0].components, rise_only_should_be) processor.operations = [sp.apodization.TopHat(falling_edge="400 s")] fall_only_dataset = processor.apply_operations(test_dataset.copy()) fall_only_should_be = np.zeros(500) fall_only_should_be[:400] = 1 assert np.allclose(fall_only_dataset.y[0].components, fall_only_should_be)
def _as_csdm_object(self, data: np.ndarray, method: Method) -> cp.CSDM: """Converts the simulation data from the given method to a CSDM object. Read `csdmpy <https://csdmpy.readthedocs.io/en/stable/>`_ for details Return: A CSDM object. """ dim = [sd.to_csdm_dimension() for sd in method.spectral_dimensions[::-1]] _ = [ item.to("ppm", "nmr_frequency_ratio") for item in dim if item.origin_offset != 0 ] dv = [ { "type": "internal", "quantity_type": "scalar", "numeric_type": "complex128", "components": [datum], **self._get_dv_metadata(index), } for index, datum in enumerate(data) ] return cp.CSDM(dimensions=dim, dependent_variables=dv)
def to_csdm(self): """ Return a csdm object containing data. Returns ------- data : csdm object CSDM object containing parameters and data """ try: import csdmpy as cp # self._oproc = {} # self._odtype = str(self._data.dtype) # create a list of dimension objects dimensions = [ cp.LinearDimension( count=value["size"], increment=f'{1 / value["sw"]} s', reciprocal={ "coordinates_offset": f'{value["car"]} Hz', "origin_offset": f'{value["obs"]} MHz', }, label=value["label"], ) for key, value in list(self._udic.items()) if type(key) == int and value["size"] != 1 ] return cp.CSDM( dimensions=dimensions[::-1], dependent_variables=[ cp.as_dependent_variable(self._data.copy()) ], ) except: raise ImportError( "csdmpy must be installed to use this function. Please install by typing 'pip install csdmpy' in the terminal." )
# %% # Here the applied offset will be the following function # # .. math:: # # f(x) = 0.00001 \cdot x^2 + 0.2 # # Next we create a CSDM object with a test dataset which our signal processor will # operate on. Here, the dataset spans 500 Hz with a delta function centered at # 100 Hz. test_data = np.zeros(500) test_data[350] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[ cp.LinearDimension(count=500, increment="1 Hz", complex_fft=True) ], ) # %% # Now to apply the processor to the CSDM object, use the # :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as # follows processed_dataset = processor.apply_operations(dataset=csdm_object.copy()).real # %% # To see the results of the exponential apodization, we create a simple plot using the # ``matplotlib`` library. import matplotlib.pyplot as plt fig, ax = plt.subplots(1,
import numpy as np from mrsimulator import signal_processing as sp __author__ = "Deepansh J. Srivastava" __email__ = "*****@*****.**" # Creating a test CSDM object. test_data = np.zeros((40, 40)) test_data[20, :] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[ cp.LinearDimension(count=40, increment="1 s", label="0", complex_fft=True), cp.Dimension(type="linear", count=40, increment="1 K", label="1", complex_fft=True), ], ) csdm_object2 = csdm_object.copy() def test_shear_01(): processor = sp.SignalProcessor(operations=[ sp.IFFT(dim_index=1), sp.affine.Shear(factor="-1 K/s", dim_index=1, parallel=0), sp.FFT(dim_index=1),
processor = sp.SignalProcessor( operations=[ sp.IFFT(), sp.apodization.TopHat(rising_edge="1 s", falling_edge="9 s"), sp.FFT(), ] ) # %% # Next we create a CSDM object with a test dataset which our signal processor will # operate on. Here, the dataset is a delta function centered at 0 Hz with a some # applied Gaussian line broadening. test_data = np.zeros(500) test_data[250] = 1 csdm_object = cp.CSDM( dependent_variables=[cp.as_dependent_variable(test_data)], dimensions=[cp.LinearDimension(count=500, increment="0.1 Hz", complex_fft=True)], ) # %% # To apply the previously defined signal processor, we use the # :py:meth:`~mrsimulator.signal_processor.SignalProcessor.apply_operations` method as # as follows processed_dataset = processor.apply_operations(dataset=csdm_object).real # %% # To see the results of the top hat apodization, we create a simple plot using the # ``matplotlib`` library. fig, ax = plt.subplots(1, 2, figsize=(8, 3.5), subplot_kw={"projection": "csdm"}) ax[0].plot(csdm_object, color="black", linewidth=1) ax[0].set_title("Before") ax[1].plot(processed_dataset.real, color="black", linewidth=1)
def generate_data(): dv1 = cp.as_dependent_variable(np.random.rand(20)) dv2 = cp.as_dependent_variable(np.random.rand(20)) dv3 = cp.as_dependent_variable(np.random.rand(20)) dim = cp.as_dimension(np.arange(20)) return cp.CSDM(dependent_variables=[dv1, dv2, dv3], dimensions=[dim])