text:
include("../structures.jl")

# data taken from http://sebastien.godard.pagesperso-orange.fr/man_sar.html

global SAR_DB = Dict(
    "B" => (
        "-B",
        "Report paging statistics. The following values are displayed:",
        Dict(
            "pgpgin/s" =>
                "Total number of kilobytes the system paged in from disk per second.",
            "pgpgout/s" =>
                "Total number of kilobytes the system paged out to disk per second.",
            "fault/s" =>
                "Number of page faults (major + minor) made by the system per second. This is not a count of page faults that generate I/O, because some page faults can be resolved without I/O.",
            "majflt/s" =>
                "Number of major faults the system has made per second, those which have required loading a memory page from disk.",
            "pgfree/s" =>
                "Number of pages placed on the free list by the system per second.",
            "pgscank/s" =>     "Number of pages scanned by the kswapd daemon per second.",
            "pgscand/s" => "Number of pages scanned directly per second.",
            "pgsteal/s" =>
                "Number of pages the system has reclaimed from cache (pagecache and swapcache) per second to satisfy its memory demands.",

            "%vmeff" =>
                "Calculated as pgsteal / pgscan, this is a metric of the efficiency of page reclaim. If it is near 100% then almost every page coming off the tail of the inactive list is being reaped. If it gets too low (e.g. less than 30%) then the virtual memory is having some difficulty. This field is displayed as zero if no pages have been scanned during the interval of time.",

        ),
    ),
    "b" => (
        "-b",
        "Report I/O and transfer rate statistics. The following values are displayed:",
        Dict(
            "tps" =>
                "Total number of transfers per second that were issued to physical devices. A transfer is an I/O request to a physical device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.",
            "rtps" =>
                "Total number of read requests per second issued to physical devices.",
            "wtps" =>
                "Total number of write requests per second issued to physical devices.",
            "dtps" =>
                "Total number of discard requests per second issued to physical devices.",
            "bread/s" =>
                "Total amount of data read from the devices in blocks per second. Blocks are equivalent to sectors and therefore have a size of 512 bytes.",
            "bwrtn/s" =>     "Total amount of data written to devices in blocks per second.",
            "bdscd/s" =>     "Total amount of data discarded for devices in blocks per second.",

        ),
    ),
    "d" => (
        "-d",
        "Report activity for each block device. When data are displayed, the device specification devM-n is generally used (DEV column). M is the major number of the device and n its minor number. Device names may also be pretty-printed if option -p is used or persistent device names can be printed if option -j is used (see below). Statistics for all devices are displayed unless a restricted list is specified using option \"--dev=\" (see corresponding option entry). Note that disk activity depends on sadc options \"-S DISK\" and \"-S XDISK\" to be collected. The following values are displayed:",
        Dict(
            "tps" =>
                "Total number of transfers per second that were issued to physical devices. A transfer is an I/O request to a physical device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size.",
            "rkB/s" => "Number of kilobytes read from the device per second.",
            "wkB/s" => "Number of kilobytes written to the device per second.",
            "dkB/s" => "Number of kilobytes discarded for the device per second.",
            "areq-sz" =>
                "The average size (in kilobytes) of the requests that were issued to the device. Note: In previous versions, this field was known as avgrq-sz and was expressed in sectors.",
            "aqu-sz" =>
                "The average queue length of the requests that were issued to the device. Note: In previous versions, this field was known as avgqu-sz.",
            "await" =>
                "The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.",

            "%util" =>
                "Percentage of elapsed time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100% for devices serving requests serially. But for devices serving requests in parallel, such as RAID arrays and modern SSDs, this number does not reflect their performance limits.",

        ),
    ),
    "F" => (
        "-F [ MOUNT ]",
        "Display statistics for currently mounted filesystems. Pseudo-filesystems are ignored. At the end of the report, sar will display a summary of all those filesystems. Use of the MOUNT parameter keyword indicates that mountpoint will be reported instead of filesystem device. Statistics for all filesystems are displayed unless a restricted list is specified using option \"--fs=\" (see corresponding  option entry). Note that filesystems statistics depend on sadc option \"-S XDISK\" to be collected. The following values are displayed:",
        Dict(
            "MBfsfree" =>
                "Total amount of free space in megabytes (including space available only to privileged user).",
            "MBfsused" => "Total amount of space used in megabytes.",
            "%fsused" =>
                "Percentage of filesystem space used, as seen by a privileged user.",
            "%ufsused" =>
                "Percenalent to option -d below, except that the timestamp is always expressed in seconds since the epoch (00:00:00 UTC 01/01/1970).tage of filesystem space used, as seen by an unprivileged user.",
            "Ifree" => "Total number of free file nodes in filesystem.",
            "Iused" => "Total number of file nodes used in filesystem.",
            "%Iused" => "Percentage of file nodes used in filesystem.",

        ),
    ),
    "H" => (
        "-H",
        "Report hugepages utilization statistics.The following values are displayed:",
        Dict(
            "kbhugfree" =>
                "Amount of hugepages memory in kilobytes that is not yet allocated.",
            "kbhugused" =>
                "Amount of hugepages memory in kilobytes that has been allocated.",
            "%hugused" =>     "Percentage of total hugepages memory that has been allocated.",
            "kbhugrsvd" => "Amount of reserved hugepages memory in kilobytes.",
            "kbhugsurp" => "Amount of surplus hugepages memory in kilobytes.",

        ),
    ),
    "m" => (
        "-m { keyword [,...] | ALL }",
        "Report power management statistics. Note that these statistics depend on sadc's option \"-S POWER\" to be collected.",
        Dict(

            # Possible keywords are CPU, FAN, FREQ, IN, TEMP and USB.
            "CPU" => (
                "With the CPU keyword, statistics about CPU are reported. The following value is displayed:",
                Dict(
                    "MHz" => "Instantaneous CPU clock frequency in MHz.",

                ),
            ),
            "FAN" => (
                "With the FAN keyword, statistics about fans speed are reported. The following values are displayed:",
                Dict(
                    "rpm" => "Fan speed expressed in revolutions per minute.",
                    "drpm" =>
                        "This field is calculated as the difference between current fan speed (rpm) and its low limit (fan_min).",
                    "DEVICE" => "Sensor device name.",

                ),
            ),
            "FREQ" => (
                "With the FREQ keyword, statistics about CPU clock frequency are reported. The following value is displayed:",
                Dict(
                    "wghMHz" =>
                        "Weighted average CPU clock frequency in MHz. Note that the cpufreq-stats driver must be compiled in the kernel for this option to work.",
                ),
            ),
            "IN" => (
                "With the IN keyword, statistics about voltage inputs are reported. The following values are displayed:",
                Dict(
                    "inV" => "Voltage input expressed in Volts.",

                    "%in" =>
                        "Relative input value. A value of 100% means that voltage input has reached its high limit (in_max) whereas a value of 0% means that it has reached its low limit (in_min).",
                    "DEVICE" => "Sensor device name.",

                ),
            ),
            "TEMP" => (
                "With the TEMP keyword, statistics about devices temperature are reported. The following values are displayed:",
                Dict(
                    "degC" =>     "Device temperature expressed in degrees Celsius.",

                    "%temp" =>
                        "Relative device temperature. A value of 100% means that temperature has reached its high limit (temp_max).",
                    "DEVICE" => "Sensor device name.",

                ),
            ),
            "USB" => (
                "With the USB keyword, the sar command takes a snapshot of all the USB devices currently plugged into the system. At the end of the report, sar will display a summary of all those USB devices. The following values are displayed:",
                Dict(
                    "BUS" => "Root hub number of the USB device.",
                    "idvendor" =>     "Vendor ID number (assigned by USB organization).",
                    "idprod" => "Product ID number (assigned by Manufacturer).",
                    "maxpower" =>
                        "Maximum power consumption of the device (expressed in mA).",
                    "manufact" => "Manufacturer name.",
                    "product" => "Product name.",

                ),
            ),
        ),
    ),

    "n" => (
        "-n { keyword [,...] | ALL }",
        "Report network statistics.",
        Dict(
            # Possible keywords are DEV, EDEV, FC, ICMP, EICMP, ICMP6, EICMP6, IP, EIP, IP6, EIP6, NFS, NFSD, SOCK, SOCK6, SOFT, TCP, ETCP, UDP and UDP6."

            "DEV" => (
                "With the DEV keyword, statistics from the network devices are reported. Statistics for all network interfaces are displayed unless a restricted list is specified using option \"--iface=\" (see corresponding option entry). The following values are displayed:",
                Dict(
                    "IFACE" =>
                        "Name of the network interface for which statistics are reported.",
                    "rxpck/s" => "Total number of packets received per second.",
                    "txpck/s" =>     "Total number of packets transmitted per second.",
                    "rxkB/s" =>     "Total number of kilobytes received per second.",
                    "txkB/s" =>     "Total number of kilobytes transmitted per second.",
                    "rxcmp/s" =>
                        "Number of compressed packets received per second (for cslip etc.).",
                    "txcmp/s" =>     "Number of compressed packets transmitted per second.",
                    "rxmcst/s" =>     "Number of multicast packets received per second.",
                    "%ifutil" =>
                        "Utilization percentage of the network interface. For half-duplex interfaces, utilization is calculated using the sum of rxkB/s and txkB/s as a percentage of the interface speed. For full-duplex, this is the greater of rxkB/S or txkB/s.",
                ),
            ),
            "EDEV" => (
                "With the EDEV keyword, statistics on failures (errors) from the network devices are reported. Statistics for all network interfaces are displayed unless a restricted list is specified using option \"--iface=\" (see corresponding option entry). The following values are displayed:",
                Dict(
                    "IFACE" =>
                        "Name of the network interface for which statistics are reported.",
                    "rxerr/s" =>     "Total number of bad packets received per second.",
                    "txerr/s" =>
                        "Total number of errors that happened per second while transmitting packets.",
                    "coll/s" =>
                        "Number of collisions that happened per second while transmitting packets.",
                    "rxdrop/s" =>
                        "Number of received packets dropped per second because of a lack of space in linux buffers.",
                    "txdrop/s" =>
                        "Number of transmitted packets dropped per second because of a lack of space in linux buffers.",
                    "txcarr/s" =>
                        "Number of carrier-errors that happened per second while transmitting packets.",
                    "rxfram/s" =>
                        "Number of frame alignment errors that happened per second on received packets.",
                    "rxfifo/s" =>
                        "Number of FIFO overrun errors that happened per second on received packets.",
                    "txfifo/s" =>
                        "Number of FIFO overrun errors that happened per second on transmitted packets.",

                ),
            ),
            "FC" => (
                "With the FC keyword, statistics about fibre channel traffic are reported. Note that fibre channel statistics depend on sadc's option \"-S DISK\" to be collected. The following values are displayed:",
                Dict(
                    "FCHOST" =>
                        "Name of the fibre channel host bus adapter (HBA) interface for which statistics are reported.",
                    "fch_rxf/s" =>     "The total number of frames received per second.",
                    "fch_txf/s" =>     "The total number of frames transmitted per second.",
                    "fch_rxw/s" =>
                        "The total number of transmission words received per second.",
                    "fch_txw/s" =>
                        "The total number of transmission words transmitted per second.",

                ),
            ),
            "ICMP" => (
                "With the ICMP keyword, statistics about ICMPv4 network traffic are reported. Note that ICMPv4 statistics depend on sadc's option \"-S SNMP\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "imsg/s" =>
                        "The total number of ICMP messages which the entity received per second [icmpInMsgs]. Note that this counter includes all those counted by ierr/s.",
                    "omsg/s" =>
                        "The total number of ICMP messages which this entity attempted to send per second [icmpOutMsgs]. Note that this counter includes all those counted by oerr/s.",
                    "iech/s" =>
                        "The number of ICMP Echo (request) messages received per second [icmpInEchos].",
                    "iechr/s" =>
                        "The number of ICMP Echo Reply messages received per second [icmpInEchoReps].",
                    "oech/s" =>
                        "The number of ICMP Echo (request) messages sent per second [icmpOutEchos].",
                    "oechr/s" =>
                        "The number of ICMP Echo Reply messages sent per second [icmpOutEchoReps].",
                    "itm/s" =>
                        "The number of ICMP Timestamp (request) messages received per second [icmpInTimestamps].",
                    "itmr/s" =>
                        "The number of ICMP Timestamp Reply messages received per second [icmpInTimestampReps].",
                    "otm/s" =>
                        "The number of ICMP Timestamp (request) messages sent per second [icmpOutTimestamps].",
                    "otmr/s" =>
                        "The number of ICMP Timestamp Reply messages sent per second [icmpOutTimestampReps].",
                    "iadrmk/s" =>
                        "The number of ICMP Address Mask Request messages received per second [icmpInAddrMasks].",
                    "iadrmkr/s" =>
                        "The number of ICMP Address Mask Reply messages received per second [icmpInAddrMaskReps].",
                    "oadrmk/s" =>
                        "The number of ICMP Address Mask Request messages sent per second [icmpOutAddrMasks].",
                    "oadrmkr/s" =>
                        "The number of ICMP Address Mask Reply messages sent per second [icmpOutAddrMaskReps].",
                ),
            ),
            "EICMP" => (
                "With the EICMP keyword, statistics about ICMPv4 error messages are reported. Note that ICMPv4 statistics depend on sadc's option \"-S SNMP\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "ierr/s" =>
                        "The number of ICMP messages per second which the entity received but determined as having ICMP-specific errors (bad ICMP checksums, bad length, etc.) [icmpInErrors].",
                    "oerr/s" =>
                        "The number of ICMP messages per second which this entity did not send due to problems discovered within ICMP such as a lack of buffers [icmpOutErrors].",
                    "idstunr/s" =>
                        "The number of ICMP Destination Unreachable messages received per second [icmpInDestUnreachs].",
                    "odstunr/s" =>
                        "The number of ICMP Destination Unreachable messages sent per second [icmpOutDestUnreachs].",
                    "itmex/s" =>
                        "The number of ICMP Time Exceeded messages received per second [icmpInTimeExcds].",
                    "otmex/s" =>
                        "The number of ICMP Time Exceeded messages sent per second [icmpOutTimeExcds].",
                    "iparmpb/s" =>
                        "The number of ICMP Parameter Problem messages received per second [icmpInParmProbs].",
                    "oparmpb/s" =>
                        "The number of ICMP Parameter Problem messages sent per second [icmpOutParmProbs].",
                    "isrcq/s" =>
                        "The number of ICMP Source Quench messages received per second [icmpInSrcQuenchs].",
                    "osrcq/s" =>
                        "The number of ICMP Source Quench messages sent per second [icmpOutSrcQuenchs].",
                    "iredir/s" =>
                        "The number of ICMP Redirect messages received per second [icmpInRedirects].",
                    "oredir/s" =>
                        "The number of ICMP Redirect messages sent per second [icmpOutRedirects].",
                ),
            ),
            "ICMP6" => (
                "With the ICMP6 keyword, statistics about ICMPv6 network traffic are reported. Note that ICMPv6 statistics depend on sadc's option \"-S IPV6\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "imsg6/s" =>
                        "The total number of ICMP messages received by the interface per second which includes all those counted by ierr6/s [ipv6IfIcmpInMsgs].",
                    "omsg6/s" =>
                        "The total number of ICMP messages which this interface attempted to send per second [ipv6IfIcmpOutMsgs].",
                    "iech6/s" =>
                        "The number of ICMP Echo (request) messages received by the interface per second [ipv6IfIcmpInEchos].",
                    "iechr6/s" =>
                        "The number of ICMP Echo Reply messages received by the interface per second [ipv6IfIcmpInEchoReplies].",
                    "oechr6/s" =>
                        "The number of ICMP Echo Reply messages sent by the interface per second [ipv6IfIcmpOutEchoReplies].",
                    "igmbq6/s" =>
                        "The number of ICMPv6 Group Membership Query messages received by the interface per second [ipv6IfIcmpInGroupMembQueries].",
                    "igmbr6/s" =>
                        "The number of ICMPv6 Group Membership Response messages received by the interface per second [ipv6IfIcmpInGroupMembResponses].",
                    "ogmbr6/s" =>
                        "The number of ICMPv6 Group Membership Response messages sent per second [ipv6IfIcmpOutGroupMembResponses].",
                    "igmbrd6/s" =>
                        "The number of ICMPv6 Group Membership Reduction messages received by the interface per second [ipv6IfIcmpInGroupMembReductions].",
                    "ogmbrd6/s" =>
                        "The number of ICMPv6 Group Membership Reduction messages sent per second [ipv6IfIcmpOutGroupMembReductions].",
                    "irtsol6/s" =>
                        "The number of ICMP Router Solicit messages received by the interface per second [ipv6IfIcmpInRouterSolicits].",
                    "ortsol6/s" =>
                        "The number of ICMP Router Solicitation messages sent by the interface per second [ipv6IfIcmpOutRouterSolicits].",
                    "irtad6/s" =>
                        "The number of ICMP Router Advertisement messages received by the interface per second [ipv6IfIcmpInRouterAdvertisements].",
                    "inbsol6/s" =>
                        "The number of ICMP Neighbor Solicit messages received by the interface per second [ipv6IfIcmpInNeighborSolicits].",
                    "onbsol6/s" =>
                        "The number of ICMP Neighbor Solicitation messages sent by the interface per second [ipv6IfIcmpOutNeighborSolicits].",
                    "inbad6/s" =>
                        "The number of ICMP Neighbor Advertisement messages received by the interface per second [ipv6IfIcmpInNeighborAdvertisements].",
                    "onbad6/s" =>
                        "The number of ICMP Neighbor Advertisement messages sent by the interface per second [ipv6IfIcmpOutNeighborAdvertisements].",
                ),
            ),
            "EICMP6" => (
                "With the EICMP6 keyword, statistics about ICMPv6 error messages are reported. Note that ICMPv6 statistics depend on sadc's option \"-S IPV6\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "ierr6/s" =>
                        "The number of ICMP messages per second which the interface received but determined as having ICMP-specific errors (bad ICMP checksums, bad length, etc.) [ipv6IfIcmpInErrors]",
                    "idtunr6/s" =>
                        "The number of ICMP Destination Unreachable messages received by the interface per second [ipv6IfIcmpInDestUnreachs].",
                    "odtunr6/s" =>
                        "The number of ICMP Destination Unreachable messages sent by the interface per second [ipv6IfIcmpOutDestUnreachs].",
                    "itmex6/s" =>
                        "The number of ICMP Time Exceeded messages received by the interface per second [ipv6IfIcmpInTimeExcds].",
                    "otmex6/s" =>
                        "The number of ICMP Time Exceeded messages sent by the interface per second [ipv6IfIcmpOutTimeExcds].",
                    "iprmpb6/s" =>
                        "The number of ICMP Parameter Problem messages received by the interface per second [ipv6IfIcmpInParmProblems].",
                    "oprmpb6/s" =>
                        "The number of ICMP Parameter Problem messages sent by the interface per second [ipv6IfIcmpOutParmProblems].",
                    "iredir6/s" =>
                        "The number of Redirect messages received by the interface per second [ipv6IfIcmpInRedirects].",
                    "oredir6/s" =>
                        "The number of Redirect messages sent by the interface by second [ipv6IfIcmpOutRedirects].",
                    "ipck2b6/s" =>
                        "The number of ICMP Packet Too Big messages received by the interface per second [ipv6IfIcmpInPktTooBigs].",
                    "opck2b6/s" =>
                        "The number of ICMP Packet Too Big messages sent by the interface per second [ipv6IfIcmpOutPktTooBigs].",
                ),
            ),
            "IP" => (
                "With the IP keyword, statistics about IPv4 network traffic are reported. Note that IPv4 statistics depend on sadc's option \"-S SNMP\" to be collected.The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "irec/s" =>
                        "The total number of input datagrams received from interfaces per second, including those received in error [ipInReceives].",
                    "fwddgm/s" =>
                        "The number of input datagrams per second, for which this entity was not their final IP destination, as a result of which an attempt was made to find a route to forward them to that final destination [ipForwDatagrams].",
                    "idel/s" =>
                        "The total number of input datagrams successfully delivered per second to IP user-protocols (including ICMP) [ipInDelivers].",
                    "orq/s" =>
                        "The total number of IP datagrams which local IP user-protocols (including ICMP) supplied per second to IP in requests for transmission [ipOutRequests]. Note that this counter does not include any datagrams counted in fwddgm/s.",
                    "asmrq/s" =>
                        "The number of IP fragments received per second which needed to be reassembled at this entity [ipReasmReqds].",
                    "asmok/s" =>
                        "The number of IP datagrams successfully re-assembled per second [ipReasmOKs].",
                    "fragok/s" =>
                        "The number of IP datagrams that have been successfully fragmented at this entity per second [ipFragOKs].",
                    "fragcrt/s" =>
                        "The number of IP datagram fragments that have been generated per second as a result of fragmentation at this entity [ipFragCreates].",
                ),
            ),
            "EIP" => (
                "With the EIP keyword, statistics about IPv4 network errors are reported. Note that IPv4 statistics depend on sadc's option \"-S SNMP\" to be collected.The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "ihdrerr/s" =>
                        "The number of input datagrams discarded per second due to errors in their IP headers, including bad checksums, version number mismatch, other format errors, time-to-live exceeded, errors discovered in processing their IP options, etc. [ipInHdrErrors]",
                    "iadrerr/s" =>
                        "The number of input datagrams discarded per second because the IP address in their IP header's destination field was not a valid address to be received at this entity. This count includes invalid addresses (e.g., 0.0.0.0) and addresses of unsupported Classes (e.g., Class E). For entities which are not IP routers and therefore do not forward datagrams, this counter includes datagrams discarded because the destination address was not a local address [ipInAddrErrors].",
                    "iukwnpr/s" =>
                        "The number of locally-addressed datagrams received successfully but discarded per second because of an unknown or unsupported protocol [ipInUnknownProtos].",
                    "idisc/s" =>
                        "The number of input IP datagrams per second for which no problems were encountered to prevent their continued processing, but which were discarded (e.g., for lack of buffer space) [ipInDiscards]. Note that this counter does not include any datagrams discarded while awaiting re-assembly.",
                    "odisc/s" =>
                        "The number of output IP datagrams per second for which no problem was encountered to prevent their transmission to their destination, but which were discarded (e.g., for lack of buffer space) [ipOutDiscards]. Note that this counter would include datagrams counted in fwddgm/s if any such packets met this (discretionary) discard criterion.",
                    "onort/s" =>
                        "The number of IP datagrams discarded per second because no route could be found to transmit them to their destination [ipOutNoRoutes]. Note that this counter includes any packets counted in fwddgm/s which meet this 'no-route' criterion. Note that this includes any datagrams which a host cannot route because all of its default routers are down.",
                    "asmf/s" =>
                        "The number of failures detected per second by the IP re-assembly algorithm (for whatever reason: timed out, errors, etc) [ipReasmFails]. Note that this is not necessarily a count of discarded IP fragments since some algorithms can lose track of the number of fragments by combining them as they are received.",
                    "fragf/s" =>
                        "The number of IP datagrams that have been discarded per second because they needed to be fragmented at this entity but could not be, e.g., because their Don't Fragment flag was set [ipFragFails].",
                ),
            ),
            "IP6" => (
                "With the IP6 keyword, statistics about IPv6 network traffic are reported. Note that IPv6 statistics depend on sadc's option \"-S IPV6\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "irec6/s" =>
                        "The total number of input datagrams received from interfaces per second, including those received in error [ipv6IfStatsInReceives].",
                    "fwddgm6/s" =>
                        "The number of output datagrams per second which this entity received and forwarded to their final destinations [ipv6IfStatsOutForwDatagrams].",
                    "idel6/s" =>
                        "The total number of datagrams successfully delivered per second to IPv6 user-protocols (including ICMP) [ipv6IfStatsInDelivers].",
                    "orq6/s" =>
                        "The total number of IPv6 datagrams which local IPv6 user-protocols (including ICMP) supplied per second to IPv6 in requests for transmission [ipv6IfStatsOutRequests]. Note that this counter does not include any datagrams counted in fwddgm6/s.",
                    "asmrq6/s" =>
                        "The number of IPv6 fragments received per second which needed to be reassembled at this interface [ipv6IfStatsReasmReqds].",
                    "asmok6/s" =>
                        "The number of IPv6 datagrams successfully reassembled per second [ipv6IfStatsReasmOKs].",
                    "imcpck6/s" =>
                        "The number of multicast packets received per second by the interface [ipv6IfStatsInMcastPkts].",
                    "omcpck6/s" =>
                        "The number of multicast packets transmitted per second by the interface [ipv6IfStatsOutMcastPkts].",
                    "fragok6/s" =>
                        "The number of IPv6 datagrams that have been successfully fragmented at this output interface per second [ipv6IfStatsOutFragOKs].",
                    "fragcr6/s" =>
                        "The number of output datagram fragments that have been generated per second as a result of fragmentation at this output interface [ipv6IfStatsOutFragCreates].",
                ),
            ),
            "EIP6" => (
                "With the EIP6 keyword, statistics about IPv6 network errors are reported. Note that IPv6 statistics depend on sadc's option \"-S IPV6\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "ihdrer6/s" =>
                        "The number of input datagrams discarded per second due to errors in their IPv6 headers, including version number mismatch, other format errors, hop count exceeded, errors discovered in processing their IPv6 options, etc. [ipv6IfStatsInHdrErrors]",
                    "iadrer6/s" =>
                        "The number of input datagrams discarded per second because the IPv6 address in their IPv6 header's destination field was not a valid address to be received at this entity. This count includes invalid addresses (e.g., ::0) and unsupported addresses (e.g., addresses with unallocated prefixes). For entities which are not IPv6 routers and therefore do not forward datagrams, this counter includes datagrams discarded because the destination address was not a local address [ipv6IfStatsInAddrErrors].",
                    "iukwnp6/s" =>
                        "The number of locally-addressed datagrams received successfully but discarded per second because of an unknown or unsupported protocol [ipv6IfStatsInUnknownProtos].",
                    "i2big6/s" =>
                        "The number of input datagrams that could not be forwarded per second because their size exceeded the link MTU of outgoing interface [ipv6IfStatsInTooBigErrors].",
                    "idisc6/s" =>
                        "The number of input IPv6 datagrams per second for which no problems were encountered to prevent their continued processing, but which were discarded (e.g., for lack of buffer space) [ipv6IfStatsInDiscards]. Note that this counter does not include any datagrams discarded while awaiting re-assembly.",
                    "odisc6/s" =>
                        "The number of output IPv6 datagrams per second for which no problem was encountered to prevent their transmission to their destination, but which were discarded (e.g., for lack of buffer space) [ipv6IfStatsOutDiscards]. Note that this counter would include datagrams counted in fwddgm6/s if any such packets met this (discretionary) discard criterion.",
                    "inort6/s" =>
                        "The number of input datagrams discarded per second because no route could be found to transmit them to their destination [ipv6IfStatsInNoRoutes].",
                    "onort6/s" =>
                        "The number of locally generated IP datagrams discarded per second because no route could be found to transmit them to their destination [unknown formal SNMP name].",
                    "asmf6/s" =>
                        "The number of failures detected per second by the IPv6 re-assembly algorithm (for whatever reason: timed out, errors, etc.) [ipv6IfStatsReasmFails]. Note that this is not necessarily a count of discarded IPv6 fragments since some algorithms can lose track of the number of fragments by combining them as they are received.",
                    "fragf6/s" =>
                        "The number of IPv6 datagrams that have been discarded per second because they needed to be fragmented at this output interface but could not be [ipv6IfStatsOutFragFails].",
                    "itrpck6/s" =>
                        "The number of input datagrams discarded per second because datagram frame didn't carry enough data [ipv6IfStatsInTruncatedPkts].",
                ),
            ),
            "NFS" => (
                "With the NFS keyword, statistics about NFS client activity are reported. The following values are displayed:",
                Dict(
                    "call/s" => "Number of RPC requests made per second.",
                    "retrans/s" =>
                        "Number of RPC requests per second, those which needed to be retransmitted (for example because of a server timeout).",
                    "read/s" => "Number of 'read' RPC calls made per second.",
                    "write/s" => "Number of 'write' RPC calls made per second.",
                    "access/s" =>     "Number of 'access' RPC calls made per second.",
                    "getatt/s" =>     "Number of 'getattr' RPC calls made per second.",
                ),
            ),
            "NFSD" => (
                "With the NFSD keyword, statistics about NFS server activity are reported. The following values are displayed:",
                Dict(
                    "scall/s" => "Number of RPC requests received per second.",
                    "badcall/s" =>
                        "Number of bad RPC requests received per second, those whose processing generated an error.",
                    "packet/s" =>     "Number of network packets received per second.",
                    "udp/s" => "Number of UDP packets received per second.",
                    "tcp/s" => "Number of TCP packets received per second.",
                    "hit/s" => "Number of reply cache hits per second.",
                    "miss/s" => "Number of reply cache misses per second.",
                    "sread/s" =>     "Number of 'read' RPC calls received per second.",
                    "swrite/s" =>     "Number of 'write' RPC calls received per second.",
                    "saccess/s" =>     "Number of 'access' RPC calls received per second.",
                    "sgetatt/s" =>     "Number of 'getattr' RPC calls received per second.",
                ),
            ),
            "SOCK" => (
                "With the SOCK keyword, statistics on sockets in use are reported (IPv4). The following values are displayed:",
                Dict(
                    "totsck" => "Total number of sockets used by the system.",
                    "tcpsck" => "Number of TCP sockets currently in use.",
                    "udpsck" => "Number of UDP sockets currently in use.",
                    "rawsck" => "Number of RAW sockets currently in use.",
                    "ip-frag" => "Number of IP fragments currently in queue.",
                    "tcp-tw" => "Number of TCP sockets in TIME_WAIT state.",
                ),
            ),
            "SOCK6" => (
                "With the SOCK6 keyword, statistics on sockets in use are reported (IPv6). Note that IPv6 statistics depend on sadc's option \"-S IPV6\" to be collected. The following values are displayed:",
                Dict(
                    "tcp6sck" => "Number of TCPv6 sockets currently in use.",
                    "udp6sck" => "Number of UDPv6 sockets currently in use.",
                    "raw6sck" => "Number of RAWv6 sockets currently in use.",
                    "ip6-frag" => "Number of IPv6 fragments currently in use.",
                ),
            ),
            "SOFT" => (
                "With the SOFT keyword, statistics about software-based network processing are reported. The following values are displayed:",
                Dict(
                    "total/s" =>     "The total number of network frames processed per second.",
                    "dropd/s" =>
                        "The total number of network frames dropped per second because there was no room on the processing queue.",
                    "squeezd/s" =>
                        "The number of times the softirq handler function terminated per second because its budget was consumed or the time limit was reached, but more work could have been done.",
                    "rx_rps/s" =>
                        "The number of times the CPU has been woken up per second to process packets via an inter-processor interrupt.",
                    "flw_lim/s" =>
                        "The number of times the flow limit has been reached per second. Flow limiting is an optional RPS feature that can be used to limit the number of packets queued to the backlog for each flow to a certain amount. This can help ensure that smaller flows are processed even though much larger flows are pushing packets in.",
                ),
            ),
            "TCP" => (
                "With the TCP keyword, statistics about TCPv4 network traffic are reported. Note that TCPv4 statistics depend on sadc's option \"-S SNMP\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "active/s" =>
                        "The number of times TCP connections have made a direct transition to the SYN-SENT state from the CLOSED state per second [tcpActiveOpens].",
                    "passive/s" =>
                        "The number of times TCP connections have made a direct transition to the SYN-RCVD state from the LISTEN state per second [tcpPassiveOpens].",
                    "iseg/s" =>
                        "The total number of segments received per second, including those received in error [tcpInSegs]. This count includes segments received on## currently established connections.",
                ),
            ),
            "oseg/s" =>
                "The total number of segments sent per second, including those on current connections but excluding those containing only retransmitted octets [tcpOutSegs].",
            "ETCP" => (
                "With the ETCP keyword, statistics about TCPv4 network errors are reported. Note that TCPv4 statistics depend on sadc's option \"-S SNMP\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "atmptf/s" =>
                        "The number of times per second TCP connections have made a direct transition to the CLOSED state from either the SYN-SENT state or the SYN-RCVD state, plus the number of times per second TCP connections have made a direct transition to the LISTEN state from the SYN-RCVD state [tcpAttemptFails].",
                    "estres/s" =>
                        "The number of times per second TCP connections have made a direct transition to the CLOSED state from either the ESTABLISHED state or the CLOSE-WAIT state [tcpEstabResets].",
                    "retrans/s" =>
                        "The total number of segments retransmitted per second - that is, the number of TCP segments transmitted containing one or more previously transmitted octets [tcpRetransSegs].",
                    "isegerr/s" =>
                        "The total number of segments received in error (e.g., bad TCP checksums) per second [tcpInErrs].",
                    "orsts/s" =>
                        "The number of TCP segments sent per second containing the RST flag [tcpOutRsts].",
                ),
            ),
            "UDP" => (
                "With the UDP keyword, statistics about UDPv4 network traffic are reported. Note that UDPv4 statistics depend on sadc's option \"-S SNMP\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "idgm/s" =>
                        "The total number of UDP datagrams delivered per second to UDP users [udpInDatagrams].",
                    "odgm/s" =>
                        "The total number of UDP datagrams sent per second from this entity [udpOutDatagrams].",
                    "noport/s" =>
                        "The total number of received UDP datagrams per second for which there was no application at the destination port [udpNoPorts].",
                    "idgmerr/s" =>
                        "The number of received UDP datagrams per second that could not be delivered for reasons other than the lack of an application at the destination port [udpInErrors].",
                ),
            ),
            "UDP6" => (
                "With the UDP6 keyword, statistics about UDPv6 network traffic are reported. Note that UDPv6 statistics depend on sadc's option \"-S IPV6\" to be collected. The following values are displayed (formal SNMP names between square brackets):",
                Dict(
                    "idgm6/s" =>
                        "The total number of UDP datagrams delivered per second to UDP users [udpInDatagrams].",
                    "odgm6/s" =>
                        "The total number of UDP datagrams sent per second from this entity [udpOutDatagrams].",
                    "noport6/s" =>
                        "The total number of received UDP datagrams per second for which there was no application at the destination port [udpNoPorts].",
                    "idgmer6/s" =>
                        "The number of received UDP datagrams per second that could not be delivered for reasons other than the lack of an application at the destination port [udpInErrors].",
                ),
            ),## The ALL keyword is equivalent to specifying all the keywords above and therefore all the network activities are reported.

        ),
    ),

    "q" => (
        "-q",
        "Report queue length and load averages. The following values are displayed:",
        Dict(
            "runq-sz" => "Run queue length (number of tasks waiting for run time).",
            "plist-sz" => "Number of tasks in the process list.",
            "ldavg-1" =>
                "System load average for the last minute. The load average is calculated as the average number of runnable or running tasks (R state), and the number of tasks in uninterruptible sleep (D state) over the specified interval.",
            "ldavg-5" => "System load average for the past 5 minutes.",
            "ldavg-15" => "System load average for the past 15 minutes.",
            "blocked" =>     "Number of tasks currently blocked, waiting for I/O to complete.",

        ),
    ),
    "r" => (
        "-r [ ALL ]",
        "Report memory utilization statistics. The ALL keyword indicates that all the memory fields should be displayed. The following values may be displayed:",
        Dict(
            "kbmemfree" => "Amount of free memory available in kilobytes.",
            "kbavail" =>
                "Estimate of how much memory in kilobytes is available for starting new applications, without swapping. The estimate takes into account that the system needs some page cache to function well, and that not all reclaimable slab will be reclaimable, due to items being in use. The impact of those factors will vary from system to system.",
            "kbmemused" =>
                "Amount of used memory in kilobytes (calculated as total installed memory - kbmemfree - kbbuffers - kbcached - kbslab).",

            "%memused" => "Percentage of used memory.",
            "kbbuffers" =>     "Amount of memory used as buffers by the kernel in kilobytes.",
            "kbcached" =>     "Amount of memory used to cache data by the kernel in kilobytes.",
            "kbcommit" =>
                "Amount of memory in kilobytes needed for current workload. This is an estimate of how much RAM/swap is needed to guarantee that there never is out of memory.",

            "%commit" =>
                "Percentage of memory needed for current workload in relation to the total amount of memory (RAM+swap). This number may be greater than 100% because the kernel usually overcommits memory.",
            "kbactive" =>
                "Amount of active memory in kilobytes (memory that has been used more recently and usually not reclaimed unless absolutely necessary).",
            "kbinact" =>
                "Amount of inactive memory in kilobytes (memory which has been less recently used. It is more eligible to be reclaimed for other  purposes).",
            "kbdirty" =>
                "Amount of memory in kilobytes waiting to get written back to the disk.",
            "kbanonpg" =>
                "Amount of non-file backed pages in kilobytes mapped into userspace page tables.",
            "kbslab" =>
                "Amount of memory in kilobytes used by the kernel to cache data structures for its own use.",
            "kbkstack" =>     "Amount of memory in kilobytes used for kernel stack space.",
            "kbpgtbl" =>
                "Amount of memory in kilobytes dedicated to the lowest level of page tables.",
            "kbvmused" =>     "Amount of memory in kilobytes of used virtual address space.",

        ),
    ),
    "S" => (
        "-S",
        "Report swap space utilization statistics. The following values are displayed:",
        Dict(
            "kbswpfree" => "Amount of free swap space in kilobytes.",
            "kbswpused" => "Amount of used swap space in kilobytes.",

            "%swpused" => "Percentage of used swap space.",
            "kbswpcad" =>
                "Amount of cached swap memory in kilobytes. This is memory that once was swapped out, is swapped back in but still also is in the swap area (if memory is needed it doesn't need to be swapped out again because it is already in the swap area. This saves I/O).",

            "%swpcad" =>
                "Percentage of cached swap memory in relation to the amount of used swap space.",

        ),
    ),
    "u" => (
        "-u [ ALL ]",
        "Report CPU utilization. The ALL keyword indicates that all the CPU fields should be displayed. The report may show the following fields:",
        Dict(
            "%system" =>
                    "Percentage of CPU utilization that occurred while executing at the system level (kernel). Note that this field includes time spent servicing hardware and software interrupts.",
            "%user" =>
                    "Percentage of CPU utilization that occurred while executing at the user level (application). Note that this field includes time spent running virtual processors.",
            "%nice" =>
                    "Percentage of CPU utilization that occurred while executing at the user level with nice priority.",
            "%idle" =>
                    "Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.",
            "%steal" =>
                    "Percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor.",
            "%iowait" =>
                "Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.",

            "ALL" => (
                "",
                Dict(
                    "%usr" =>
                        "Percentage of CPU utilization that occurred while executing at the user level (application). Note that this field does NOT include time spent running virtual processors.",
                    "%sys" =>
                        "Percentage of CPU utilization that occurred while executing at the system level (kernel). Note that this field does NOT include time spent servicing hardware and software interrupts.",
                    "%irq" =>
                        "Percentage of time spent by the CPU or CPUs to service hardware interrupts.",
                    "%soft" =>
                        "Percentage of time spent by the CPU or CPUs to service software interrupts.",
                    "%guest" =>
                        "Percentage of time spent by the CPU or CPUs to run a virtual processor.",
                    "%gnice" =>
                        "Percentage of time spent by the CPU or CPUs to run a niced guest.",

                ),
            ),
        ),
    ),
    "v" => (
        "-v",
        "Report status of inode, file and other kernel tables. The following values are displayed:",
        Dict(
            "dentunusd" => "Number of unused cache entries in the directory cache.",
            "file-nr" => "Number of file handles used by the system.",
            "inode-nr" => "Number of inode handlers used by the system.",
            "pty-nr" => "Number of pseudo-terminals used by the system.",

        ),
    ),
    "W" => (
        "-W",
        "Report swapping statistics. The following values are displayed:",
        Dict(
            "pswpin/s" =>     "Total number of swap pages the system brought in per second.",
            "pswpout/s" =>     "Total number of swap pages the system brought out per second.",

        ),
    ),
    "w" => (
        "-w",
        "Report task creation and system switching activity.",
        Dict(
            "proc/s" => "Total number of tasks created per second.",
            "cswch/s" => "Total number of context switches per second.",

        ),
    ),
    "y" => (
        "-y",
        "Report TTY devices activity. The following values are displayed:",
        Dict(
            "rcvin/s" =>
                "Number of receive interrupts per second for current serial line. Serial line number is given in the TTY column.",
            "xmtin/s" =>
                "Number of transmit interrupts per second for current serial line.",
            "framerr/s" =>     "Number of frame errors per second for current serial line.",
            "prtyerr/s" =>     "Number of parity errors per second for current serial line.",
            "brk/s" => "Number of breaks per second for current serial line.",
            "ovrun/s" =>     "Number of overrun errors per second for current serial line.",
        ),
    ),
)

