text:
import collections
import collections.abc

import event_model
import numpy

from .bluesky_run import DocumentCache, BlueskyRun

class StreamExists(event_model.EventModelRuntimeError):
    ...

class StreamDoesNotExist(event_model.EventModelRuntimeError):
    ...

class RunBuilder:
    """
    Construct a BlueskyRun from xarray.Dataset, pandas.DataFrame, or dict-of-lists.

    Parameters
    ----------
    metadata : dict, optional
        This should include metadata about what will be done and any adminstrative info.
    uid : string, optional
        By default, a UUID4 is generated.
    time : float, optional
        POSIX epoch time. By default, the current time is used.

    Examples
    --------

    Minimal example

    >>> builder = RunBuilder()
    >>> run = builder.get_run()  # -> BluekyRun. This may be called at any point.
    >>> builder.add_stream("primary", data={"x": [1, 2, 3]})
    >>> builder.close()

    Context manager closes automatically on exit from context, and marks

    >>> with RunBuilder() as builder:
    >>> ... builder.add_stream("primary", data={"x": [1, 2, 3]})
    >>> ...

    Add data incrementally.

    >>> with RunBuilder() as builder:
    >>> ... builder.add_stream("primary", data={"x": [1, 2, 3]})
    >>> ... builder.add_data("primary", data={"x": [4, 5, 6]})
    >>> ... builder.add_data("primary", data={"x": [7, 8, 9]})
    >>> ...

    Define the stream without adding data at first.

    >>> with RunBuilder() as builder:
    >>> ... builder.add_stream("primary",
    >>> ...     data_keys={"x": {"dtype": "number", "shape": [], "source": "whatever"}}
    >>> ... builder.add_data("primary", data={"x": [1, 2, 3]})

    Additional metadata (placed in Run Start document)

    >>> with RunBuilder({"sample": "Cu"}) as builder:
    >>> ... builder.add_stream("primary", data={"x": [1, 2, 3]})
    >>> ...

    """

    def __init__(self, metadata=None, uid=None, time=None):
        self._cache = DocumentCache()
        self._run_bundle = event_model.compose_run(
            uid=uid, time=time, metadata=metadata
        )
        self._cache.start(self._run_bundle.start_doc)
        # maps stream name to bundle returned by compose_descriptor
        self._streams = {}

    def add_stream(
        self,
        name,
        *,
        data=None,
        data_keys=None,
        uid=None,
        time=None,
        object_keys=None,
        configuration=None,
        hints=None,
    ):
        """
        Define a new stream (table of data) and, optionally add data to it.

        Parameters
        ----------
        name : string
            Any string. The name "primary" is conventional (but not required)
            for the primary table of interest, if any.
        data : xarray.Dataset, pandas.DataFrame, or dict-of-arrays-or-lists, optional
        data_keys : dict, optional
            Metadata about the columns. If not specified, it will be inferred.
            Required items are:

                * source --- a freeform string
                * dtype --- one of "array", "bool", "string", "integer", "number"
                * shape --- list of dimensions (empty for scalars)

        uid : string, optional
            By default, a UUID4 is generated.
        time : float, optional
            POSIX epoch time. By default, the current time is used.
        object_keys : dict, optional
            Map a device map to a list of the associated keys in data_keys
        configuration : dict, optional
            Map each device in object_keys to a dict of data_keys, data, and
            timestamps.
        hints : dict, optional
            Dict mapping "fields" to list of most commonly interesting
            keys in data_keys to aid in producing best-effort
            visualizations and other downstream tools.

        Raises
        ------
        StreamExists
            If the name has already been used. Use :meth:`add_data` to add data
            to an existing stream.
        """
        if data is None and data_keys is None:
            raise event_model.EventModelValueError(
                "Neither 'data' nor 'data_keys' was given. At least one is " "required."
            )
        if name in self._streams:
            raise StreamExists(
                f"The stream {name!r} exists. Use the method `add_data` to add "
                "more data to the stream."
            )
        if data_keys is None:
            # Infer the data_keys from the data.
            data_keys = {
                k: {
                    "source": "RunBuilder",
                    "dtype": _infer_dtype(next(iter(v))),
                    "shape": _infer_shape(v),
                }
                for k, v in data.items()
            }
        bundle = self._run_bundle.compose_descriptor(
            name=name,
            data_keys=data_keys,
            time=time,
            object_keys=object_keys,
            configuration=configuration,
            hints=hints,
        )
        self._streams[name] = bundle
        self._cache.descriptor(bundle.descriptor_doc)
        if data is not None:
            self.add_data(name=name, data=data, time=time)

    def add_data(self, name, data, time=None, timestamps=None, seq_num=None, uid=None):
        """
        Add data to an existing stream.

        Parameters
        ----------
        name : string
            Name of a stream previously created using :meth:`add_stream`
        data : xarray.Dataset, pandas.DataFrame, or dict-of-arrays-or-lists
        time : array, optional
            POSIX epoch times. By default, an array of the current time is used.
        timestamps : xarray.Dataset, pandas.DataFrame, or dict-of-arrays-or-lists
            Individual timestamps for every element in data. This level of
            detail is sometimes available from data acquisition systems, but it
            is rarely available or useful in other contexts.
        seq_num : array, optional
            Sequence numbers.
        uid : string, optional
            By default, a UUID4 is generated.

        Raises
        ------
        StreamDoesNotExist
            If stream with the given name has not yet been created using
            :meth:`add_stream`.
        EventModelRuntimeErrror
            If the run has been closed.
        """
        if name not in self._streams:
            raise StreamDoesNotExist(
                "First use the method `add_stream` to create a stream named "
                f"{name!r}. You can add data there and/or add it later separately "
                "using this method, `add_data`."
            )
        if self.closed:
            raise event_model.EventModelRuntimeError("Run is closed.")
        import time as time_module

        len_ = len(data[next(iter(data))])
        now = time_module.time()
        if time is None:
            time = [now] * len_
        if timestamps is None:
            timestamps = {k: [now] * len_ for k in data}
        if seq_num is None:
            seq_num = (1 + numpy.arange(len_, dtype=int)).tolist()
        bundle = self._streams[name]
        doc = bundle.compose_event_page(
            time=time,
            data=_normalize_dataframe_like(data),
            timestamps=_normalize_dataframe_like(timestamps),
            seq_num=seq_num,
            uid=uid,
        )
        self._cache.event_page(doc)

    def update_configuration(self, name, configuration):
        """
        Update the configuration in a stream.

        This issues a new Event Descriptor for a stream.

        Paramters
        ---------
        name : string
        configuration : dict
            See add_stream for expected structure.

        Raises
        ------
        StreamDoesNotExist
            If stream with the given name has not yet been created using
            :meth:`add_stream`.
        EventModelRuntimeErrror
            If the run has been closed.
        """
        if self.closed:
            raise event_model.EventModelRuntimeError("Run is closed.")
        if name not in self._streams:
            raise StreamDoesNotExist(
                "First use the method `add_stream` to create a stream named "
                f"{name!r}."
            )
        # Fill in everything except configuration using the previous Event
        # Descriptor in this stream.
        old_bundle = self._streams[name]
        doc = old_bundle.descriptor_doc
        bundle = self._compose_descriptor(
            name=doc["name"],
            data_keys=doc["data_keys"],
            time=doc["time"],
            object_keys=doc["object_keys"],
            configuration=configuration,
            hints=doc["hints"],
        )
        self._streams[name] = bundle
        self._cache.descriptor(bundle.descriptor_doc)

    def close(self, exit_status="success", reason="", uid=None, time=None):
        """
        Mark the Run as complete.

        It will not be possible to add new streams or new data once this is
        called.

        Parameters
        ----------
        exit_status : {"success", "abort", "fail"}, optional
        reason : string, optional
        uid : string, optional
            By default, a UUID4 is generated.
        time : float
            POSIX epoch time. By default, the current time is used.
        """
        if self.closed:
            raise event_model.EventModelRuntimeError("Run is already closed.")
        doc = self._run_bundle.compose_stop(
            exit_status=exit_status, reason=reason, uid=uid, time=time
        )
        self._cache.stop(doc)

    @property
    def closed(self):
        """
        True if the Run has been closed and cannot accept new data.
        """
        return self._cache.stop_doc is not None

    def get_run(
        self,
        *,
        handler_registry=None,
        root_map=None,
        filler_class=event_model.Filler,
        transforms=None,
    ):
        """
        Provide a BlueskyRun built by this RunBuilder.

        Returns
        -------
        BlueskyRun
        """
        return BlueskyRun(
            document_cache=self._cache,
            # TODO These arguments are *proabably* not needed.
            handler_registry=handler_registry,
            root_map=root_map,
            filler_class=filler_class,
            transforms=transforms,
        )

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        if type is not None:
            reason = repr(value)
            if not self.closed:
                self.close(exit_status="fail", reason=reason)
        else:
            if not self.closed:
                self.close()  # success

def build_simple_run(
    data, metadata=None, uid=None, time=None, exit_status="success", reason=""
):
    """
    Build a simple run from a dataset.

    By "simple", we mean it has a single "stream" of data and does not expose
    some of the options available from the more advanced RunBuilder, such as
    adding data incrementally or including configuration and more detailed
    timestamps.

    This function is suitable for consuming data that could be represented as a
    single "spreadsheet" plus a dictionary of metadata.

    Parameters
    ----------
    data : xarray.Dataset, pandas.DataFrame, or dict-of-arrays-or-lists, optional
    metadata : dict, optional
        This should include metadata about what will be done and any adminstrative info.
    uid : string, optional
        By default, a UUID4 is generated.
    time : float, optional
        POSIX epoch time. By default, the current time is used.
    exit_status : {"success", "abort", "fail"}, optional
    reason : string, optional

    Returns
    -------
    BlueskyRun

    Examples
    --------

    >>> build_simple_run({'x': [1, 2, 3], 'y': [4, 5, 6], metadata={'sample': 'Cu'})
    """
    with RunBuilder(metadata=metadata, uid=uid, time=time) as builder:
        builder.add_stream("primary", data=data)
        builder.close(exit_status=exit_status, reason=reason)
    return builder.get_run()

