element.style.left returns NaN

如何修复内联CSS样式问题
Two ways to fix it.

1. put the inline css style within the html element

<div style="left:280px;">bla</div>


[color=red]or[/color]


2. use the function below

function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}



http://www.quirksmode.org/dom/getstyles.html
import _ = require("../index"); declare module "../index" { interface LoDashStatic { /** * Attempts to invoke func, returning either the result or the caught error object. Any additional arguments * are provided to func when it’s invoked. * * @param func The function to attempt. * @return Returns the func result or error object. */ attempt<TResult>(func: (...args: any[]) => TResult, ...args: any[]): TResult | Error; } interface LoDashImplicitWrapper<TValue> { /** * @see _.attempt */ attempt<TResult>(...args: any[]): TResult | Error; } interface LoDashExplicitWrapper<TValue> { /** * @see _.attempt */ attempt<TResult>(...args: any[]): ExpChain<TResult | Error>; } interface LoDashStatic { /** * Binds methods of an object to the object itself, overwriting the existing method. Method names may be * specified as individual arguments or as arrays of method names. If no method names are provided all * enumerable function properties, own and inherited, of object are bound. * * Note: This method does not set the "length" property of bound functions. * * @param object The object to bind and assign the bound methods to. * @param methodNames The object method names to bind, specified as individual method names or arrays of * method names. * @return Returns object. */ bindAll<T>(object: T, ...methodNames: Array<Many<string>>): T; } interface LoDashImplicitWrapper<TValue> { /** * @see _.bindAll */ bindAll(...methodNames: Array<Many<string>>): this; } interface LoDashExplicitWrapper<TValue> { /** * @see _.bindAll */ bindAll(...methodNames: Array<Many<string>>): this; } interface LoDashStatic { /** * Creates a function that iterates over `pairs` and invokes the corresponding * function of the first predicate to return truthy. The predicate-function * pairs are invoked with the `this` binding and arguments of the created * function. * * @since 4.0.0 * @category Util * @param pairs The predicate-function pairs. * @returns Returns the new composite function. * @example * * var func = _.cond([ * [_.matches({ 'a': 1 }), _.constant('matches A')], * [_.conforms({ 'b': _.isNumber }), _.constant('matches B')], * [_.stubTrue, _.constant('no match')] * ]); * * func({ 'a': 1, 'b': 2 }); * // => 'matches A' * * func({ 'a': 0, 'b': 1 }); * // => 'matches B' * * func({ 'a': '1', 'b': '2' }); * // => 'no match' */ cond<T, R>(pairs: Array<CondPair<T, R>>): (Target: T) => R; } type ConformsPredicateObject<T> = { [P in keyof T]: T[P] extends (arg: infer A) => any ? A : any }; interface LoDashStatic { /** * Creates a function that invokes the predicate properties of `source` with the corresponding * property values of a given object, returning true if all predicates return truthy, else false. */ conforms<T>(source: ConformsPredicateObject<T>): (value: T) => boolean; } interface LoDashImplicitWrapper<TValue> { /** * @see _.conforms */ conforms(): Function<(value: ConformsPredicateObject<TValue>) => boolean>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.conforms */ conforms(): FunctionChain<(value: ConformsPredicateObject<TValue>) => boolean>; } interface LoDashStatic { /** * Creates a function that returns value. * * @param value The value to return from the new function. * @return Returns the new function. */ constant<T>(value: T): () => T; } interface LoDashImplicitWrapper<TValue> { /** * @see _.constant */ constant(): Function<() => TValue>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.constant */ constant(): FunctionChain<() => TValue>; } interface LoDashStatic { /** * Checks `value` to determine whether a default value should be returned in * its place. The `defaultValue` is returned if `value` is `NaN`, `null`, * or `undefined`. * * @param value The value to check. * @param defaultValue The default value. * @returns Returns the resolved value. */ defaultTo<T>(value: T | null | undefined, defaultValue: T): T; /** * @see _.defaultTo */ defaultTo<T, TDefault>(value: T | null | undefined, defaultValue: TDefault): T | TDefault; } interface LoDashImplicitWrapper<TValue> { /** * @see _.defaultTo */ defaultTo(defaultValue: TValue): TValue; /** * @see _.defaultTo */ defaultTo<TDefault>(defaultValue: TDefault): TValue extends null | undefined ? TDefault : TValue | TDefault; } interface LoDashExplicitWrapper<TValue> { /** * @see _.defaultTo */ defaultTo(defaultValue: TValue): ExpChain<TValue>; /** * @see _.defaultTo */ defaultTo<TDefault>(defaultValue: TDefault): ExpChain<TValue extends null | undefined ? TDefault : TValue | TDefault>; } interface LoDashStatic { /** * Creates a function that returns the result of invoking the provided functions with the this binding of the * created function, where each successive invocation is supplied the return value of the previous. * * @param funcs Functions to invoke. * @return Returns the new function. */ flow<A extends any[], R1, R2, R3, R4, R5, R6, R7>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6, f7: (a: R6) => R7): (...args: A) => R7; /** * @see _.flow */ flow<A extends any[], R1, R2, R3, R4, R5, R6, R7>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6, f7: (a: R6) => R7, ...func: Array<Many<(a: any) => any>>): (...args: A) => any; /** * @see _.flow */ flow<A extends any[], R1, R2, R3, R4, R5, R6>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6): (...args: A) => R6; /** * @see _.flow */ flow<A extends any[], R1, R2, R3, R4, R5>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5): (...args: A) => R5; /** * @see _.flow */ flow<A extends any[], R1, R2, R3, R4>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4): (...args: A) => R4; /** * @see _.flow */ flow<A extends any[], R1, R2, R3>(f1: (...args: A) => R1, f2: (a: R1) => R2, f3: (a: R2) => R3): (...args: A) => R3; /** * @see _.flow */ flow<A extends any[], R1, R2>(f1: (...args: A) => R1, f2: (a: R1) => R2): (...args: A) => R2; /** * @see _.flow */ flow(...func: Array<Many<(...args: any[]) => any>>): (...args: any[]) => any; } interface Function<T extends (...arg: any) => any> { /** * @see _.flow */ flow<R2, R3, R4, R5, R6, R7>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6, f7: (a: R6) => R7): Function<(...args: Parameters<T>) => R7>; /** * @see _.flow */ flow<R2, R3, R4, R5, R6, R7>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6, f7: (a: R6) => R7, ...func: Array<Many<(a: any) => any>>): Function<(...args: Parameters<T>) => any>; /** * @see _.flow */ flow<R2, R3, R4, R5, R6>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6): Function<(...args: Parameters<T>) => R6>; /** * @see _.flow */ flow<R2, R3, R4, R5>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5): Function<(...args: Parameters<T>) => R5>; /** * @see _.flow */ flow<R2, R3, R4>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4): Function<(...args: Parameters<T>) => R4>; /** * @see _.flow */ flow<R2, R3>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3): Function<(...args: Parameters<T>) => R3>; /** * @see _.flow */ flow<R2>(f2: (a: ReturnType<T>) => R2): Function<(...args: Parameters<T>) => R2>; /** * @see _.flow */ flow(...func: Array<Many<(...args: any[]) => any>>): Function<(...args: any[]) => any>; } interface FunctionChain<T> { /** * @see _.flow */ flow<R2, R3, R4, R5, R6, R7>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6, f7: (a: R6) => R7): FunctionChain<(...args: Parameters<T>) => R7>; /** * @see _.flow */ flow<R2, R3, R4, R5, R6, R7>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6, f7: (a: R6) => R7, ...func: Array<Many<(a: any) => any>>): FunctionChain<(...args: Parameters<T>) => any>; /** * @see _.flow */ flow<R2, R3, R4, R5, R6>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5, f6: (a: R5) => R6): FunctionChain<(...args: Parameters<T>) => R6>; /** * @see _.flow */ flow<R2, R3, R4, R5>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4, f5: (a: R4) => R5): FunctionChain<(...args: Parameters<T>) => R5>; /** * @see _.flow */ flow<R2, R3, R4>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3, f4: (a: R3) => R4): FunctionChain<(...args: Parameters<T>) => R4>; /** * @see _.flow */ flow<R2, R3>(f2: (a: ReturnType<T>) => R2, f3: (a: R2) => R3): FunctionChain<(...args: Parameters<T>) => R3>; /** * @see _.flow */ flow<R2>(f2: (a: ReturnType<T>) => R2): FunctionChain<(...args: Parameters<T>) => R2>; /** * @see _.flow */ flow(...func: Array<Many<(...args: any[]) => any>>): FunctionChain<(...args: any[]) => any>; } interface LoDashStatic { /** * This method is like _.flow except that it creates a function that invokes the provided functions from right * to left. * * @param funcs Functions to invoke. * @return Returns the new function. */ flowRight<A extends any[], R1, R2, R3, R4, R5, R6, R7>(f7: (a: R6) => R7, f6: (a: R5) => R6, f5: (a: R4) => R5, f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R7; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4, R5, R6>(f6: (a: R5) => R6, f5: (a: R4) => R5, f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R6; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4, R5>(f5: (a: R4) => R5, f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R5; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4>(f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R4; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3>(f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R3; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2>(f2: (a: R1) => R2, f1: (...args: A) => R1): (...args: A) => R2; /** * @see _.flowRight */ flowRight(...func: Array<Many<(...args: any[]) => any>>): (...args: any[]) => any; } interface Function<T> { /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4, R5>(f6: (a: R5) => Parameters<T>["0"], f5: (a: R4) => R5, f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): Function<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4>(f5: (a: R4) => Parameters<T>["0"], f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): Function<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3>(f4: (a: R3) => Parameters<T>["0"], f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): Function<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2>(f3: (a: R2) => Parameters<T>["0"], f2: (a: R1) => R2, f1: (...args: A) => R1): Function<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1>(f2: (a: R1) => Parameters<T>["0"], f1: (...args: A) => R1): Function<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[]>(f1: (...args: A) => Parameters<T>["0"]): Function<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight(...func: Array<Many<(...args: any[]) => any>>): Function<(...args: any[]) => any>; } interface FunctionChain<T> { /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4, R5>(f6: (a: R5) => Parameters<T>["0"], f5: (a: R4) => R5, f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): FunctionChain<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3, R4>(f5: (a: R4) => Parameters<T>["0"], f4: (a: R3) => R4, f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): FunctionChain<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2, R3>(f4: (a: R3) => Parameters<T>["0"], f3: (a: R2) => R3, f2: (a: R1) => R2, f1: (...args: A) => R1): FunctionChain<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1, R2>(f3: (a: R2) => Parameters<T>["0"], f2: (a: R1) => R2, f1: (...args: A) => R1): FunctionChain<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[], R1>(f2: (a: R1) => Parameters<T>["0"], f1: (...args: A) => R1): FunctionChain<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight<A extends any[]>(f1: (...args: A) => Parameters<T>["0"]): FunctionChain<(...args: A) => ReturnType<T>>; /** * @see _.flowRight */ flowRight(...func: Array<Many<(...args: any[]) => any>>): FunctionChain<(...args: any[]) => any>; } interface LoDashStatic { /** * This method returns the first argument provided to it. * * @param value Any value. * @return Returns value. */ identity<T>(value: T): T; /** * @see _.identity */ identity(): undefined; } interface LoDashImplicitWrapper<TValue> { /** * @see _.identity */ identity(): TValue; } interface LoDashExplicitWrapper<TValue> { /** * @see _.identity */ identity(): this; } interface LoDashStatic { /** * Creates a function that invokes `func` with the arguments of the created * function. If `func` is a property name the created callback returns the * property value for a given element. If `func` is an object the created * callback returns `true` for elements that contain the equivalent object properties, otherwise it returns `false`. * * @category Util * @param [func=_.identity] The value to convert to a callback. * @returns Returns the callback. * @example * * var users = [ * { 'user': 'barney', 'age': 36 }, * { 'user': 'fred', 'age': 40 } * ]; * * // create custom iteratee shorthands * _.iteratee = _.wrap(_.iteratee, function(callback, func) { * var p = /^(\S+)\s*([<>])\s*(\S+)$/.exec(func); * return !p ? callback(func) : function(object) { * return (p[2] == '>' ? object[p[1]] > p[3] : object[p[1]] < p[3]); * }; * }); * * _.filter(users, 'age > 36'); * // => [{ 'user': 'fred', 'age': 40 }] */ iteratee<TFunction extends (...args: any[]) => any>(func: TFunction): TFunction; /** * @see _.iteratee */ iteratee(func: string | object): (...args: any[]) => any; } interface Function<T extends (...args: any) => any> { /** * @see _.iteratee */ iteratee(): Function<T>; } interface Collection<T> { /** * @see _.iteratee */ iteratee(): Function<(o: object) => boolean>; } interface Object<T> { /** * @see _.iteratee */ iteratee(): Function<(o: T) => boolean>; } interface String { /** * @see _.iteratee */ iteratee(): Function<(o: object) => any>; } interface FunctionChain<T extends (...args: any) => any> { /** * @see _.iteratee */ iteratee(): FunctionChain<T>; } interface CollectionChain<T> { /** * @see _.iteratee */ iteratee(): FunctionChain<(o: object) => boolean>; } interface ObjectChain<T> { /** * @see _.iteratee */ iteratee(): FunctionChain<(o: T) => boolean>; } interface StringChain { /** * @see _.iteratee */ iteratee(): FunctionChain<(o: object) => any>; } interface StringNullableChain { /** * @see _.iteratee */ iteratee(): FunctionChain<(o: object) => any>; } interface LoDashStatic { /** * Creates a function that performs a deep comparison between a given object and source, returning true if the * given object has equivalent property values, else false. * * Note: This method supports comparing arrays, booleans, Date objects, numbers, Object objects, regexes, and * strings. Objects are compared by their own, not inherited, enumerable properties. For comparing a single own * or inherited property value see _.matchesProperty. * * @param source The object of property values to match. * @return Returns the new function. */ matches<T>(source: T): (value: any) => boolean; /** * @see _.matches */ matches<T, V>(source: T): (value: V) => boolean; } interface LoDashImplicitWrapper<TValue> { /** * @see _.matches */ matches<V>(): Function<(value: V) => boolean>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.matches */ matches<V>(): FunctionChain<(value: V) => boolean>; } interface LoDashStatic { /** * Creates a function that compares the property value of path on a given object to value. * * Note: This method supports comparing arrays, booleans, Date objects, numbers, Object objects, regexes, and * strings. Objects are compared by their own, not inherited, enumerable properties. * * @param path The path of the property to get. * @param srcValue The value to match. * @return Returns the new function. */ matchesProperty<T>(path: PropertyPath, srcValue: T): (value: any) => boolean; /** * @see _.matchesProperty */ matchesProperty<T, V>(path: PropertyPath, srcValue: T): (value: V) => boolean; } interface LoDashImplicitWrapper<TValue> { /** * @see _.matchesProperty */ matchesProperty<SrcValue>(srcValue: SrcValue): Function<(value: any) => boolean>; /** * @see _.matchesProperty */ matchesProperty<SrcValue, Value>(srcValue: SrcValue): Function<(value: Value) => boolean>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.matchesProperty */ matchesProperty<SrcValue>(srcValue: SrcValue): FunctionChain<(value: any) => boolean>; /** * @see _.matchesProperty */ matchesProperty<SrcValue, Value>(srcValue: SrcValue): FunctionChain<(value: Value) => boolean>; } interface LoDashStatic { /** * Creates a function that invokes the method at path on a given object. Any additional arguments are provided * to the invoked method. * * @param path The path of the method to invoke. * @param args The arguments to invoke the method with. * @return Returns the new function. */ method(path: PropertyPath, ...args: any[]): (object: any) => any; } interface LoDashImplicitWrapper<TValue> { /** * @see _.method */ method(...args: any[]): Function<(object: any) => any>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.method */ method(...args: any[]): FunctionChain<(object: any) => any>; } interface LoDashStatic { /** * The opposite of _.method; this method creates a function that invokes the method at a given path on object. * Any additional arguments are provided to the invoked method. * * @param object The object to query. * @param args The arguments to invoke the method with. * @return Returns the new function. */ methodOf(object: object, ...args: any[]): (path: PropertyPath) => any; } interface LoDashImplicitWrapper<TValue> { /** * @see _.methodOf */ methodOf(...args: any[]): LoDashImplicitWrapper<(path: PropertyPath) => any>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.methodOf */ methodOf(...args: any[]): LoDashExplicitWrapper<(path: PropertyPath) => any>; } interface MixinOptions { /** * @see _.chain */ chain?: boolean; } interface LoDashStatic { /** * Adds all own enumerable function properties of a source object to the destination object. If object is a * function then methods are added to its prototype as well. * * Note: Use _.runInContext to create a pristine lodash function to avoid conflicts caused by modifying * the original. * * @param object The destination object. * @param source The object of functions to add. * @param options The options object. * @param options.chain Specify whether the functions added are chainable. * @return Returns object. */ mixin<TObject>(object: TObject, source: Dictionary<(...args: any[]) => any>, options?: MixinOptions): TObject; /** * @see _.mixin */ mixin<TResult>(source: Dictionary<(...args: any[]) => any>, options?: MixinOptions): LoDashStatic; } interface LoDashImplicitWrapper<TValue> { /** * @see _.mixin */ mixin(source: Dictionary<(...args: any[]) => any>, options?: MixinOptions): this; /** * @see _.mixin */ mixin(options?: MixinOptions): LoDashImplicitWrapper<LoDashStatic>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.mixin */ mixin(source: Dictionary<(...args: any[]) => any>, options?: MixinOptions): this; /** * @see _.mixin */ mixin(options?: MixinOptions): LoDashExplicitWrapper<LoDashStatic>; } interface LoDashStatic { /** * Reverts the _ variable to its previous value and returns a reference to the lodash function. * * @return Returns the lodash function. */ noConflict(): typeof _; } interface LoDashImplicitWrapper<TValue> { /** * @see _.noConflict */ noConflict(): typeof _; } interface LoDashExplicitWrapper<TValue> { /** * @see _.noConflict */ noConflict(): LoDashExplicitWrapper<typeof _>; } interface LoDashStatic { /** * A no-operation function that returns undefined regardless of the arguments it receives. * * @return undefined */ noop(...args: any[]): void; } interface LoDashImplicitWrapper<TValue> { /** * @see _.noop */ noop(...args: any[]): void; } interface LoDashExplicitWrapper<TValue> { /** * @see _.noop */ noop(...args: any[]): PrimitiveChain<undefined>; } interface LoDashStatic { /** * Creates a function that returns its nth argument. * * @param n The index of the argument to return. * @return Returns the new function. */ nthArg(n?: number): (...args: any[]) => any; } interface LoDashImplicitWrapper<TValue> { /** * @see _.nthArg */ nthArg(): Function<(...args: any[]) => any>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.nthArg */ nthArg(): FunctionChain<(...args: any[]) => any>; } interface LoDashStatic { /** * Creates a function that invokes iteratees with the arguments provided to the created function and returns * their results. * * @param iteratees The iteratees to invoke. * @return Returns the new function. */ over<TResult>(...iteratees: Array<Many<(...args: any[]) => TResult>>): (...args: any[]) => TResult[]; } interface Collection<T> { /** * @see _.over */ over<TResult>(...iteratees: Array<Many<(...args: any[]) => TResult>>): Function<(...args: any[]) => TResult[]>; } interface Function<T> { /** * @see _.over */ over<TResult>(...iteratees: Array<Many<(...args: any[]) => TResult>>): Function<(...args: any[]) => Array<ReturnType<T> | TResult>>; } interface CollectionChain<T> { /** * @see _.over */ over<TResult>(...iteratees: Array<Many<(...args: any[]) => TResult>>): FunctionChain<(...args: any[]) => TResult[]>; } interface FunctionChain<T> { /** * @see _.over */ over<TResult>(...iteratees: Array<Many<(...args: any[]) => TResult>>): FunctionChain<(...args: any[]) => Array<ReturnType<T> | TResult>>; } interface LoDashStatic { /** * Creates a function that checks if all of the predicates return truthy when invoked with the arguments * provided to the created function. * * @param predicates The predicates to check. * @return Returns the new function. */ overEvery<T, Result1 extends T, Result2 extends T>(...predicates: [ (arg: T) => arg is Result1, (arg: T) => arg is Result2 ]): (arg: T) => arg is Result1 & Result2; overEvery<T>(...predicates: Array<Many<(...args: T[]) => boolean>>): (...args: T[]) => boolean; } interface Collection<T> { /** * @see _.overEvery */ overEvery<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): Function<(...args: TArgs[]) => boolean>; } interface Function<T> { /** * @see _.overEvery */ overEvery<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): Function<(...args: Parameters<T> | TArgs[]) => boolean>; } interface CollectionChain<T> { /** * @see _.overEvery */ overEvery<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): FunctionChain<(...args: TArgs[]) => boolean>; } interface FunctionChain<T> { /** * @see _.overEvery */ overEvery<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): FunctionChain<(...args: Parameters<T> | TArgs[]) => boolean>; } interface LoDashStatic { /** * Creates a function that checks if any of the predicates return truthy when invoked with the arguments * provided to the created function. * * @param predicates The predicates to check. * @return Returns the new function. */ overSome<T, Result1 extends T, Result2 extends T>(...predicates: [ (arg: T) => arg is Result1, (arg: T) => arg is Result2 ]): (arg: T) => arg is Result1 | Result2; overSome<T>(...predicates: Array<Many<(...args: T[]) => boolean>>): (...args: T[]) => boolean; } interface Collection<T> { /** * @see _.overSome */ overSome<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): Function<(...args: TArgs[]) => boolean>; } interface Function<T> { /** * @see _.overSome */ overSome<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): Function<(...args: Parameters<T> | TArgs[]) => boolean>; } interface CollectionChain<T> { /** * @see _.overSome */ overSome<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): FunctionChain<(...args: TArgs[]) => boolean>; } interface FunctionChain<T> { /** * @see _.overSome */ overSome<TArgs>(...iteratees: Array<Many<(...args: TArgs[]) => boolean>>): FunctionChain<(...args: Parameters<T> | TArgs[]) => boolean>; } interface LoDashStatic { /** * Creates a function that returns the property value at path on a given object. * * @param path The path of the property to get. * @return Returns the new function. */ property<TObj, TResult>(path: PropertyPath): (obj: TObj) => TResult; } interface LoDashImplicitWrapper<TValue> { /** * @see _.property */ property<TObj, TResult>(): Function<(obj: TObj) => TResult>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.property */ property<TObj, TResult>(): FunctionChain<(obj: TObj) => TResult>; } interface LoDashStatic { /** * The opposite of _.property; this method creates a function that returns the property value at a given path * on object. * * @param object The object to query. * @return Returns the new function. */ propertyOf<T extends {}>(object: T): (path: PropertyPath) => any; } interface LoDashImplicitWrapper<TValue> { /** * @see _.propertyOf */ propertyOf(): LoDashImplicitWrapper<(path: PropertyPath) => any>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.propertyOf */ propertyOf(): LoDashExplicitWrapper<(path: PropertyPath) => any>; } interface LoDashStatic { /** * Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end. * If end is not specified it’s set to start with start then set to 0. If end is less than start a zero-length * range is created unless a negative step is specified. * * @param start The start of the range. * @param end The end of the range. * @param step The value to increment or decrement by. * @return Returns a new range array. */ range(start: number, end?: number, step?: number): number[]; /** * @see _.range */ range(end: number, index: string | number, guard: object): number[]; } interface LoDashImplicitWrapper<TValue> { /** * @see _.range */ range(end?: number, step?: number): Collection<number>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.range */ range(end?: number, step?: number): CollectionChain<number>; } interface LoDashStatic { /** * This method is like `_.range` except that it populates values in * descending order. * * @category Util * @param start The start of the range. * @param end The end of the range. * @param step The value to increment or decrement by. * @returns Returns the new array of numbers. * @example * * _.rangeRight(4); * // => [3, 2, 1, 0] * * _.rangeRight(-4); * // => [-3, -2, -1, 0] * * _.rangeRight(1, 5); * // => [4, 3, 2, 1] * * _.rangeRight(0, 20, 5); * // => [15, 10, 5, 0] * * _.rangeRight(0, -4, -1); * // => [-3, -2, -1, 0] * * _.rangeRight(1, 4, 0); * // => [1, 1, 1] * * _.rangeRight(0); * // => [] */ rangeRight(start: number, end?: number, step?: number): number[]; /** * @see _.rangeRight */ rangeRight(end: number, index: string | number, guard: object): number[]; } interface LoDashImplicitWrapper<TValue> { /** * @see _.rangeRight */ rangeRight(end?: number, step?: number): Collection<number>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.rangeRight */ rangeRight(end?: number, step?: number): CollectionChain<number>; } interface LoDashStatic { /** * Create a new pristine lodash function using the given context object. * * @param context The context object. * @return Returns a new lodash function. */ runInContext(context?: object): LoDashStatic; } interface LoDashImplicitWrapper<TValue> { /** * @see _.runInContext */ runInContext(): LoDashStatic; } interface LoDashStatic { /** * This method returns a new empty array. * * @returns Returns the new empty array. */ stubArray(): any[]; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubArray */ stubArray(): any[]; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubArray */ stubArray(): CollectionChain<any>; } interface LoDashStatic { /** * This method returns `false`. * * @returns Returns `false`. */ stubFalse(): false; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubFalse */ stubFalse(): false; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubFalse */ stubFalse(): PrimitiveChain<false>; } interface LoDashStatic { /** * This method returns a new empty object. * * @returns Returns the new empty object. */ stubObject(): any; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubObject */ stubObject(): any; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubObject */ stubObject(): LoDashExplicitWrapper<any>; } interface LoDashStatic { /** * This method returns an empty string. * * @returns Returns the empty string. */ stubString(): string; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubString */ stubString(): string; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubString */ stubString(): StringChain; } interface LoDashStatic { /** * This method returns `true`. * * @returns Returns `true`. */ stubTrue(): true; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubTrue */ stubTrue(): true; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubTrue */ stubTrue(): PrimitiveChain<true>; } interface LoDashStatic { /** * Invokes the iteratee function n times, returning an array of the results of each invocation. The iteratee * is invoked with one argument; (index). * * @param n The number of times to invoke iteratee. * @param iteratee The function invoked per iteration. * @return Returns the array of results. */ times<TResult>(n: number, iteratee: (num: number) => TResult): TResult[]; /** * @see _.times */ times(n: number): number[]; } interface LoDashImplicitWrapper<TValue> { /** * @see _.times */ times<TResult>(iteratee: (num: number) => TResult): TResult[]; /** * @see _.times */ times(): number[]; } interface LoDashExplicitWrapper<TValue> { /** * @see _.times */ times<TResult>(iteratee: (num: number) => TResult): CollectionChain<TResult>; /** * @see _.times */ times(): CollectionChain<number>; } interface LoDashStatic { /** * Converts `value` to a property path array. * * @category Util * @param value The value to convert. * @returns Returns the new property path array. * @example * * _.toPath('a.b.c'); * // => ['a', 'b', 'c'] * * _.toPath('a[0].b.c'); * // => ['a', '0', 'b', 'c'] * * var path = ['a', 'b', 'c'], * newPath = _.toPath(path); * * console.log(newPath); * // => ['a', 'b', 'c'] * * console.log(path === newPath); * // => false */ toPath(value: any): string[]; } interface LoDashImplicitWrapper<TValue> { /** * @see _.toPath */ toPath(): Collection<string>; } interface LoDashExplicitWrapper<TValue> { /** * @see _.toPath */ toPath(): CollectionChain<string>; } interface LoDashStatic { /** * Generates a unique ID. If prefix is provided the ID is appended to it. * * @param prefix The value to prefix the ID with. * @return Returns the unique ID. */ uniqueId(prefix?: string): string; } interface LoDashImplicitWrapper<TValue> { /** * @see _.uniqueId */ uniqueId(): string; } interface LoDashExplicitWrapper<TValue> { /** * @see _.uniqueId */ uniqueId(): StringChain; } // stubTrue interface LoDashStatic { /** * This method returns true. * * @return Returns true. */ stubTrue(): true; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubTrue */ stubTrue(): true; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubTrue */ stubTrue(): LoDashExplicitWrapper<true>; } // stubFalse interface LoDashStatic { /** * This method returns false. * * @return Returns false. */ stubFalse(): false; } interface LoDashImplicitWrapper<TValue> { /** * @see _.stubFalse */ stubFalse(): false; } interface LoDashExplicitWrapper<TValue> { /** * @see _.stubFalse */ stubFalse(): LoDashExplicitWrapper<false>; } } 报A rest parameter must be of an array type.错误
最新发布
07-31
二,yolov8添加w-iou具体步骤 !!!只需要修改两个python文件,metrics.py和loss.py 首先打开metrics.py文件,替换一些代码。 以下是原始代码 # Ultralytics YOLO 🚀, AGPL-3.0 license """Model validation metrics.""" import math import warnings from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch from ultralytics.utils import LOGGER, SimpleClass, TryExcept, plt_settings OKS_SIGMA = ( np.array([0.26, 0.25, 0.25, 0.35, 0.35, 0.79, 0.79, 0.72, 0.72, 0.62, 0.62, 1.07, 1.07, 0.87, 0.87, 0.89, 0.89]) / 10.0 ) def bbox_ioa(box1, box2, iou=False, eps=1e-7): """ Calculate the intersection over box2 area given box1 and box2. Boxes are in x1y1x2y2 format. Args: box1 (np.ndarray): A numpy array of shape (n, 4) representing n bounding boxes. box2 (np.ndarray): A numpy array of shape (m, 4) representing m bounding boxes. iou (bool): Calculate the standard IoU if True else return inter_area/box2_area. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (np.ndarray): A numpy array of shape (n, m) representing the intersection over box2 area. """ # Get the coordinates of bounding boxes b1_x1, b1_y1, b1_x2, b1_y2 = box1.T b2_x1, b2_y1, b2_x2, b2_y2 = box2.T # Intersection area inter_area = (np.minimum(b1_x2[:, None], b2_x2) - np.maximum(b1_x1[:, None], b2_x1)).clip(0) * ( np.minimum(b1_y2[:, None], b2_y2) - np.maximum(b1_y1[:, None], b2_y1) ).clip(0) # Box2 area area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) if iou: box1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1) area = area + box1_area[:, None] - inter_area # Intersection over box2 area return inter_area / (area + eps) def box_iou(box1, box2, eps=1e-7): """ Calculate intersection-over-union (IoU) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Based on https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py Args: box1 (torch.Tensor): A tensor of shape (N, 4) representing N bounding boxes. box2 (torch.Tensor): A tensor of shape (M, 4) representing M bounding boxes. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (torch.Tensor): An NxM tensor containing the pairwise IoU values for every element in box1 and box2. """ # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2) inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp_(0).prod(2) # IoU = inter / (area1 + area2 - inter) return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps) def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7): """ Calculate Intersection over Union (IoU) of box1(1, 4) to box2(n, 4). Args: box1 (torch.Tensor): A tensor representing a single bounding box with shape (1, 4). box2 (torch.Tensor): A tensor representing n bounding boxes with shape (n, 4). xywh (bool, optional): If True, input boxes are in (x, y, w, h) format. If False, input boxes are in (x1, y1, x2, y2) format. Defaults to True. GIoU (bool, optional): If True, calculate Generalized IoU. Defaults to False. DIoU (bool, optional): If True, calculate Distance IoU. Defaults to False. CIoU (bool, optional): If True, calculate Complete IoU. Defaults to False. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (torch.Tensor): IoU, GIoU, DIoU, or CIoU values depending on the specified flags. """ # Get the coordinates of bounding boxes if xywh: # transform from xywh to xyxy (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2 b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_ b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_ else: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1) b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1) w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps # Intersection area inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp_(0) * ( b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1) ).clamp_(0) # Union Area union = w1 * h1 + w2 * h2 - inter + eps # IoU iou = inter / union if CIoU or DIoU or GIoU: cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = cw.pow(2) + ch.pow(2) + eps # convex diagonal squared rho2 = ( (b2_x1 + b2_x2 - b1_x1 - b1_x2).pow(2) + (b2_y1 + b2_y2 - b1_y1 - b1_y2).pow(2) ) / 4 # center dist**2 if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi**2) * ((w2 / h2).atan() - (w1 / h1).atan()).pow(2) with torch.no_grad(): alpha = v / (v - iou + (1 + eps)) return iou - (rho2 / c2 + v * alpha) # CIoU return iou - rho2 / c2 # DIoU c_area = cw * ch + eps # convex area return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf return iou # IoU def mask_iou(mask1, mask2, eps=1e-7): """ Calculate masks IoU. Args: mask1 (torch.Tensor): A tensor of shape (N, n) where N is the number of ground truth objects and n is the product of image width and height. mask2 (torch.Tensor): A tensor of shape (M, n) where M is the number of predicted objects and n is the product of image width and height. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (torch.Tensor): A tensor of shape (N, M) representing masks IoU. """ intersection = torch.matmul(mask1, mask2.T).clamp_(0) union = (mask1.sum(1)[:, None] + mask2.sum(1)[None]) - intersection # (area1 + area2) - intersection return intersection / (union + eps) def kpt_iou(kpt1, kpt2, area, sigma, eps=1e-7): """ Calculate Object Keypoint Similarity (OKS). Args: kpt1 (torch.Tensor): A tensor of shape (N, 17, 3) representing ground truth keypoints. kpt2 (torch.Tensor): A tensor of shape (M, 17, 3) representing predicted keypoints. area (torch.Tensor): A tensor of shape (N,) representing areas from ground truth. sigma (list): A list containing 17 values representing keypoint scales. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (torch.Tensor): A tensor of shape (N, M) representing keypoint similarities. """ d = (kpt1[:, None, :, 0] - kpt2[..., 0]).pow(2) + (kpt1[:, None, :, 1] - kpt2[..., 1]).pow(2) # (N, M, 17) sigma = torch.tensor(sigma, device=kpt1.device, dtype=kpt1.dtype) # (17, ) kpt_mask = kpt1[..., 2] != 0 # (N, 17) e = d / ((2 * sigma).pow(2) * (area[:, None, None] + eps) * 2) # from cocoeval # e = d / ((area[None, :, None] + eps) * sigma) ** 2 / 2 # from formula return ((-e).exp() * kpt_mask[:, None]).sum(-1) / (kpt_mask.sum(-1)[:, None] + eps) def _get_covariance_matrix(boxes): """ Generating covariance matrix from obbs. Args: boxes (torch.Tensor): A tensor of shape (N, 5) representing rotated bounding boxes, with xywhr format. Returns: (torch.Tensor): Covariance metrixs corresponding to original rotated bounding boxes. """ # Gaussian bounding boxes, ignore the center points (the first two columns) because they are not needed here. gbbs = torch.cat((boxes[:, 2:4].pow(2) / 12, boxes[:, 4:]), dim=-1) a, b, c = gbbs.split(1, dim=-1) cos = c.cos() sin = c.sin() cos2 = cos.pow(2) sin2 = sin.pow(2) return a * cos2 + b * sin2, a * sin2 + b * cos2, (a - b) * cos * sin def probiou(obb1, obb2, CIoU=False, eps=1e-7): """ Calculate the prob IoU between oriented bounding boxes, https://arxiv.org/pdf/2106.06072v1.pdf. Args: obb1 (torch.Tensor): A tensor of shape (N, 5) representing ground truth obbs, with xywhr format. obb2 (torch.Tensor): A tensor of shape (N, 5) representing predicted obbs, with xywhr format. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (torch.Tensor): A tensor of shape (N, ) representing obb similarities. """ x1, y1 = obb1[..., :2].split(1, dim=-1) x2, y2 = obb2[..., :2].split(1, dim=-1) a1, b1, c1 = _get_covariance_matrix(obb1) a2, b2, c2 = _get_covariance_matrix(obb2) t1 = ( ((a1 + a2) * (y1 - y2).pow(2) + (b1 + b2) * (x1 - x2).pow(2)) / ((a1 + a2) * (b1 + b2) - (c1 + c2).pow(2) + eps) ) * 0.25 t2 = (((c1 + c2) * (x2 - x1) * (y1 - y2)) / ((a1 + a2) * (b1 + b2) - (c1 + c2).pow(2) + eps)) * 0.5 t3 = ( ((a1 + a2) * (b1 + b2) - (c1 + c2).pow(2)) / (4 * ((a1 * b1 - c1.pow(2)).clamp_(0) * (a2 * b2 - c2.pow(2)).clamp_(0)).sqrt() + eps) + eps ).log() * 0.5 bd = (t1 + t2 + t3).clamp(eps, 100.0) hd = (1.0 - (-bd).exp() + eps).sqrt() iou = 1 - hd if CIoU: # only include the wh aspect ratio part w1, h1 = obb1[..., 2:4].split(1, dim=-1) w2, h2 = obb2[..., 2:4].split(1, dim=-1) v = (4 / math.pi**2) * ((w2 / h2).atan() - (w1 / h1).atan()).pow(2) with torch.no_grad(): alpha = v / (v - iou + (1 + eps)) return iou - v * alpha # CIoU return iou def batch_probiou(obb1, obb2, eps=1e-7): """ Calculate the prob IoU between oriented bounding boxes, https://arxiv.org/pdf/2106.06072v1.pdf. Args: obb1 (torch.Tensor | np.ndarray): A tensor of shape (N, 5) representing ground truth obbs, with xywhr format. obb2 (torch.Tensor | np.ndarray): A tensor of shape (M, 5) representing predicted obbs, with xywhr format. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7. Returns: (torch.Tensor): A tensor of shape (N, M) representing obb similarities. """ obb1 = torch.from_numpy(obb1) if isinstance(obb1, np.ndarray) else obb1 obb2 = torch.from_numpy(obb2) if isinstance(obb2, np.ndarray) else obb2 x1, y1 = obb1[..., :2].split(1, dim=-1) x2, y2 = (x.squeeze(-1)[None] for x in obb2[..., :2].split(1, dim=-1)) a1, b1, c1 = _get_covariance_matrix(obb1) a2, b2, c2 = (x.squeeze(-1)[None] for x in _get_covariance_matrix(obb2)) t1 = ( ((a1 + a2) * (y1 - y2).pow(2) + (b1 + b2) * (x1 - x2).pow(2)) / ((a1 + a2) * (b1 + b2) - (c1 + c2).pow(2) + eps) ) * 0.25 t2 = (((c1 + c2) * (x2 - x1) * (y1 - y2)) / ((a1 + a2) * (b1 + b2) - (c1 + c2).pow(2) + eps)) * 0.5 t3 = ( ((a1 + a2) * (b1 + b2) - (c1 + c2).pow(2)) / (4 * ((a1 * b1 - c1.pow(2)).clamp_(0) * (a2 * b2 - c2.pow(2)).clamp_(0)).sqrt() + eps) + eps ).log() * 0.5 bd = (t1 + t2 + t3).clamp(eps, 100.0) hd = (1.0 - (-bd).exp() + eps).sqrt() return 1 - hd def smooth_BCE(eps=0.1): """ Computes smoothed positive and negative Binary Cross-Entropy targets. This function calculates positive and negative label smoothing BCE targets based on a given epsilon value. For implementation details, refer to https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441. Args: eps (float, optional): The epsilon value for label smoothing. Defaults to 0.1. Returns: (tuple): A tuple containing the positive and negative label smoothing BCE targets. """ return 1.0 - 0.5 * eps, 0.5 * eps class ConfusionMatrix: """ A class for calculating and updating a confusion matrix for object detection and classification tasks. Attributes: task (str): The type of task, either 'detect' or 'classify'. matrix (np.ndarray): The confusion matrix, with dimensions depending on the task. nc (int): The number of classes. conf (float): The confidence threshold for detections. iou_thres (float): The Intersection over Union threshold. """ def __init__(self, nc, conf=0.25, iou_thres=0.45, task="detect"): """Initialize attributes for the YOLO model.""" self.task = task self.matrix = np.zeros((nc + 1, nc + 1)) if self.task == "detect" else np.zeros((nc, nc)) self.nc = nc # number of classes self.conf = 0.25 if conf in (None, 0.001) else conf # apply 0.25 if default val conf is passed self.iou_thres = iou_thres def process_cls_preds(self, preds, targets): """ Update confusion matrix for classification task. Args: preds (Array[N, min(nc,5)]): Predicted class labels. targets (Array[N, 1]): Ground truth class labels. """ preds, targets = torch.cat(preds)[:, 0], torch.cat(targets) for p, t in zip(preds.cpu().numpy(), targets.cpu().numpy()): self.matrix[p][t] += 1 def process_batch(self, detections, gt_bboxes, gt_cls): """ Update confusion matrix for object detection task. Args: detections (Array[N, 6] | Array[N, 7]): Detected bounding boxes and their associated information. Each row should contain (x1, y1, x2, y2, conf, class) or with an additional element `angle` when it's obb. gt_bboxes (Array[M, 4]| Array[N, 5]): Ground truth bounding boxes with xyxy/xyxyr format. gt_cls (Array[M]): The class labels. """ if gt_cls.shape[0] == 0: # Check if labels is empty if detections is not None: detections = detections[detections[:, 4] > self.conf] detection_classes = detections[:, 5].int() for dc in detection_classes: self.matrix[dc, self.nc] += 1 # false positives return if detections is None: gt_classes = gt_cls.int() for gc in gt_classes: self.matrix[self.nc, gc] += 1 # background FN return detections = detections[detections[:, 4] > self.conf] gt_classes = gt_cls.int() detection_classes = detections[:, 5].int() is_obb = detections.shape[1] == 7 and gt_bboxes.shape[1] == 5 # with additional `angle` dimension iou = ( batch_probiou(gt_bboxes, torch.cat([detections[:, :4], detections[:, -1:]], dim=-1)) if is_obb else box_iou(gt_bboxes, detections[:, :4]) ) x = torch.where(iou > self.iou_thres) if x[0].shape[0]: matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() if x[0].shape[0] > 1: matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 1], return_index=True)[1]] matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 0], return_index=True)[1]] else: matches = np.zeros((0, 3)) n = matches.shape[0] > 0 m0, m1, _ = matches.transpose().astype(int) for i, gc in enumerate(gt_classes): j = m0 == i if n and sum(j) == 1: self.matrix[detection_classes[m1[j]], gc] += 1 # correct else: self.matrix[self.nc, gc] += 1 # true background if n: for i, dc in enumerate(detection_classes): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # predicted background def matrix(self): """Returns the confusion matrix.""" return self.matrix def tp_fp(self): """Returns true positives and false positives.""" tp = self.matrix.diagonal() # true positives fp = self.matrix.sum(1) - tp # false positives # fn = self.matrix.sum(0) - tp # false negatives (missed detections) return (tp[:-1], fp[:-1]) if self.task == "detect" else (tp, fp) # remove background class if task=detect @TryExcept("WARNING ⚠️ ConfusionMatrix plot failure") @plt_settings() def plot(self, normalize=True, save_dir="", names=(), on_plot=None): """ Plot the confusion matrix using seaborn and save it to a file. Args: normalize (bool): Whether to normalize the confusion matrix. save_dir (str): Directory where the plot will be saved. names (tuple): Names of classes, used as labels on the plot. on_plot (func): An optional callback to pass plots path and data when they are rendered. """ import seaborn as sn array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1e-9) if normalize else 1) # normalize columns array[array < 0.005] = np.nan # don't annotate (would appear as 0.00) fig, ax = plt.subplots(1, 1, figsize=(12, 9), tight_layout=True) nc, nn = self.nc, len(names) # number of classes, names sn.set_theme(font_scale=1.0 if nc < 50 else 0.8) # for label size labels = (0 < nn < 99) and (nn == nc) # apply names to ticklabels ticklabels = (list(names) + ["background"]) if labels else "auto" with warnings.catch_warnings(): warnings.simplefilter("ignore") # suppress empty matrix RuntimeWarning: All-NaN slice encountered sn.heatmap( array, ax=ax, annot=nc < 30, annot_kws={"size": 8}, cmap="Blues", fmt=".2f" if normalize else ".0f", square=True, vmin=0.0, xticklabels=ticklabels, yticklabels=ticklabels, ).set_facecolor((1, 1, 1)) title = "Confusion Matrix" + " Normalized" * normalize ax.set_xlabel("True") ax.set_ylabel("Predicted") ax.set_title(title) plot_fname = Path(save_dir) / f'{title.lower().replace(" ", "_")}.png' fig.savefig(plot_fname, dpi=250) plt.close(fig) if on_plot: on_plot(plot_fname) def print(self): """Print the confusion matrix to the console.""" for i in range(self.nc + 1): LOGGER.info(" ".join(map(str, self.matrix[i]))) def smooth(y, f=0.05): """Box filter of fraction f.""" nf = round(len(y) * f * 2) // 2 + 1 # number of filter elements (must be odd) p = np.ones(nf // 2) # ones padding yp = np.concatenate((p * y[0], y, p * y[-1]), 0) # y padded return np.convolve(yp, np.ones(nf) / nf, mode="valid") # y-smoothed @plt_settings() def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names=(), on_plot=None): """Plots a precision-recall curve.""" fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) py = np.stack(py, axis=1) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py.T): ax.plot(px, y, linewidth=1, label=f"{names[i]} {ap[i, 0]:.3f}") # plot(recall, precision) else: ax.plot(px, py, linewidth=1, color="grey") # plot(recall, precision) ax.plot(px, py.mean(1), linewidth=3, color="blue", label="all classes %.3f mAP@0.5" % ap[:, 0].mean()) ax.set_xlabel("Recall") ax.set_ylabel("Precision") ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left") ax.set_title("Precision-Recall Curve") fig.savefig(save_dir, dpi=250) plt.close(fig) if on_plot: on_plot(save_dir) @plt_settings() def plot_mc_curve(px, py, save_dir=Path("mc_curve.png"), names=(), xlabel="Confidence", ylabel="Metric", on_plot=None): """Plots a metric-confidence curve.""" fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py): ax.plot(px, y, linewidth=1, label=f"{names[i]}") # plot(confidence, metric) else: ax.plot(px, py.T, linewidth=1, color="grey") # plot(confidence, metric) y = smooth(py.mean(0), 0.05) ax.plot(px, y, linewidth=3, color="blue", label=f"all classes {y.max():.2f} at {px[y.argmax()]:.3f}") ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left") ax.set_title(f"{ylabel}-Confidence Curve") fig.savefig(save_dir, dpi=250) plt.close(fig) if on_plot: on_plot(save_dir) def compute_ap(recall, precision): """ Compute the average precision (AP) given the recall and precision curves. Args: recall (list): The recall curve. precision (list): The precision curve. Returns: (float): Average precision. (np.ndarray): Precision envelope curve. (np.ndarray): Modified recall curve with sentinel values added at the beginning and end. """ # Append sentinel values to beginning and end mrec = np.concatenate(([0.0], recall, [1.0])) mpre = np.concatenate(([1.0], precision, [0.0])) # Compute the precision envelope mpre = np.flip(np.maximum.accumulate(np.flip(mpre))) # Integrate area under curve method = "interp" # methods: 'continuous', 'interp' if method == "interp": x = np.linspace(0, 1, 101) # 101-point interp (COCO) ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate else: # 'continuous' i = np.where(mrec[1:] != mrec[:-1])[0] # points where x-axis (recall) changes ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve return ap, mpre, mrec def ap_per_class( tp, conf, pred_cls, target_cls, plot=False, on_plot=None, save_dir=Path(), names=(), eps=1e-16, prefix="" ): """ Computes the average precision per class for object detection evaluation. Args: tp (np.ndarray): Binary array indicating whether the detection is correct (True) or not (False). conf (np.ndarray): Array of confidence scores of the detections. pred_cls (np.ndarray): Array of predicted classes of the detections. target_cls (np.ndarray): Array of true classes of the detections. plot (bool, optional): Whether to plot PR curves or not. Defaults to False. on_plot (func, optional): A callback to pass plots path and data when they are rendered. Defaults to None. save_dir (Path, optional): Directory to save the PR curves. Defaults to an empty path. names (tuple, optional): Tuple of class names to plot PR curves. Defaults to an empty tuple. eps (float, optional): A small value to avoid division by zero. Defaults to 1e-16. prefix (str, optional): A prefix string for saving the plot files. Defaults to an empty string. Returns: (tuple): A tuple of six arrays and one array of unique classes, where: tp (np.ndarray): True positive counts at threshold given by max F1 metric for each class.Shape: (nc,). fp (np.ndarray): False positive counts at threshold given by max F1 metric for each class. Shape: (nc,). p (np.ndarray): Precision values at threshold given by max F1 metric for each class. Shape: (nc,). r (np.ndarray): Recall values at threshold given by max F1 metric for each class. Shape: (nc,). f1 (np.ndarray): F1-score values at threshold given by max F1 metric for each class. Shape: (nc,). ap (np.ndarray): Average precision for each class at different IoU thresholds. Shape: (nc, 10). unique_classes (np.ndarray): An array of unique classes that have data. Shape: (nc,). p_curve (np.ndarray): Precision curves for each class. Shape: (nc, 1000). r_curve (np.ndarray): Recall curves for each class. Shape: (nc, 1000). f1_curve (np.ndarray): F1-score curves for each class. Shape: (nc, 1000). x (np.ndarray): X-axis values for the curves. Shape: (1000,). prec_values: Precision values at mAP@0.5 for each class. Shape: (nc, 1000). """ # Sort by objectness i = np.argsort(-conf) tp, conf, pred_cls = tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes, nt = np.unique(target_cls, return_counts=True) nc = unique_classes.shape[0] # number of classes, number of detections # Create Precision-Recall curve and compute AP for each class x, prec_values = np.linspace(0, 1, 1000), [] # Average precision, precision and recall curves ap, p_curve, r_curve = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000)) for ci, c in enumerate(unique_classes): i = pred_cls == c n_l = nt[ci] # number of labels n_p = i.sum() # number of predictions if n_p == 0 or n_l == 0: continue # Accumulate FPs and TPs fpc = (1 - tp[i]).cumsum(0) tpc = tp[i].cumsum(0) # Recall recall = tpc / (n_l + eps) # recall curve r_curve[ci] = np.interp(-x, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases # Precision precision = tpc / (tpc + fpc) # precision curve p_curve[ci] = np.interp(-x, -conf[i], precision[:, 0], left=1) # p at pr_score # AP from recall-precision curve for j in range(tp.shape[1]): ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) if plot and j == 0: prec_values.append(np.interp(x, mrec, mpre)) # precision at mAP@0.5 prec_values = np.array(prec_values) # (nc, 1000) # Compute F1 (harmonic mean of precision and recall) f1_curve = 2 * p_curve * r_curve / (p_curve + r_curve + eps) names = [v for k, v in names.items() if k in unique_classes] # list: only classes that have data names = dict(enumerate(names)) # to dict if plot: plot_pr_curve(x, prec_values, ap, save_dir / f"{prefix}PR_curve.png", names, on_plot=on_plot) plot_mc_curve(x, f1_curve, save_dir / f"{prefix}F1_curve.png", names, ylabel="F1", on_plot=on_plot) plot_mc_curve(x, p_curve, save_dir / f"{prefix}P_curve.png", names, ylabel="Precision", on_plot=on_plot) plot_mc_curve(x, r_curve, save_dir / f"{prefix}R_curve.png", names, ylabel="Recall", on_plot=on_plot) i = smooth(f1_curve.mean(0), 0.1).argmax() # max F1 index p, r, f1 = p_curve[:, i], r_curve[:, i], f1_curve[:, i] # max-F1 precision, recall, F1 values tp = (r * nt).round() # true positives fp = (tp / (p + eps) - tp).round() # false positives return tp, fp, p, r, f1, ap, unique_classes.astype(int), p_curve, r_curve, f1_curve, x, prec_values class Metric(SimpleClass): """ Class for computing evaluation metrics for YOLOv8 model. Attributes: p (list): Precision for each class. Shape: (nc,). r (list): Recall for each class. Shape: (nc,). f1 (list): F1 score for each class. Shape: (nc,). all_ap (list): AP scores for all classes and all IoU thresholds. Shape: (nc, 10). ap_class_index (list): Index of class for each AP score. Shape: (nc,). nc (int): Number of classes. Methods: ap50(): AP at IoU threshold of 0.5 for all classes. Returns: List of AP scores. Shape: (nc,) or []. ap(): AP at IoU thresholds from 0.5 to 0.95 for all classes. Returns: List of AP scores. Shape: (nc,) or []. mp(): Mean precision of all classes. Returns: Float. mr(): Mean recall of all classes. Returns: Float. map50(): Mean AP at IoU threshold of 0.5 for all classes. Returns: Float. map75(): Mean AP at IoU threshold of 0.75 for all classes. Returns: Float. map(): Mean AP at IoU thresholds from 0.5 to 0.95 for all classes. Returns: Float. mean_results(): Mean of results, returns mp, mr, map50, map. class_result(i): Class-aware result, returns p[i], r[i], ap50[i], ap[i]. maps(): mAP of each class. Returns: Array of mAP scores, shape: (nc,). fitness(): Model fitness as a weighted combination of metrics. Returns: Float. update(results): Update metric attributes with new evaluation results. """ def __init__(self) -> None: """Initializes a Metric instance for computing evaluation metrics for the YOLOv8 model.""" self.p = [] # (nc, ) self.r = [] # (nc, ) self.f1 = [] # (nc, ) self.all_ap = [] # (nc, 10) self.ap_class_index = [] # (nc, ) self.nc = 0 @property def ap50(self): """ Returns the Average Precision (AP) at an IoU threshold of 0.5 for all classes. Returns: (np.ndarray, list): Array of shape (nc,) with AP50 values per class, or an empty list if not available. """ return self.all_ap[:, 0] if len(self.all_ap) else [] @property def ap(self): """ Returns the Average Precision (AP) at an IoU threshold of 0.5-0.95 for all classes. Returns: (np.ndarray, list): Array of shape (nc,) with AP50-95 values per class, or an empty list if not available. """ return self.all_ap.mean(1) if len(self.all_ap) else [] @property def mp(self): """ Returns the Mean Precision of all classes. Returns: (float): The mean precision of all classes. """ return self.p.mean() if len(self.p) else 0.0 @property def mr(self): """ Returns the Mean Recall of all classes. Returns: (float): The mean recall of all classes. """ return self.r.mean() if len(self.r) else 0.0 @property def map50(self): """ Returns the mean Average Precision (mAP) at an IoU threshold of 0.5. Returns: (float): The mAP at an IoU threshold of 0.5. """ return self.all_ap[:, 0].mean() if len(self.all_ap) else 0.0 @property def map75(self): """ Returns the mean Average Precision (mAP) at an IoU threshold of 0.75. Returns: (float): The mAP at an IoU threshold of 0.75. """ return self.all_ap[:, 5].mean() if len(self.all_ap) else 0.0 @property def map(self): """ Returns the mean Average Precision (mAP) over IoU thresholds of 0.5 - 0.95 in steps of 0.05. Returns: (float): The mAP over IoU thresholds of 0.5 - 0.95 in steps of 0.05. """ return self.all_ap.mean() if len(self.all_ap) else 0.0 def mean_results(self): """Mean of results, return mp, mr, map50, map.""" return [self.mp, self.mr, self.map50, self.map] def class_result(self, i): """Class-aware result, return p[i], r[i], ap50[i], ap[i].""" return self.p[i], self.r[i], self.ap50[i], self.ap[i] @property def maps(self): """MAP of each class.""" maps = np.zeros(self.nc) + self.map for i, c in enumerate(self.ap_class_index): maps[c] = self.ap[i] return maps def fitness(self): """Model fitness as a weighted combination of metrics.""" w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (np.array(self.mean_results()) * w).sum() def update(self, results): """ Updates the evaluation metrics of the model with a new set of results. Args: results (tuple): A tuple containing the following evaluation metrics: - p (list): Precision for each class. Shape: (nc,). - r (list): Recall for each class. Shape: (nc,). - f1 (list): F1 score for each class. Shape: (nc,). - all_ap (list): AP scores for all classes and all IoU thresholds. Shape: (nc, 10). - ap_class_index (list): Index of class for each AP score. Shape: (nc,). Side Effects: Updates the class attributes `self.p`, `self.r`, `self.f1`, `self.all_ap`, and `self.ap_class_index` based on the values provided in the `results` tuple. """ ( self.p, self.r, self.f1, self.all_ap, self.ap_class_index, self.p_curve, self.r_curve, self.f1_curve, self.px, self.prec_values, ) = results @property def curves(self): """Returns a list of curves for accessing specific metrics curves.""" return [] @property def curves_results(self): """Returns a list of curves for accessing specific metrics curves.""" return [ [self.px, self.prec_values, "Recall", "Precision"], [self.px, self.f1_curve, "Confidence", "F1"], [self.px, self.p_curve, "Confidence", "Precision"], [self.px, self.r_curve, "Confidence", "Recall"], ] class DetMetrics(SimpleClass): """ This class is a utility class for computing detection metrics such as precision, recall, and mean average precision (mAP) of an object detection model. Args: save_dir (Path): A path to the directory where the output plots will be saved. Defaults to current directory. plot (bool): A flag that indicates whether to plot precision-recall curves for each class. Defaults to False. on_plot (func): An optional callback to pass plots path and data when they are rendered. Defaults to None. names (tuple of str): A tuple of strings that represents the names of the classes. Defaults to an empty tuple. Attributes: save_dir (Path): A path to the directory where the output plots will be saved. plot (bool): A flag that indicates whether to plot the precision-recall curves for each class. on_plot (func): An optional callback to pass plots path and data when they are rendered. names (tuple of str): A tuple of strings that represents the names of the classes. box (Metric): An instance of the Metric class for storing the results of the detection metrics. speed (dict): A dictionary for storing the execution time of different parts of the detection process. Methods: process(tp, conf, pred_cls, target_cls): Updates the metric results with the latest batch of predictions. keys: Returns a list of keys for accessing the computed detection metrics. mean_results: Returns a list of mean values for the computed detection metrics. class_result(i): Returns a list of values for the computed detection metrics for a specific class. maps: Returns a dictionary of mean average precision (mAP) values for different IoU thresholds. fitness: Computes the fitness score based on the computed detection metrics. ap_class_index: Returns a list of class indices sorted by their average precision (AP) values. results_dict: Returns a dictionary that maps detection metric keys to their computed values. curves: TODO curves_results: TODO """ def __init__(self, save_dir=Path("."), plot=False, on_plot=None, names=()) -> None: """Initialize a DetMetrics instance with a save directory, plot flag, callback function, and class names.""" self.save_dir = save_dir self.plot = plot self.on_plot = on_plot self.names = names self.box = Metric() self.speed = {"preprocess": 0.0, "inference": 0.0, "loss": 0.0, "postprocess": 0.0} self.task = "detect" def process(self, tp, conf, pred_cls, target_cls): """Process predicted results for object detection and update metrics.""" results = ap_per_class( tp, conf, pred_cls, target_cls, plot=self.plot, save_dir=self.save_dir, names=self.names, on_plot=self.on_plot, )[2:] self.box.nc = len(self.names) self.box.update(results) @property def keys(self): """Returns a list of keys for accessing specific metrics.""" return ["metrics/precision(B)", "metrics/recall(B)", "metrics/mAP50(B)", "metrics/mAP50-95(B)"] def mean_results(self): """Calculate mean of detected objects & return precision, recall, mAP50, and mAP50-95.""" return self.box.mean_results() def class_result(self, i): """Return the result of evaluating the performance of an object detection model on a specific class.""" return self.box.class_result(i) @property def maps(self): """Returns mean Average Precision (mAP) scores per class.""" return self.box.maps @property def fitness(self): """Returns the fitness of box object.""" return self.box.fitness() @property def ap_class_index(self): """Returns the average precision index per class.""" return self.box.ap_class_index @property def results_dict(self): """Returns dictionary of computed performance metrics and statistics.""" return dict(zip(self.keys + ["fitness"], self.mean_results() + [self.fitness])) @property def curves(self): """Returns a list of curves for accessing specific metrics curves.""" return ["Precision-Recall(B)", "F1-Confidence(B)", "Precision-Confidence(B)", "Recall-Confidence(B)"] @property def curves_results(self): """Returns dictionary of computed performance metrics and statistics.""" return self.box.curves_results class SegmentMetrics(SimpleClass): """ Calculates and aggregates detection and segmentation metrics over a given set of classes. Args: save_dir (Path): Path to the directory where the output plots should be saved. Default is the current directory. plot (bool): Whether to save the detection and segmentation plots. Default is False. on_plot (func): An optional callback to pass plots path and data when they are rendered. Defaults to None. names (list): List of class names. Default is an empty list. Attributes: save_dir (Path): Path to the directory where the output plots should be saved. plot (bool): Whether to save the detection and segmentation plots. on_plot (func): An optional callback to pass plots path and data when they are rendered. names (list): List of class names. box (Metric): An instance of the Metric class to calculate box detection metrics. seg (Metric): An instance of the Metric class to calculate mask segmentation metrics. speed (dict): Dictionary to store the time taken in different phases of inference. Methods: process(tp_m, tp_b, conf, pred_cls, target_cls): Processes metrics over the given set of predictions. mean_results(): Returns the mean of the detection and segmentation metrics over all the classes. class_result(i): Returns the detection and segmentation metrics of class `i`. maps: Returns the mean Average Precision (mAP) scores for IoU thresholds ranging from 0.50 to 0.95. fitness: Returns the fitness scores, which are a single weighted combination of metrics. ap_class_index: Returns the list of indices of classes used to compute Average Precision (AP). results_dict: Returns the dictionary containing all the detection and segmentation metrics and fitness score. """ def __init__(self, save_dir=Path("."), plot=False, on_plot=None, names=()) -> None: """Initialize a SegmentMetrics instance with a save directory, plot flag, callback function, and class names.""" self.save_dir = save_dir self.plot = plot self.on_plot = on_plot self.names = names self.box = Metric() self.seg = Metric() self.speed = {"preprocess": 0.0, "inference": 0.0, "loss": 0.0, "postprocess": 0.0} self.task = "segment" def process(self, tp, tp_m, conf, pred_cls, target_cls): """ Processes the detection and segmentation metrics over the given set of predictions. Args: tp (list): List of True Positive boxes. tp_m (list): List of True Positive masks. conf (list): List of confidence scores. pred_cls (list): List of predicted classes. target_cls (list): List of target classes. """ results_mask = ap_per_class( tp_m, conf, pred_cls, target_cls, plot=self.plot, on_plot=self.on_plot, save_dir=self.save_dir, names=self.names, prefix="Mask", )[2:] self.seg.nc = len(self.names) self.seg.update(results_mask) results_box = ap_per_class( tp, conf, pred_cls, target_cls, plot=self.plot, on_plot=self.on_plot, save_dir=self.save_dir, names=self.names, prefix="Box", )[2:] self.box.nc = len(self.names) self.box.update(results_box) @property def keys(self): """Returns a list of keys for accessing metrics.""" return [ "metrics/precision(B)", "metrics/recall(B)", "metrics/mAP50(B)", "metrics/mAP50-95(B)", "metrics/precision(M)", "metrics/recall(M)", "metrics/mAP50(M)", "metrics/mAP50-95(M)", ] def mean_results(self): """Return the mean metrics for bounding box and segmentation results.""" return self.box.mean_results() + self.seg.mean_results() def class_result(self, i): """Returns classification results for a specified class index.""" return self.box.class_result(i) + self.seg.class_result(i) @property def maps(self): """Returns mAP scores for object detection and semantic segmentation models.""" return self.box.maps + self.seg.maps @property def fitness(self): """Get the fitness score for both segmentation and bounding box models.""" return self.seg.fitness() + self.box.fitness() @property def ap_class_index(self): """Boxes and masks have the same ap_class_index.""" return self.box.ap_class_index @property def results_dict(self): """Returns results of object detection model for evaluation.""" return dict(zip(self.keys + ["fitness"], self.mean_results() + [self.fitness])) @property def curves(self): """Returns a list of curves for accessing specific metrics curves.""" return [ "Precision-Recall(B)", "F1-Confidence(B)", "Precision-Confidence(B)", "Recall-Confidence(B)", "Precision-Recall(M)", "F1-Confidence(M)", "Precision-Confidence(M)", "Recall-Confidence(M)", ] @property def curves_results(self): """Returns dictionary of computed performance metrics and statistics.""" return self.box.curves_results + self.seg.curves_results class PoseMetrics(SegmentMetrics): """ Calculates and aggregates detection and pose metrics over a given set of classes. Args: save_dir (Path): Path to the directory where the output plots should be saved. Default is the current directory. plot (bool): Whether to save the detection and segmentation plots. Default is False. on_plot (func): An optional callback to pass plots path and data when they are rendered. Defaults to None. names (list): List of class names. Default is an empty list. Attributes: save_dir (Path): Path to the directory where the output plots should be saved. plot (bool): Whether to save the detection and segmentation plots. on_plot (func): An optional callback to pass plots path and data when they are rendered. names (list): List of class names. box (Metric): An instance of the Metric class to calculate box detection metrics. pose (Metric): An instance of the Metric class to calculate mask segmentation metrics. speed (dict): Dictionary to store the time taken in different phases of inference. Methods: process(tp_m, tp_b, conf, pred_cls, target_cls): Processes metrics over the given set of predictions. mean_results(): Returns the mean of the detection and segmentation metrics over all the classes. class_result(i): Returns the detection and segmentation metrics of class `i`. maps: Returns the mean Average Precision (mAP) scores for IoU thresholds ranging from 0.50 to 0.95. fitness: Returns the fitness scores, which are a single weighted combination of metrics. ap_class_index: Returns the list of indices of classes used to compute Average Precision (AP). results_dict: Returns the dictionary containing all the detection and segmentation metrics and fitness score. """ def __init__(self, save_dir=Path("."), plot=False, on_plot=None, names=()) -> None: """Initialize the PoseMetrics class with directory path, class names, and plotting options.""" super().__init__(save_dir, plot, names) self.save_dir = save_dir self.plot = plot self.on_plot = on_plot self.names = names self.box = Metric() self.pose = Metric() self.speed = {"preprocess": 0.0, "inference": 0.0, "loss": 0.0, "postprocess": 0.0} self.task = "pose" def process(self, tp, tp_p, conf, pred_cls, target_cls): """ Processes the detection and pose metrics over the given set of predictions. Args: tp (list): List of True Positive boxes. tp_p (list): List of True Positive keypoints. conf (list): List of confidence scores. pred_cls (list): List of predicted classes. target_cls (list): List of target classes. """ results_pose = ap_per_class( tp_p, conf, pred_cls, target_cls, plot=self.plot, on_plot=self.on_plot, save_dir=self.save_dir, names=self.names, prefix="Pose", )[2:] self.pose.nc = len(self.names) self.pose.update(results_pose) results_box = ap_per_class( tp, conf, pred_cls, target_cls, plot=self.plot, on_plot=self.on_plot, save_dir=self.save_dir, names=self.names, prefix="Box", )[2:] self.box.nc = len(self.names) self.box.update(results_box) @property def keys(self): """Returns list of evaluation metric keys.""" return [ "metrics/precision(B)", "metrics/recall(B)", "metrics/mAP50(B)", "metrics/mAP50-95(B)", "metrics/precision(P)", "metrics/recall(P)", "metrics/mAP50(P)", "metrics/mAP50-95(P)", ] def mean_results(self): """Return the mean results of box and pose.""" return self.box.mean_results() + self.pose.mean_results() def class_result(self, i): """Return the class-wise detection results for a specific class i.""" return self.box.class_result(i) + self.pose.class_result(i) @property def maps(self): """Returns the mean average precision (mAP) per class for both box and pose detections.""" return self.box.maps + self.pose.maps @property def fitness(self): """Computes classification metrics and speed using the `targets` and `pred` inputs.""" return self.pose.fitness() + self.box.fitness() @property def curves(self): """Returns a list of curves for accessing specific metrics curves.""" return [ "Precision-Recall(B)", "F1-Confidence(B)", "Precision-Confidence(B)", "Recall-Confidence(B)", "Precision-Recall(P)", "F1-Confidence(P)", "Precision-Confidence(P)", "Recall-Confidence(P)", ] @property def curves_results(self): """Returns dictionary of computed performance metrics and statistics.""" return self.box.curves_results + self.pose.curves_results class ClassifyMetrics(SimpleClass): """ Class for computing classification metrics including top-1 and top-5 accuracy. Attributes: top1 (float): The top-1 accuracy. top5 (float): The top-5 accuracy. speed (Dict[str, float]): A dictionary containing the time taken for each step in the pipeline. Properties: fitness (float): The fitness of the model, which is equal to top-5 accuracy. results_dict (Dict[str, Union[float, str]]): A dictionary containing the classification metrics and fitness. keys (List[str]): A list of keys for the results_dict. Methods: process(targets, pred): Processes the targets and predictions to compute classification metrics. """ def __init__(self) -> None: """Initialize a ClassifyMetrics instance.""" self.top1 = 0 self.top5 = 0 self.speed = {"preprocess": 0.0, "inference": 0.0, "loss": 0.0, "postprocess": 0.0} self.task = "classify" def process(self, targets, pred): """Target classes and predicted classes.""" pred, targets = torch.cat(pred), torch.cat(targets) correct = (targets[:, None] == pred).float() acc = torch.stack((correct[:, 0], correct.max(1).values), dim=1) # (top1, top5) accuracy self.top1, self.top5 = acc.mean(0).tolist() @property def fitness(self): """Returns mean of top-1 and top-5 accuracies as fitness score.""" return (self.top1 + self.top5) / 2 @property def results_dict(self): """Returns a dictionary with model's performance metrics and fitness score.""" return dict(zip(self.keys + ["fitness"], [self.top1, self.top5, self.fitness])) @property def keys(self): """Returns a list of keys for the results_dict property.""" return ["metrics/accuracy_top1", "metrics/accuracy_top5"] @property def curves(self): """Returns a list of curves for accessing specific metrics curves.""" return [] @property def curves_results(self): """Returns a list of curves for accessing specific metrics curves.""" return [] class OBBMetrics(SimpleClass): def __init__(self, save_dir=Path("."), plot=False, on_plot=None, names=()) -> None: self.save_dir = save_dir self.plot = plot self.on_plot = on_plot self.names = names self.box = Metric() self.speed = {"preprocess": 0.0, "inference": 0.0, "loss": 0.0, "postprocess": 0.0} def process(self, tp, conf, pred_cls, target_cls): """Process predicted results for object detection and update metrics.""" results = ap_per_class( tp, conf, pred_cls, target_cls, plot=self.plot, save_dir=self.save_dir, names=self.names, on_plot=self.on_plot, )[2:] self.box.nc = len(self.names) self.box.update(results) @property def keys(self): """Returns a list of keys for accessing specific metrics.""" return ["metrics/precision(B)", "metrics/recall(B)", "metrics/mAP50(B)", "metrics/mAP50-95(B)"] def mean_results(self): """Calculate mean of detected objects & return precision, recall, mAP50, and mAP50-95.""" return self.box.mean_results() def class_result(self, i): """Return the result of evaluating the performance of an object detection model on a specific class.""" return self.box.class_result(i) @property def maps(self): """Returns mean Average Precision (mAP) scores per class.""" return self.box.maps @property def fitness(self): """Returns the fitness of box object.""" return self.box.fitness() @property def ap_class_index(self): """Returns the average precision index per class.""" return self.box.ap_class_index @property def results_dict(self): """Returns dictionary of computed performance metrics and statistics.""" return dict(zip(self.keys + ["fitness"], self.mean_results() + [self.fitness])) @property def curves(self): """Returns a list of curves for accessing specific metrics curves.""" return [] @property def curves_results(self): """Returns a list of curves for accessing specific metrics curves.""" return [] AI生成项目 python 运行 将上面原始代码中的def bbox_iou那一段代码替换成以下代码 class WIoU_Scale: ''' monotonous: { None: origin v1 True: monotonic FM v2 False: non-monotonic FM v3 } momentum: The momentum of running mean''' iou_mean = 1. monotonous = False _momentum = 1 - 0.5 ** (1 / 7000) _is_train = True def __init__(self, iou): self.iou = iou self._update(self) @classmethod def _update(cls, self): if cls._is_train: cls.iou_mean = (1 - cls._momentum) * cls.iou_mean + \ cls._momentum * self.iou.detach().mean().item() @classmethod def _scaled_loss(cls, self, gamma=1.9, delta=3): if isinstance(self.monotonous, bool): if self.monotonous: return (self.iou.detach() / self.iou_mean).sqrt() else: beta = self.iou.detach() / self.iou_mean alpha = delta * torch.pow(gamma, beta - delta) return beta / alpha return 1 def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, EIoU=False, WIoU=False, Focal=False, alpha=1, gamma=0.5, scale=False, eps=1e-7): # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4) # Get the coordinates of bounding boxes if xywh: # transform from xywh to xyxy (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2 b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_ b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_ else: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1) b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1) w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps) w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps) # Intersection area inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * \ (b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)).clamp(0) # Union Area union = w1 * h1 + w2 * h2 - inter + eps if scale: self = WIoU_Scale(1 - (inter / union)) # IoU # iou = inter / union # ori iou iou = torch.pow(inter/(union + eps), alpha) # alpha iou if CIoU or DIoU or GIoU or EIoU or SIoU or WIoU: cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height if CIoU or DIoU or EIoU or SIoU or WIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal squared rho2 = (((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4) ** alpha # center dist ** 2 if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi ** 2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2) with torch.no_grad(): alpha_ciou = v / (v - iou + (1 + eps)) if Focal: return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)), torch.pow(inter/(union + eps), gamma) # Focal_CIoU else: return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU elif EIoU: rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2 rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2 cw2 = torch.pow(cw ** 2 + eps, alpha) ch2 = torch.pow(ch ** 2 + eps, alpha) if Focal: return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2), torch.pow(inter/(union + eps), gamma) # Focal_EIou else: return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2) # EIou elif SIoU: # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + eps s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + eps sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5) sin_alpha_1 = torch.abs(s_cw) / sigma sin_alpha_2 = torch.abs(s_ch) / sigma threshold = pow(2, 0.5) / 2 sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1) angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2) rho_x = (s_cw / cw) ** 2 rho_y = (s_ch / ch) ** 2 gamma = angle_cost - 2 distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y) omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2) omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2) shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4) if Focal: return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha), torch.pow(inter/(union + eps), gamma) # Focal_SIou else: return iou - torch.pow(0.5 * (distance_cost + shape_cost) + eps, alpha) # SIou elif WIoU: if Focal: raise RuntimeError("WIoU do not support Focal.") elif scale: return getattr(WIoU_Scale, '_scaled_loss')(self), (1 - iou) * torch.exp((rho2 / c2)), iou # WIoU https://arxiv.org/abs/2301.10051 else: return iou, torch.exp((rho2 / c2)) # WIoU v1 if Focal: return iou - rho2 / c2, torch.pow(inter/(union + eps), gamma) # Focal_DIoU else: return iou - rho2 / c2 # DIoU c_area = cw * ch + eps # convex area if Focal: return iou - torch.pow((c_area - union) / c_area + eps, alpha), torch.pow(inter/(union + eps), gamma) # Focal_GIoU https://arxiv.org/pdf/1902.09630.pdf else: return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU https://arxiv.org/pdf/1902.09630.pdf if Focal: return iou, torch.pow(inter/(union + eps), gamma) # Focal_IoU else: return iou # IoU AI生成项目 python 运行 在替换的代码中: monotonous =None:表示Wise-IoU v1 monotonous =True:表示Wise-IoU v2 monotonous =False:表示Wise-IoU v3(我使用的是第三种) 然后修改loss.py,这里有两处需要修改的地方。 第一处,找到 iou = bbox_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask], xywh=False, CIoU=True) AI生成项目 python 运行 将其替换成 iou = bbox_iou(pred_bboxes[fg_mask], target_bboxes[fg_mask], xywh=False, WIoU=True, scale=True) AI生成项目 python 运行 ❗️注意:scale需要设置为True,它是wiou中的一个缩放参数 第二处,找到 loss_iou = ((1.0 - iou) * weight).sum() / target_scores_sum AI生成项目 python 运行 将其替换成 if type(iou) is tuple: if len(iou) == 2: loss_iou = ((1.0 - iou[0]) * iou[1].detach() * weight).sum() / target_scores_sum else: loss_iou = (iou[0] * iou[1] * weight).sum() / target_scores_sum else: loss_iou = ((1.0 - iou) * weight).sum() / target_scores_sum 我进行了这些修改,帮我看看box_loss2轮后在上升是为什么呢
07-29
function [B,FB] = fillmissing(A,fillMethod,varargin) %FILLMISSING Fill missing entries % First argument must be numeric, datetime, duration, calendarDuration, % string, categorical, character array, cell array of character vectors, % a table, or a timetable. % Standard missing data is defined as: % NaN - for double and single floating-point arrays % NaN - for duration and calendarDuration arrays % NaT - for datetime arrays % <missing> - for string arrays % <undefined> - for categorical arrays % empty character {''} - for cell arrays of character vectors % % B = FILLMISSING(A,'constant',C) fills missing entries in A with the % constant scalar value C. You can also use a vector C to specify % different fill constants for each column (or table variable) in A: C(i) % represents the fill constant used for the i-th column of A. For tables % A, C can also be a cell containing fill constants of different types. % % B = FILLMISSING(A,INTERP) fills standard missing entries using the % interpolation method specified by INTERP, which must be: % 'previous' - Previous non-missing entry. % 'next' - Next non-missing entry. % 'nearest' - Nearest non-missing entry. % 'linear' - Linear interpolation of non-missing entries. % 'spline' - Piecewise cubic spline interpolation. % 'pchip' - Shape-preserving piecewise cubic spline interpolation. % 'makima' - modified Akima cubic interpolation. % % B = FILLMISSING(A,MOV,K) fills standard missing entries using a % centered moving window formed from neighboring non-missing entries. % K specifies the window length and must be a positive integer scalar. % MOV specifies the moving window method, which must be: % 'movmean' - Moving average of neighboring non-missing entries. % 'movmedian' - Moving median of neighboring non-missing entries. % % B = FILLMISSING(A,MOV,[NB NF]) uses a moving window defined by the % previous NB elements, the current element, and the next NF elements. % % B = FILLMISSING(A,'knn') fills standard missing entries with the % corresponding element from the nearest neighbor row, calculated based % on the Euclidean distance between rows. % % B = FILLMISSING(A,'knn',k) fills standard missing entries with the % mean of the corresponding entries in the k-nearest neighbor rows, % calculated based on the Euclidean distance between rows. % % B = FILLMISSING(A,fillfun,K) fills standard missing entries using the % function handle fillfun and a centered fixed window formed from % neighboring non-missing entries. K specifies the window length and must % be a positive scalar. The function handle fillfun requires % three input arguments, (xs, ts, tq), which are vectors containing the % sample data xs of length K, the sample data locations ts of length K, % and the missing data locations tq. The vectors ts and tq are subsets of % the 'SamplePoints' vector. The output of fillfun must be either a % scalar or a vector with the same length as tq. % % B = FILLMISSING(A,fillfun,[NB NF]) uses a fixed window defined by the % NB elements before a gap of missing values and the NF elements after % the gap when specifying a function handle fillfun. % % Optional arguments: % % B = FILLMISSING(A,METHOD,...,'MissingLocations',M) specifies the % missing data locations. Elements of M that are true indicate missing % data in the corresponding elements of A. % % B = FILLMISSING(A,METHOD,...,'EndValues',E) also specifies how to % extrapolate leading and trailing missing values. E must be: % 'extrap' - (default) Use METHOD to also extrapolate missing data. % 'previous' - Previous non-missing entry. % 'next' - Next non-missing entry. % 'nearest' - Nearest non-missing entry. % 'none' - No extrapolation of missing values. % VALUE - Use an extrapolation constant. VALUE must be a scalar % or a vector of type numeric, duration, or datetime. % 'EndValues' is not supported for the 'knn' method. % % B = FILLMISSING(A,METHOD,...,'SamplePoints',X) also specifies the % sample points X used by the fill method. X must be a floating-point, % duration, or datetime vector. If the first input A is a table, X can % also specify a table variable in A. X must be sorted and contain unique % points. You can use X to specify time stamps for the data. By default, % FILLMISSING uses data sampled uniformly at points X = [1 2 3 ... ]. Not % supported for the 'knn' method. % % B = FILLMISSING(A,...,'MaxGap',G) specifies a maximum gap size to fill. % Gaps larger than G will not be filled. A gap is a set of consecutive % missing data points whose size is the distance between the known values % at the ends of the gap. Here, distance is relative to the Sample % Points. Not supported for the 'knn' method. % % B = FILLMISSING(A,'knn',...,'Distance',D) specifies the distance metric % used to calculate the nearest neighbors. D must be: % 'euclidean' - (default) Euclidean distance % 'seuclidean' - Scaled Euclidean distance % function handle - A distance function % A distance function must accept two inputs: a 2xn matrix, table, or % timetable containing two vectors to be compared, and a 2xn logical % matrix indicating the locations of missing values in the vectors. It % must return the distance as a real, scalar double. % % B = FILLMISSING(A,METHOD,DIM,...) also specifies a dimension DIM to % operate along. A must be an array. % % [B,FB] = FILLMISSING(A,...) also returns a logical array FB indicating % the filled entries in B that were previously missing. FB has the same % size as B. % % Arguments supported only for table inputs: % % B = FILLMISSING(A,...,'DataVariables',DV) fills missing data only in % the table variables specified by DV. The default is all table variables % in A. DV must be a table variable name, a cell array of table variable % names, a vector of table variable indices, a logical vector, a function % handle that returns a logical scalar (such as @isnumeric), or a table % vartype subscript. Output table B has the same size as input table A. % % B = FILLMISSING(...,'ReplaceValues',TF) specifies how the filled data % is returned. TF must be one of the following: % true - (default) replace table variables with the filled data % false - append the filled data as additional table variables % % Examples: % % % Linear interpolation of NaN entries % a = [NaN 1 2 NaN 4 NaN] % b = fillmissing(a,'linear') % % % Quadratic fitting using a custom function handle % t = linspace(0,1,10); % a = sin(2*pi*t); a(a > 0.7 | a < -0.7) = NaN % fn = @(xs, ts, tq) polyval(polyfit(ts, xs, 2), tq) % b = fillmissing(a, fn, [2 2]) % % % Fill leading and trailing NaN entries with their nearest neighbors % a = [NaN 1 2 NaN 4 NaN] % b = fillmissing(a,'linear','EndValues','nearest') % % % Fill NaN entries with their previous neighbors (zero-order-hold) % A = [1000 1 -10; NaN 1 NaN; NaN 1 NaN; -1 77 5; NaN(1,3)] % B = fillmissing(A,'previous') % % % Fill NaN entries with the mean of each column % A = [NaN(1,3); 13 1 -20; NaN(4,1) (1:4)' NaN(4,1); -1 7 -10; NaN(1,3)] % C = mean(A,'omitnan'); % B = fillmissing(A,'constant',C) % % % Linear interpolation of NaN entries for non-uniformly spaced data % x = [linspace(-3,1,120) linspace(1.1,7,30)]; % a = exp(-0.1*x).*sin(2*x); a(a > -0.2 & a < 0.2) = NaN; % [b,id] = fillmissing(a,'linear','SamplePoints',x); % plot(x,a,'.', x(id),b(id),'o') % title('''linear'' fill') % xlabel('Sample points x'); % legend('original data','filled missing data') % % % Fill missing entries in tables with their previous neighbors % temperature = [21.1 21.5 NaN 23.1 25.7 24.1 25.3 NaN 24.1 25.5]'; % windSpeed = [12.9 13.3 12.1 13.5 10.9 NaN NaN 12.2 10.8 17.1]'; % windDirection = categorical({'W' 'SW' 'SW' '' 'SW' 'S' ... % 'S' 'SW' 'SW' 'SW'})'; % conditions = {'PTCLDY' '' '' 'PTCLDY' 'FAIR' 'CLEAR' ... % 'CLEAR' 'FAIR' 'PTCLDY' 'MOSUNNY'}'; % T = table(temperature,windSpeed,windDirection,conditions) % U = fillmissing(T,'previous') % % % Fill NaN entries with the corresponding entry from the most similar % % row (based on the Euclidean distance between rows): % A = [1 NaN 3 2; 7 2 3 2; NaN 1 3 2; 1 3 2 2] % B = fillmissing(A,'knn') % % % Fill NaN entries with the corresponding entry from the most similar % % row (based on the city block distance, ignoring all NaNs): % A = [1 NaN 3 2; 7 2 3 2; NaN 1 3 2; 1 3 2 2] % cityBlockDist = @(x,~) sum(abs(diff(x)),'omitmissing'); % B = fillmissing(A,'knn','Distance',cityBlockDist) % % % Fill only the entries specified by the logical mask % a = [1 NaN 3 4 5] % mask = [false false false true false] % fillmissing(a,'constant',10,'MissingLocations',mask) % % % Fill missing entries only in gaps less than or equal to 3 % a = [20 NaN NaN NaN NaN 10 8 NaN NaN 2] % b = fillmissing(a,'linear','MaxGap',3) % % See also ISMISSING, STANDARDIZEMISSING, RMMISSING, ISNAN, ISNAT % ISOUTLIER, FILLMISSING2, FILLOUTLIERS, RMOUTLIERS, SMOOTHDATA % Copyright 2015-2023 The MathWorks, Inc. [A,AisTable,intM,intConstOrWindowSizeOrK,extM,x,dim,dataVars,ma,maxgap,replace,distance] = parseInputs(A,fillMethod,varargin{:}); if strcmp(intM,'knn') [B,FB] = knnFill(A,AisTable,intConstOrWindowSizeOrK,dataVars,ma,distance,dim,replace); return end if ~AisTable [intConstOrWindowSizeOrK,extM] = checkArrayType(A,intM,intConstOrWindowSizeOrK,extM,x,false,ma); if nargout < 2 B = fillArray(A,intM,intConstOrWindowSizeOrK,extM,x,dim,false,ma,maxgap); else [B,FB] = fillArray(A,intM,intConstOrWindowSizeOrK,extM,x,dim,false,ma,maxgap); end else if nargout < 2 B = fillTable(A,intM,intConstOrWindowSizeOrK,extM,x,dataVars,ma,maxgap,replace); else [B,FB] = fillTable(A,intM,intConstOrWindowSizeOrK,extM,x,dataVars,ma,maxgap,replace); end end end %-------------------------------------------------------------------------- function [B,FA] = fillTable(A,intMethod,intConst,extMethod,x,dataVars,ma,maxgap,replace) % Fill table according to DataVariables if replace B = A; else B = A(:,dataVars); dataVars = 1:width(B); end if nargout > 1 FA = false(size(B)); end useJthFillConstant = strcmp(intMethod,'constant') && ~isscalar(intConst) && ~ischar(intConst); useJthExtrapConstant = ~ischar(extMethod) && ~isscalar(extMethod); indVj = 1; if istabular(ma) % Convert names in cell arrays to string to allow direct dot index % Need to get the actual names to allow random order of % variable names in tabular MissingLocations tnames = string(A.Properties.VariableNames); end for vj = dataVars if isempty(ma) mavj = ma; % Need to call ismissing else % 'MissingLocations' provided if istabular(ma) name = tnames(vj); mavj = ma.(name); else mavj = ma(:,vj); end end if nargout < 2 B.(vj) = fillTableVar(indVj,B.(vj),intMethod,intConst,extMethod,x,useJthFillConstant,useJthExtrapConstant,mavj,maxgap,B,vj); else [B.(vj),FA(:,vj)] = fillTableVar(indVj,B.(vj),intMethod,intConst,extMethod,x,useJthFillConstant,useJthExtrapConstant,mavj,maxgap,B,vj); end indVj = indVj+1; end if ~replace % alternate FA output: % FA = array2table(FA); % FA.Properties.VariableNames = B.Properties.VariableNames; B = matlab.internal.math.appendDataVariables(A,B,"filled"); if nargout > 1 FA = [false(size(A)) FA]; end end end % fillTable %-------------------------------------------------------------------------- function [Bvj,FAvj] = fillTableVar(indVj,Avj,intMethod,intConst,extMethod,x,useJthFillConstant,useJthExtrapConstant,ma,maxgap,A,vj) % Fill each table variable intConstVj = intConst; extMethodVj = extMethod; if useJthFillConstant intConstVj = intConst(indVj); end if iscell(intConstVj) intConstVj = checkConstantsSize(Avj,false,true,intConstVj{1},1,[],''); end if useJthExtrapConstant extMethodVj = extMethod(indVj); end % Validate types of array and fill constants [intConstVj,extMethodVj] = checkArrayType(Avj,intMethod,intConstVj,extMethodVj,x,true,ma,A,vj); % Treat row in a char table variable as a string AisCharTableVar = ischar(Avj); if AisCharTableVar AvjCharInit = Avj; Avj = matlab.internal.math.charRows2string(Avj); if strcmp(intMethod,'constant') intConstVj = matlab.internal.math.charRows2string(intConstVj,true); end end % Fill if nargout < 2 Bvj = fillArray(Avj,intMethod,intConstVj,extMethodVj,x,1,true,ma,maxgap); else [Bvj,FAvj] = fillArray(Avj,intMethod,intConstVj,extMethodVj,x,1,true,ma,maxgap); end % Convert back to char table variable if AisCharTableVar if all(ismissing(Avj(:))) % For completely blank char table variables, force B to equal A Bvj = AvjCharInit; else Bvj = matlab.internal.math.string2charRows(Bvj); end end end % fillTableVar %-------------------------------------------------------------------------- function [B,FA] = fillArray(A,intMethod,intConstOrWindowSizeOrK,extMethod,x,dim,AisTableVar,ma,maxgap) % Perform FILLMISSING of standard missing entries in an array A B = A; didIsmissing = isempty(ma); if didIsmissing FA = ismissing(A); else % 'MissingLocations' provided if AisTableVar FA = repmat(ma,1,prod(size(A,2:ndims(A)))); else FA = ma; end end ndimsBin = ndims(B); % Quick return if ~AisTableVar && dim > ndimsBin && ~isa(intMethod,'function_handle') if isnumeric(B) && ~isreal(B) B(true(size(B))) = B; end if ~isfinite(maxgap) if nargout < 2 B = extrapolateWithConstant(B,intMethod,intConstOrWindowSizeOrK,extMethod,FA,FA); else [B,FA] = extrapolateWithConstant(B,intMethod,intConstOrWindowSizeOrK,extMethod,FA,FA); end end % else consider the gap too large, don't fill return end % Permute and reshape into a matrix permNeeded = dim ~= 1 || ndimsBin > 2; if permNeeded dim = min(ndimsBin + 1, dim); % all dim > ndimsBin behave the same way, this avoids errors for arbitrarily large dim perm = [dim, 1:(dim-1), (dim+1):ndimsBin]; sizeBperm = size(B, perm); ncolsB = prod(sizeBperm(2:end)); nrowsB = sizeBperm(1); B = reshape(permute(B, perm),[nrowsB, ncolsB]); % permute errors expectedly for ND sparse matrix FA = reshape(permute(FA, perm),[nrowsB, ncolsB]); else ncolsB = size(B,2); end % Fill each column if didIsmissing || nargout < 2 % For ismissing, compute the filled mask at the very end. This ensures % that tall/fillmissing takes 2 passes instead of 3 for two outputs. for jj = 1:ncolsB B(:,jj) = fillArrayColumn(jj,B(:,jj),FA(:,jj),intMethod,intConstOrWindowSizeOrK,extMethod,x,maxgap,didIsmissing); end else % For 'MissingLocations', also compute the filled mask for jj = 1:ncolsB [B(:,jj),FA(:,jj)] = fillArrayColumn(jj,B(:,jj),FA(:,jj),intMethod,intConstOrWindowSizeOrK,extMethod,x,maxgap,didIsmissing); end end % Reshape and permute back to original size if AisTableVar && nargout > 1 FA = any(FA,2); if didIsmissing FA = xor(FA,any(ismissing(B),2)); % Compute the filled mask end end if permNeeded B = ipermute(reshape(B,sizeBperm), perm); end if ~AisTableVar && nargout > 1 if permNeeded FA = ipermute(reshape(FA,sizeBperm), perm); end if didIsmissing FA(FA) = xor(FA(FA),ismissing(B(FA))); % Compute the filled mask end end end % fillArray %-------------------------------------------------------------------------- function [b,ma] = fillArrayColumn(jj,a,ma,intMethod,intConstOrWindowSizeOrK,extMethod,x,maxgap,didIsmissing) % Fill one column. Do not error if we cannot fill all missing entries. % jj = j-th column numeric index. Used to select the j-th fill constant. % a = the j-th column itself. Can be numeric, logical, duration, datetime, % calendarDuration, char, string, cellstr, or categorical. % ma = logical mask of missing entries found in a. % intMethod = interpolation method. % intConstOrWindowSizeOrK = interpolation constant for 'constant' or window size % for 'movmean'. [] if intMethod is not 'constant'/'mov*'. % extMethod = extrap method. If not a char, it holds the extrap constant. % x = the abscissa ('SamplePoints'). Can be float, duration, or datetime. b = a; % Quick return nma = ~ma; numNonMissing = nnz(nma); useDefaultX = isempty(x); spFlag = ~useDefaultX; % whether sample points are used % Default sample points only need to be generated when MaxGap is used, the % input data is non-numeric, or the method is a function handle or an % interpolation method that uses sample points. Note that "knn" also needs % default sample points, but does not use this function. if useDefaultX && (~isnumeric(a) || isfinite(maxgap) || ... isa(intMethod,'function_handle') || ... ~matches(intMethod,["constant","next","previous","movmean","movmedian"])) x = (1:size(a,1)).'; end if numNonMissing == 0 % Column is full of missing data: if ~isfinite(maxgap) % Fill with constant if nargout > 1 if isa(intMethod,'function_handle') && strcmp(extMethod,'extrap') b = handlefill(b,ma,intMethod,intConstOrWindowSizeOrK,spFlag,x); ma = ~ismissing(b); else [b,ma] = extrapolateWithConstant(b,intMethod,intConstOrWindowSizeOrK,extMethod,ma,jj); end else if isa(intMethod,'function_handle') && strcmp(extMethod,'extrap') b = handlefill(b,ma,intMethod,intConstOrWindowSizeOrK,spFlag,x); else b = extrapolateWithConstant(b,intMethod,intConstOrWindowSizeOrK,extMethod,ma,jj); end end end % else, column is a "large gap": do not fill return end % Ignore gaps of missing data bigger than maxgap ma = removeLargeGaps(ma,maxgap,x); maBeforeInterp = ma; % (1) Interpolate if issparse(b) b = full(b); end if strcmp(intMethod,'constant') b = assignConstant(b,intConstOrWindowSizeOrK,ma,jj); elseif strcmp(intMethod,'movmean') if didIsmissing if useDefaultX newb = movmean(b,intConstOrWindowSizeOrK,'omitnan'); else newb = movmean(b,intConstOrWindowSizeOrK,'omitnan','SamplePoints',x); end b(ma) = newb(ma); else % 'MissingLocations' case b(ma) = missing; if useDefaultX newb = movmean(b,intConstOrWindowSizeOrK,'omitnan'); else newb = movmean(b,intConstOrWindowSizeOrK,'omitnan','SamplePoints',x); end b(ma) = newb(ma); ma(ma) = xor(ma(ma),ismissing(b(ma))); end elseif strcmp(intMethod,'movmedian') if didIsmissing if useDefaultX newb = movmedian(b,intConstOrWindowSizeOrK,'omitnan'); else newb = movmedian(b,intConstOrWindowSizeOrK,'omitnan','SamplePoints',x); end b(ma) = newb(ma); else % 'MissingLocations' case b(ma) = missing; if useDefaultX newb = movmedian(b,intConstOrWindowSizeOrK,'omitnan'); else newb = movmedian(b,intConstOrWindowSizeOrK,'omitnan','SamplePoints',x); end b(ma) = newb(ma); ma(ma) = xor(ma(ma),ismissing(b(ma))); end elseif isnumeric(b) && strcmp(intMethod,'next') if numNonMissing > 1 b = fillWithNext(b,ma); end elseif isnumeric(b) && strcmp(intMethod,'previous') if numNonMissing > 1 b = fillWithPrevious(b,ma); end elseif ~isa(intMethod,'function_handle') % function handle case handled below % griddedInterpolant/interp1 require at least 2 grid points. % Do not error if we cannot fill. Instead, return the original array. % For example, fillmissing([NaN 1 NaN],'linear') returns [NaN 1 NaN]. if numNonMissing > 1 isfloatb = isfloat(b); if isfloatb && isfloat(x) G = griddedInterpolant(x(nma),b(nma),intMethod); b(ma) = G(x(ma)); % faster than interp1 elseif isfloatb || isduration(b) || isdatetime(b) b(ma) = interp1(x(nma),b(nma),x(ma),intMethod,'extrap'); else % calendarDuration, char, string, cellstr, or categorical: % No griddedInterpolant because x may be datetime/duration vq = interp1(x(nma),find(nma),x(ma),intMethod,'extrap'); indvq = ~isnan(vq); % vq may have leading or trailing NaN iatmp = find(ma); b(iatmp(indvq)) = b(vq(indvq)); % copy non-missing to missing end end end % (2) Correct for EndValues, including the logical mask of what got filled % use ma to find non-missing for correct maxgap behavior % ma has at least one false, all-missing case was quick returned if maBeforeInterp(1) indBeg = find(~maBeforeInterp,1); else indBeg = 1; end if maBeforeInterp(end) indEnd = find(~maBeforeInterp,1,'last'); else indEnd = numel(a); end if indBeg > 1 || indEnd < numel(a) if ischar(extMethod) || (isstring(extMethod) && isscalar(extMethod)) if strcmp(extMethod,'none') b(1:indBeg-1) = a(1:indBeg-1); b(indEnd+1:end) = a(indEnd+1:end); if nargout > 1 % 'MissingLocations' case ma(1:indBeg-1) = false; ma(indEnd+1:end) = false; end elseif strcmp(extMethod,'nearest') || (strcmp(extMethod,'extrap') && strcmp(intMethod,'nearest')) b(1:indBeg-1) = a(indBeg); b(indEnd+1:end) = a(indEnd); if nargout > 1 % 'MissingLocations' case ma(1:indBeg-1) = true; ma(indEnd+1:end) = true; end elseif strcmp(extMethod,'previous') || (strcmp(extMethod,'extrap') && strcmp(intMethod,'previous')) b(1:indBeg-1) = a(1:indBeg-1); b(indEnd+1:end) = a(indEnd); if nargout > 1 % 'MissingLocations' case ma(1:indBeg-1) = false; ma(indEnd+1:end) = true; end elseif strcmp(extMethod,'next') || (strcmp(extMethod,'extrap') && strcmp(intMethod,'next')) b(1:indBeg-1) = a(indBeg); b(indEnd+1:end) = a(indEnd+1:end); if nargout > 1 % 'MissingLocations' case ma(1:indBeg-1) = true; ma(indEnd+1:end) = false; end end else % Extrapolate with given value(s) if isscalar(extMethod) b([1:indBeg-1, indEnd+1:end]) = extMethod; elseif ~isa(intMethod,'function_handle') % function handle has separate implementation (directly below) b([1:indBeg-1, indEnd+1:end]) = extMethod(jj); end if nargout > 1 ma([1:indBeg-1, indEnd+1:end]) = true; end end end if isa(intMethod,'function_handle') isExtrap = strcmp(extMethod,'extrap'); if ~isExtrap ma([1:indBeg-1, indEnd+1:end]) = false; end if nargout < 2 % one output case newb = handlefill(b,ma,intMethod,intConstOrWindowSizeOrK,spFlag,x); b(ma) = newb(ma); else % two output case b(ma) = missing; newb = handlefill(b,ma,intMethod,intConstOrWindowSizeOrK,spFlag,x); b(ma) = newb(ma); ma(ma) = xor(ma(ma),ismissing(b(ma))); if ~isExtrap ma([1:indBeg-1, indEnd+1:end]) = true; end end end end % fillArrayColumn %-------------------------------------------------------------------------- function MItoBeFilled = removeLargeGaps(MI,maxgap,x) % set elements in the given missing indicator within large gaps to false % MI is a vector, maxgap is either numeric or duration scalar MItoBeFilled = MI; % x has at least 1 element, empties are already special cased if ~isfinite(maxgap) % no gaps will be too large to fill, don't change MI return end % find all segments in the missing indicator vector segmentLengths = diff([0; find(diff(MI(:))); numel(MI)]); % gaps span x_j to x_k k = cumsum(segmentLengths); j = k - segmentLengths + 1; % The gap size is defined as x_(k+1)-x_(j-1) % If the segment is at the end of a vector, we use the nearest sample point x = [x(1); x(:); x(end)]; % for this x, the size of a gap is x_(k+2)-x_(j) for idx =1:numel(segmentLengths) % only act on segments of missing data if MI(j(idx)) % check to see if the segment is small enough to fill doFill = x(j(idx)) + maxgap >= x(k(idx)+2); if ~doFill % if it is too large, don't fill, i.e. treat as nonmissing MItoBeFilled(j(idx):k(idx)) = false; end end end end % removeLargeGaps %-------------------------------------------------------------------------- function [B,FA] = extrapolateWithConstant(B,intMethod,intConst,extMethod,lhsIndex,rhsIndex) % Fill all missings with a constant. Used if B is full of missing data, or % for array B with dim > ndims(B). rhsIndex may be logical or numeric. % Fill only when we have specified an extrapolation constant: if nargout > 1 FA = lhsIndex; end if ~ischar(extMethod) && ~(isstring(extMethod) && isscalar(extMethod)) % Either through EndValues: % fillmissing(A,METHOD,'EndValues',ConstVals) B = assignConstant(B,extMethod,lhsIndex,rhsIndex); elseif strcmp(intMethod,'constant') && strcmp(extMethod,'extrap') % Or through the 'constant' fill method: % fillmissing(A,'constant',ConstVals) % fillmissing(A,'constant',ConstVals,'EndValues','extrap') B = assignConstant(B,intConst,lhsIndex,rhsIndex); elseif nargout > 1 FA(:) = false; end end % extrapolateWithConstant %-------------------------------------------------------------------------- function B = assignConstant(B,ConstVals,lhsIndex,rhsIndex) if isscalar(ConstVals) B(lhsIndex) = ConstVals; else B(lhsIndex) = ConstVals(rhsIndex); end end %-------------------------------------------------------------------------- function [A,AisTable,intMethod,intConstOrWindowSizeOrK,extMethod,x,dim,dataVars,ma,maxgap,replace,distance] = ... parseInputs(A,fillMethod,varargin) % Parse FILLMISSING inputs AisTable = istabular(A); if ~AisTable && ~isSupportedArray(A) error(message('MATLAB:fillmissing:FirstInputInvalid')); end % Parse fill method. Empty '' or [] fill method is not allowed. validIntMethods = {'constant','previous','next','nearest','linear',... 'spline','pchip','movmean','movmedian','makima','knn'}; if ischar(fillMethod) || isstring(fillMethod) indIntMethod = matlab.internal.math.checkInputName(fillMethod,validIntMethods); if sum(indIntMethod) ~= 1 % Also catch ambiguities for fillmissing(A,'ne') and fillmissing(A,'p') error(message('MATLAB:fillmissing:MethodInvalid')); end intMethod = validIntMethods{indIntMethod}; indIntMethod = find(indIntMethod); if indIntMethod == 11 && ~ismatrix(A) % tables and timetables return TRUE for ismatrix error(message('MATLAB:fillmissing:knnMustBeMatrixTableOrTimetable')) end intConstOrWindowSizeOrK = []; % Parse fillmissing(A,'constant',c) and fillmissing(A,MOVFUN,windowSize) intConstOffset = 0; if any(indIntMethod == [1 8 9]) if nargin > 2 intConstOrWindowSizeOrK = varargin{1}; else error(message(['MATLAB:fillmissing:',intMethod,'Input'])); end intConstOffset = 1; elseif indIntMethod == 11 if nargin > 2 && isnumeric(varargin{1}) intConstOrWindowSizeOrK = varargin{1}; intConstOffset = 1; else intConstOrWindowSizeOrK = 1; end end elseif isa(fillMethod,'function_handle') if nargin(fillMethod) < 3 error(message('MATLAB:fillmissing:FunctionHandleNumberOfArguments')); end intMethod = fillMethod; intConstOffset = 1; indIntMethod = []; if nargin < 3 error(message('MATLAB:fillmissing:FunctionHandleInput')); end intConstOrWindowSizeOrK = varargin{1}; else error(message('MATLAB:fillmissing:MethodInvalid')); end % Parse optional inputs extMethod = 'extrap'; x = []; ma = []; maxgap = []; dataVarsProvided = false; missingLocationProvided = false; replace = true; distance = 'euclidean'; if ~AisTable dim = matlab.internal.math.firstNonSingletonDim(A); dataVars = []; % not supported for arrays else dim = 1; % Fill each table variable separately dataVars = 1:width(A); end if nargin > 2+intConstOffset % Third input can be a constant, a window size, the dimension, or an % argument Name from a Name-Value pair: % fillmissing(A,'constant',C,...) and C may be a char itself % fillmissing(A,'movmean',K,...) with K numeric, numel(K) == 1 or 2 % fillmissing(A,'linear',DIM,...) % fillmissing(A,'linear','EndValues',...) firstOptionalInput = varargin{1+intConstOffset}; % The dimension dimOffset = 0; if isnumeric(firstOptionalInput) || islogical(firstOptionalInput) if AisTable error(message('MATLAB:fillmissing:DimensionTable')); end dimOffset = 1; dim = firstOptionalInput; if ~isscalar(dim) || ~isreal(dim) || fix(dim) ~= dim || dim < 1 || ~isfinite(dim) error(message('MATLAB:fillmissing:DimensionInvalid')); end end % Trailing N-V pairs indNV = (1+intConstOffset+dimOffset):numel(varargin); if rem(length(indNV),2) ~= 0 error(message('MATLAB:fillmissing:NameValuePairs')); end spvar = []; for i = indNV(1:2:end) if matlab.internal.math.checkInputName(varargin{i},'EndValues') if indIntMethod == 11 error(message('MATLAB:fillmissing:unsupportedNVPair','EndValues','''knn''')) end extMethod = varargin{i+1}; if ischar(extMethod) || (isstring(extMethod) && isscalar(extMethod)) validExtMethods = {'extrap','previous','next','nearest','none'}; indExtMethod = matlab.internal.math.checkInputName(extMethod,validExtMethods); if sum(indExtMethod) ~= 1 % Also catch ambiguities between nearest and next error(message('MATLAB:fillmissing:EndValuesInvalidMethod')); end extMethod = validExtMethods{indExtMethod}; end elseif matlab.internal.math.checkInputName(varargin{i},'DataVariables') if AisTable dataVars = matlab.internal.math.checkDataVariables(A,varargin{i+1},'fillmissing'); dataVarsProvided = true; else error(message('MATLAB:fillmissing:DataVariablesArray')); end elseif matlab.internal.math.checkInputName(varargin{i},'ReplaceValues') if AisTable replace = matlab.internal.datatypes.validateLogical(varargin{i+1},'ReplaceValues'); else error(message('MATLAB:fillmissing:ReplaceValuesArray')); end elseif matlab.internal.math.checkInputName(varargin{i},'SamplePoints') if indIntMethod == 11 error(message('MATLAB:fillmissing:unsupportedNVPair','SamplePoints','''knn''')); end if istimetable(A) error(message('MATLAB:samplePoints:SamplePointsTimeTable')); end [x,spvar] = matlab.internal.math.checkSamplePoints(varargin{i+1},A,AisTable,false,dim); elseif matlab.internal.math.checkInputName(varargin{i},'MissingLocations',2) ma = varargin{i+1}; missingLocationProvided = true; elseif matlab.internal.math.checkInputName(varargin{i},'MaxGap',2) if indIntMethod == 11 error(message('MATLAB:fillmissing:unsupportedNVPair','MaxGap','knn')) end maxgap = varargin{i+1}; if ~isscalar(maxgap) || ~(isnumeric(maxgap) || isduration(maxgap) || iscalendarduration(maxgap)) ||... ~isreal(maxgap) || isnan(maxgap) || (~iscalendarduration(maxgap) && maxgap <= 0) error(message('MATLAB:fillmissing:MaxGapInvalid')) end elseif matlab.internal.math.checkInputName(varargin{i},'Distance') if indIntMethod ~= 11 error(message('MATLAB:fillmissing:DistanceNonKNNMethod')) end distance = varargin{i+1}; if ischar(distance) || (isstring(distance) && isscalar(distance)) validDistanceMetrics = {'euclidean','seuclidean'}; distMask = matlab.internal.math.checkInputName(distance,validDistanceMetrics); if ~any(distMask) error(message('MATLAB:fillmissing:InvalidDistance')); end distance = validDistanceMetrics{distMask}; elseif ~isa(distance,'function_handle') error(message('MATLAB:fillmissing:InvalidDistance')) end else error(message('MATLAB:fillmissing:NameValueNames')); end end if ~isempty(spvar) dataVars(dataVars == spvar) = []; % remove sample points var from data vars end if missingLocationProvided if istabular(ma) && AisTable dataVars = validateTabularMissingLocations(A,ma,dataVars,dataVarsProvided); else if AisTable sizedv = size(A(:,dataVars)); sizev = size(ma); if ~islogical(ma) || (~isequal(sizedv(2),sizev(2)) && ~isequal(size(A),size(ma))) error(message('MATLAB:fillmissing:MissingLocationsInvalid')); end else if ~islogical(ma) || ~isequal(size(A),size(ma)) error(message('MATLAB:fillmissing:MissingLocationsInvalid')); end end end end % Ensure not both MaxGap and MissingLocations specified if ~isempty(ma) && ~isempty(maxgap) error(message('MATLAB:fillmissing:MaxGapMissingLocations')) end end % Validate fill constants size if indIntMethod == 1 % 'constant' fill method intConstOrWindowSizeOrK = checkConstantsSize(A,AisTable,false,intConstOrWindowSizeOrK,dim,dataVars,''); elseif indIntMethod == 11 % Validate number of nearest neighbors if ~isnumeric(intConstOrWindowSizeOrK) || ~isscalar(intConstOrWindowSizeOrK) || ... fix(intConstOrWindowSizeOrK) ~= intConstOrWindowSizeOrK || ... intConstOrWindowSizeOrK < 1 || ~isreal(intConstOrWindowSizeOrK) error(message('MATLAB:fillmissing:InvalidK')); end end if ~ischar(extMethod) && ~(isstring(extMethod) && isscalar(extMethod)) extMethod = checkConstantsSize(A,AisTable,false,extMethod,dim,dataVars,'Extrap'); end % Default abscissa if isempty(x) && istimetable(A) x = matlab.internal.math.checkSamplePoints(A.Properties.RowTimes,A,false,true,dim); end % Default Sample Points if isa(intMethod,'function_handle') if isempty(x) checkHandleWindow(A,intConstOrWindowSizeOrK,false,1:numel(A)); else checkHandleWindow(A,intConstOrWindowSizeOrK,true,x); end end % Default maxgap/check datatype against abscissa if isempty(maxgap) maxgap = inf; elseif (isnumeric(x) && ~isnumeric(maxgap)) || (~isnumeric(x) && isnumeric(maxgap)) ||... (isduration(x) && iscalendarduration(maxgap)) error(message('MATLAB:fillmissing:MaxGapDurationInvalid')) end end % parseInputs %-------------------------------------------------------------------------- function tf = isSupportedArray(A) % Check if array type is supported tf = isnumeric(A) || islogical(A) || ... isstring(A) || iscategorical(A) || iscellstr(A) || ischar(A) || ... isdatetime(A) || isduration(A) || iscalendarduration(A); end % isSupportedArray %-------------------------------------------------------------------------- function C = checkConstantsSize(A,AisTable,AisTableVar,C,dim,dataVars,eid) % Validate the size of the fill constant. We can fill all columns with the % same scalar, or use a different scalar for each column. if ischar(C) && (~ischar(A) || AisTableVar) % A char fill constant is treated as a scalar for string, categorical % and cellstr (arrays or table variables), and char table variables if ~isrow(C) && ~isempty(C) % '' is not a row error(message('MATLAB:fillmissing:CharRowVector')); end elseif ~isscalar(C) sizeA = size(A); if AisTable % numel(constant) must equal numel 'DataVariables' value sizeA(2) = length(dataVars); end if dim <= ndims(A) sizeA(dim) = []; nVects = prod(sizeA); else % fillmissing(A,'constant',c) supported % fillmissing(A,METHOD,'EndValues',constant_value) supported nVects = numel(A); end if (numel(C) ~= nVects) if nVects <= 1 error(message(['MATLAB:fillmissing:SizeConstantScalar',eid])); else error(message(['MATLAB:fillmissing:SizeConstant',eid],nVects)); end end C = C(:); end end % checkConstantsSize %-------------------------------------------------------------------------- function [intConst,extMethod] = checkArrayType(A,intMethod,intConst,extMethod,x,AisTableVar,ma,T,vj) % Check if array types match if AisTableVar && ~isSupportedArray(A) error(message('MATLAB:fillmissing:UnsupportedTableVariable',class(A))); end if ~(isnumeric(A) || islogical(A) || isduration(A) || isdatetime(A)) && ... ~any(strcmp(intMethod,{'nearest','next','previous','constant'})) && ... ~isa(intMethod,'function_handle') if AisTableVar error(message('MATLAB:fillmissing:InterpolationInvalidTableVariable',intMethod)); else error(message('MATLAB:fillmissing:InterpolationInvalidArray',intMethod,class(A))); end end % 'MissingLocations' doesn't work with all methods for integer and logical if ~isempty(ma) && (isinteger(A) || islogical(A)) && ... ~(any(strcmp(intMethod,{'nearest','next','previous','constant','knn'})) || ... isa(intMethod,'function_handle')) error(message('MATLAB:fillmissing:MissingLocationsInteger')); end try if strcmp(intMethod,'constant') intConst = checkConstantType(A,intConst,''); end if ~ischar(extMethod) && ~(isstring(extMethod) && isscalar(extMethod)) extMethod = checkConstantType(A,extMethod,'Extrap'); end catch ME if AisTableVar && matlab.internal.math.checkInputName('MATLAB:fillmissing:Constant',ME.identifier) % Generic error message for tables varNames = T.Properties.VariableNames; error(message('MATLAB:fillmissing:ConstantInvalidTypeForTableVariable',varNames{vj})); else % Specific error message for arrays throw(ME); end end if isa(x,'single') && (isduration(A) || isdatetime(A)) error(message('MATLAB:samplePoints:SamplePointsSingle')); end end % checkArrayType %-------------------------------------------------------------------------- function C = checkConstantType(A,C,eid) % Check if constant type matches the array type if ~isempty(eid) && ~isnumeric(C) && ~islogical(C) && ... ~isdatetime(C) && ~isduration(C) && ~iscalendarduration(C) error(message('MATLAB:fillmissing:ConstantInvalidTypeExtrap')); end if isnumeric(A) && ~isnumeric(C) && ~islogical(C) error(message(['MATLAB:fillmissing:ConstantNumeric',eid])); elseif isdatetime(A) && ~isdatetime(C) error(message(['MATLAB:fillmissing:ConstantDatetime',eid])); elseif isduration(A) && ~isduration(C) error(message(['MATLAB:fillmissing:ConstantDuration',eid])); elseif iscalendarduration(A) && ~iscalendarduration(C) error(message(['MATLAB:fillmissing:ConstantCalendarDuration',eid])); elseif iscategorical(A) if ischar(C) C = string(C); % make char a scalar string elseif iscategorical(C) && (isordinal(A) ~= isordinal(C)) error(message('MATLAB:fillmissing:ConstantCategoricalOrdMismatch')); elseif iscategorical(C) && isordinal(C) && ~isequal(categories(C),categories(A)) error(message('MATLAB:fillmissing:ConstantCategoricalCatMismatch')); elseif (~iscellstr(C) && ~isstring(C) && ~iscategorical(C)) error(message(['MATLAB:fillmissing:ConstantCategorical',eid])); end elseif ischar(A) && ~ischar(C) error(message(['MATLAB:fillmissing:ConstantChar',eid])); elseif iscellstr(A) if ischar(C) C = {C}; % make char a scalar cellstr elseif ~iscellstr(C) %#ok<ISCLSTR> % string constants not supported error(message(['MATLAB:fillmissing:ConstantCellstr',eid])); end elseif isstring(A) && ~isstring(C) % char and cellstr constants not supported error(message(['MATLAB:fillmissing:ConstantString',eid])); end end % checkConstantType function datavariables = validateTabularMissingLocations(a,loc,datavariables,dataVarsProvided) vnames = loc.Properties.VariableNames; tnames = a.Properties.VariableNames; if dataVarsProvided if ~all(ismember(tnames(datavariables),vnames)) % DataVariable names must be present in loc table error(message('MATLAB:fillmissing:InvalidLocationsWithDataVars')); end else try datavariables = matlab.internal.math.checkDataVariables(a, vnames, 'fillmissing'); catch error(message('MATLAB:fillmissing:InvalidTabularLocationsFirstInput')); end end vnames = string(vnames); for ii=vnames if ~islogical(loc.(ii)) || ~isequal(size(a.(ii)),size(loc.(ii))) error(message('MATLAB:fillmissing:LogicalVarsRequired')); end end end %-------------------------------------------------------------------------- function checkHandleWindow(A,intConstOrWindowSizeOrK,spFlag,t) needDuration = (~spFlag && istimetable(A)) || ... (spFlag && (isduration(t) || isdatetime(t))); if (isduration(intConstOrWindowSizeOrK) || isnumeric(intConstOrWindowSizeOrK)) && ... isreal(intConstOrWindowSizeOrK) && any(numel(intConstOrWindowSizeOrK) == [1 2]) && ... allfinite(intConstOrWindowSizeOrK) && any(intConstOrWindowSizeOrK > 0) && ... all(intConstOrWindowSizeOrK >= 0) if needDuration && ~isduration(intConstOrWindowSizeOrK) error(message('MATLAB:fillmissing:FunctionHandleInvalidWindowDuration')); elseif ~needDuration && isduration(intConstOrWindowSizeOrK) error(message('MATLAB:fillmissing:FunctionHandleInvalidWindow')); end else error(message('MATLAB:fillmissing:FunctionHandleInvalidWindow')); end end % checkHandleWindow %-------------------------------------------------------------------------- function ide = getMissingIntervals(MI) % get the 2-column array of first and last indices of each gap segmentLengths = diff([0; find(diff(MI(:))); numel(MI)]); % This assumes MI is not empty, which cannot happen when this is called k = cumsum(segmentLengths); % last index of each interval alt = MI(k); % which intervals are missing vs non-missing ide = zeros(sum(alt),2); if alt(1) % if the first interval is missing ide(:,2) = k(1:2:end); ide(:,1) = k(1:2:end) - segmentLengths(1:2:end) + 1; elseif numel(alt) >= 2 && alt(2) % if the second interval is missing ide(:,2) = k(2:2:end); ide(:,1) = k(2:2:end) - segmentLengths(2:2:end) + 1; end end % getMissingIntervals %-------------------------------------------------------------------------- function Y = handlefill(A,MI,fillfun,intConstOrWindowSizeOrK,spFlag,t) A = A(:); t = t(:); tidx = 1:numel(t); % Initialize the output Y = A; % Quick return if isempty(MI) return end ide = getMissingIntervals(MI); % array of first and last indices of each gap % Split into left and right window values if numel(intConstOrWindowSizeOrK) == 2 a = intConstOrWindowSizeOrK(1); b = intConstOrWindowSizeOrK(2); elseif ~spFlag a = floor(intConstOrWindowSizeOrK/2); b = a; else a = intConstOrWindowSizeOrK/2; b = a; end % Call fillfun on each interval of missing data skip filling gaps when xin % is empty nide = size(ide,1); for i = 1:nide if spFlag ind = find(((t <= t(ide(i+nide)) + b) & (t > t(ide(i+nide)))) | ... ((t >= t(ide(i)) - a) & (t < t(ide(i))))); else ind = [max(1, ceil(ide(i) - a)):ide(i)-1, ide(i+nide)+1:min(numel(A), floor(ide(i+nide) + b))]; end xin = A(ind); tin = t(ind); toutidx = tidx(ide(i,1):ide(i,2)); tout = t(toutidx); try ytmp = fillfun(xin, tin, tout); catch ME if isempty(xin) m = message('MATLAB:fillmissing:FunctionHandleEmptyInput'); throw(addCause(MException(m.Identifier,'%s',getString(m)),ME)); else throw(ME); end end if isscalar(ytmp) || (isvector(ytmp) && isequal(numel(ytmp),numel(toutidx))) Y(toutidx) = ytmp; else % Bad output size error(message('MATLAB:fillmissing:FunctionHandleInvalidOutputSize')); end end end % handlefill %-------------------------------------------------------------------------- function [B,FB] = knnFill(A,AisTable,k,dataVars,ma,distance,dim,replace) if isempty(ma) ma = ismissing(A); end % Quick return when no filling is needed if isempty(A) || ~any(ma,'all') || dim > 2 if replace B = A; else B = matlab.internal.math.appendDataVariables(A,A(:,dataVars),"filled"); end FB = false(size(B)); return end if ~isa(distance,'function_handle') [B,FB] = knnFillBuiltInDistances(A,AisTable,k,dataVars,ma,distance,dim,replace); else [B,FB] = knnFillCustomDistances(A,AisTable,k,dataVars,ma,distance,dim,replace); end end % knnFill %-------------------------------------------------------------------------- function [B,FB] = knnFillBuiltInDistances(A,AisTable,k,dataVars,ma,distance,dim,replace) if istimetable(A) error(message('MATLAB:fillmissing:DistanceTimetableNotSupported')); end % matlab.internal.math.fillmissingKNN fills using the k-nearest columns, so % A is transposed unless dim == 2 transposeData = dim == 1; if AisTable AT = checkAndExtractTableVars(A,dataVars); ma = ma(:,dataVars).'; elseif transposeData AT = A.'; ma = ma.'; else % dim == 2 AT = A; end if ~isfloat(AT) error(message('MATLAB:fillmissing:DistanceNonFloatsNotSupported')); end if strcmp(distance,'seuclidean') if transposeData && ~AisTable scalingVector = double(std(A,'omitnan')); else scalingVector = double(std(AT,[],2,'omitnan')); end [BOutTmp,FBTmp] = matlab.internal.math.fillmissingKNN(full(AT),full(ma),k,full(scalingVector)); else [BOutTmp,FBTmp] = matlab.internal.math.fillmissingKNN(full(AT),full(ma),k); end if issparse(A) BOutTmp = sparse(BOutTmp); FBTmp = sparse(FBTmp); end if AisTable if replace B = A; BDataVars = dataVars; else B = A(:,dataVars); BDataVars = 1:width(B); end FB = false(size(B)); if ~any(FBTmp,'all') return end % Note that FBTmp and BOutTmp are based on the transpose of the data varsToReplace = find(any(FBTmp,2)).'; for ii = varsToReplace B.(BDataVars(ii)) = cast(BOutTmp(ii,:)',class(B.(ii))); end if replace FB(:,dataVars) = FBTmp'; else B = matlab.internal.math.appendDataVariables(A,B,"filled"); FB = [false(size(A)),FBTmp']; end else if transposeData B = BOutTmp'; FB = FBTmp'; else B = BOutTmp; FB = FBTmp; end end end % knnFillBuiltInDistances %-------------------------------------------------------------------------- function [B,FB] = knnFillCustomDistances(A,AisTable,k,dataVars,ma,distance,dim,replace) if dim == 2 A = A.'; ma = ma'; end if AisTable ADataVars = A(:,dataVars); ma = ma(:,dataVars); else ADataVars = A; dataVars = 1:size(A,2); end if replace B = A; BDataVars = dataVars; else % can only be hit for tables and timetables B = A(:,dataVars); BDataVars = 1:width(B); end if issparse(A) FB = logical(sparse(size(B,1),size(B,2))); else FB = false(size(B)); end numOfVectors = size(A,1); vectorDistances = zeros(numOfVectors,1); % Walk through the rows (vectors) of BIn, calculating nearest neighbors as needed for ii = 1:numOfVectors maCurrentVector = ma(ii,:); if any(maCurrentVector) && ~all(maCurrentVector) % all-missing vectors cannot be filled % Calculate nearest neighbors for jj = 1:numOfVectors if ii ~= jj try vectorDistances(jj) = double(distance(ADataVars([ii,jj],:),ma([ii,jj],:))); catch ME if strcmp(ME.identifier,'MATLAB:invalidConversion') baseException = MException(message('MATLAB:fillmissing:InvalidCustomDistance')); else baseException = MException(message('MATLAB:fillmissing:DistanceCalculationFailed')); end baseException = addCause(baseException, ME); throw(baseException); end else vectorDistances(jj) = NaN; end end if ~isreal(vectorDistances) error(message('MATLAB:fillmissing:InvalidCustomDistance')); end % vectorDistances is sorted before NaNs are removed to preserve indices [sortedDistances,sortedIndices] = sort(vectorDistances); sortedIndices = sortedIndices(~isnan(sortedDistances)); % Don't use NaN distances if ~isempty(sortedIndices) % Only fill if there are non-NaN values to fill with for jj = find(maCurrentVector) kIndices = sortedIndices(~ma(sortedIndices,jj)); % Don't try to fill with a missing value if ~isempty(kIndices) kIndices = kIndices(1:min(numel(kIndices),k)); % Keep the k lowest distances (the k nearest neighbors) % Note that if n > k vectors are the same distance from the % current vector, the k vectors with the smallest index are % used. if isscalar(kIndices) fillValue = ADataVars(kIndices,jj); B(ii,BDataVars(jj)) = fillValue; else if AisTable try fillValue = mean(ADataVars{kIndices,jj},1,'native'); catch ME baseException = MException(message('MATLAB:fillmissing:AggregationFailed')); baseException = addCause(baseException, ME); throw(baseException); end B{ii,BDataVars(jj)} = fillValue; else try fillValue = mean(ADataVars(kIndices,jj),1,'native'); catch ME baseException = MException(message('MATLAB:fillmissing:AggregationFailed')); baseException = addCause(baseException, ME); throw(baseException); end B(ii,jj) = fillValue; % dataVars are unnecessary for the non-tabular case end end FB(ii,BDataVars(jj)) = ~ismissing(fillValue); end end end end end if ~replace B = matlab.internal.math.appendDataVariables(A,B,"filled"); FB = [false(size(A)),FB]; end if dim == 2 B = B.'; FB = FB.'; end end % knnFillCustomDistances %-------------------------------------------------------------------------- function AMatrixT = checkAndExtractTableVars(ATable,dataVars) AMatrixT = zeros(numel(dataVars),height(ATable)); % This will hold the transpose of the data in ATable for ii = 1:numel(dataVars) tmp = ATable.(dataVars(ii)); if ~isfloat(tmp) error(message('MATLAB:fillmissing:DistanceNonFloatsNotSupported')); end if size(tmp,2) ~= 1 error(message('MATLAB:fillmissing:DistanceMultiColumnTableVars')) end AMatrixT(ii,:) = tmp'; end end % checkAndExtractTableVars %-------------------------------------------------------------------------- function b = fillWithNext(b,ma) maInds = flip(find(ma)); if ma(end) % Last value is missing firstFillableElement = 2; % Walk through maInds to find the first non-consecutive element % This will correspond to the last missing value that is followed by a % non-missing value while(firstFillableElement <= numel(maInds) && ... maInds(firstFillableElement) + 1 == maInds(firstFillableElement - 1)) firstFillableElement = firstFillableElement + 1; end maInds = maInds(firstFillableElement:end); end for ii = maInds.' b(ii) = b(ii + 1); end end % fillWithNext %-------------------------------------------------------------------------- function b = fillWithPrevious(b,ma) maInds = find(ma); if ma(1) % First value is missing firstFillableElement = 2; % Walk through maInds to find the first non-consecutive element % This will correspond to the first missing value that is preceded by a % non-missing value while(firstFillableElement <= numel(maInds) && ... maInds(firstFillableElement) - 1 == maInds(firstFillableElement - 1)) firstFillableElement = firstFillableElement + 1; end maInds = maInds(firstFillableElement:end); end for ii = maInds.' b(ii) = b(ii - 1); end end % Fill with previous 我的数据集就是就是之前我告诉你的那些变量名称,请你帮我修改以上代码
05-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值