# Displays the SAR_DB structure
function iterateInfo()
    for (letter, (command, description, content)) in SAR_DB
        println("$command :: $description")

        for (k, v) in content
            if typeof(v) <: String
                println("  $k :: $v")
            else
                println("  $k :: $(v[1])")
                for (l, u) in v[2]
                    println("$l :: $u")
                end
            end
        end
    end
end

function getCommand(attr::Sysstat)
    for (letter, (command, description, content)) in SAR_DB
        for (k, v) in content
            if typeof(v) <: String
                if k == attr.property
                    return letter, ""
                end
            else
                for (l, u) in v[2]
                    if l == attr.property
                        return letter, k
                    end
                end
            end
        end
    end
    logger(attr, "Couldn't find command", true)
    return nothing, nothing
end

max_stars_repo_name: Agapanthus/server-diary

max_issues_repo_name: Agapanthus/server-diary

max_forks_repo_name: Agapanthus/server-diary


text:
# Generate EinCode From a lattice.
using OMEinsum, LightGraphs
export eincode
function eincode(graph::Graph)
    tensor_dim = [length(neighbors(graph, i)) for i in LightGraphs.vertices(graph)]
    code = [zeros(Int, tensor_dim[i]) for i = 1:length(tensor_dim)]
    t = ones(Int, length(tensor_dim))
    edges = [i for i in LightGraphs.edges(graph)]

    for i = 1:ne(graph)
        s, d = src(edges[i]), dst(edges[i])
        code[s][t[s]] = i
        code[d][t[d]] = i
        t[src(edges[i])] += 1
        t[dst(edges[i])] += 1
    end
    return EinCode(([Tuple(i) for i in code]...,), ())