def build_one_run_from_documents(document_generator):
    """
    Build one BlueskyRun from a stream of documents --- (name, doc) pairs.

    This assumes document_generator contains documents for one run.
    It will fail if:

    * There is no 'start' document (e.g. empty generator); or
    * There is more than one 'start' document (e.g. a stream of multiple
      sequenced or interleaved runs).

    Parameters
    ----------
    document_generator: Iterable[Tuple[String, Dict]]
        Iterable of ``(name, doc)`` pairs.

    Returns
    -------
    BlueskyRun
    """
    cache = DocumentCache()
    for item in document_generator:
        cache(*item)
    return BlueskyRun(cache)

def build_runs_from_documents(document_generator):
    """
    Build BlueskyRuns from a stream of documents --- (name, doc) pairs.

    This will work for any number of runs, including 0 (empty generator).

    Parameters
    ----------
    document_generator: Iterable[Tuple[String, Dict]]
        Iterable of ``(name, doc)`` pairs.

    Returns
    -------
    List[BlueskyRun]
    """
    runs = []

    def factory(name, doc):
        dc = DocumentCache()

        def add_run_to_list(event):
            run = BlueskyRun(dc)
            runs.append(run)

        dc.events.started.connect(add_run_to_list)
        return [dc], []

    rr = event_model.RunRouter([factory])
    for item in document_generator:
        rr(*item)
    return runs

def _infer_dtype(obj):
    "Infer the dtype for Event Descriptor data_keys based on the data."
    if isinstance(obj, (numpy.generic, numpy.ndarray)):
        if numpy.isscalar(obj):
            obj = obj.item()
        else:
            return "array"
    if isinstance(obj, str):
        return "string"
    elif isinstance(obj, collections.abc.Iterable):
        return "array"
    elif isinstance(obj, bool):
        return "boolean"
    elif isinstance(obj, int):
        return "integer"
    else:
        return "number"

def _infer_shape(obj):
    "Infer the shape for Event Descriptor data_keys based on the data."
    if hasattr(obj, "shape"):
        # The first axis is the "Event" axis. We want the shape of the data
        # within a given Event, so we trim off the first axis.
        return obj.shape[1:]
    else:
        return []

def _normalize_dataframe_like(df):
    "Normalize xarray.Dataset and pandas.DataFrame to be dict-of-arrays."
    import xarray
    import pandas

    if isinstance(df, xarray.Dataset):
        return {k: v["data"] for k, v in df.to_dict()["data_vars"].items()}
    elif isinstance(df, pandas.DataFrame):
        # Is there a better way?
        return {k: v.values for k, v in df.items()}
    else:
        return df

max_stars_repo_name: dmgav/bluesky-live

max_issues_repo_name: dmgav/bluesky-live

max_forks_repo_name: dmgav/bluesky-live


text:
from keras.layers import LSTM as KERAS_LSTM
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.optimizers import Adam
import numpy as np
from .dnn import DNN

class LSTM(DNN):
    def __init__(self, model: Sequential, trained: bool = False) -> None:
        super(LSTM, self).__init__(model, trained)

    @classmethod
    def make(
        cls,
        input_shape: int,
        rnn_size: int,
        hidden_size: int,
        dropout: float = 0.5,
        n_classes: int = 6,
        lr: float = 0.001
    ):
        """
        搭建模型

        Args:
            input_shape (int): 特征维度
            rnn_size (int): LSTM 隐藏层大小
            hidden_size (int): 全连接层大小
            dropout (float, optional, default=0.5): dropout
            n_classes (int, optional, default=6): 标签种类数量
            lr (float, optional, default=0.001): 学习率
        """
        model = Sequential()

        model.add(KERAS_LSTM(rnn_size, input_shape=(1, input_shape)))  # (time_steps = 1, n_feats)
        model.add(Dropout(dropout))
        model.add(Dense(hidden_size, activation='relu'))
        # model.add(Dense(rnn_size, activation='tanh'))

        model.add(Dense(n_classes, activation='softmax'))  # 分类层
        optimzer = Adam(lr=lr)
        model.compile(loss='categorical_crossentropy', optimizer=optimzer, metrics=['accuracy'])

        return cls(model)

    def reshape_input(self, data: np.ndarray) -> np.ndarray:
        """二维数组转三维"""
        # (n_samples, n_feats) -> (n_samples, time_steps = 1, input_size = n_feats)
        # time_steps * input_size = n_feats
        data = np.reshape(data, (data.shape[0], 1, data.shape[1]))
        return data

ext: py

reason: import numpy


text:
import unittest
from tshirt.pipeline import phot_pipeline, spec_pipeline
from tshirt.pipeline.instrument_specific import rowamp_sub
from astropy.io import fits,ascii
from pkg_resources import resource_filename
from copy import deepcopy
from tshirt.pipeline import sim_data
import os
import numpy as np
from tshirt.pipeline.utils import get_baseDir

class driftPhot(unittest.TestCase):

    def setUp(self):
        """Set up test fixtures, if any."""
        testPhotParams = 'parameters/phot_params/test_params/test_drifting_psf.yaml'
        testParamPath = phot_pipeline.resource_filename('tshirt',testPhotParams)
        self.phot = phot_pipeline.phot(testParamPath)
        if self.phot.nImg == 0:
            sim_data.sim_phot_w_large_shift()
            self.phot = phot_pipeline.phot(testParamPath)

        drift_file = os.path.join(get_baseDir(),
                                  'example_tshirt_data',
                                  'sim_data','drift_phot',
                                  'drift.csv')
        self.drift_tab = ascii.read(drift_file)

    def test_no_default_drift(self):
        phot = phot_pipeline.phot()
        self.assertTrue(np.allclose(phot.drift_dat['dx'],0.0))
        self.assertTrue(np.allclose(phot.drift_dat['dy'],0.0))

    def test_centering(self):
        self.phot.get_allimg_cen(recenter=True,useMultiprocessing=False)
        pos = self.phot.cenArr
        xArr = pos[:,0,0]
        yArr = pos[:,0,1]
        self.assertTrue(np.max(np.abs(xArr - self.drift_tab['x truth'])) < 2.)
        self.assertTrue(np.max(np.abs(yArr - self.drift_tab['y truth'])) < 2.)

    def test_phot_result(self):
        self.phot.get_allimg_cen(recenter=True,useMultiprocessing=True)
        self.phot.do_phot(useMultiprocessing=True)
        t1 = self.phot.print_phot_statistics(returnOnly=True)
        ## make sure error is
        self.assertTrue(t1['Stdev (%)'][0] < 20.)

class rowAmpBacksub(unittest.TestCase):

    def setUp(self):
        """Set up test fixtures, if any."""
        testPhotParams = 'parameters/phot_params/test_params/test_rowamp_sub.yaml'
        testParamPath = phot_pipeline.resource_filename('tshirt',testPhotParams)
        self.phot = phot_pipeline.phot(testParamPath)

    def test_backsub(self):
        img, head = self.phot.get_default_im()
        outimg, outmodel = rowamp_sub.do_backsub(img,self.phot,saveDiagnostics=True,evenOdd=False)
        diag_dir = os.path.join(get_baseDir(),'diagnostics','rowamp_sub')
        out_path = os.path.join(diag_dir,"{}_subtracted.fits".format(self.phot.dataFileDescrip))
        self.assertTrue(os.path.exists(out_path))

    def test_backsub_with_even_odd(self):
        param = deepcopy(self.phot.param)
        param['srcNameShort'] = 'test_rowamp_even_odd'
        newPhot = phot_pipeline.phot(directParam=param)
        img, head = newPhot.get_default_im()
        outimg, outmodel = rowamp_sub.do_backsub(img,newPhot,saveDiagnostics=True,evenOdd=True)
        diag_dir = os.path.join(get_baseDir(),'diagnostics','rowamp_sub')
        out_path = os.path.join(diag_dir,"{}_subtracted.fits".format(newPhot.dataFileDescrip))
        self.assertTrue(os.path.exists(out_path))

    def test_phot_result(self):
        self.phot.do_phot(useMultiprocessing=False)
        ## make sure the phot looks good
        #self.assertTrue(t1['Stdev (%)'][0] < 20.)

if __name__ == '__main__':
    unittest.main()

reason: import numpy,from astropy


text:
# import collections
from typing import Union, Optional, List

# from habitat.core.dataset import Dataset
# from habitat.core.embodied_task import Action, EmbodiedTask, Measure
# from habitat.core.simulator import ActionSpaceConfiguration, Sensor, Simulator
# from habitat.core.utils import Singleton

from habitat.core.simulator import (
    AgentState,
    Config,
    DepthSensor,
    Observations,
    RGBSensor,
    SemanticSensor,
    Sensor,
    SensorSuite,
    ShortestPathPoint,
    Simulator,
    VisualObservation,
)
from habitat.core.registry import registry
from habitat.sims.habitat_simulator.habitat_simulator import HabitatSim
from habitat.config.default import Config

from habitat.utils.geometry_utils import (
    get_heading_error,
    quat_to_rad,
)
from habitat.tasks.utils import cartesian_to_polar

import habitat_sim

import random
import quaternion
import magnum as mn
import math
import numpy as np

MAX_ANG = np.deg2rad(90)
MAX_LIN = 0.50
TIME_STEP = 0.1

