Python numpy.isin() Examples

The following are 30 code examples for showing how to use numpy.isin(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: neuropythy   Author: noahbenson   File: core.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def dataframe_select(df, *cols, **filters):
    '''
    dataframe_select(df, k1=v1, k2=v2...) yields df after selecting all the columns in which the
      given keys (k1, k2, etc.) have been selected such that the associated columns in the dataframe
      contain only the rows whose cells match the given values.
    dataframe_select(df, col1, col2...) selects the given columns.
    dataframe_select(df, col1, col2..., k1=v1, k2=v2...) selects both.
    
    If a value is a tuple/list of 2 elements, then it is considered a range where cells must fall
    between the values. If value is a tuple/list of more than 2 elements or is a set of any length
    then it is a list of values, any one of which can match the cell.
    '''
    ii = np.ones(len(df), dtype='bool')
    for (k,v) in six.iteritems(filters):
        vals = df[k].values
        if   pimms.is_set(v):                    jj = np.isin(vals, list(v))
        elif pimms.is_vector(v) and len(v) == 2: jj = (v[0] <= vals) & (vals < v[1])
        elif pimms.is_vector(v):                 jj = np.isin(vals, list(v))
        else:                                    jj = (vals == v)
        ii = np.logical_and(ii, jj)
    if len(ii) != np.sum(ii): df = df.loc[ii]
    if len(cols) > 0: df = df[list(cols)]
    return df 
Example 2
Project: NeuroKit   Author: neuropsychology   File: emg_activation.py    License: MIT License 6 votes vote down vote up
def _emg_activation_activations(activity, duration_min=0.05):

    activations = events_find(activity, threshold=0.5, threshold_keep="above", duration_min=duration_min)
    activations["offset"] = activations["onset"] + activations["duration"]

    baseline = events_find(activity == 0, threshold=0.5, threshold_keep="above", duration_min=duration_min)
    baseline["offset"] = baseline["onset"] + baseline["duration"]

    # Cross-comparison
    valid = np.isin(activations["onset"], baseline["offset"])
    onsets = activations["onset"][valid]
    offsets = activations["offset"][valid]

    new_activity = np.array([])
    for x, y in zip(onsets, offsets):
        activated = np.arange(x, y)
        new_activity = np.append(new_activity, activated)

    # Prepare Output.
    info = {"EMG_Onsets": onsets, "EMG_Offsets": offsets, "EMG_Activity": new_activity}

    return info 
Example 3
Project: simnibs   Author: simnibs   File: mesh_io.py    License: GNU General Public License v3.0 6 votes vote down vote up
def find_all_elements_with_node(self, node_nr):
        """ Finds all elements that have a given node

        Parameters
        -----------------
        node_nr: int
            number of node

        Returns
        ---------------
        elm_nr: np.ndarray
            array with indices of element numbers

        """
        elm_with_node = np.any(
            np.isin(self.node_number_list, node_nr),
            axis=1)
        return self.elm_number[elm_with_node] 
Example 4
Project: simnibs   Author: simnibs   File: mesh_io.py    License: GNU General Public License v3.0 6 votes vote down vote up
def nodes_with_tag(self, tags):
        ''' Gets all nodes indexes that are part of at least one element with the given
        tags

        Parameters
        -----------
        tags: list
            Integer tags to search

        Returns
        -------------
        nodes: ndarray of integer
            Indexes of nodes with given tag
        '''
        nodes = np.unique(self[np.isin(self.tag1, tags)].reshape(-1))
        nodes = nodes[nodes > 0]
        return nodes 
Example 5
Project: simnibs   Author: simnibs   File: mesh_io.py    License: GNU General Public License v3.0 6 votes vote down vote up
def mean_field_norm(self):
        ''' Calculates V*w/sum(w)
        Where V is the norm of the field, and w is the volume or area of the mesh where
        the field is defined. This can be used as a focality metric. It should give out
        small values when the field is focal and

        Returns
        ----------
        eff_area: float
            Area or volume of mesh, weighted by the field
        '''
        self._test_msh()
        if np.all(np.isin([2, 4], self.mesh.elm.elm_type)):
            warnings.warn('Calculating effective volume/area of fields in meshes with'
                          ' triangles and tetrahedra can give misleading results')

        norm = self._norm()
        weights = self._weights()

        return np.sum(norm * weights) / np.sum(weights) 
Example 6
Project: recruit   Author: Frank-qlu   File: test_extras.py    License: Apache License 2.0 6 votes vote down vote up
def test_isin(self):
        # the tests for in1d cover most of isin's behavior
        # if in1d is removed, would need to change those tests to test
        # isin instead.
        a = np.arange(24).reshape([2, 3, 4])
        mask = np.zeros([2, 3, 4])
        mask[1, 2, 0] = 1
        a = array(a, mask=mask)
        b = array(data=[0, 10, 20, 30,  1,  3, 11, 22, 33],
                  mask=[0,  1,  0,  1,  0,  1,  0,  1,  0])
        ec = zeros((2, 3, 4), dtype=bool)
        ec[0, 0, 0] = True
        ec[0, 0, 1] = True
        ec[0, 2, 3] = True
        c = isin(a, b)
        assert_(isinstance(c, MaskedArray))
        assert_array_equal(c, ec)
        #compare results of np.isin to ma.isin
        d = np.isin(a, b[~b.mask]) & ~a.mask
        assert_array_equal(c, d) 
Example 7
Project: pytim   Author: Marcello-Sega   File: correlator.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _sample_intermittent(self, group):
        # we need to collect also the residence
        # function
        # the residence function (1 if in the reference group, 0 otherwise)
        mask = np.isin(self.reference, group)
        # append the residence function to its timeseries
        self.maskseries.append(list(mask))
        if self.observable is not None:
            # this copies a vector of zeros with the correct shape
            sampled = self.reference_obs.copy()
            obs = self.observable.compute(group)
            sampled[np.where(mask)] = obs
            self.timeseries.append(list(sampled.flatten()))
        else:
            self.timeseries = self.maskseries
            if self.shape is None:
                self.shape = (1, )
            sampled = mask
        return sampled 
Example 8
Project: pytorch_geometric   Author: rusty1s   File: test_sampler.py    License: MIT License 6 votes vote down vote up
def test_sampler():
    torch.manual_seed(12345)
    edge_index = erdos_renyi_graph(num_nodes=10, edge_prob=0.5)
    E = edge_index.size(1)

    loader = NeighborSampler(edge_index, sizes=[2, 4], batch_size=2)
    assert loader.__repr__() == 'NeighborSampler(sizes=[2, 4])'
    assert len(loader) == 5

    for batch_size, n_id, adjs in loader:
        assert batch_size == 2
        assert all(np.isin(n_id, torch.arange(10)).tolist())
        assert n_id.unique().size(0) == n_id.size(0)
        for (edge_index, e_id, size) in adjs:
            assert int(edge_index[0].max() + 1) <= size[0]
            assert int(edge_index[1].max() + 1) <= size[1]
            assert all(np.isin(e_id, torch.arange(E)).tolist())
            assert e_id.unique().size(0) == e_id.size(0)
            assert size[0] >= size[1]

    out = loader.sample([1, 2])
    assert len(out) == 3 
Example 9
Project: pyeo   Author: clcr   File: raster_manipulation.py    License: GNU General Public License v3.0 6 votes vote down vote up
def create_mask_from_class_map(class_map_path, out_path, classes_of_interest, buffer_size=0, out_resolution=None):
    """Creates a mask from a classification mask: 1 for each pixel containing one of classes_of_interest, otherwise 0"""
    # TODO: pull this out of the above function
    class_image = gdal.Open(class_map_path)
    class_array = class_image.GetVirtualMemArray()
    mask_array = np.isin(class_array, classes_of_interest)
    out_mask = create_matching_dataset(class_image, out_path, datatype=gdal.GDT_Byte)
    out_array = out_mask.GetVirtualMemArray(eAccess=gdal.GA_Update)
    np.copyto(out_array, mask_array)
    class_array = None
    class_image = None
    out_array = None
    out_mask = None
    if out_resolution:
        resample_image_in_place(out_path, out_resolution)
    if buffer_size:
        buffer_mask_in_place(out_path, buffer_size)
    return out_path 
Example 10
Project: pyeo   Author: clcr   File: raster_manipulation.py    License: GNU General Public License v3.0 6 votes vote down vote up
def create_mask_from_fmask(in_l1_dir, out_path):
    log = logging.getLogger(__name__)
    log.info("Creating fmask for {}".format(in_l1_dir))
    with TemporaryDirectory() as td:
        temp_fmask_path = os.path.join(td, "fmask.tif")
        apply_fmask(in_l1_dir, temp_fmask_path)
        fmask_image = gdal.Open(temp_fmask_path)
        fmask_array = fmask_image.GetVirtualMemArray()
        out_image = create_matching_dataset(fmask_image, out_path, datatype=gdal.GDT_Byte)
        out_array = out_image.GetVirtualMemArray(eAccess=gdal.GA_Update)
        log.info("fmask created, converting to binary cloud/shadow mask")
        out_array[:,:] = np.isin(fmask_array, (2, 3, 4), invert=True)
        out_array = None
        out_image = None
        fmask_array = None
        fmask_image = None
        resample_image_in_place(out_path, 10) 
Example 11
Project: vnpy_crypto   Author: birforce   File: test_extras.py    License: MIT License 6 votes vote down vote up
def test_isin(self):
        # the tests for in1d cover most of isin's behavior
        # if in1d is removed, would need to change those tests to test
        # isin instead.
        a = np.arange(24).reshape([2, 3, 4])
        mask = np.zeros([2, 3, 4])
        mask[1, 2, 0] = 1
        a = array(a, mask=mask)
        b = array(data=[0, 10, 20, 30,  1,  3, 11, 22, 33],
                  mask=[0,  1,  0,  1,  0,  1,  0,  1,  0])
        ec = zeros((2, 3, 4), dtype=bool)
        ec[0, 0, 0] = True
        ec[0, 0, 1] = True
        ec[0, 2, 3] = True
        c = isin(a, b)
        assert_(isinstance(c, MaskedArray))
        assert_array_equal(c, ec)
        #compare results of np.isin to ma.isin
        d = np.isin(a, b[~b.mask]) & ~a.mask
        assert_array_equal(c, d) 
Example 12
Project: seagull   Author: ljvmiranda921   File: rules.py    License: MIT License 6 votes vote down vote up
def life_rule(X: np.ndarray, rulestring: str) -> np.ndarray:
    """A generalized life rule that accepts a rulestring in B/S notation

    Rulestrings are commonly expressed in the B/S notation where B (birth) is a
    list of all numbers of live neighbors that cause a dead cell to come alive,
    and S (survival) is a list of all the numbers of live neighbors that cause
    a live cell to remain alive.

    Parameters
    ----------
    X : np.ndarray
        The input board matrix
    rulestring : str
        The rulestring in B/S notation

    Returns
    -------
    np.ndarray
        Updated board after applying the rule
    """
    birth_req, survival_req = _parse_rulestring(rulestring)
    neighbors = _count_neighbors(X)
    birth_rule = (X == 0) & (np.isin(neighbors, birth_req))
    survival_rule = (X == 1) & (np.isin(neighbors, survival_req))
    return birth_rule | survival_rule 
Example 13
Project: BrainSpace   Author: MICA-MNI   File: data_object.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def GetPolys2D(self):
        """Returns the polys as a 2D VTKArray instance.

        Returns
        -------
        polys : 2D ndarray, shape = (n_points, n)
            PolyData polys.

        Raises
        ------
        ValueError
            If PolyData has different poly types.
        """

        v = self.GetPolys()
        if v is None:
            return v

        ct = self.cell_types
        if np.isin([VTK_QUAD, VTK_TRIANGLE], ct).all() or VTK_POLYGON in ct:
            raise ValueError('PolyData contains different poly types')
        return v.reshape(-1, v[0] + 1)[:, 1:] 
Example 14
Project: pysheds   Author: mdbartos   File: rfsm.py    License: GNU General Public License v3.0 6 votes vote down vote up
def set_node_transfers(self):
        for index, mapping in enumerate(self.tmap):
            for pair, node in mapping.items():
                i, j = pair
                comm = int(node.comm)
                comm_elev = node.elev
                neighbors = Grid._select_surround_ravel(self, comm, self.dem.shape)
                ser = pd.DataFrame(np.column_stack([neighbors, self.dem.flat[neighbors],
                                                    self.ws[index].flat[neighbors]]))
                ser = ser[ser[2].isin(list(pair))]
                g = ser.groupby(2).idxmin()[1].apply(lambda x: ser.loc[x, 0])
                fullix = self.drop.flat[g.values.astype(int)]
                lv = self.dropmap.loc[fullix][0].values
                nm = self.dropmap.loc[fullix][1].values
                g = pd.DataFrame(np.column_stack([lv, nm]), index=g.index.values.astype(int),
                                columns=['level', 'name']).to_dict(orient='index')
                # Children will always be in numeric order from left to right
                lt, rt = g[j], g[i]
                node.l.t = self.nodes[lt['level']][lt['name']]
                node.r.t = self.nodes[rt['level']][rt['name']]
        self.set_singleton_transfer(self.root) 
Example 15
Project: pysheds   Author: mdbartos   File: rfsm.py    License: GNU General Public License v3.0 6 votes vote down vote up
def set_cumulative_capacities(self, node):
        if node.l:
            self.set_cumulative_capacities(node.l)
        if node.r:
            self.set_cumulative_capacities(node.r)
        if node.parent:
            if node.name:
                elevdiff = node.parent.elev - self.dem[self.ws[node.level] == node.name]
                vol = abs(np.asscalar(elevdiff[elevdiff > 0].sum()) * self.x * self.y)
                node.vol = vol
            else:
                leaves = []
                self.enumerate_leaves(node, level=node.level, stack=leaves)
                mask = np.isin(self.ws[node.level], leaves)
                boundary = list(chain.from_iterable([self.b[node.level].setdefault(pair, [])
                                                        for pair in combinations(leaves, 2)]))
                mask.flat[boundary] = True
                elevdiff = node.parent.elev - self.dem[mask]
                vol = abs(np.asscalar(elevdiff[elevdiff > 0].sum()) * self.x * self.y)
                node.vol = vol 
Example 16
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: test_extras.py    License: MIT License 6 votes vote down vote up
def test_isin(self):
        # the tests for in1d cover most of isin's behavior
        # if in1d is removed, would need to change those tests to test
        # isin instead.
        a = np.arange(24).reshape([2, 3, 4])
        mask = np.zeros([2, 3, 4])
        mask[1, 2, 0] = 1
        a = array(a, mask=mask)
        b = array(data=[0, 10, 20, 30,  1,  3, 11, 22, 33],
                  mask=[0,  1,  0,  1,  0,  1,  0,  1,  0])
        ec = zeros((2, 3, 4), dtype=bool)
        ec[0, 0, 0] = True
        ec[0, 0, 1] = True
        ec[0, 2, 3] = True
        c = isin(a, b)
        assert_(isinstance(c, MaskedArray))
        assert_array_equal(c, ec)
        #compare results of np.isin to ma.isin
        d = np.isin(a, b[~b.mask]) & ~a.mask
        assert_array_equal(c, d) 
Example 17
Project: gym-electric-motor   Author: upb-lea   File: test_core.py    License: MIT License 6 votes vote down vote up
def test_reward(self, monkeypatch, physical_system, reference_generator, observed_state_idx, violated_state_idx):
        observed_states = list(np.array(physical_system.state_names)[observed_state_idx])
        rf = RewardFunction(observed_states)
        rf.set_modules(physical_system, reference_generator)
        monkeypatch.setattr(rf, "_reward", self.mock_standard_reward)
        monkeypatch.setattr(rf, "_limit_violation_reward", self.mock_limit_violation_reward)
        state = np.ones_like(physical_system.state_names, dtype=float) * 0.5
        state[violated_state_idx] = 1.5
        reward, done = rf.reward(state, None)
        if np.any(np.isin(observed_state_idx, violated_state_idx)):
            assert reward == -1
            assert done
        else:
            assert reward == 1
            assert not done
        # Test negative limit violations
        state[violated_state_idx] = -1.5
        reward, done = rf.reward(state, None)

        if np.any(np.isin(observed_state_idx, violated_state_idx)):
            assert reward == -1
            assert done
        else:
            assert reward == 1
            assert not done 
Example 18
Project: batchflow   Author: analysiscenter   File: dataset.py    License: Apache License 2.0 6 votes vote down vote up
def create_subset(self, index):
        """ Create a dataset based on the given subset of indices

            Parameters
            ----------
            index : DatasetIndex or np.array

            Returns
            -------
            Dataset

            Raises
            ------
            IndexError
                When a user wants to create a subset from source dataset it is necessary to be confident
                that the index of new subset lies in the range of source dataset's index.
                If the index lies out of the source dataset index's range, the IndexError is raised.

        """
        indices = index.indices if isinstance(index, DatasetIndex) else index
        if not np.isin(indices, self.indices).all():
            raise IndexError
        return type(self).from_dataset(self, self.index.create_subset(index)) 
Example 19
Project: tape-neurips2019   Author: songlab-cal   File: RandomSequenceMask.py    License: MIT License 6 votes vote down vote up
def _generate_bert_mask(self, inputs):

        def _numpy_generate_contiguous_mask(array):
            mask = np.random.random(array.shape) < (1 / self.avg_seq_len)
            mask = np.cumsum(mask, 1)
            seqvals = np.max(mask)
            mask_prob = self.percentage * array.shape[1] / seqvals  # increase probability because fewer sequences
            vals_to_mask = np.arange(seqvals)[np.random.random((seqvals,)) < mask_prob]
            indices_to_mask = np.isin(mask, vals_to_mask)
            mask[indices_to_mask] = 1
            mask[~indices_to_mask] = 0

            return np.asarray(mask, np.bool)

        bert_mask = tf.py_func(_numpy_generate_contiguous_mask, [inputs], tf.bool)
        bert_mask.set_shape(inputs.shape)
        return bert_mask 
Example 20
Project: GraphicDesignPatternByPython   Author: Relph1119   File: test_extras.py    License: MIT License 6 votes vote down vote up
def test_isin(self):
        # the tests for in1d cover most of isin's behavior
        # if in1d is removed, would need to change those tests to test
        # isin instead.
        a = np.arange(24).reshape([2, 3, 4])
        mask = np.zeros([2, 3, 4])
        mask[1, 2, 0] = 1
        a = array(a, mask=mask)
        b = array(data=[0, 10, 20, 30,  1,  3, 11, 22, 33],
                  mask=[0,  1,  0,  1,  0,  1,  0,  1,  0])
        ec = zeros((2, 3, 4), dtype=bool)
        ec[0, 0, 0] = True
        ec[0, 0, 1] = True
        ec[0, 2, 3] = True
        c = isin(a, b)
        assert_(isinstance(c, MaskedArray))
        assert_array_equal(c, ec)
        #compare results of np.isin to ma.isin
        d = np.isin(a, b[~b.mask]) & ~a.mask
        assert_array_equal(c, d) 
Example 21
Project: georinex   Author: geospace-code   File: test_time.py    License: MIT License 6 votes vote down vote up
def test_tlim(fn, tlim, tref, tlen):
    """
    Important test, be sure it's runnable on all systems
    """
    dat = gr.load(fn, tlim=tlim)

    times = gr.to_datetime(dat.time)

    assert (times == tref).all()

    if dat.rinextype == 'obs' and 2 <= dat.version < 3:
        assert dat.fast_processing

    alltimes = gr.gettime(fn)
    assert isinstance(alltimes, np.ndarray)
    assert alltimes.size == tlen

    assert np.isin(times, alltimes).size == times.size


# %% currently, interval is only for OBS2 and OBS3 
Example 22
Project: brainiak   Author: brainiak   File: iem.py    License: Apache License 2.0 6 votes vote down vote up
def _check_params(self):
        if self.range_start >= self.range_stop:
            raise ValueError("range_start {} must be less than "
                             "{} range_stop.".format(self.range_start,
                                                     self.range_stop))
        if self.stimulus_mode == 'halfcircular':
            if (self.range_stop - self.range_start) != 180.:
                raise ValueError("For half-circular feature spaces,"
                                 "the range must be 180 degrees, "
                                 "not {}".format(self.range_stop
                                                 - self.range_start))
        elif self.stimulus_mode == 'circular':
            if (self.range_stop - self.range_start) != 360.:
                raise ValueError("For circular feature spaces, the"
                                 " range must be 360 degrees"
                                 "not {}".format(self.range_stop
                                                 - self.range_start))
        if self.n_channels < 2:
            raise ValueError("Insufficient number of channels.")
        if not np.isin(self.stimulus_mode, ['circular',
                                            'halfcircular']):
            raise ValueError("Stimulus mode must be one of these: "
                             "'circular', 'halfcircular'") 
Example 23
def test_isin(self):
        # the tests for in1d cover most of isin's behavior
        # if in1d is removed, would need to change those tests to test
        # isin instead.
        a = np.arange(24).reshape([2, 3, 4])
        mask = np.zeros([2, 3, 4])
        mask[1, 2, 0] = 1
        a = array(a, mask=mask)
        b = array(data=[0, 10, 20, 30,  1,  3, 11, 22, 33],
                  mask=[0,  1,  0,  1,  0,  1,  0,  1,  0])
        ec = zeros((2, 3, 4), dtype=bool)
        ec[0, 0, 0] = True
        ec[0, 0, 1] = True
        ec[0, 2, 3] = True
        c = isin(a, b)
        assert_(isinstance(c, MaskedArray))
        assert_array_equal(c, ec)
        #compare results of np.isin to ma.isin
        d = np.isin(a, b[~b.mask]) & ~a.mask
        assert_array_equal(c, d) 
Example 24
Project: neuropythy   Author: noahbenson   File: core.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def dataframe_except(df, *cols, **filters):
    '''
    dataframe_except(df, k1=v1, k2=v2...) yields df after selecting all the columns in which the
      given keys (k1, k2, etc.) have been selected such that the associated columns in the dataframe
      contain only the rows whose cells match the given values.
    dataframe_except(df, col1, col2...) selects all columns except for the given columns.
    dataframe_except(df, col1, col2..., k1=v1, k2=v2...) selects on both conditions.
    
    The dataframe_except() function is identical to the dataframe_select() function with the single
    difference being that the column names provided to dataframe_except() are dropped from the
    result while column names passed to dataframe_select() are kept.

    If a value is a tuple/list of 2 elements, then it is considered a range where cells must fall
    between the values. If value is a tuple/list of more than 2 elements or is a set of any length
    then it is a list of values, any one of which can match the cell.
    '''
    ii = np.ones(len(df), dtype='bool')
    for (k,v) in six.iteritems(filters):
        vals = df[k].values
        if   pimms.is_set(v):                    jj = np.isin(vals, list(v))
        elif pimms.is_vector(v) and len(v) == 2: jj = (v[0] <= vals) & (vals < v[1])
        elif pimms.is_vector(v):                 jj = np.isin(vals, list(v))
        else:                                    jj = (vals == v)
        ii = np.logical_and(ii, jj)
    if len(ii) != np.sum(ii): df = df.loc[ii]
    if len(cols) > 0: df = df.drop(list(cols), axis=1, inplace=False)
    return df 
Example 25
Project: simnibs   Author: simnibs   File: mesh_io.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_focality(self, cuttofs=[50, 70], peak_percentile=99.9):
        ''' Caluclates field focality as the area/volume of the mesh experiencing a field
        norm of above (cut_off% of the field peak). peak_percentile gives what is the
        field peak

        Parameters
        ------------
        cuttofs: ndarray (optional)
            Percentage of the peak value for the cut_off, between 0 and 100. Default: [50, 70]

        peak_percentile: float (optional)
            Percentile to be used to calculate peak value. Default: 99.9
        
        Returns
        ---------
        focality: ndarray
            Area/volume exceeding the cuttof of the peak value
        '''
        self._test_msh()
        if np.all(np.isin([2, 4], self.mesh.elm.elm_type)):
            warnings.warn('Calculating focality of fields in meshes with'
                          ' triangles and tetrahedra can give misleading results')

        norm = self._norm()
        s = np.argsort(norm)
        norm = norm[s]
        weights = self._weights()[s]
        weights_norm = np.cumsum(weights)
        weights_norm = weights_norm * 100 / weights_norm[-1]
        peak_value = norm[np.argmin(np.abs(weights_norm - peak_percentile))]

        co = np.array(cuttofs, dtype=float).reshape(-1) / 100
        focality = np.zeros(co.shape, dtype=int)
        for i, c in enumerate(co):
            focality[i] = np.sum(weights[norm > c * peak_value])

        return focality 
Example 26
Project: simnibs   Author: simnibs   File: optimize_tms.py    License: GNU General Public License v3.0 5 votes vote down vote up
def define_target_region(mesh, target_position, target_radius, tags, elm_type=4):
    ''' Defines a target based on a position, a radius and an element tag

    Paramters
    ------------
    mesh: simnibs.mesh_io.msh
        Mesh
    target_position: array of size (3,)
        Position of target
    target_radius: float
        Size of target
    tags: array of ints
        Tag where the target is located
    elm_type: int
        Type of target element (4 for tetrahedra and 2 for triangles)

    Returns
    -------
    elements: array of size (n,)
        Numbers (1-based) of elements in the tag
    '''
    bar = mesh.elements_baricenters()[:]
    dist = np.linalg.norm(bar - target_position, axis=1)
    elm = mesh.elm.elm_number[
        (dist < target_radius) *
        np.isin(mesh.elm.tag1, tags) *
        np.isin(mesh.elm.elm_type, elm_type)
    ]
    return elm 
Example 27
Project: simnibs   Author: simnibs   File: test_opt_struct.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_find_indexes_pos_elm(self, tissues, pos, sphere_surf, r):
        index, mapping = opt_struct._find_indexes(
            sphere_surf, 'element',
            positions=pos, radius=r,
            tissues=tissues)

        if tissues is None:
            elm_with_tag = sphere_surf.elm.elm_number
        else:
            elm_with_tag = sphere_surf.elm.elm_number[np.isin(sphere_surf.elm.tag1, tissues)]
        elms = sphere_surf.elements_baricenters()[elm_with_tag]
        roi = []
        mp = []
        pos = np.atleast_2d(pos)
        for i, p in enumerate(pos):
            dist = np.linalg.norm(p - elms, axis=1)
            idx_nearest = elm_with_tag[np.argmin(dist)]
            if r > 0:
                center = elms[np.argmin(dist)]
                dist_center = np.linalg.norm(center - elms, axis=1)
                n_roi = np.sum(dist_center <= r)
                roi.extend(elm_with_tag[dist_center <= r])
            else:
                n_roi = 1
                roi.append(idx_nearest)

            mp.extend(n_roi*(i,))

        assert np.all(np.sort(index) == np.sort(roi))
        assert np.all(np.sort(mapping) == np.sort(mp)) 
Example 28
Project: simnibs   Author: simnibs   File: test_optimize_tms.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_define_target_region(sphere3_msh):
    c = [85, 0, 0]
    r = 5
    t = 3
    elm = optimize_tms.define_target_region(sphere3_msh, c, r, t)
    outside = sphere3_msh.elm.elm_number[
        ~np.isin(sphere3_msh.elm.elm_number, elm)
    ]
    bar = sphere3_msh.elements_baricenters()
    dist = np.linalg.norm(bar[:] - c, axis=1)
    assert np.all(dist[elm - 1] < r)
    assert np.all(sphere3_msh.elm.tag1[elm - 1] == 3)
    assert np.all(sphere3_msh.elm.elm_type[elm - 1] == 4) 
Example 29
Project: respy   Author: OpenSourceEconomics   File: test_solve.py    License: MIT License 5 votes vote down vote up
def test_explicitly_nonpec_choice_rewards_of_kw_94_one():
    params, options = get_example_model("kw_94_one", with_data=False)

    solve = get_solve_func(params, options)
    state_space = solve(params)

    assert (state_space.nonpecs[:, :2] == 0).all()
    assert np.isin(state_space.nonpecs[:, 2], [0, -4_000, -400_000, -404_000]).all()
    assert (state_space.nonpecs[:, 3] == 17_750).all() 
Example 30
Project: respy   Author: OpenSourceEconomics   File: test_solve.py    License: MIT License 5 votes vote down vote up
def test_explicitly_nonpec_choice_rewards_of_kw_94_two():
    params, options = get_example_model("kw_94_two", with_data=False)

    solve = get_solve_func(params, options)
    state_space = solve(params)

    assert (state_space.nonpecs[:, :2] == 0).all()
    assert np.isin(
        state_space.nonpecs[:, 2], [5_000, 0, -10_000, -15_000, -400_000, -415_000]
    ).all()
    assert (state_space.nonpecs[:, 3] == 14_500).all()