end

ext: jl

max_stars_repo_name: qiyang-ustc/FTensorKit

max_issues_repo_name: qiyang-ustc/FTensorKit

max_forks_repo_name: qiyang-ustc/FTensorKit


text:
# Based on https://arxiv.org/abs/1409.0473

using Flux: combine, flip, crossentropy, reset!, throttle

include("0-data.jl")

Nin = length(alphabet)
Nh = 30 # size of hidden layer

# A recurrent model which takes a token and returns a context-dependent
# annotation.

forward  = LSTM(Nin, Nh÷2)
backward = LSTM(Nin, Nh÷2)
encode(tokens) = vcat.(forward.(tokens), flip(backward, tokens))

alignnet = Dense(2Nh, 1)
align(s, t) = alignnet(combine(t, s))

# A recurrent model which takes a sequence of annotations, attends, and returns
# a predicted output token.

recur   = LSTM(Nh+length(phones), Nh)
toalpha = Dense(Nh, length(phones))

function asoftmax(xs)
  xs = [exp.(x) for x in xs]
  s = sum(xs)
  return [x ./ s for x in xs]
end

function decode1(tokens, phone)
  weights = asoftmax([align(recur.state[2], t) for t in tokens])
  context = sum(map((a, b) -> a .* b, weights, tokens))
  y = recur(vcat(phone, context))
  return softmax(toalpha(y))