@registry.register_simulator(name="iGibsonSocialNav")
class iGibsonSocialNav(HabitatSim):
    def __init__(self, config: Config) -> None:
        super().__init__(config=config)
        obj_templates_mgr = self.get_object_template_manager()
        self.people_template_ids = obj_templates_mgr.load_configs(
            "/coc/testnvme/nyokoyama3/flash_datasets/igibson_challenge/person_meshes" #"/private/home/naokiyokoyama/gc/datasets/person_meshes"
        )
        self.person_ids = []
        self.people_mask = config.get('PEOPLE_MASK', False)
        self.num_people = config.get('NUM_PEOPLE', 1)
        self.social_nav = True
        self.interactive_nav = False

    def reset_people(self):
        agent_position = self.get_agent_state().position
        obj_templates_mgr = self.get_object_template_manager()

        # Check if humans have been erased (sim was reset)
        if not self.get_existing_object_ids():
            self.person_ids = []
            for _ in range(self.num_people):
                for person_template_id in self.people_template_ids:
                    self.person_ids.append(self.add_object(person_template_id))

        # Spawn humans
        min_path_dist = 3
        max_level = 0.6
        agent_x, agent_y, agent_z = self.get_agent_state(0).position
        self.people = []
        for person_id in self.person_ids:
            valid_walk = False
            while not valid_walk:
                start = np.array(self.sample_navigable_point())
                goal = np.array(self.sample_navigable_point())
                distance = self.geodesic_distance(start, goal)
                valid_distance = distance > min_path_dist
                valid_level = (
                    abs(start[1]-agent_position[1]) < max_level
                    and abs(goal[1]-agent_position[1]) < max_level
                )
                sp = habitat_sim.nav.ShortestPath()
                sp.requested_start = start
                sp.requested_end   = goal
                found_path = self.pathfinder.find_path(sp)
                valid_start = np.sqrt(
                    (start[0]-agent_x)**2
                    +(start[2]-agent_z)**2
                ) > 0.5
                valid_walk = (
                    valid_distance and valid_level
                    and found_path and valid_start
                )
                if not valid_distance:
                    min_path_dist *= 0.95

            waypoints = sp.points
            heading = np.random.rand()*2*np.pi-np.pi
            rotation = np.quaternion(np.cos(heading),0,np.sin(heading),0)
            rotation = np.normalized(rotation)
            rotation = mn.Quaternion(
                rotation.imag, rotation.real
            )
            self.set_translation([start[0], start[1]+0.9, start[2]], person_id)
            self.set_rotation(rotation, person_id)
            self.set_object_motion_type(
                habitat_sim.physics.MotionType.KINEMATIC,
                person_id
            )
            spf = ShortestPathFollowerv2(
                sim=self,
                object_id=person_id,
                waypoints=waypoints
            )
            self.people.append(spf)

    def get_observations_at(
        self,
        position: Optional[List[float]] = None,
        rotation: Optional[List[float]] = None,
        keep_agent_at_new_pose: bool = False,
    ) -> Optional[Observations]:
        observations = super().get_observations_at(
            position,
            rotation,
            keep_agent_at_new_pose,
        )

        if observations is None:
            return None

        if not self.people_mask:
            return observations

        '''
        Get pixels of just people
        '''
        # 'Remove' people
        all_pos = []
        for person_id in self.get_existing_object_ids():
            pos = self.get_translation(person_id)
            all_pos.append(pos)
            self.set_translation([pos[0], pos[1]+10, pos[2]], person_id)

        # Refresh observations
        no_ppl_observations = super().get_observations_at(
            position=position,
            rotation=rotation,
            keep_agent_at_new_pose=True,
        )

        # Remove non-people pixels
        observations['people'] = observations['depth'].copy()
        observations['people'][
            observations['people'] == no_ppl_observations['depth']
        ] = 0

        # Put people back
        for pos, person_id in zip(all_pos, self.get_existing_object_ids()):
            self.set_translation(pos, person_id)

        return observations

class ShortestPathFollowerv2:
    def __init__(
        self,
        sim,
        object_id,
        waypoints
    ):
        self._sim = sim
        self.object_id = object_id

        self.vel_control = habitat_sim.physics.VelocityControl()
        self.vel_control.controlling_lin_vel = True
        self.vel_control.controlling_ang_vel = True
        self.vel_control.lin_vel_is_local    = True
        self.vel_control.ang_vel_is_local    = True

        self.waypoints = list(waypoints)+list(waypoints)[::-1][1:-1]
        self.next_waypoint_idx = 1
        self.done_turning = False
        self.current_position = waypoints[0]

        self.max_linear_vel = np.random.rand()*(0.1)+0.1

    def step(self, time_step=TIME_STEP):
        waypoint_idx = self.next_waypoint_idx % len(self.waypoints)
        waypoint = np.array(self.waypoints[waypoint_idx])

        translation = self._sim.get_translation(self.object_id)
        mn_quat     = self._sim.get_rotation(self.object_id)

        # Face the next waypoint if we aren't already facing it
        if not self.done_turning:
            # Get current global heading
            heading = np.quaternion(mn_quat.scalar, *mn_quat.vector)
            heading = -quat_to_rad(heading)+np.pi/2

            # Get heading necessary to face next waypoint
            theta = math.atan2(
                waypoint[2]-translation[2], waypoint[0]-translation[0]
            )

            theta_diff = get_heading_error(heading, theta)
            direction = 1 if theta_diff < 0 else -1

            # If next turn would normally overshoot, turn just the right amount
            if MAX_ANG*time_step*1.2 >= abs(theta_diff):
                angular_velocity = -theta_diff / time_step
                self.done_turning = True
            else:
                angular_velocity = MAX_ANG*direction

            self.vel_control.linear_velocity = np.zeros(3)
            self.vel_control.angular_velocity = np.array([
                0.0, angular_velocity, 0.0
            ])

        # Move towards the next waypoint
        else:
            # If next move would normally overshoot, move just the right amount
            distance = np.sqrt(
                (translation[0]-waypoint[0])**2+(translation[2]-waypoint[2])**2
            )
            if self.max_linear_vel*time_step*1.2 >= distance:
                linear_velocity = distance / time_step
                self.done_turning = False
                self.next_waypoint_idx += 1
            else:
                linear_velocity = self.max_linear_vel

            self.vel_control.angular_velocity = np.zeros(3)
            self.vel_control.linear_velocity = np.array([
                0.0, 0.0, linear_velocity
            ])

        rigid_state = habitat_sim.bindings.RigidState(
            mn_quat,
            translation
        )
        rigid_state = self.vel_control.integrate_transform(
            time_step, rigid_state
        )

        self._sim.set_translation(rigid_state.translation, self.object_id)
        self._sim.set_rotation(rigid_state.rotation, self.object_id)
        self.current_position = rigid_state.translation

max_stars_repo_name: qianLyu/habitat-lab

max_issues_repo_name: qianLyu/habitat-lab

max_forks_repo_name: qianLyu/habitat-lab


text:
#!/usr/bin/env python

##
# Massimiliano Patacchiola, Plymouth University 2016
#
# In this example I show you how to use a pretrained Deep Neural Network (DNN)
# for head pose estimation. It requires a tensorflow file containing the weights
# of the network, which are loaded at the beginning of the session.
#
# Attention: this example works with greyscale images of dimension 64x64 pixels

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import numpy as np
import tensorflow as tf
import cv2

# Create model
def multilayer_model(_X, _input0, _biases_input0, _hidden1, _biases_hidden1, _hidden2, _biases_hidden2, _output3, _biases_output3):

    _input0_result = tf.matmul(_X, _input0) + _biases_input0
    _hidden1_result = tf.nn.tanh(tf.matmul(_input0_result, _hidden1) + _biases_hidden1)
    _hidden2_result = tf.nn.tanh(tf.matmul(_hidden1_result, _hidden2) + _biases_hidden2)
    _output3_result = tf.nn.tanh(tf.matmul(_hidden2_result, _output3) + _biases_output3)
    return _output3_result

graph = tf.Graph()
with graph.as_default():

    print("Starting Graph creation...")

    # Variables
    image_size = 64
    num_hidden_units_1 = 256
    num_hidden_units_2 = 256
    num_hidden_units_3 = 256
    num_labels = 3

    #0- the input placeholder
    tf_input = tf.placeholder(tf.float32,shape=(batch_size, image_size * image_size))

    #1- weights
    #tf.truncated_normal(shape, mean=0.0, stddev=1.0)
    weights_input0 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_units_1], 0.0, 1.0))
    weights_hidden1 = tf.Variable(tf.truncated_normal([num_hidden_units_1, num_hidden_units_2], 0.0, 1.0))
    weights_hidden2 = tf.Variable(tf.truncated_normal([num_hidden_units_2, num_hidden_units_3], 0.0, 1.0))
    weights_output3 = tf.Variable(tf.truncated_normal([num_hidden_units_3, num_labels], 0.0, 1.0))

    #2- biases
    biases_input0 = tf.Variable(tf.zeros([num_hidden_units_1]))
    biases_hidden1 = tf.Variable(tf.zeros([num_hidden_units_2]))
    biases_hidden2 = tf.Variable(tf.zeros([num_hidden_units_3]))
    biases_output3 = tf.Variable(tf.zeros([num_labels]))

    #3- testing
    prediction = multilayer_model(tf_train_dataset,
                     weights_input0, biases_input0,
                     weights_hidden1, biases_hidden1,
                     weights_hidden2, biases_hidden2,
                     weights_output3, biases_output3)

    print("Finished.")

#Print the variables
print("========== ALL TF VARS ======== ")
all_vars = tf.all_variables()
for k in all_vars:
      print(k.name)