end

decode(tokens, phones) = [decode1(tokens, phone) for phone in phones]

# The full model

state = (forward, backward, alignnet, recur, toalpha)

function model(x, y)
  ŷ = decode(encode(x), y)
  reset!(state)
  return ŷ
end

loss(x, yo, y) = sum(crossentropy.(model(x, yo), y))

evalcb = () -> @show loss(data[500]...)
opt = ADAM(params(state))

Flux.train!(loss, data, opt, cb = throttle(evalcb, 10))

# Prediction

using StatsBase: wsample

function predict(s)
  ts = encode(tokenise(s, alphabet))
  ps = Any[:start]
  for i = 1:50
    dist = decode1(ts, onehot(ps[end], phones))
    next = wsample(phones, Flux.Tracker.value(dist))
    next == :end && break
    push!(ps, next)
  end
  return ps[2:end]
end

predict("PHYLOGENY")

max_stars_repo_name: domluna/model-zoo

max_issues_repo_name: domluna/model-zoo

max_forks_repo_name: domluna/model-zoo


text:
VERSION >= v"0.4.0-dev+6521" && __precompile__()

module WORLD

# A light-weight julia wrapper for WORLD.

export
    # Types
    DioOption,
    CheapTrickOption,
    D4COption,

    # WORLD functions
    dio,
    stonemask,
    cheaptrick,
    d4c,
    synthesis,

    # utils
    get_fftsize_for_cheaptrick,

    # matlab functions
    interp1!,
    interp1,

    # conversion
    sp2mc,  # spectrum envelope to mel-cesptrum
    mc2sp   # mel-cepstrum to spectrum envelope

### Binary dependency loading ###
deps = joinpath(Pkg.dir("WORLD"), "deps", "deps.jl")
if isfile(deps)
    include(deps)
else
    error("WORLD not properly installed. Please run Pkg.build(\"WORLD\")")
end

### Check binary dependency compatibility ###
# This should prevent segfaults when you are using WORLD on system paths (e.g.
# /usr/local/lib/libworld.so) and accidentaly change the verion of WORLD.
try
    # function GetWORLDVersion was added in v0.2.1-2
    versionstr = bytestring(ccall((:GetWORLDVersion, libworld), Ptr{Cchar}, ()))
    global const version = convert(VersionNumber, versionstr)