#Load the checkpoint
ckpt = tf.train.get_checkpoint_state("./dnn_1600i_4h_3o")

#Create the session
_sess = tf.Session()

#Associate the weights stored in the checkpoint file to the
#local tensorflow variables
tf.train.Saver(({"dnn_weights_input0": weights_input0, "dnn_biases_input0": biases_input0,
                 "dnn_weights_hidden1": weights_hidden1, "dnn_biases_hidden1": biases_hidden1,
                 "dnn_weights_hidden2": weights_hidden2, "dnn_biases_hidden2": biases_hidden2,
                 "dnn_weights_output3": weights_output3, "dnn_biases_output3": biases_output3
                 })).restore(_sess, ckpt.model_checkpoint_path)

#Load the image in greyscale with OpenCV
image = cv2.imread("image.jpg", 0)
h,w = image.shape

#Resize the image if needed and get the predictions from the model
if(h == w and h>64):
    image_resized = cv2.resize(image, (64, 64), interpolation = cv2.INTER_AREA)
    image_normalised = np.add(image_resized, -127) #normalisation of the input
    feed_dict = {tf_input : image_normalised}
    predictions = _sess.run([prediction], feed_dict=feed_dict)
elif(h == w and h==64):
    image_normalised = np.add(image_resized, -127) #normalisation of the input
    feed_dict = {tf_input : image_normalised}
    predictions = _sess.run([prediction], feed_dict=feed_dict)
    print(predictions)
    #Here to see the output in degrees you should
    #multiply the first value inside prediction (roll) times 25
    #the second value in prediction (pitch) times 45
    #and the third value (yaw) times 90
else:
    raise ValueError('DnnHeadPoseEstimation: the image given as input is not squared or it is smaller than 64px.')

max_stars_repo_name: KayaDevSolutions/deepgaze

max_issues_repo_name: KayaDevSolutions/deepgaze

max_forks_repo_name: KayaDevSolutions/deepgaze


text:
# Copyright (c) 2020-2021, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

import carb
from pxr import UsdGeom, Gf, UsdPhysics, Sdf, UsdLux
import omni.ext
import omni.usd
import omni.kit.settings

import asyncio
from omni.isaac.dynamic_control import _dynamic_control
import omni.physx as _physx

from omni.physx.scripts.physicsUtils import add_ground_plane
from omni.isaac.utils.scripts.nucleus_utils import find_nucleus_server
from omni.isaac.utils.scripts.scene_utils import set_translate, set_up_z_axis, setup_physics

import os
import numpy as np

def create_prim_from_usd(stage, prim_env_path, prim_usd_path, location):
    envPrim = stage.DefinePrim(prim_env_path, "Xform")  # create an empty Xform at the given path
    envPrim.GetReferences().AddReference(prim_usd_path)  # attach the USD to the given path
    set_translate(envPrim, location)  # set pose

class Replay:
    def __init__(self):
        self._timeline = omni.timeline.get_timeline_interface()
        self._dc = _dynamic_control.acquire_dynamic_control_interface()

        self.created = False
        self._save_dir = None
        self._replay_data = False
        self._replay_count = 0
        self.traj_lengths = 0
        self._reset = False
        self._ar = _dynamic_control.INVALID_HANDLE

    def create_robot(self):
        """ load robot from USD
        """

        self._stage = omni.usd.get_context().get_stage()
        self._ar = _dynamic_control.INVALID_HANDLE

        ## unit conversions: RMP is in meters, kit is by default in cm
        self._meters_per_unit = UsdGeom.GetStageMetersPerUnit(self._stage)
        self._units_per_meter = 1.0 / UsdGeom.GetStageMetersPerUnit(self._stage)

        set_up_z_axis(self._stage)
        add_ground_plane(self._stage, "/physics/groundPlane", "Z", 1000.0, Gf.Vec3f(0.0), Gf.Vec3f(1.0))
        setup_physics(self._stage)

        result, nucleus_server = find_nucleus_server()
        if result is False:
            carb.log_error("Could not find nucleus server with /Isaac folder")
            return
        asset_path = nucleus_server + "/Isaac"
        robot_usd = asset_path + "/Robots/Franka/franka.usd"
        robot_path = "/scene/robot"
        create_prim_from_usd(self._stage, robot_path, robot_usd, Gf.Vec3d(0, 0, 0))

        # self._physxIFace.release_physics_objects()
        # self._physxIFace.force_load_physics_from_usd()

        light_prim = UsdLux.DistantLight.Define(self._stage, Sdf.Path("/World/defaultLight"))
        light_prim.CreateIntensityAttr(500)

        self.created = True

    def step(self, step):
        """This function is called every timestep in the editor

        Arguments:
            step (float): elapsed time between steps
        """

        if self.created and self._timeline.is_playing():
            if self._reset:
                if self._ar == _dynamic_control.INVALID_HANDLE:
                    self._ar = self._dc.get_articulation("/scene/robot")
                home_joint_pos = [0.0, -1.30, 0.0, -2.83, 0.0, 2.03, 0.75, 0.0, 0.0]
                self._dc.wake_up_articulation(self._ar)
                self._dc.set_articulation_dof_position_targets(self._ar, home_joint_pos)
                self._replay_data = False
                self._replay_count = 0
                arrived = self.arrival_check(home_joint_pos)
                if arrived:
                    self._reset = False

            if self._replay_data:
                if self._replay_count < self.traj_lengths:
                    self._dc.wake_up_articulation(self._ar)
                    self._dc.set_articulation_dof_position_targets(
                        self._ar, self.joint_positions[self._replay_count, :].tolist()
                    )
                    self._replay_count += 1
                else:
                    self._replay_data = False
                    print("Replay Complete")
                    self._replay_count = 0

    def arrival_check(self, target_pos):
        current_dof = self._dc.get_articulation_dof_states(self._ar, _dynamic_control.STATE_POS)
        current_pos = current_dof["pos"]
        arrived = np.allclose(np.array(target_pos), np.array(current_pos), rtol=1e2)
        return arrived

    def save_dir(self, dir_name):
        self._save_dir = dir_name

    def replay_data(self):
        if not self._replay_data:
            self._replay_data = True
            filename = self._save_dir
            # check if file exist
            if not os.path.exists(filename):
                carb.log_warn(f"filename {filename} doesn't exist")
                self._replay_data = False
            else:
                print("file used: ", filename)
                with open(filename, "r") as f:
                    traj_dict = eval(f.read())

                self.joint_positions = np.array(traj_dict["joint state"])
                self.traj_lengths = np.shape(self.joint_positions)[0]
                if self._ar == _dynamic_control.INVALID_HANDLE:
                    self._ar = self._dc.get_articulation("/scene/robot")
                self._replay_count = 0

        else:
            self._replay_data = False
            self._replay_count = 0

    def stop_tasks(self):
        self.created = False
        self._replay_count = 0
        self._replay_data = False
        self._reset = False

    def reset(self):
        self._ar = _dynamic_control.INVALID_HANDLE
        self._reset = True


text:
# Coordinated Spline Motion and Robot Control Project
#
# Copyright (c) 2017 Olga Petrova <olga.petrova@cvut.cz>
# Advisor: Pavel Pisa <pisa@cmp.felk.cvut.cz>
# FEE CTU Prague, Czech Republic
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# In 2017, project funded by PiKRON s.r.o. http://www.pikron.com/

''' Module provides functions for robot object initialization '''

import numpy as np

from robCRSdkt import robCRSdkt
from robCRSgripper import robCRSgripper, robCRSgripperinit
from robCRSikt import robCRSikt

#  based on BlueBot and Bosch Toolbox
#  by  O. Certik, V. Smutny, P. Krsek, M. Matousek