catch e
    try
        ccall((:GetFFTSizeForStar, libworld), Cint, (Cint,), 48000)
        version = v"0.1.4"
    catch e
        version = v"0.2.0"
    end
end

# v0.2.1-4 breaks API of CheapTrick and D4C, and it's hard to support both old
# and new WORLD library, so throws an error when older version of WORLD is
# detected.
if version < v"0.2.1-4"
    error("""Binary dependency incompatibility

          WORLD version 0.2.1-4 or later must be required ($(version) is detected).
          If you have WORLD installed on your system paths, please get the latest
          stable WORLD and install it, and then re-build WORLD.jl with:

          julia>  Pkg.build(\"WORLD\")

          If not, simpily re-building WORLD.jl with the above command. This
          should install the proper version of WORLD.

          If you want to use WORLD.jl with older WORLD, please downgrade WORLD.jl
          for your need. E.g.

          julia> cd(Pkg.dir(\"WORLD\"))
          shell> git checkout v0.1.3 # that supprts v0.2.0 to v0.2.1-3.
         """)
end

for name in ["bridge", "mcep", "deprecated"]
    include(string(name, ".jl"))
end

end # module WORLD


text:
""" Count diagram numbers
see Ref. https://arxiv.org/pdf/cond-mat/0512342.pdf for details
We assume:
1. interaction is spin-symmetric.
2. the propagator conserves the spin.
"""

function count_ver3_g2v(innerLoopNum, spin)
    @assert innerLoopNum >= 0
    if innerLoopNum == 0
        return 1
    elseif innerLoopNum == 1
        return 1
    elseif innerLoopNum == 2
        return 3 * (2 + spin)
    elseif innerLoopNum == 3
        return 5 * (10 + 9 * spin + spin^2)
    else
        error("not implemented!")
    end
end

function count_ver3_G2v(innerLoopNum, spin)
    @assert innerLoopNum >= 0
    if innerLoopNum == 0
        return 1
    elseif innerLoopNum == 1
        return 1
    elseif innerLoopNum == 2
        return 4 + 3 * spin
    elseif innerLoopNum == 3
        return 27 + 31 * spin + 5 * spin^2
    else
        error("not implemented!")
    end
end

function count_ver3_G2W(innerLoopNum, spin)
    @assert innerLoopNum >= 0
    if innerLoopNum == 0
        return 1
    elseif innerLoopNum == 1
        return 1
    elseif innerLoopNum == 2
        return 4 + 2 * spin
    elseif innerLoopNum == 3
        return 27 + 22 * spin
    else
        error("not implemented!")
    end
end

function count_sigma_G2v(innerLoopNum, spin)
    @assert innerLoopNum >= 1
    if innerLoopNum == 1
        return 1
    elseif innerLoopNum == 2
        return 1 + spin
    elseif innerLoopNum == 3
        return 4 + 5 * spin + spin^2
    elseif innerLoopNum == 4
        return 27 + 40 * spin + 14 * spin^2 + spin^3
    else
        error("not implemented!")
    end
end

function count_sigma_G2W(innerLoopNum, spin)
    @assert innerLoopNum >= 1
    return count_ver3_G2W(innerLoopNum, spin)
end

function count_polar_G2v(innerLoopNum, spin)
    @assert innerLoopNum >= 1
    return spin * count_ver3_G2v(innerLoopNum - 1, spin)
end

function count_polar_G2W(innerLoopNum, spin)
    return spin * count_ver3_G2W(innerLoopNum - 1, spin)
end

function count_polar_g2v_noFock_upup(innerLoopNum, spin)
    #polarization for <n↑⋅n↑>
    @assert innerLoopNum >= 1
    @assert spin == 2 "only spin=2 has been implemented!"
    if innerLoopNum == 1
        return 2
    elseif innerLoopNum == 2
        return 2
    elseif innerLoopNum == 3
        return 28
    elseif innerLoopNum == 4
        return 274
    elseif innerLoopNum == 5
        return 3586
    else
        error("not implemented!")
    end
end

function count_polar_g2v_noFock_updown(innerLoopNum, spin)
    #polarization for <n↑⋅n↓>
    @assert innerLoopNum >= 1
    @assert spin == 2 "only spin=2 has been implemented!"
    if innerLoopNum == 1
        return 0
    elseif innerLoopNum == 2
        return 0
    elseif innerLoopNum == 3
        return 4
    elseif innerLoopNum == 4
        return 52
    elseif innerLoopNum == 5
        return 844
    else
        error("not implemented!")
    end
end

function count_polar_g2v_noFock(innerLoopNum, spin)
    return count_polar_g2v_noFock_upup(innerLoopNum, spin) +
           count_polar_g2v_noFock_updown(innerLoopNum, spin)
end


text:
module MLJBase

# ===================================================================
# IMPORTS

import Base: ==, precision, getindex, setindex!
import Base.+, Base.*

# Scitype
using ScientificTypes

# Traits for models and measures (which are being overloaded):
using StatisticalTraits

for trait in StatisticalTraits.TRAITS
    eval(:(import StatisticalTraits.$trait))
end

import Base.instances # considered a trait for measures
import StatisticalTraits.snakecase
import StatisticalTraits.info

# Interface

# HACK: When https://github.com/JuliaAI/MLJModelInterface.jl/issues/124 and
# https://github.com/JuliaAI/MLJModelInterface.jl/issues/131 is resolved:
# Uncomment next line and delete "Hack Block"
# using MLJModelInterface
#####################
# Hack Block begins #
#####################
exported_names(m::Module) =
    filter!(x -> Base.isexported(m, x),
            Base.names(m; all=true, imported=true))
import MLJModelInterface
for name in exported_names(MLJModelInterface)
    name in [
        :UnivariateFinite,
        :augmented_transform,
        :info,
        :scitype # Needed to avoid clashing with `ScientificTypes.scitype`
    ] && continue
    quote
        import MLJModelInterface.$name
    end |> eval
end
###################
# Hack Block ends #
###################

import MLJModelInterface: fit, update, update_data, transform,
    inverse_transform, fitted_params, predict, predict_mode,
    predict_mean, predict_median, predict_joint,
    evaluate, clean!, is_same_except,
    save, restore, is_same_except, istransparent,
    params, training_losses

# Macros
using Parameters

# Containers & data manipulation
using Serialization
using Tables
import PrettyTables
using DelimitedFiles
using OrderedCollections
using CategoricalArrays
import CategoricalArrays.DataAPI.unwrap
import InvertedIndices: Not
import Dates

# Distributed computing
using Distributed
using ComputationalResources
import ComputationalResources: CPU1, CPUProcesses, CPUThreads

using ProgressMeter
import .Threads

# Operations & extensions
import LossFunctions
import StatsBase
import StatsBase: fit!, mode, countmap
import Missings: levels
using Missings
import Distributions
using CategoricalDistributions
import Distributions: pdf, logpdf, sampler
const Dist = Distributions

# from Standard Library:
using Statistics, LinearAlgebra, Random, InteractiveUtils

# ===================================================================
## CONSTANTS

# for variable global constants, see src/init.jl

const PREDICT_OPERATIONS = (:predict,
                            :predict_mean,
                            :predict_mode,
                            :predict_median,
                            :predict_joint)

const OPERATIONS = (PREDICT_OPERATIONS..., :transform, :inverse_transform)

# the directory containing this file: (.../src/)
const MODULE_DIR = dirname(@__FILE__)

# horizontal space for field names in `MLJType` object display:
const COLUMN_WIDTH = 24
# how deep to display fields of `MLJType` objects:
const DEFAULT_SHOW_DEPTH = 0
const DEFAULT_AS_CONSTRUCTED_SHOW_DEPTH = 2
const INDENT = 2

const Arr = AbstractArray
const Vec = AbstractVector

# Note the following are existential (union) types. In particular,
# ArrMissing{Integer} is not the same as Arr{Union{Missing,Integer}},
# etc.
const ArrMissing{T,N} = Arr{<:Union{Missing,T},N}
const VecMissing{T} = ArrMissing{T,1}
const CatArrMissing{T,N} = ArrMissing{CategoricalValue{T},N}

const MMI = MLJModelInterface
const FI  = MLJModelInterface.FullInterface

const MARGIN_LOSSES = [
    :DWDMarginLoss,
    :ExpLoss,
    :L1HingeLoss,
    :L2HingeLoss,
    :L2MarginLoss,
    :LogitMarginLoss,
    :ModifiedHuberLoss,
    :PerceptronLoss,
    :SigmoidLoss,
    :SmoothedL1HingeLoss,
    :ZeroOneLoss
]

const DISTANCE_LOSSES = [
    :HuberLoss,
    :L1EpsilonInsLoss,
    :L2EpsilonInsLoss,
    :LPDistLoss,
    :LogitDistLoss,
    :PeriodicLoss,
    :QuantileLoss
]

const WITH_PARAMETERS = [
    :DWDMarginLoss,
    :SmoothedL1HingeLoss,
    :HuberLoss,
    :L1EpsilonInsLoss,
    :L2EpsilonInsLoss,
    :LPDistLoss,
    :QuantileLoss,
]

const MEASURE_TYPE_ALIASES = [
    :FPR, :FNR, :TPR, :TNR,
    :FDR, :PPV, :NPV, :Recall, :Specificity,
    :MFPR, :MFNR, :MTPR, :MTNR,
    :MFDR, :MPPV, :MNPV, :MulticlassRecall, :MulticlassSpecificity,
    :MCR,
    :MCC,
    :BAC, :BACC,
    :RMS, :RMSPV, :RMSL, :RMSLP, :RMSP,
    :MAV, :MAE, :MAPE,
    :RSQ, :LogCosh,
    :CrossEntropy,
    :AUC
]

const LOSS_FUNCTIONS = vcat(MARGIN_LOSSES, DISTANCE_LOSSES)

# ===================================================================
# Computational Resource
# default_resource allows to switch the mode of parallelization

default_resource()    = DEFAULT_RESOURCE[]
default_resource(res) = (DEFAULT_RESOURCE[] = res;)

# ===================================================================
# Includes

include("init.jl")
include("utilities.jl")
include("show.jl")
include("interface/data_utils.jl")
include("interface/model_api.jl")

include("sources.jl")
include("machines.jl")

include("composition/abstract_types.jl")
include("composition/learning_networks/nodes.jl")
include("composition/learning_networks/inspection.jl")
include("composition/learning_networks/machines.jl")

include("composition/models/methods.jl")
include("composition/models/from_network.jl")
include("composition/models/inspection.jl")
include("composition/models/deprecated.jl")
include("composition/models/pipelines.jl")
include("composition/models/transformed_target_model.jl")

include("operations.jl")
include("resampling.jl")

include("hyperparam/one_dimensional_ranges.jl")
include("hyperparam/one_dimensional_range_methods.jl")

include("data/data.jl")
include("data/datasets.jl")
include("data/datasets_synthetic.jl")

include("measures/measures.jl")
include("measures/measure_search.jl")
include("measures/doc_strings.jl")

include("composition/models/stacking.jl")

# function on the right-hand side is defined in src/measures/meta_utilities.jl:
const MEASURE_TYPES_ALIASES_AND_INSTANCES = measures_for_export()

# ===================================================================
## EXPORTS

# -------------------------------------------------------------------
# re-exports from MLJModelInterface, ScientificTypes
# NOTE: MLJBase does **not** re-export UnivariateFinite to avoid
# ambiguities between the raw constructor (MLJBase.UnivariateFinite)
# and the general method (MLJModelInterface.UnivariateFinite)