class robCRS:
    """Common specifications for CRS robots"""
    def __init__(self):

        self.portname = 'COM4'
        self.controller = 'MARS8'
        self.demoname = 'robCRSdemo'

        self.DOF = 6

        # Robot link dimensions (these constants are read from documentation)
        inch2mm = 25.4  # conversion constant
        self.L1 = 13 * inch2mm - 25  # J2 above working desk
        self.L2 = 12 * inch2mm  # J2-J3
        self.L3 = 13 * inch2mm  # J3-J5
        self.L4 = 3 * inch2mm  # J5-flange
        self.L5 = inch2mm * (3.78 + 0.5)  # gripper length
        self.L6 = inch2mm * 1  # gripper finger length

        # Posible change of coordinate direction
        self.direction = np.array([1, 1, -1, -1, -1, -1])

        # IRC per rotation of motor.
        self.irc = np.array([1000, 1000, 1000, 500, 500, 500])  # from manual, it should work
        # motors' gearing
        self.gearing = np.array([100, 100, 100, 101, 100, 101])  # from manual
        # degtoirc=1 degree in IRC
        self.degtoirc = self.irc * self.direction * self.gearing * 4 / 360

        self.activemotors = 'ABCDEF'
        # axes in hh_axes_list are in order of initialization
        self.hh_axes_list = 'BACDEF'
        self.control_axes_list = 'ABCDEF'
        self.coord_axes = 'ABCDEF'

        # DH notation
        self.offset = np.array([0.0, 270.0, 90.0, 0.0, 0.0, 0.0]) / 180 * np.pi
        self.sign = np.array([1, 1, 1, 1, 1, 1])
        self.d = [self.L1, 0, 0, self.L3, 0, self.L4 + self.L5 + self.L6]
        self.a = [0, self.L2, 0, 0, 0, 0]
        self.alpha = np.array([90.0, 0.0, 270.0, 90.0, 270.0, 0]) / 180 * np.pi
        self.base = np.eye(4)
        self.tool = np.array([[0.0,0.0,-1.0,0.0],
                              [0.0,1.0, 0.0,0.0],
                              [1.0, 0.0,0.0,0.0],
                              [0.0,0.0,0.0,1.0]])

        # Base position in degrees
        # (corresponds to position robot.hhirc in IRC)
        self.hhdeg = [0, 0, 0, 0, 0, 0]

        # Softhome position in degrees
        #  - Robot is moved to this position during startup and closing.
        #  - The position is set by calling bbsofthome
        self.shdeg = [0, 0, -45, 0, -45, 0]

        self.REGME = [32000, 32000, 32000, 32000, 32000, 32000]

        # Constants of PID regulators
        self.REGP = [10, 12, 70, 35, 45, 100]
        self.REGI = [80, 63, 50, 80, 65, 300]
        self.REGD = [300, 200, 200, 130, 230, 350]

        # min and max speeds (IRC/256/msec)
        self.minspeed = np.rint(self.defaultspeed / 5)
        self.maxspeed = np.rint(self.defaultspeed * 2)

        # min a max acceleration
        self.minacceleration = np.rint(self.defaultacceleration / 5)
        self.maxacceleration = np.rint(self.defaultacceleration * 2)

        # smutneho hodnoty robot.REGCFG = [1370 1369 1369 1361 1369 1369]
        # D ma HW problem, protoze indexova znacka se casto nachazi o
        # (mnoho)otacek jinde
        # E nema indexy nikdy,
        # problem D a E identifikovan: V rozporu s dokumentaci ma HEDS na D a E
        # signal znacky v podmnozine kombinace kanalu AB=00, ackoliv by mel jit az
        # do hrany A mezi 0 a 1 (ci naopak), pricemz B ma byt nula. Cip, ktery
        # zpracovava signaly to jinak neumi.
        # Potencialne to lze resit nejakym RC clankem mezi HEDS a budicem v robotu,
        # daji se ocekavat problemy s ruznymi rychlostmi. Od tohoto reseni je
        # upusteno.
        # V teto verzi tedy D a E najizdi jen na MARK, nikoliv na indexovou znacku.
        # robot.REGCFG = [1494 1494 1494 1484 1476 1492]# D melo 1498,
        # robot.REGCFG = [1492 1494 1494 1484 1476 1492]# D melo 1498,

        # Uprava rychlosti HOME
        # robot.REGCFG = [1490 1491 1491 1482 1474 1490]# D melo 1498,

        # Speed reduced due to power suply problem
        self.REGCFG = [1489, 1490, 1490, 1481, 1474, 1490]  # D melo 1498,

        # CRS robot shall not release after some time
        self.IDLEREL = 1200

        # homing sequence TODO
        # robot.hardhome = robCRShardhome

        self.timeout = 200

        # Gripper parameters

        self.gripper = robCRSgripper
        self.gripper_init = robCRSgripperinit

        # Kind of gripper (CRSGripper, Magnetic)
        self.gripper_type = 'CRSGripper'

        # Analog input of position sensor
        self.gripper_ADC = 10
        # Maximal curent limit (0-255)
        # Value 16 corresponds to 250mA when feedback is 500
        self.gripper_current = 16
        # Limitation constant (feeadback from overcurrent)
        self.gripper_feedback = 500
        # Maximal energy limits voltage on motor
        # (0 - 32000 corresponds to 0-24V)
        # 2010-02-17 Martin Matousek note:
        # Manual says that gripper can survive up to 15 Volts, but
        # force more than 75# must not be used longer then 15 second. Thus
        # safe power seems to be 11.25 V. We are using more conservative value here
        # 10/32 * 24 = 7.5 V

        self.gripper_REGME = 10000
        # Maximal speed
        self.gripper_REGMS = 500
        # Axis configuration word
        self.gripper_REGCFG = 256
        # PID parameter of controller
        self.gripper_REGP = 200
        self.gripper_REGI = 0
        self.gripper_REGD = 100

        self.gripper_poll_time = 0.2
        self.gripper_poll_diff = 50

        self.ikt = robCRSikt
        self.dkt = robCRSdkt

        self.degtoirc = np.multiply(self.direction, np.multiply(self.irc, self.gearing)) * 4.0 / 360.0

class robCRS97(robCRS, object):
    """Robot specification: CRS97"""

    def __init__(self):
        self.description = 'CRS 1997 with gripper'

        # Base position in IRC - calibrated !!
        # (corresponds to position self.hhdeg in degrees)
        self.hhirc = [-182500, 252, -63625, 99200, 14300, -98150]

        # self bounds in IRC
        self.bound = [[-370000, -100000, -190000, -5000, -44000, -199500],
                      [10000, 100000, 65000, 202000, 72000, 4000]]

        # Speed reduced due to power suply problem
        self.defaultspeed = np.array([30, 8, 20, 30, 30, 55]) * 256
        self.defaultacceleration = np.rint(
            np.array([(30.0 / 400.0), (8.0 / 400.0), (20.0 / 400.0), (30.0 / 400.0), 1.0 / 2.0, 3.0 / 5.0]) * 256.0)

        # Gripper range
        self.gripper_bounds = [1001, 48]
        self.gripper_bounds_force = [1001 + 100, 48 - 1000]  # TODO must be verified

        # Axis(motor) of gripper
        self.gripper_ax = 'H'
        self.verbose = True
        # ------------------------------------------------------------------------------
        # Common parameters for CRS selfs
        super(robCRS97, self).__init__()

class robCRS93(robCRS, object):
    """Robot specification: CRS93"""

    def __init__(self):
        self.description = 'CRS 1993 with gripper'

        # Base position in IRC - calibrated !!
        # (corresponds to position self.hhdeg in degrees)
        self.hhirc = [-181650, -349, -62200, 99200, 8300, -96500]

        # self bounds in IRC
        self.bound = [[-370000, -100000, -190000,  -5000, -50000, -199500],
                 [10000,  100000,   63000, 203500,  67000,  4600]]

        # Speed reduced due to power suply problem
        self.defaultspeed = np.array([30, 8, 20, 30, 30, 75]) * 256
        self.defaultacceleration = np.rint(
            np.array([0.2, (8.0 / 200.0), (20.0 / 250.0), (30.0 / 200.0), 0.5, 3.0]) * 256.0)

        # Gripper range
        self.gripper_bounds = [840, 103]
        self.gripper_bounds_force = [840+1000, 103-1000]  # TODO must be verified

        # Axis(motor) of gripper
        self.gripper_ax = 'G'
        self.verbose = True
        # ------------------------------------------------------------------------------
        # Common parameters for CRS selfs
        super(robCRS93, self).__init__()

max_stars_repo_name: cvut/pyrocon

max_issues_repo_name: cvut/pyrocon

max_forks_repo_name: cvut/pyrocon


text:
#!/usr/bin/env python

import math
import numpy as np
import matplotlib.pyplot as plt

try:
  import gi
  gi.require_version('NumCosmo', '1.0')
  gi.require_version('NumCosmoMath', '1.0')
except:
  pass

from gi.repository import NumCosmo as Nc
from gi.repository import NumCosmoMath as Ncm

Ncm.cfg_init ()

#
#  New homogeneous and isotropic cosmological model NcHICosmoDEXcdm
#
cosmo = Nc.HICosmo.new_from_name (Nc.HICosmo, "NcHICosmoDEXcdm")
cosmo.omega_x2omega_k ()
cosmo.param_set_by_name ("Omegak",    0.0)

#
#  New cosmological distance objects optimizied to perform calculations
#  up to redshift 2.0.
#
dist = Nc.Distance.new (1.0)

#
# New matter density profile
#
nfw = Nc.DensityProfile.new_from_name ("NcDensityProfileNFW{'Delta':<200.0>}")
nfw.param_set_by_name ('cDelta', 4.0) # 4 as Douglas. In LCDM c = 5 corresponds to cluster masses. (see Lokas and G. Mamon, astro-ph/0002395)
nfw.param_set_by_name ('MDelta', 1.e15)
mdelta = 1.e15
cdelta = 4.0
delta = 200.0

zcluster = 1.0
zsource = 1.5

#
# New weak lensing surface mass density
#
smd = Nc.WLSurfaceMassDensity.new (dist)

#
#  Setting values for the cosmological model, those not set keep their
#  default values. Remember to use the _orig_ version to set the original
#  parameters when a reparametrization is used.
#
cosmo.props.H0     = 70.0
cosmo.props.Omegab = 0.045
cosmo.props.Omegac = 0.255
cosmo.props.Omegax = 0.7
cosmo.param_set_by_name ("Omegak", 0.0) # This line sets a flat universe, modifying Omega_x (small difference).
#This is necessary since CLASS require the inclusion of the radiation density.

dist.prepare (cosmo)

npoints = 500
r_a = np.logspace(math.log10(5.e-3), 2., npoints, endpoint=True)

Sigma = []
meanSigma = []
DeltaSigma = []
convergence = []
shear = []
reduced_shear = []
reduced_shear_inf = []

for i in range(0, npoints):
  ri       = r_a[i]
  Sig      = smd.sigma (nfw, cosmo, ri, zcluster)
  meanSig  = smd.sigma_mean (nfw, cosmo, ri, zcluster)
  kappa    = smd.convergence (nfw, cosmo, ri, zsource, zcluster, zcluster)
  sh       = smd.shear (nfw, cosmo, ri, zsource, zcluster, zcluster)
  reds     = smd.reduced_shear (nfw, cosmo, ri, zsource, zcluster, zcluster)
  reds_inf = smd.reduced_shear_infinity (nfw, cosmo, ri, zsource, zcluster, zcluster)

  Sigma.append (Sig)
  meanSigma.append (meanSig)
  DeltaSigma.append (meanSig - Sig)
  convergence.append (kappa)
  shear.append (sh)
  reduced_shear.append (reds)
  reduced_shear_inf.append (reds_inf)

  print (ri, Sig, meanSig, kappa, sh, reds)