# traits for measures and models:
using StatisticalTraits

for trait in StatisticalTraits.TRAITS
    eval(:(export $trait))
end

export implemented_methods # defined here and not in StatisticalTraits

export UnivariateFinite

# MLJType equality
export is_same_except

# model constructor + metadata
export @mlj_model, metadata_pkg, metadata_model

# model api
export fit, update, update_data, transform, inverse_transform,
    fitted_params, predict, predict_mode, predict_mean,
    predict_median, predict_joint,
    evaluate, clean!, training_losses

# data operations
export matrix, int, classes, decoder, table,
    nrows, selectrows, selectcols, select

# re-export from ComputationalResources.jl:
export CPU1, CPUProcesses, CPUThreads

# re-exports from ScientificTypes
export Unknown, Known, Finite, Infinite,
    OrderedFactor, Multiclass, Count, Continuous, Textual,
    Binary, ColorImage, GrayImage, Image, Table

export scitype, scitype_union, elscitype, nonmissing
export coerce, coerce!, autotype, schema, info

# re-exports from CategoricalDistributions:
export UnivariateFiniteArray, UnivariateFiniteVector

# -------------------------------------------------------------------
# exports from this module, MLJBase

# computational_resources.jl:
export default_resource

# one_dimensional_ranges.jl:
export ParamRange, NumericRange, NominalRange, iterator, scale

# parameter_inspection.jl:
export params # note this is *not* an extension of StatsBase.params

# data.jl:
export partition, unpack, complement, restrict, corestrict

# utilities.jl:
export flat_values, recursive_setproperty!,
    recursive_getproperty, pretty, unwind

# show.jl
export HANDLE_GIVEN_ID, @more, @constant, color_on, color_off

# datasets.jl:
export load_boston, load_ames, load_iris, load_sunspots,
    load_reduced_ames, load_crabs, load_smarket,
    @load_boston, @load_ames, @load_iris, @load_sunspots,
    @load_reduced_ames, @load_crabs, @load_smarket

# sources.jl:
export source, Source, CallableReturning

# machines.jl:
export machine, Machine, fit!, report, fit_only!, default_scitype_check_level

# datasets_synthetics.jl
export make_blobs, make_moons, make_circles, make_regression

# composition (surrogates and composites are exported in composition):
export machines, sources, @from_network, @pipeline, Stack,
    glb, @tuple, node, @node, sources, origins, return!,
    nrows_at_source, machine, rebind!, nodes, freeze!, thaw!,
    Node, AbstractNode, Pipeline,
    ProbabilisticPipeline, DeterministicPipeline, UnsupervisedPipeline,
    StaticPipeline, IntervalPipeline

export TransformedTargetModel

# aliases to the above,  kept for backwards compatibility:
export  DeterministicNetwork, ProbabilisticNetwork, UnsupervisedNetwork

# resampling.jl:
export ResamplingStrategy, Holdout, CV, StratifiedCV, TimeSeriesCV,
    evaluate!, Resampler, PerformanceEvaluation

# `MLJType` and the abstract `Model` subtypes are exported from within
# src/composition/abstract_types.jl

# -------------------------------------------------------------------
# exports from MLJBase specific to measures

# measure names:
for m in MEASURE_TYPES_ALIASES_AND_INSTANCES
    :(export $m) |> eval
end

# measures/registry.jl:
export measures, metadata_measure

# measure/measures.jl (excluding traits):
export aggregate, default_measure, value, skipinvalid

# measures/probabilistic:
export roc_curve, roc

# measures/finite.jl (averaging modes for multiclass scores)
export no_avg, macro_avg, micro_avg

# -------------------------------------------------------------------
# re-export from Random, StatsBase, Statistics, Distributions,
# OrderedCollections, CategoricalArrays, InvertedIndices:
export pdf, sampler, mode, median, mean, shuffle!, categorical, shuffle,
   levels, levels!, std, Not, support, logpdf, LittleDict

end # module


text:
@kwdef mutable struct TransposeHandler <: AbstractHandler
    id = 0
end

@register(TransposeHandler, permutedims, begin
    [Vector{SBit}] => [Matrix{SBit}]
    [Vector{DBit}] => [Matrix{DBit}]
    [Matrix{SBit}] => [Matrix{SBit}]
    [Matrix{DBit}] => [Matrix{DBit}]
end)

function (handler::TransposeHandler)(netlist::Netlist,
                                     inputs::Vector{Variable},
                                     outputs::Vector{Variable})
    # compute output size
    outsize = getsize(netlist, getname(outputs[1]))

    # add output net to netlist
    setsigned!(netlist, getname(outputs[1]), true)
    setreg!(netlist, getname(outputs[1]))

    outstring = """
        $stdcomment
        // BEGIN transpose$(handler.id)
        $(handler.id > 0 ? "" : "integer i, j;")
        always @(*) begin
            for (i = 0; i < $(outsize[2]); i = i + 1) begin
                for (j = 0; j < $(outsize[1]); j = j + 1) begin
                    $(outputs[1].name)_p[(j*$(outsize[2])) + i] <= $(inputs[1].name)_p[(i*$(outsize[1])) + j];
                    $(outputs[1].name)_m[(j*$(outsize[2])) + i] <= $(inputs[1].name)_m[(i*$(outsize[1])) + j];
                end
            end
        end
        // END transpose$(handler.id)
        \n"""

    handler.id += 1

    return outstring
end


text:
module PcapTools

using Dates
using Mmap
using UnixTimes
using UnsafeArrays

export PcapHeader, RecordHeader
export PcapRecord
export PcapReader, PcapStreamReader, PcapBufferReader
export PcapWriter, PcapStreamWriter
export LINKTYPE_NULL, LINKTYPE_ETHERNET
export splitcap

abstract type PcapReader end
abstract type PcapWriter end

include("pcap_header.jl")
include("record_header.jl")
include("record.jl")
include("buffer_reader.jl")
include("stream_reader.jl")
include("stream_writer.jl")
include("splitcap.jl")

end


text:
using ImageMethodReverb
using Test
using LinearAlgebra
using DelimitedFiles, Random

@testset "ImageMethodReverb" begin
  @testset "Image source method" begin
    include("test_ism.jl")
  end
  @testset "equivalence with MATLAB" begin
    include("test_julia_vs_matlab.jl")
  end
end

max_stars_repo_name: nantonel/JuRIM

max_issues_repo_name: nantonel/JuRIM

max_forks_repo_name: nantonel/JuRIM


text:
function sim_data(name;theta_dist="uniform", Vcirc = x-> 100.0*sqrt(x),I= x->70.0,PA=x->33.0, Sb = x->100.0, sigma_inst=x->10.0, cloud_mass=1e5 ,xl=2.0,yl=5.0,vl=350.0,dx=0.05,dy=0.05,dv=20.0,beam=[0.08,0.07,32.0],Rmin=0.2,dR=0.1,Rmax = 4.5,a=1000.0,rms=0.05,Rcmax=2.1,Rcmap=1.7,k=3,plots=false)

    XX=[]
    YY=[]
    ZZ=[]
    VV=[]
    RR=[]
    Rc =[]
    M=[]
    sigma=[]

    for Ri in Rmin:dR:Rmax
        Nclouds = Int(round( 1.0e6*pi*((Ri+dR)^2.0 - (Ri-dR)^2.0) * Sb(Ri)/cloud_mass ))
        R=sqrt.(rand(Uniform((Ri-dR)^2.0,(Ri+dR)^2.0),Nclouds))

        if theta_dist == "uniform"
            theta=rand(Uniform(0.0,2.0*pi),Nclouds)
        else
            R0=0.1
            theta0=deg2rad(50.0)
            pitch=deg2rad(50.0)
            theta=log.(R ./R0) ./tan.(pitch) .+theta0
            append!(theta,log.(R ./ R0) ./tan.(pitch) .+(theta0+pi))
            append!(R,R)
        end

        Rci=10.0 .^rand(TriangularDist(1.0,Rcmax,Rcmap),Nclouds)
        Mi = Sb(Ri) .*Rci .^2
        sigmai = sqrt.( (0.7 .*sqrt.(Rci)).^2.0 .+(sigma_inst.(Ri)).^2.0)
        i=deg2rad(I(Ri))
        phi0=deg2rad(PA(Ri) .+90.0)
        phi=atan.(cos(i) *tan.(theta)) .+ phi0
        r = R.*cos.(theta) ./cos.(phi .-phi0  )
        Xc=r .* cos.(phi)
        Yc=r .* sin.(phi)
        Zc = -tan.(i) .* (Xc .*cos.(deg2rad(PA(Ri))) .+Yc .*sin.(deg2rad(PA(Ri))))
        VLOS = Vcirc.(R) .* sin(i) .* cos.(theta)
        push!(XX,Xc)
        push!(YY,Yc)
        push!(ZZ,Zc)
        push!(VV,VLOS)
        push!(RR,R)
        push!(Rc,Rci)
        push!(M,Mi)
        push!(sigma,sigmai)
        # if plots
        #     Rd_sky=Ri ./(1.0 .+tan(deg2rad(I(Ri)))^2.0 .* cos.(phis .-deg2rad(PA(Ri))).^2.0).^0.5
        #     xd=Rd_sky .*cos.(phis)
        #     yd=Rd_sky .*sin.(phis)
        #     lines!(axm0,xd,yd,ratio=1,label=false,transparency = true,color=RGBA(0.0,0.0,0.0,0.1))
        # end
    end
    Xc=collect(Iterators.flatten(XX))
    Yc=collect(Iterators.flatten(YY))
    Zc=collect(Iterators.flatten(ZZ))
    Vc=collect(Iterators.flatten(VV))
    Rc=collect(Iterators.flatten(Rc))
    RR=collect(Iterators.flatten(RR))
    M=collect(Iterators.flatten(M))
    sigma=collect(Iterators.flatten(sigma))

    # if plots

    # end
    Y = collect(-yl:dx:yl)
    X = collect(-xl:dy:xl)
    V = collect(-vl:dv:vl)

    s=zeros(length(X),length(Y),length(V));
    Fc=[]
    for (r,xc,yc,vc,sx,sy,sv) in zip(RR,Xc,Yc,Vc,Rc ./1000,Rc ./1000,sigma)
        F=a *Sb(r)/sv
        push!(Fc,F)
        ix=argmin(abs.(xc.-X))
        iy=argmin(abs.(yc.-Y))
        iv=argmin(abs.(vc.-V))

        dix=max(Int(round(sx/dx)),1)
        diy=max(Int(round(sy/dx)),1)
        div=max(Int(round(sv/dv)),1)

        xr=ix-k*dix:ix+k*dix
        yr=iy-k*diy:iy+k*diy
        vr=iv-k*div:iv+k*div
        if (minimum(xr)>0) & (minimum(yr)>0) & (minimum(vr)>0) & (maximum(xr)<size(s)[1]) & (maximum(yr)<size(s)[2]) & (maximum(vr)<size(s)[3])
            s[xr,yr,vr]=s[xr,yr,vr].+ [F *exp(-0.5*( ((x-ix)/dix)^2.0 + ((y-iy)/diy)^2.0 +((v-iv)/div)^2.0)) for x in xr, y in yr, v in vr]
        end
    end
    s=rand.(Normal.(s,rms))
    bx = Int(round(beam[1]/dx))
    by = Int(round(beam[2]/dy))
    for i in 1:size(s)[3]
        imfilter!(s[:,:,i],s[:,:,i],Kernel.gaussian([bx,by]))
    end

    if plots
        figd=Figure(resolution = (1000,1500))
        axRc= Axis(figd[1,1],Aspect=:equal)
        axM= Axis(figd[1,2],Aspect=:equal)
        axS= Axis(figd[1,3],Aspect=:equal)

        scatter!(axRc,RR,Rc,markersize=1,trasnparency=true)
        #scatter!(axM,Xc,Yc,markersize=1,trasnparency=true)
        scatter!(axM,RR,log10.(M),markersize=1,trasnparency=true)
        scatter!(axS,RR,sigma,markersize=1,trasnparency=true)
        #scatter!(axxy,Xc,Yc,color=Vc,colorrange=[-300.0,300.0],colormap=:seismic)
        #display(fig)
        save("$(name)_data.png",figd)
        fig=Figure(resolution = (2200,1500))
        #axm0v = Axis(fig[1,1],Aspect=:equal)
        axm0 = Axis(fig[1,1],Aspect=:equal)
        axm1 = Axis(fig[1,2],Aspect=:equal)
        axm2 = Axis(fig[1,3],Aspect=:equal)
        #ax3d = Axis(fig[1,4],Aspect=:equal)
        axpvd1=Axis(fig[2,1],Aspect=:equal)
        axpvd2=Axis(fig[2,2],Aspect=:equal)
        phis=0.0:0.01:2*pi

        mom0=sum(s,dims=3)[:,:,1]
        mom1=[sum(V .* s[ix,iy,:]) for ix in 1:size(s,1), iy in 1:size(s,2)] ./mom0
        mom2=sqrt.(abs.([sum((V .- mom1[ix,iy]) .^2.0 .* s[ix,iy,:]) for ix in 1:size(s,1), iy in 1:size(s,2)] ./mom0))
        #heatmap!(axm0v,Y,V,sum(s,dims=2)[:,1,:])
        hm=heatmap!(axm0,X,Y,mom0)
        cb=Colorbar(fig[1, 1][1,2], hm)
        hm=heatmap!(axm1,X,Y,mom1,colorrange=[-300.0,300.0],colormap=:seismic)
        cb=Colorbar(fig[1, 2][1,2], hm)
        hm=heatmap!(axm2,X,Y,mom2,colorrange=[0.0,60.0])
        cb=Colorbar(fig[1, 3][1,2], hm)

        #scatter!(ax3d,Xc,Yc,Zc)

        pa=0.0
        θ=deg2rad(pa)
        k=3
        A=zeros(size(s))
        for i in 2:size(s)[3]
            A[:,:,i]=collect(imrotate(s[:,:,i], θ,size(s[:,:,1]),fill=0.0))[:,:,1]
        end
        #A[isnan.(A)] .=0.0
        pvA= sum(A[Int(floor(size(A)[1]/2))-k:Int(floor(size(A)[1]/2))+k,:,:], dims=1)
        contourf!(axpvd1,Y,V,pvA[1,:,:])

        pa=90.0
        θ=deg2rad(pa)
        k=3
        A=zeros(size(s))
        for i in 2:size(s)[3]
            A[:,:,i]=collect(imrotate(s[:,:,i], θ,size(s[:,:,1]),fill=0.0))[:,:,1]
        end
        #A[isnan.(A)] .=0.0
        pvA= sum(A[Int(floor(size(A)[1]/2))-k:Int(floor(size(A)[1]/2))+k,:,:], dims=1)
        contourf!(axpvd2,Y,V,pvA[1,:,:])
        save("$(name)_moments_pvd.png",fig)

        ff,ss,aa=scatter(Xc,Yc,Zc,axis = (; type = Axis3, protrusions = (0, 0, 0, 0),
        viewmode = :fit, limits = (-5, 5, -5, 5, -5, 5)))
        save("$(name)_3d.png",ff)
        #display(ff)
    end

    return Dict("name"=>name,"data"=>Float64.(s),"X"=>X,"Y"=>Y,"V"=>V,"dx"=>dx,"dv"=>dv,"Xc"=>Xc,"Yc"=>Yc,"Zc"=>Zc,"Vc"=>Vc,"Fc"=>Fc,"Sc"=>sigma,"rms"=>rms,"beam"=>beam)