fig = plt.figure(figsize=(6, 5)) #in inches
ax = plt.subplot()

ax.plot(r_a, Sigma, label=r'$\Sigma(R)$')
ax.plot(r_a, meanSigma, label=r'Mean $\overline{\Sigma}(<R)$')
ax.plot(r_a, DeltaSigma, label=r'Excess/differential  $\Delta{\Sigma}(R)$')

ax.set_xlabel(r'$R$ [Mpc]', fontsize=14)
ax.set_ylabel(r'Surface Mass Density', fontsize=12)
ax.set_xscale('log')
ax.set_yscale('log')

ax.annotate(r'$[\Sigma] = M_\odot/Mpc^2$', xy=(0.65, 0.8), xycoords='axes fraction', fontsize=12)
ax.set_title (r'NFW, $c_{200} = 4$, $M_{200} = 10^{15} \, M_\odot$')

plt.legend(loc = 3)

plt.savefig ('wl_smd_sigmas.svg')
plt.show ()
plt.clf ()

ax = plt.subplot()

ax.plot(r_a, convergence, label=r'Convergence $\kappa (R)$')
ax.plot(r_a, shear, label=r'Shear $\gamma (R)$')
ax.plot(r_a, reduced_shear, label=r'Reduced Shear $g(R)$')
ax.plot(r_a, reduced_shear_inf, label=r'Reduced Shear $g_\infty(R)$')

ax.set_xlabel(r'$R$ [Mpc]', fontsize=14)
ax.set_xscale('log')
ax.set_yscale('log')

ax.set_title (r'NFW, $c_{200} = 4$, $M_{200} = 10^{15} \, M_\odot$')

plt.legend(loc = 1)

plt.savefig('wl_smd_convergence_shear.svg')
plt.show()


text:
import xml.etree.ElementTree as ET
from collections import OrderedDict as ODict
import logging, sys

import numpy as np
import math

import kgprim.core as primitives
import kgprim.motions as motions
from kgprim.motions import MotionSequence
from kgprim.motions import PoseSpec
from kgprim.motions import MotionPath

import robmodel.connectivity
import robmodel.ordering
import robmodel.frames
import robmodel.geometry
from robmodel.connectivity import JointKind

logger = logging.getLogger(__name__)

'''
Simply reads the XML file and stores the links/joints data, no conversions
'''
class URDFWrapper :
    class Link:
        def __init__(self, name):
            self.name    = name
            self.inertia = None
            self.parent  = None
            self.supportingJoint = None
    class Joint:
        def __init__(self, name):
            self.name = name
            self.type  = None
            self.frame = None
            self.parent= None
            self.child = None
            #self.predec_H_joint = np.identity(4)

    iMomentsLabels = ['ixx', 'iyy', 'izz', 'ixy', 'ixz', 'iyz']

    def __init__(self, urdfInFile):
        root = ET.parse(urdfInFile)
        self.robotName = root.getroot().get('name')

        linkNodes  = root.findall("link")
        jointNodes = root.findall("joint")

        self.links  = ODict()
        self.joints = ODict()
        self.frames = ODict()

        for nodelink in linkNodes:
            name = nodelink.get('name')
            link = URDFWrapper.Link( name )
            link.inertia = self.readInertialData(nodelink)
            self.links[name] = link

        for nodejoint in jointNodes:
            name = nodejoint.get('name')
            joint = URDFWrapper.Joint( name )
            joint.type  = nodejoint.get('type')
            joint.frame = self.readJointFrameData( nodejoint )
            #joint.predec_H_joint[:3,:3] = getR_extrinsicXYZ( * joint.frame['rpy'] )
            #joint.predec_H_joint[:3,3]  = np.array( joint.frame['xyz'] )
            joint.parent= nodejoint.find('parent').get('link')
            joint.child = nodejoint.find('child').get('link')

            # Note I keep URDF nomenclature ("parent" and "child") just to
            # stress the bond with the source URDF XML file. I will later use
            # the more appropriate terms (e.g. "predecessor")

            self.joints[name] = joint

            predecessor = self.links[ joint.parent ]
            successor   = self.links[ joint.child ]
            successor.parent = predecessor # a Link instance, not a name
            successor.supportingJoint = joint

    def readInertialData(self, linkNode):
        params = dict()
        paramsNode = linkNode.find('inertial')

        # Default inertia parameters if the URDF does not have the data
        if paramsNode == None :
            params['mass'] = 0.0
            params['xyz']  = (0.0, 0.0, 0.0)
            for m in URDFWrapper.iMomentsLabels :
                params[m] = 0.0
            return params

        mass = float(paramsNode.find('mass').get('value'))

        xyz = (0.0, 0.0, 0.0)
        originNode = paramsNode.find('origin')
        if originNode != None :
            comstr = originNode.get('xyz')
            if(comstr != None) :
                xyz = tuple([float(x) for x in comstr.split()])

            # We cannot deal with non-zero values for the 'rpy' attribute
            rpystr = originNode.get('rpy')
            if(rpystr != None) :
                tmp = [float(x) for x in rpystr.split()]
                if(sum(tmp) != 0) :
                    logger.warning('The rpy attribute in the inertial section is not yet supported (link ' + linkNode.get('name') + '). Ignoring it.')

        moments = paramsNode.find('inertia')
        for m in URDFWrapper.iMomentsLabels :
            params[m] = float(moments.get(m))

        params['mass'] = mass
        params['xyz']  = xyz
        return params

    def readJointFrameData(self, jointNode):
        params = dict()

        # URDF defaults:
        params['xyz'] = (0.,0.,0.)
        params['rpy'] = (0.,0.,0.)

        frameNode = jointNode.find('origin')
        if frameNode != None :
            xyz_node = frameNode.get('xyz')
            if xyz_node != None :
                params['xyz'] = tuple([float(x) for x in xyz_node.split()])
            rpy_node = frameNode.get('rpy')
            if rpy_node != None :
                params['rpy'] = tuple([float(x) for x in rpy_node.split()])

        axis_node = jointNode.find('axis')
        if axis_node != None :
            params['axis'] = tuple([float(x) for x in axis_node.get('xyz').split()])
        else :
            params['axis'] = (1.,0.,0.) # URDF default

        return params

def toValidID( name ) :
    return name.replace('-', '__')

def linkFrameToJointFrameInURDF(urdfjoint):
    '''
    Return the data about the location of the joint frame relative to the
    predecessor link frame.

    This function returns three values:
    - the `xyz` attribute as found in the source URDF
    - the `rpy` attribute as found in the source URDF
    - the rigid motion model representing the relative pose (this is the motion
      that the link frame should undergo to coincide with the joint frame)
    '''
    xyz = urdfjoint.frame['xyz']
    rpy = urdfjoint.frame['rpy']

    tr = [motions.translation(a, xyz[a.value]) for a in motions.Axis if round(xyz[a.value],5) != 0.0]
    rt = [motions.rotation   (a, rpy[a.value]) for a in motions.Axis if round(rpy[a.value],5) != 0.0]

    motion__linkToJoint = MotionSequence(tr+rt, MotionSequence.Mode.fixedFrame)
    return xyz, rpy, motion__linkToJoint

def convert( urdf ) :
    '''
    Reads the model from a URDFWrapper instance, and construct the corresponding
    models in our format.
    '''

    robotName = urdf.robotName
    links  = ODict()
    joints = ODict()
    pairs = []
    children = {}
    orphans = []

    for urdfname in urdf.links.keys() :
        name = toValidID( urdfname )
        link = robmodel.connectivity.Link(name)
        links[name] = link
        children[name] = []
        if urdf.links[urdfname].parent == None :
            orphans.append(link)

    if len(orphans)==0 :
        logger.fatal("Could not find any root link (i.e. a link without parent).")
        logger.fatal("Check for kinematic loops.")
        print("Error, no root link found. Aborting", file=sys.stderr)
        sys.exit(-1)
    if len(orphans) > 1 :
        logger.warning("Found {0} links without parent, only one expected".format(len(orphans)))
        logger.warning("Any robot model must have exactly one root element.")
        logger.warning("This might lead to unexpected results.")
    robotBase = orphans[0]

    for jname in urdf.joints.keys() :
        urdfjoint = urdf.joints[jname]
        name      = toValidID( jname )
        jkind     = urdfjoint.type
        if jkind in JointKind.__members__.keys() :
            jkind = JointKind[jkind] # otherwise, it stays a string (like 'fixed')
        joint     = robmodel.connectivity.Joint(name, jkind)
        parent    = links[ toValidID(urdfjoint.parent) ]
        child     = links[ toValidID(urdfjoint.child)  ]
        children[parent.name].append( child )

        joints[name] = joint
        pairs.append( robmodel.connectivity.KPair(joint, parent, child) )

    # CONNECTIVITY MODEL
    connectivityModel = robmodel.connectivity.Robot(
        urdf.robotName, links, joints, pairs)

    # REGULAR NUMBERING
    # There is no numbering scheme in the URDF format, so we arbitrarily
    # associate code to each link via a Depth-First-Traversal
    code = 0
    numbering = {}
    def setCode(currentLink):
        nonlocal code, numbering
        if currentLink == None : return
        numbering[currentLink.name] = code
        for child in children[currentLink.name] :
            code = code + 1
            setCode( child )

    setCode( robotBase )
    ordering = { 'robot': robotName, 'nums' : numbering }
    orderedModel = robmodel.ordering.Robot(connectivityModel, ordering)

    # FRAMES
    # The URDF does not have explicit frames, so there are no more frames than
    # joints and links; thus the second argument is always the empty list
    framesModel = robmodel.frames.RobotDefaultFrames(orderedModel, [])

    # GEOMETRY MEASUREMENTS
    poses = []
    axes = {}
    for joint in urdf.joints.values() :
        # Get the current joint and link (predecessor)
        myjoint = joints[ toValidID( joint.name ) ]
        mylink  = orderedModel.predecessor(myjoint)

        logger.debug("Processing joint * {0} * and predecessor link * {1} *".format(myjoint.name, mylink.name) )

        # The relative pose of the URDF joint frame relative to the URDF link frame
        xyz, rpy, motion_link_to_joint = linkFrameToJointFrameInURDF(joint)

        jaxis = np.round( np.array(joint.frame['axis']), 5)

        logger.debug("Joint axis in URDF coordinates    : {0}".format(jaxis) )
        logger.debug("URDF joint xyz and rpy attributes : {0}   {1}".format(xyz, rpy) )

        frame_joint = framesModel.framesByName[ myjoint.name ]
        frame_link  = framesModel.framesByName[ mylink.name  ]
        pose = primitives.Pose(target=frame_joint, reference=frame_link)
        poses.append( PoseSpec(pose, motion_link_to_joint) )
        axes[myjoint.name] = joint.frame['axis']

    posesModel = motions.PosesSpec(robotName, poses)

    geometryModel = robmodel.geometry.Geometry(orderedModel, framesModel, posesModel, axes)
    return connectivityModel, orderedModel, framesModel, geometryModel


text:
##################################################################################
##################################################################################
# Country Simulation (Python 3.7)
##################################################################################
##################################################################################
from scipy.integrate import odeint
from timeit import default_timer as timer
from datetime import timedelta
import numpy as np
from random import randint

import core.EpiEquations
import core.ParameterSet

def SetupCountryModel(Parms, intRed, Population, initInfected, initExposedMult):

        ParmSet = core.ParameterSet.ParametersExtended(*Parms, Population, initInfected, initExposedMult)
        Parms = (intRed, *ParmSet.Constants)
        Y0 = np.array(ParmSet.Y0).flatten()
        CompartNames = ParmSet.CompartmentNames
        return Y0, Parms, CompartNames

def RunCountrySim(model, y0, t, parms):
    # Simulate
    start = timer()
    Y = odeint(model, y0, t, parms)

    end = timer()
    # print("Run completed in ", timedelta(seconds=end - start))
    results = np.array(Y)

    return results

max_stars_repo_name: yupengyanghuhu/CDC-Modelling-Challenge

max_issues_repo_name: yupengyanghuhu/USA-CDC-Bayesian-ODE

max_forks_repo_name: yupengyanghuhu/USA-CDC-Bayesian-ODE

reason: import numpy,from scipy


text:
import numpy as np

def step(i,state) :

    if (i > 1) :
        dsign = np.sign(state.q[i-1,:]) - np.sign(state.q[i,:])
        s_ind = (dsign != 0)
        if np.size(state.m[i,s_ind]) :
            state.m[i,:] = state.m[i-1,:]
            state.m[i,s_ind] = state.m[i-1,s_ind] + 1
        else :
            state.m[i,:] = state.m[i-1,:]

max_stars_repo_name: alxlhr/FLUOR

max_issues_repo_name: alxlhr/FLUOR

max_forks_repo_name: alxlhr/FLUOR


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
CODE DESCRIPTION HERE

Created on 2021-01-2021-01-05 13:53