end;


text:
for N in [Float64, Rational{Int}, Float32]
    # random line
    rand(Line2D)

    # construction
    a1 = N[0, 1]
    b1 = N(1)
    l1 = Line2D(a1, b1)
    l2 = Line2D(N[1, 0], N(1))
    l3 = Line2D(N[0, 1], N(2))
    l4 = Line2D(N[1, 1], N(0))
    l1_copy = Line2D(N[0, 1], N(1))

    # alternative construction from two points
    # line with positive slope
    L = Line2D([1.0, 0.0], [2.0, 0.5])
    @test [1.0, 0.0] ∈ L &&
          [2.0, 0.5] ∈ L &&
          [2.0, 1.5] ∉ L

    # vertical line
    L = Line2D([1.0, 0.0], [1.0, 2.5])
    @test [1.0, 0.0] ∈ L &&
          [1.0, 2.5] ∈ L &&
          [1.0, 10.0] ∈ L
          [10.0, 10.0] ∉ L

    # corner case: zero normal vector
    @test_throws AssertionError Line2D(N[0, 0], N(1))

    # dimension
    @test dim(l1) == 2

    # support vector
    σ(N[0, 1], l1)
    σ(N[1, 0], l2)
    σ(N[0, 1], l3)

    # boundedness
    @test !isbounded(l1)

    # is_polyhedral
    @test is_polyhedral(l1)

    # universality
    @test !isuniversal(l1)
    res, w = isuniversal(l1, true)
    @test !res && w ∉ l1

    # isempty
    @test !isempty(l1)

    # an_element and membership
    an_element(l1) ∈ l1
    an_element(l2) ∈ l2
    an_element(l3) ∈ l3
    an_element(l4) ∈ l4

    # constrained dimensions
    @test constrained_dimensions(l1) == [2]
    @test constrained_dimensions(l4) == [1, 2]

    # constraints_list
    @test ispermutation(constraints_list(l1),
                        [HalfSpace(a1, b1), HalfSpace(-a1, -b1)])

    # concrete intersection
    cap11 = intersection(l1, l1_copy)
    cap12 = intersection(l1, l2)
    cap13 = intersection(l1, l3)
    @test cap11 isa Line2D && cap11.a == l1.a && cap11.b == l1.b
    @test cap12 isa Singleton && element(cap12) == N[1, 1]
    @test cap13 isa EmptySet{N}

    # concrete linear map of a line
    L = Line2D(N[1, -1], N(0)) # x = y
    M = N[1 0; 0 0] # non-invertible matrix
    # projection is y = 0
    if test_suite_polyhedra
        lm = linear_map(M, L)
        if N == Float32 || N == Float64
            @test lm isa Line2D{Float64}
            @test lm.a ≈ N[0, -1] && lm.b ≈ N(0)

            # returned set is universal
            @test linear_map(N[1 1], L) == Universe{N}(1)
        elseif N == Rational{Int}
            @test lm isa Line2D{Rational{BigInt}}
            @test lm.a == N[0//1, -1//1] && lm.b == N(0//1)
        end
    end
    M = N[2 2; 0 1] # invertible matrix
    lm = linear_map(M, L)
    @test lm isa Line2D{N, Vector{N}}
    @test lm.a ≈ N[1/2, -2] && lm.b ≈ N(0)

    # projection
    L = Line2D(N[1, -1], N(0))  # x = y
    @test project(L, [1]) == project(L, [2]) == Universe{N}(1)
    @test project(L, [1, 2]) == L
    L = Line2D(N[2, 0], N(4))  # x = 2
    @test project(L, [1]) == Singleton(N[2])
    @test project(L, [2]) == Universe{N}(1)
    @test project(L, [1, 2]) == L

    # translation
    @test translate(l1, N[1, 2]) == Line2D(a1, N(3))

    # projecting a point onto a line
    L = Line2D(N[1, -1], N(0))  # x = y
    @test project(N[1, 0], L) ≈ N[1//2, 1//2]
end

# tests that only work with Float64 and Float32
for N in [Float64, Float32]
    # sampling
    L = Line2D(N[1, -1], N(0))  # x = y
    for x in sample(L, 10)
        # approximate membership test due to floating-point errors
        @test isapproxzero(dot(x, L.a) - L.b)
    end
end


text:
# It would be cool if this were an actual REPL with completion etc
#Right now, we have some emacs keybindings for moving forward and back, (ctrl-f, ctrl-b, ctrl-a, ctrl-e)
# as well as ctrl-k, ctrl-j to get previous/next commands from command history

function evalcmd(b::Buffer, s::String)
    try
        eval(Base.parse_input_line(s))
    catch
        "INVALID COMMAND"
    end
end
function evalcmd(b::Buffer)
    try
        escape(b)
        if b.state[:command][1] == '?'
            c = eval(Base.parse_input_line("@doc "*b.state[:command][2:end]))
        else
            c = eval(Base.parse_input_line(b.state[:command]))
        end
        if b.state[:command][end] != ';'
            b.state[:log] = "$c"
        end
        b.state[:command_history] *= b.state[:command]*"\n"
        b.state[:command] = ""
        command_n = parse(b.state[:command_n])
        b.state[:command_n] = "$(command_n+1)"
        b.state[:command_ind] = "$(b.state[:command_n])"
    catch y
        b.state[:log] = "INVALID COMMAND: $y"
        setmode(b, command_mode)
    end
end

command_fn(c) = exec_fn(c, :command, evalcmd)
command_mode = Mode("command", Action(command_fn, x->true))


text:
import JUDI: judipmap
export surface_gather, offset_map

"""
    surface_gather(model, q, data; offsets=nothing, options=Options())

Compute the surface offset gathers volume (nx (X ny) X nz X no) for via the double rtm method whith `no` offsets.

Parameters
* `model`: JUDI Model structure.
* `q`: Source, judiVector.
* `data`: Obeserved data, judiVector.
* `offsets`: List of offsets to compute the gather at. Optional (defaults to 0:10*model.d:model.extent)
* `options`: JUDI Options structure.
"""
function surface_gather(model::Model, q::judiVector, data::judiVector; offsets=nothing, options=Options())
    isnothing(offsets) && (offsets = 0f0:10*model.d[1]:(model.n[1]-1)*model.d[1])
    offsets = collect(offsets)
    results = judipmap(i-> double_rtm_cig(model, subsample(q, i), subsample(data, i), offsets, subsample(options, i)), 1:q.nsrc)
    return sum(results)
end

"""
    double_rtm_cig(model, q, data, offsets, options)

Compute the single shot contribution to the surface offset gather via double rtm. This single source contribution consists of the following steps:

1. Mute direct arrival in the data.
2. Compute standard RTM ``R``.
3. Compute the offset RTM ``R_o`` for the the weighted data where each trace is weighted by its offset `(rec_x - src_x)`.
4. Compute the envelope ``R_e = \\mathcal{E}(R)`` and ``R_{oe} = \\mathcal{E}(R_o)``.
5. Compute the offset map ``\\frac{R_e \\odot R_{oe}}{R_e \\odot R_e + \\epsilon}``.
6. Apply illumination correction and laplace filter ``R_l = \\mathcal{D} \\Delta R``.
7. Compute each offset contribution ``\\mathcal{I}[:, h] = R_l \\odot \\delta[ha - h]_{tol}`` [`delta_h`](@ref).
8. Return ``\\mathcal{I}``.

"""
function double_rtm_cig(model_full, q::judiVector, data::judiVector, offs, options)
    # Load full geometry for out-of-core geometry containers
    data.geometry = Geometry(data.geometry)
    q.geometry = Geometry(q.geometry)
    ndim = length(model_full.n)

    # Limit model to area with sources/receivers
    if options.limit_m == true
        model = deepcopy(model_full)
        model, _ = limit_model_to_receiver_area(q.geometry, data.geometry, model, options.buffer_size)
    else
        model = model_full
    end

    # Set up Python model
    modelPy = devito_model(model, options)
    dtComp = get_dt(model; dt=options.dt_comp)

    # Extrapolate input data to computational grid
    qIn = time_resample(q.data[1], q.geometry, dtComp)[1]
    obsd = typeof(data.data[1]) == JUDI.SegyIO.SeisCon ? convert(Array{Float32,2}, data.data[1][1].data) : data.data[1]
    res = time_resample(obsd, data.geometry, dtComp)[1]

    # Set up coordinates
    src_coords = setup_grid(q.geometry, model.n)  # shifts source coordinates by origin
    rec_coords = setup_grid(data.geometry, model.n)    # shifts rec coordinates by origin

    # Src-rec offsets
    scale = 5f3
    off_r = abs.(data.geometry.xloc[1] .- q.geometry.xloc[1]) .+ scale

    # mute
    mute!(res, off_r .- scale; dt=dtComp/1f3, t0=.25)
    res_o = res .* off_r'
    # Double rtm
    rtm, rtmo, illum = pycall(dbr."double_rtm", Tuple{Array{Float32, modelPy.dim},  Array{Float32, modelPy.dim}, Array{Float32, modelPy.dim}},
                              modelPy, qIn, src_coords, res, res_o, rec_coords, space_order=options.space_order)
    rtm = remove_padding(rtm, modelPy.padsizes)
    rtmo = remove_padding(rtmo, modelPy.padsizes)
    illum = remove_padding(illum, modelPy.padsizes)

    # offset map
    h_map = offset_map(rtm, rtmo; scale=scale)

    rtm = laplacian(rtm)
    rtm[illum .> 0] ./= illum[illum .> 0]

    soffs = zeros(Float32, model.n..., length(offs))

    for (i, h) in enumerate(offs)
        soffs[:, :, i] .+= rtm .* delta_h(h_map, h, 2*diff(offs)[1])
    end
    GC.gc()
    return soffs
end


text:
module MarkovChains_monotonicity
# Module for checking monotonicities in Markov chains.
# 2017: Pawel Lorek, Piotr Markowski

#
# All functions require order matrix C and transition matrix P
# Consult Lorek, P., Markowski, P. "Monotonicity requirements for efficient exact sampling with Markov chains"
# for details concerning monotonicities.
#
# Tested in Julia 0.5.0

export

  is_mobius_down,
  is_mobius_up,
  is_weak_down,
  is_weak_up,
  is_stochastic,
  which_is_not_real

function rationalize_matrix(P::Array{Float64,2})
    s=size(P)[1]
    PP=zeros(Rational{Int64},s,s)
    for i=1:s
      for j=1:s
        PP[i,j]=rationalize(P[i,j])
      end
    end
    return PP
end

# multiplying elements of matrix by lcm of their denominators
function convert_to_min_Int(P::Array{Rational{Int64},2})
   return convert(Array{Int64,2},lcm(map(den,P))*P)
end

# convert C into upper_triangular. Adjust P accordingly.
function upper_triangular(C::Array{Int64,2},P::Array{Rational{Int64},2})
  s=size(C)[1]
  c=zeros(Int64,s,2)
  for i=1:s
    c[i,:]=[i,sum(C[i,:])]
  end
  c=sortrows(c,by=x -> x[2],rev=true)
  CC=zeros(Int64,s,s)
  PP=zeros(P)
  for i=1:s
    for j=1:s
      CC[i,j]=C[c[i,1],c[j,1]]
      PP[i,j]=P[c[i,1],c[j,1]]
    end
  end
  return CC,PP
end

# create offspring matrix (consult the article)
function offspring_matrix(C::Array{Int64,2})
  D=deepcopy(C)
  s=size(D)[1]
  PP=zeros(Int64,0,s)
  for i=1:s
    for k=i+1:s
      if D[i,k]==1
        D[i,:]-=D[k,:]
        PP=[PP;sparse([1,1,1],[i,k,s],[1,-1,0])]
      end
    end
  end
  return PP
end

# create all upsets
function up_sets(C::Array{Int64,2})
  s=size(C)[1]
  D=zeros(Int,0,s)
  for i=1:s
    d=deepcopy(C[i,:])'
    for k=(i+1):s if d[1,k]==0
        for j=1:size(d)[1] if d[j,k]==0
            d=[d;d[j,:]'+C[k,:]']
        end end
    end end
    D=[D;d]
  end
  return sign(D)'
end

# check mobius_down monotonicity
function is_mobius_down(C::Array{Int64,2},P::Array{Rational{Int64},2})
  (C,P)=upper_triangular(C,P)
  R=convert_to_min_Int(P)
  return minimum(inv(C)*R*C .>= 0)
end

function is_mobius_down(C::Array{Int64,2},P::Array{Float64,2})
   return is_mobius_down(C,rationalize_matrix(P))
end

# check mobius_up monotonicity
function is_mobius_up(C::Array{Int64,2},P::Array{Rational{Int64},2})
  (C,P)=upper_triangular(C,P)
  R=convert_to_min_Int(P)
  return minimum(inv(C')*R*C' .>= 0)
end

function is_mobius_up(C::Array{Int64,2},P::Array{Float64,2})
   return is_mobius_up(C,rationalize_matrix(P))
end

# check weak_down monotonicity
function is_weak_down(C::Array{Int64,2},P::Array{Rational{Int64},2})
  (C,P)=upper_triangular(C,P)
  R=convert_to_min_Int(P)
  return minimum(offspring_matrix(C)*R*C .>= 0)
end

function is_weak_down(C::Array{Int64,2},P::Array{Float64,2})
   return is_weak_down(C,rationalize_matrix(P))
end

# check weak_up monotonicity
function is_weak_up(C::Array{Int64,2},P::Array{Rational{Int64},2})
  #(C,P)=upper_triangular(C,P)
  R=convert_to_min_Int(P)
  return minimum(offspring_matrix(C)*R*C' .<= 0)
end

function is_weak_up(C::Array{Int64,2},P::Array{Float64,2})
   return is_weak_up(C,rationalize_matrix(P))
end

# check stochastic monotonicity
function is_stochastic(C::Array{Int64,2},P::Array{Rational{Int64},2})
  (C,P)=upper_triangular(C,P)
  R=convert_to_min_Int(P)
  return minimum(offspring_matrix(C)*R*up_sets(C) .<= 0)
end

function is_stochastic(C::Array{Int64,2},P::Array{Float64,2})
   return is_stochastic(C,rationalize_matrix(P))
end

end


text:
# ---
# title: 1617. Count Subtrees With Max Distance Between Cities
# id: problem1617
# author: Tian Jun
# date: 2020-10-31
# difficulty: Hard
# categories: Backtracking
# link: <https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/description/>
# hidden: true
# ---
#
# There are `n` cities numbered from `1` to `n`. You are given an array `edges`
# of size `n-1`, where `edges[i] = [ui, vi]` represents a bidirectional edge
# between cities `ui` and `vi`. There exists a unique path between each pair of
# cities. In other words, the cities form a **tree**.
#
# A **subtree** is a subset of cities where every city is reachable from every
# other city in the subset, where the path between each pair passes through only
# the cities from the subset. Two subtrees are different if there is a city in
# one subtree that is not present in the other.
#
# For each `d` from `1` to `n-1`, find the number of subtrees in which the
# **maximum distance** between any two cities in the subtree is equal to `d`.
#
# Return _an array of size_ `n-1` _where the_`dth` ___element **(1-indexed)** is
# the number of subtrees in which the **maximum distance** between any two
# cities is equal to _`d`.
#
# **Notice**  that the **distance** between the two cities is the number of
# edges in the path between them.
#
#
#
# **Example 1:**
#
# **![](https://assets.leetcode.com/uploads/2020/09/21/p1.png)**
#
#
#
#     Input: n = 4, edges = [[1,2],[2,3],[2,4]]
#     Output: [3,4,0]
#     Explanation: The subtrees with subsets {1,2}, {2,3} and {2,4} have a max distance of 1.
#     The subtrees with subsets {1,2,3}, {1,2,4}, {2,3,4} and {1,2,3,4} have a max distance of 2.
#     No subtree has two nodes where the max distance between them is 3.
#
#
# **Example 2:**
#
#
#
#     Input: n = 2, edges = [[1,2]]
#     Output: [1]
#
#
# **Example 3:**
#
#
#
#     Input: n = 3, edges = [[1,2],[2,3]]
#     Output: [2,1]
#
#
#
#
# **Constraints:**
#
#   * `2 <= n <= 15`
#   * `edges.length == n-1`
#   * `edges[i].length == 2`
#   * `1 <= ui, vi <= n`
#   * All pairs `(ui, vi)` are distinct.
#
#
## @lc code=start
using LeetCode

## add your code here:
## @lc code=end


text:
@testset "GeometrySet" begin
  @test !isgrid(GeometrySet)

  s = Segment(P2(0,0), P2(1,1))
  t = Triangle(P2(0,0), P2(1,0), P2(0,1))
  p = PolyArea(P2[(0,0), (1,0), (1,1), (0,1), (0,0)])
  gset = GeometrySet([s, t, p])
  @test [centroid(gset, i) for i in 1:3] == P2[(1/2,1/2), (1/3,1/3), (1/2,1/2)]
end


text:
module PairAsPipe

export @pap

using MacroTools
using MacroTools: @capture

macro pap(ex)

    has_newcol = @capture(ex, newcol_ = rhs_)

    if !has_newcol
        rhs = ex
    end

    # for obtaining symbols
    symbols = QuoteNode[]
    gen_symbols = Symbol[]
    rhs = MacroTools.postwalk(function(x)
        if x isa QuoteNode
            push!(symbols, x)
            push!(gen_symbols, MacroTools.gensym(x.value))
            return gen_symbols[end]
        else
            return x
        end
    end, rhs)

    lhs = Expr(:tuple, gen_symbols...)

    # the fn in
    # :col => fn
    fn = Expr(:->, lhs, rhs)

    # the [:col1, :col2] in
    # the [:col1, :col2] => fn
    cols = Expr(:vect, symbols...)

    if has_newcol
        fn = Expr(:call, :(=>), fn, QuoteNode(newcol))
    end

    esc(Expr(:call, :(=>), cols, fn))
end

end


text:
module MPITools

import MPI

export silence_stdout

"""
    silence_stdout(comm)

Silence standard output of all but one MPI process.
"""
function silence_stdout(comm, root=0)
    dev_null = @static Sys.iswindows() ? "nul" : "/dev/null"
    MPI.Comm_rank(comm) == root || redirect_stdout(open(dev_null, "w"))
    nothing
end

end


text:
using Revise
using Connectomes
using Makie
using FileIO
using Colors
using LightXML

assetpath = "/"*relpath((@__FILE__)*"/../..","/") * "/assets/"
connectome_path = assetpath * "connectomes/hcp-scale1-standard-master.graphml"
connectome = Connectome(connectome_path)

plot_cortex()

plot_cortex(:connectome)

plot_parc(connectome, :left; view=:left)

subcortex = findall( x -> occursin("subcortical",x), connectome.parc.Region)

plot_roi(connectome, connectome.parc[subcortex,:Label])

f_connectome =  graph_filter(connectome, 0.01)

plot_connectome(f_connectome)

degree = [connectome.D[i,i] for i in 1:83]
d = degree ./ maximum(degree)

plot_connectome(f_connectome; node_size = d*5)

plot_cortex(:all;colour=(:grey,0.05), transparent=true)
plot_roi!(81, (:blue, 0.5))