@author: cook
"""
import logging
import numpy as np
import os
from typing import Any, Dict, List, Union

import recipe1
import recipe2

# =============================================================================
# Define variables
# =============================================================================
# define runs per group
RUNS_PER_GROUP = 50
# define number of groups
NUM_GROUPS = 10
# define whether this is a test run (just write to file don't run)
TEST_RUN = False
# Number of cores (when using multiprocessing)
NUM_CORES = 20
# output file name
RUN_FILE = 'apero-runs.txt'
# remove previous runs / log file
if os.path.exists('apero-log.txt'):
    os.remove('apero-log.txt')
if os.path.exists(RUN_FILE):
    os.remove(RUN_FILE)
# define logger
logger = logging.basicConfig(filename='apero-log.txt', level=logging.INFO,
                             format='%(asctime)s | %(levelname)s | %(message)s')

# =============================================================================
# Define functions
# =============================================================================
class Run:
    def __init__(self, recipename: str,
                 args: Union[List[Any], None] = None,
                 kwargs: Union[Dict[str, Any], None] = None):
        """
        This just stores the information for each run - for now it is
        either used to just write the "runstring" or to run the
        "recipe" .main function

        we use get_run_program at run time to get the correct module

        runstring is:
            recipename arg[0] arg[1] arg[2] --kwarg[0]=kwargs[kwarg[0]]

        :param recipename: str, the name of the recipe (for the run string)
        :param args: a list of positional arguments (can be None) passed to
                     recipe.main
        :param kwargs: a list of kwarg arguments passed to recipe.main
        """
        self.name = recipename
        if args is None:
            self.args = []
        else:
            self.args = list(args)
        if kwargs is None:
            self.kwargs = dict()
        else:
            self.kwargs = dict(kwargs)
        # get run string
        self.runstring = ''
        self.get_runstring()

    def get_runstring(self):
        self.runstring = '{0}'.format(self.name)
        for arg in self.args:
            self.runstring += ' {0}'.format(arg)
        for kwarg in self.kwargs:
            self.runstring += ' --{0}={1}'.format(kwarg, self.kwargs[kwarg])

    def get_run_program(self):
        if self.name == 'recipe1.py':
            return recipe1
        if self.name == 'recipe2.py':
            return recipe2

def linear_process(run_object):
    """
    Wrapper around run object

    :param run_object: Run instance
    :return:
    """
    # get run module
    rmod = run_object.get_run_program()
    # run main with args and kwargs
    return rmod.main(*run_object.args, **run_object.kwargs)

# =============================================================================
# Start of code
# =============================================================================
if __name__ == "__main__":
    # generate runs (just calls to recipe.py with group number and run number)
    runs = []
    groups = []
    # loop around groups
    for group_num in range(NUM_GROUPS):
        # loop around runs
        for run_num in range(RUNS_PER_GROUP):
            # define parity
            if group_num % 2 == 0:
                parity = 1
                rmod = recipe1
            else:
                parity = 2
                rmod = recipe2
            # generate command
            recipename = 'recipe{0}.py'.format(parity)
            run = Run(recipename, args=[group_num, run_num])
            # log message
            msg = 'PROCESSING: Adding "{0}"'.format(run.runstring)
            print(msg)
            # adding command
            logging.info(msg)
            # add to runs
            runs.append(run)
            # append group number
            groups.append(group_num)
    # define a counter for current group
    current_group = -1
    # -------------------------------------------------------------------------
    if TEST_RUN:
        # log message
        msg = 'PROCESSING: Writing run file: {0}'.format(RUN_FILE)
        print(msg)
        # write runs to a file
        with open(RUN_FILE, 'w') as wfile:
            # loop around run files
            for it, run in enumerate(runs):
                # get this rows group number
                group_num = groups[it]
                # see if current group has changed
                if current_group != group_num:
                    # write a command
                    wfile.write('\n# Group {0}\n'.format(group_num))
                    # update current group
                    current_group = int(group_num)
                # write line
                wfile.write(run.runstring + '\n')
    # -------------------------------------------------------------------------
    # Else we run with multiprocessing
    # TODO: this is what we need to replace - this works well on one machine
    #       but how do we get this to work on a cluster with nodes?
    else:
        # deal with Pool specific imports
        from multiprocessing import get_context
        from multiprocessing import set_start_method
        try:
            set_start_method("spawn")
        except RuntimeError:
            pass
        # get unique groups
        ugroups = np.unique(groups)
        # make groups and runs numpy arrays
        groups, runs = np.array(groups), np.array(runs)
        # storage for returned outputs
        return_dict = dict()
        # loop around groups
        for ugroup in ugroups:
            # get list for return
            return_dict[ugroup] = []
            # get all runs in group
            mask = groups == ugroup
            # arguments to pool must be a list of arguments
            poolargs = []
            for run in runs[mask]:
                poolargs.append([run])
            # send the runs for this mask to the pool
            with get_context('spawn').Pool(NUM_CORES, maxtasksperchild=1) as pool:
                # get parallel process
                results = pool.starmap(linear_process, poolargs)
            # get results
            for row in range(len(results)):
                return_dict[ugroup].append(results[row])

        # print the results after
        print('Results of multiprocess:')
        for ugroup in return_dict:
            print('Group {0}'.format(ugroup))
            for row in range(len(return_dict[ugroup])):
                margs = [row, return_dict[ugroup][row]]
                print('\t\tResult {0}: {1}'.format(*margs))

# =============================================================================
# End of code
# =============================================================================


text:
# Copyright (c) Microsoft Corporation and contributors.
# Licensed under the MIT License.

from typing import Optional, Union

import networkx as nx
import numpy as np

from ..utils import to_laplacian
from .base import BaseSpectralEmbed

class LaplacianSpectralEmbed(BaseSpectralEmbed):
    r"""
    Class for computing the laplacian spectral embedding of a graph.

    The laplacian spectral embedding (LSE) is a k-dimensional Euclidean representation
    of the graph based on its Laplacian matrix. It relies on an SVD to reduce
    the dimensionality to the specified ``n_components``, or if ``n_components`` is
    unspecified, can find a number of dimensions automatically.

    Parameters
    ----------
    form : {'DAD' (default), 'I-DAD', 'R-DAD'}, optional
        Specifies the type of Laplacian normalization to use.

    n_components : int or None, default = None
        Desired dimensionality of output data. If "full",
        ``n_components`` must be ``<= min(X.shape)``. Otherwise, ``n_components`` must be
        ``< min(X.shape)``. If None, then optimal dimensions will be chosen by
        :func:`~graspologic.embed.select_dimension` using ``n_elbows`` argument.

    n_elbows : int, optional, default: 2
        If ``n_components`` is None, then compute the optimal embedding dimension using
        :func:`~graspologic.embed.select_dimension`. Otherwise, ignored.

    algorithm : {'randomized' (default), 'full', 'truncated'}, optional
        SVD solver to use:

        - 'randomized'
            Computes randomized svd using
            :func:`sklearn.utils.extmath.randomized_svd`
        - 'full'
            Computes full svd using :func:`scipy.linalg.svd`
        - 'truncated'
            Computes truncated svd using :func:`scipy.sparse.linalg.svds`

    n_iter : int, optional (default = 5)
        Number of iterations for randomized SVD solver. Not used by 'full' or
        'truncated'. The default is larger than the default in randomized_svd
        to handle sparse matrices that may have large slowly decaying spectrum.

    check_lcc : bool , optional (defult = True)
        Whether to check if input graph is connected. May result in non-optimal
        results if the graph is unconnected. If True and input is unconnected,
        a UserWarning is thrown. Not checking for connectedness may result in
        faster computation.

    regularizer: int, float or None, optional (default=None)
        Constant to be added to the diagonal of degree matrix. If None, average
        node degree is added. If int or float, must be >= 0. Only used when
        ``form`` is 'R-DAD'.

    concat : bool, optional (default False)
        If graph is directed, whether to concatenate left and right (out and in) latent
        positions along axis 1.

    Attributes
    ----------
    n_features_in_: int
        Number of features passed to the
        :func:`~graspologic.embed.LaplacianSpectralEmbed.fit` method.

    latent_left_ : array, shape (n_samples, n_components)
        Estimated left latent positions of the graph.

    latent_right_ : array, shape (n_samples, n_components), or None
        Only computed when the graph is directed, or adjacency matrix is assymetric.
        Estimated right latent positions of the graph. Otherwise, None.

    singular_values_ : array, shape (n_components)
        Singular values associated with the latent position matrices.

    svd_seed : int or None (default ``None``)
        Only applicable for ``algorithm="randomized"``; allows you to seed the
        randomized svd solver for deterministic, albeit pseudo-randomized behavior.

    See Also
    --------
    graspologic.embed.select_svd
    graspologic.embed.select_dimension
    graspologic.utils.to_laplacian

    Notes
    -----
    The singular value decomposition:

    .. math:: A = U \Sigma V^T

    is used to find an orthonormal basis for a matrix, which in our case is the
    Laplacian matrix of the graph. These basis vectors (in the matrices U or V) are
    ordered according to the amount of variance they explain in the original matrix.
    By selecting a subset of these basis vectors (through our choice of dimensionality
    reduction) we can find a lower dimensional space in which to represent the graph.

    References
    ----------
    .. [1] Sussman, D.L., Tang, M., Fishkind, D.E., Priebe, C.E.  "A
       Consistent Adjacency Spectral Embedding for Stochastic Blockmodel Graphs,"
       Journal of the American Statistical Association, Vol. 107(499), 2012.
    .. [2] Von Luxburg, Ulrike. "A tutorial on spectral clustering," Statistics
        and computing, Vol. 17(4), pp. 395-416, 2007.
    .. [3] Rohe, Karl, Sourav Chatterjee, and Bin Yu. "Spectral clustering and
        the high-dimensional stochastic blockmodel," The Annals of Statistics,
        Vol. 39(4), pp. 1878-1915, 2011.
    """

    def __init__(
        self,
        form: str = "DAD",
        n_components: Optional[int] = None,
        n_elbows: Optional[int] = 2,
        algorithm: str = "randomized",
        n_iter: int = 5,
        check_lcc: bool = True,
        regularizer: Optional[float] = None,
        concat: bool = False,
        svd_seed: Optional[int] = None,
    ):
        super().__init__(
            n_components=n_components,
            n_elbows=n_elbows,
            algorithm=algorithm,
            n_iter=n_iter,
            check_lcc=check_lcc,
            concat=concat,
            svd_seed=svd_seed,
        )
        self.form = form
        self.regularizer = regularizer

    def fit(self, graph: Union[np.ndarray, nx.Graph], y=None):
        """
        Fit LSE model to input graph

        By default, uses the Laplacian normalization of the form:

        .. math:: L = D^{-1/2} A D^{-1/2}

        Parameters
        ----------
        graph : array-like, scipy.sparse.csr_matrix, or networkx.Graph
            Input graph to embed. see graspologic.utils.import_graph

        Returns
        -------
        self : object
            Returns an instance of self.
        """
        A = self._fit(graph)
        L_norm = to_laplacian(A, form=self.form, regularizer=self.regularizer)
        self._reduce_dim(L_norm)

        self.is_fitted_ = True

        return self

    def _compute_oos_prediction(self, X, directed):
        """
        Computes the out-of-sample latent position estimation.
        Parameters
        ----------
        X: np.ndarray
            Input to do oos embedding on.
        directed: bool
            Indication if graph is directed or undirected
        Returns
        -------
        out : array_like or tuple, shape
        """

        if not directed:
            if X.ndim == 1:
                X = np.expand_dims(X, axis=0)

            return ((X @ self._pinv_left).T / np.sum(X, axis=1)).T
        elif directed:
            X_0 = X[0]
            X_1 = X[1]

            if X_0.ndim == 1:
                X_0 = np.expand_dims(X_0, axis=0)
                X_1 = np.expand_dims(X_1, axis=0)

            return ((X_1 @ self._pinv_right).T / np.sum(X_1, axis=1)).T, (
                (X_0 @ self._pinv_left).T / np.sum(X_0, axis=1)
            ).T

max_stars_repo_name: pbourke/graspologic

max_issues_repo_name: pbourke/graspologic

max_forks_repo_name: pbourke/graspologic

reason: import numpy,import networkx


text:
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
from keras.callbacks import EarlyStopping
from matplotlib import pyplot as plt

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

source = pd.read_csv("Datasets/titanic_all_numeric.csv", delimiter=",")
# Source file times one convert True/False into 1/0
source = source * 1
# Convert DataFrame into the numpy matrix
source = source.as_matrix()
# Select whole of the columns except for the target value
predictors = np.delete(source, 0, 1)
# Number of the features or attributes
n_cols = predictors.shape[1]
# Select the first column which contained target values
target = source[:, 0]
# You can also obtain survived (target variable) by using source.survived
# Convert the target to categorical target
target = to_categorical(target)

def model_generator(number_of_unit):
    # Define a model
    model = Sequential()
    model.add(Dense(number_of_unit, activation='relu', input_shape=(n_cols,)))
    model.add(Dense(number_of_unit, activation='relu'))
    model.add(Dense(2, activation='softmax'))
    return model

model1 = model_generator(10)
model2 = model_generator(100)

# Compile the models
model1.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model2.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Using EarlyStopping monitor callback
# patience=3 indicates how many epochs should pass without improving
# Imporoving here means less val_loss
early_stopping_monitor = EarlyStopping(monitor='val_loss', patience=3)
# Fit the model
model1_training = model1.fit(predictors, target, epochs=15, validation_split=0.2, callbacks=[early_stopping_monitor], verbose=False)
model2_training = model2.fit(predictors, target, epochs=15, validation_split=0.2, callbacks=[early_stopping_monitor], verbose=False)
# Create the plot
plt.plot(model1_training.history['val_loss'], 'r', model2_training.history['val_loss'], 'b')
plt.suptitle('Note: Model1 and Model2 have 2 hidden \nlayers with 10 and 100 units respectively', fontsize=11)
plt.xlabel('Epochs')
plt.ylabel('Validation Score')
plt.legend(('Model1', 'Model2'), loc='upper right')
plt.show()

max_stars_repo_name: JoyeBright/Deep-Learning

max_issues_repo_name: JoyeBright/Deep-Learning

max_forks_repo_name: JoyeBright/Deep-Learning


text:
#!/usr/bin/python
# -*- coding:utf-8 -*-

"""
递归神经网络测试
"""

import numpy as np
from NeuralNetwork.RNN import rnn
from NeuralNetwork.ANN.activators import IdentityActivator

def gradient_check():
    """
    梯度检查
    """
    # 设计一个误差函数，取所有节点输出项之和
    error_function = lambda o: o.sum()
    rl = rnn.RecurrentLayer(3, 2, IdentityActivator(), 1e-3)

    # 计算forward值
    x, d = rnn.data_set()
    rl.forward(x[0])
    rl.forward(x[1])

    # 求取sensitivity map
    sensitivity_array = np.ones(rl.state_list[-1].shape, dtype=np.float64)

    # 计算梯度
    rl.backward(sensitivity_array, IdentityActivator())

    # 检查梯度
    epsilon = 10e-4
    for i in range(rl.W.shape[0]):
        for j in range(rl.W.shape[1]):
            rl.W[i, j] += epsilon
            rl.reset_state()
            rl.forward(x[0])
            rl.forward(x[1])
            err1 = error_function(rl.state_list[-1])
            rl.W[i, j] -= 2 * epsilon
            rl.reset_state()
            rl.forward(x[0])
            rl.forward(x[1])
            err2 = error_function(rl.state_list[-1])
            expect_grad = (err1 - err2) / (2 * epsilon)
            rl.W[i, j] += epsilon
            print('weights(%d,%d): expected - actural %f - %f' % (i, j, expect_grad, rl.gradient[i, j]))

if __name__ == '__main__':
    gradient_check()

max_stars_repo_name: neohope/MLStudy

max_issues_repo_name: neohope/MLStudy

max_forks_repo_name: neohope/MLStudy