repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
sequencelengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
sequencelengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
joferkington/mplstereonet
mplstereonet/utilities.py
parse_strike_dip
def parse_strike_dip(strike, dip): """ Parses strings of strike and dip and returns strike and dip measurements following the right-hand-rule. Dip directions are parsed, and if the measurement does not follow the right-hand-rule, the opposite end of the strike measurement is returned. Accepts either quadrant-formatted or azimuth-formatted strikes. For example, this would convert a strike of "N30E" and a dip of "45NW" to a strike of 210 and a dip of 45. Parameters ---------- strike : string A strike measurement. May be in azimuth or quadrant format. dip : string The dip angle and direction of a plane. Returns ------- azi : float Azimuth in degrees of the strike of the plane with dip direction indicated following the right-hand-rule. dip : float Dip of the plane in degrees. """ strike = parse_azimuth(strike) dip, direction = split_trailing_letters(dip) if direction is not None: expected_direc = strike + 90 if opposite_end(expected_direc, direction): strike += 180 if strike > 360: strike -= 360 return strike, dip
python
def parse_strike_dip(strike, dip): """ Parses strings of strike and dip and returns strike and dip measurements following the right-hand-rule. Dip directions are parsed, and if the measurement does not follow the right-hand-rule, the opposite end of the strike measurement is returned. Accepts either quadrant-formatted or azimuth-formatted strikes. For example, this would convert a strike of "N30E" and a dip of "45NW" to a strike of 210 and a dip of 45. Parameters ---------- strike : string A strike measurement. May be in azimuth or quadrant format. dip : string The dip angle and direction of a plane. Returns ------- azi : float Azimuth in degrees of the strike of the plane with dip direction indicated following the right-hand-rule. dip : float Dip of the plane in degrees. """ strike = parse_azimuth(strike) dip, direction = split_trailing_letters(dip) if direction is not None: expected_direc = strike + 90 if opposite_end(expected_direc, direction): strike += 180 if strike > 360: strike -= 360 return strike, dip
[ "def", "parse_strike_dip", "(", "strike", ",", "dip", ")", ":", "strike", "=", "parse_azimuth", "(", "strike", ")", "dip", ",", "direction", "=", "split_trailing_letters", "(", "dip", ")", "if", "direction", "is", "not", "None", ":", "expected_direc", "=", "strike", "+", "90", "if", "opposite_end", "(", "expected_direc", ",", "direction", ")", ":", "strike", "+=", "180", "if", "strike", ">", "360", ":", "strike", "-=", "360", "return", "strike", ",", "dip" ]
Parses strings of strike and dip and returns strike and dip measurements following the right-hand-rule. Dip directions are parsed, and if the measurement does not follow the right-hand-rule, the opposite end of the strike measurement is returned. Accepts either quadrant-formatted or azimuth-formatted strikes. For example, this would convert a strike of "N30E" and a dip of "45NW" to a strike of 210 and a dip of 45. Parameters ---------- strike : string A strike measurement. May be in azimuth or quadrant format. dip : string The dip angle and direction of a plane. Returns ------- azi : float Azimuth in degrees of the strike of the plane with dip direction indicated following the right-hand-rule. dip : float Dip of the plane in degrees.
[ "Parses", "strings", "of", "strike", "and", "dip", "and", "returns", "strike", "and", "dip", "measurements", "following", "the", "right", "-", "hand", "-", "rule", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L5-L44
joferkington/mplstereonet
mplstereonet/utilities.py
parse_rake
def parse_rake(strike, dip, rake): """ Parses strings of strike, dip, and rake and returns a strike, dip, and rake measurement following the right-hand-rule, with the "end" of the strike that the rake is measured from indicated by the sign of the rake (positive rakes correspond to the strike direction, negative rakes correspond to the opposite end). Accepts either quadrant-formatted or azimuth-formatted strikes. For example, this would convert a strike of "N30E", dip of "45NW", with a rake of "10NE" to a strike of 210, dip of 45, and rake of 170. Rake angles returned by this function will always be between 0 and 180 If no directions are specified, the measuriement is assumed to follow the usual right-hand-rule convention. Parameters ---------- strike : string A strike measurement. May be in azimuth or quadrant format. dip : string The dip angle and direction of a plane. rake : string The rake angle and direction that the rake is measured from. Returns ------- strike, dip, rake : floats Measurements of strike, dip, and rake following the conventions outlined above. """ strike, dip = parse_strike_dip(strike, dip) rake, direction = split_trailing_letters(rake) if direction is not None: if opposite_end(strike, direction): rake = -rake if rake < 0: rake += 180 elif rake > 180: rake -= 180 return strike, dip, rake
python
def parse_rake(strike, dip, rake): """ Parses strings of strike, dip, and rake and returns a strike, dip, and rake measurement following the right-hand-rule, with the "end" of the strike that the rake is measured from indicated by the sign of the rake (positive rakes correspond to the strike direction, negative rakes correspond to the opposite end). Accepts either quadrant-formatted or azimuth-formatted strikes. For example, this would convert a strike of "N30E", dip of "45NW", with a rake of "10NE" to a strike of 210, dip of 45, and rake of 170. Rake angles returned by this function will always be between 0 and 180 If no directions are specified, the measuriement is assumed to follow the usual right-hand-rule convention. Parameters ---------- strike : string A strike measurement. May be in azimuth or quadrant format. dip : string The dip angle and direction of a plane. rake : string The rake angle and direction that the rake is measured from. Returns ------- strike, dip, rake : floats Measurements of strike, dip, and rake following the conventions outlined above. """ strike, dip = parse_strike_dip(strike, dip) rake, direction = split_trailing_letters(rake) if direction is not None: if opposite_end(strike, direction): rake = -rake if rake < 0: rake += 180 elif rake > 180: rake -= 180 return strike, dip, rake
[ "def", "parse_rake", "(", "strike", ",", "dip", ",", "rake", ")", ":", "strike", ",", "dip", "=", "parse_strike_dip", "(", "strike", ",", "dip", ")", "rake", ",", "direction", "=", "split_trailing_letters", "(", "rake", ")", "if", "direction", "is", "not", "None", ":", "if", "opposite_end", "(", "strike", ",", "direction", ")", ":", "rake", "=", "-", "rake", "if", "rake", "<", "0", ":", "rake", "+=", "180", "elif", "rake", ">", "180", ":", "rake", "-=", "180", "return", "strike", ",", "dip", ",", "rake" ]
Parses strings of strike, dip, and rake and returns a strike, dip, and rake measurement following the right-hand-rule, with the "end" of the strike that the rake is measured from indicated by the sign of the rake (positive rakes correspond to the strike direction, negative rakes correspond to the opposite end). Accepts either quadrant-formatted or azimuth-formatted strikes. For example, this would convert a strike of "N30E", dip of "45NW", with a rake of "10NE" to a strike of 210, dip of 45, and rake of 170. Rake angles returned by this function will always be between 0 and 180 If no directions are specified, the measuriement is assumed to follow the usual right-hand-rule convention. Parameters ---------- strike : string A strike measurement. May be in azimuth or quadrant format. dip : string The dip angle and direction of a plane. rake : string The rake angle and direction that the rake is measured from. Returns ------- strike, dip, rake : floats Measurements of strike, dip, and rake following the conventions outlined above.
[ "Parses", "strings", "of", "strike", "dip", "and", "rake", "and", "returns", "a", "strike", "dip", "and", "rake", "measurement", "following", "the", "right", "-", "hand", "-", "rule", "with", "the", "end", "of", "the", "strike", "that", "the", "rake", "is", "measured", "from", "indicated", "by", "the", "sign", "of", "the", "rake", "(", "positive", "rakes", "correspond", "to", "the", "strike", "direction", "negative", "rakes", "correspond", "to", "the", "opposite", "end", ")", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L46-L91
joferkington/mplstereonet
mplstereonet/utilities.py
parse_plunge_bearing
def parse_plunge_bearing(plunge, bearing): """ Parses strings of plunge and bearing and returns a consistent plunge and bearing measurement as floats. Plunge angles returned by this function will always be between 0 and 90. If no direction letter(s) is present, the plunge is assumed to be measured from the end specified by the bearing. If a direction letter(s) is present, the bearing will be switched to the opposite (180 degrees) end if the specified direction corresponds to the opposite end specified by the bearing. Parameters ---------- plunge : string A plunge measurement. bearing : string A bearing measurement. May be in azimuth or quadrant format. Returns ------- plunge, bearing: floats The plunge and bearing following the conventions outlined above. Examples --------- >>> parse_plunge_bearing("30NW", 160) ... (30, 340) """ bearing = parse_azimuth(bearing) plunge, direction = split_trailing_letters(plunge) if direction is not None: if opposite_end(bearing, direction): bearing +=180 if plunge < 0: bearing += 180 plunge = -plunge if plunge > 90: bearing += 180 plunge = 180 - plunge if bearing > 360: bearing -= 360 return plunge, bearing
python
def parse_plunge_bearing(plunge, bearing): """ Parses strings of plunge and bearing and returns a consistent plunge and bearing measurement as floats. Plunge angles returned by this function will always be between 0 and 90. If no direction letter(s) is present, the plunge is assumed to be measured from the end specified by the bearing. If a direction letter(s) is present, the bearing will be switched to the opposite (180 degrees) end if the specified direction corresponds to the opposite end specified by the bearing. Parameters ---------- plunge : string A plunge measurement. bearing : string A bearing measurement. May be in azimuth or quadrant format. Returns ------- plunge, bearing: floats The plunge and bearing following the conventions outlined above. Examples --------- >>> parse_plunge_bearing("30NW", 160) ... (30, 340) """ bearing = parse_azimuth(bearing) plunge, direction = split_trailing_letters(plunge) if direction is not None: if opposite_end(bearing, direction): bearing +=180 if plunge < 0: bearing += 180 plunge = -plunge if plunge > 90: bearing += 180 plunge = 180 - plunge if bearing > 360: bearing -= 360 return plunge, bearing
[ "def", "parse_plunge_bearing", "(", "plunge", ",", "bearing", ")", ":", "bearing", "=", "parse_azimuth", "(", "bearing", ")", "plunge", ",", "direction", "=", "split_trailing_letters", "(", "plunge", ")", "if", "direction", "is", "not", "None", ":", "if", "opposite_end", "(", "bearing", ",", "direction", ")", ":", "bearing", "+=", "180", "if", "plunge", "<", "0", ":", "bearing", "+=", "180", "plunge", "=", "-", "plunge", "if", "plunge", ">", "90", ":", "bearing", "+=", "180", "plunge", "=", "180", "-", "plunge", "if", "bearing", ">", "360", ":", "bearing", "-=", "360", "return", "plunge", ",", "bearing" ]
Parses strings of plunge and bearing and returns a consistent plunge and bearing measurement as floats. Plunge angles returned by this function will always be between 0 and 90. If no direction letter(s) is present, the plunge is assumed to be measured from the end specified by the bearing. If a direction letter(s) is present, the bearing will be switched to the opposite (180 degrees) end if the specified direction corresponds to the opposite end specified by the bearing. Parameters ---------- plunge : string A plunge measurement. bearing : string A bearing measurement. May be in azimuth or quadrant format. Returns ------- plunge, bearing: floats The plunge and bearing following the conventions outlined above. Examples --------- >>> parse_plunge_bearing("30NW", 160) ... (30, 340)
[ "Parses", "strings", "of", "plunge", "and", "bearing", "and", "returns", "a", "consistent", "plunge", "and", "bearing", "measurement", "as", "floats", ".", "Plunge", "angles", "returned", "by", "this", "function", "will", "always", "be", "between", "0", "and", "90", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L93-L141
joferkington/mplstereonet
mplstereonet/utilities.py
dip_direction2strike
def dip_direction2strike(azimuth): """ Converts a planar measurment of dip direction using the dip-azimuth convention into a strike using the right-hand-rule. Parameters ---------- azimuth : number or string The dip direction of the plane in degrees. This can be either a numerical azimuth in the 0-360 range or a string representing a quadrant measurement (e.g. N30W). Returns ------- strike : number The strike of the plane in degrees following the right-hand-rule. """ azimuth = parse_azimuth(azimuth) strike = azimuth - 90 if strike < 0: strike += 360 return strike
python
def dip_direction2strike(azimuth): """ Converts a planar measurment of dip direction using the dip-azimuth convention into a strike using the right-hand-rule. Parameters ---------- azimuth : number or string The dip direction of the plane in degrees. This can be either a numerical azimuth in the 0-360 range or a string representing a quadrant measurement (e.g. N30W). Returns ------- strike : number The strike of the plane in degrees following the right-hand-rule. """ azimuth = parse_azimuth(azimuth) strike = azimuth - 90 if strike < 0: strike += 360 return strike
[ "def", "dip_direction2strike", "(", "azimuth", ")", ":", "azimuth", "=", "parse_azimuth", "(", "azimuth", ")", "strike", "=", "azimuth", "-", "90", "if", "strike", "<", "0", ":", "strike", "+=", "360", "return", "strike" ]
Converts a planar measurment of dip direction using the dip-azimuth convention into a strike using the right-hand-rule. Parameters ---------- azimuth : number or string The dip direction of the plane in degrees. This can be either a numerical azimuth in the 0-360 range or a string representing a quadrant measurement (e.g. N30W). Returns ------- strike : number The strike of the plane in degrees following the right-hand-rule.
[ "Converts", "a", "planar", "measurment", "of", "dip", "direction", "using", "the", "dip", "-", "azimuth", "convention", "into", "a", "strike", "using", "the", "right", "-", "hand", "-", "rule", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L143-L164
joferkington/mplstereonet
mplstereonet/utilities.py
strike2dip_direction
def strike2dip_direction(strike): """ Converts a planar measurement of strike using the right-hand-rule into the dip direction (i.e. the direction that the plane dips). Parameters ---------- strike : number or string The strike direction of the plane in degrees. This can be either a numerical azimuth in the 0-360 range or a string representing a quadrant measurement (e.g. N30W). Returns ------- azimuth : number The dip direction of the plane in degrees (0-360). """ strike = parse_azimuth(strike) dip_direction = strike + 90 if dip_direction > 360: dip_direction -= 360 return dip_direction
python
def strike2dip_direction(strike): """ Converts a planar measurement of strike using the right-hand-rule into the dip direction (i.e. the direction that the plane dips). Parameters ---------- strike : number or string The strike direction of the plane in degrees. This can be either a numerical azimuth in the 0-360 range or a string representing a quadrant measurement (e.g. N30W). Returns ------- azimuth : number The dip direction of the plane in degrees (0-360). """ strike = parse_azimuth(strike) dip_direction = strike + 90 if dip_direction > 360: dip_direction -= 360 return dip_direction
[ "def", "strike2dip_direction", "(", "strike", ")", ":", "strike", "=", "parse_azimuth", "(", "strike", ")", "dip_direction", "=", "strike", "+", "90", "if", "dip_direction", ">", "360", ":", "dip_direction", "-=", "360", "return", "dip_direction" ]
Converts a planar measurement of strike using the right-hand-rule into the dip direction (i.e. the direction that the plane dips). Parameters ---------- strike : number or string The strike direction of the plane in degrees. This can be either a numerical azimuth in the 0-360 range or a string representing a quadrant measurement (e.g. N30W). Returns ------- azimuth : number The dip direction of the plane in degrees (0-360).
[ "Converts", "a", "planar", "measurement", "of", "strike", "using", "the", "right", "-", "hand", "-", "rule", "into", "the", "dip", "direction", "(", "i", ".", "e", ".", "the", "direction", "that", "the", "plane", "dips", ")", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L166-L187
joferkington/mplstereonet
mplstereonet/utilities.py
parse_azimuth
def parse_azimuth(azimuth): """ Parses an azimuth measurement in azimuth or quadrant format. Parameters ----------- azimuth : string or number An azimuth measurement in degrees or a quadrant measurement of azimuth. Returns ------- azi : float The azimuth in degrees clockwise from north (range: 0-360) See Also -------- parse_quadrant_measurement parse_strike_dip parse_plunge_bearing """ try: azimuth = float(azimuth) except ValueError: if not azimuth[0].isalpha(): raise ValueError('Ambiguous azimuth: {}'.format(azimuth)) azimuth = parse_quadrant_measurement(azimuth) return azimuth
python
def parse_azimuth(azimuth): """ Parses an azimuth measurement in azimuth or quadrant format. Parameters ----------- azimuth : string or number An azimuth measurement in degrees or a quadrant measurement of azimuth. Returns ------- azi : float The azimuth in degrees clockwise from north (range: 0-360) See Also -------- parse_quadrant_measurement parse_strike_dip parse_plunge_bearing """ try: azimuth = float(azimuth) except ValueError: if not azimuth[0].isalpha(): raise ValueError('Ambiguous azimuth: {}'.format(azimuth)) azimuth = parse_quadrant_measurement(azimuth) return azimuth
[ "def", "parse_azimuth", "(", "azimuth", ")", ":", "try", ":", "azimuth", "=", "float", "(", "azimuth", ")", "except", "ValueError", ":", "if", "not", "azimuth", "[", "0", "]", ".", "isalpha", "(", ")", ":", "raise", "ValueError", "(", "'Ambiguous azimuth: {}'", ".", "format", "(", "azimuth", ")", ")", "azimuth", "=", "parse_quadrant_measurement", "(", "azimuth", ")", "return", "azimuth" ]
Parses an azimuth measurement in azimuth or quadrant format. Parameters ----------- azimuth : string or number An azimuth measurement in degrees or a quadrant measurement of azimuth. Returns ------- azi : float The azimuth in degrees clockwise from north (range: 0-360) See Also -------- parse_quadrant_measurement parse_strike_dip parse_plunge_bearing
[ "Parses", "an", "azimuth", "measurement", "in", "azimuth", "or", "quadrant", "format", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L223-L249
joferkington/mplstereonet
mplstereonet/utilities.py
parse_quadrant_measurement
def parse_quadrant_measurement(quad_azimuth): """ Parses a quadrant measurement of the form "AxxB", where A and B are cardinal directions and xx is an angle measured relative to those directions. In other words, it converts a measurement such as E30N into an azimuth of 60 degrees, or W10S into an azimuth of 260 degrees. For ambiguous quadrant measurements such as "N30S", a ValueError is raised. Parameters ----------- quad_azimuth : string An azimuth measurement in quadrant form. Returns ------- azi : float An azimuth in degrees clockwise from north. See Also -------- parse_azimuth """ def rotation_direction(first, second): return np.cross(_azimuth2vec(first), _azimuth2vec(second)) # Parse measurement quad_azimuth = quad_azimuth.strip() try: first_dir = quadrantletter_to_azimuth(quad_azimuth[0].upper()) sec_dir = quadrantletter_to_azimuth(quad_azimuth[-1].upper()) except KeyError: raise ValueError('{} is not a valid azimuth'.format(quad_azimuth)) angle = float(quad_azimuth[1:-1]) # Convert quadrant measurement into an azimuth direc = rotation_direction(first_dir, sec_dir) azi = first_dir + direc * angle # Catch ambiguous measurements such as N10S and raise an error if abs(direc) < 0.9: raise ValueError('{} is not a valid azimuth'.format(quad_azimuth)) # Ensure that 0 <= azi <= 360 if azi < 0: azi += 360 elif azi > 360: azi -= 360 return azi
python
def parse_quadrant_measurement(quad_azimuth): """ Parses a quadrant measurement of the form "AxxB", where A and B are cardinal directions and xx is an angle measured relative to those directions. In other words, it converts a measurement such as E30N into an azimuth of 60 degrees, or W10S into an azimuth of 260 degrees. For ambiguous quadrant measurements such as "N30S", a ValueError is raised. Parameters ----------- quad_azimuth : string An azimuth measurement in quadrant form. Returns ------- azi : float An azimuth in degrees clockwise from north. See Also -------- parse_azimuth """ def rotation_direction(first, second): return np.cross(_azimuth2vec(first), _azimuth2vec(second)) # Parse measurement quad_azimuth = quad_azimuth.strip() try: first_dir = quadrantletter_to_azimuth(quad_azimuth[0].upper()) sec_dir = quadrantletter_to_azimuth(quad_azimuth[-1].upper()) except KeyError: raise ValueError('{} is not a valid azimuth'.format(quad_azimuth)) angle = float(quad_azimuth[1:-1]) # Convert quadrant measurement into an azimuth direc = rotation_direction(first_dir, sec_dir) azi = first_dir + direc * angle # Catch ambiguous measurements such as N10S and raise an error if abs(direc) < 0.9: raise ValueError('{} is not a valid azimuth'.format(quad_azimuth)) # Ensure that 0 <= azi <= 360 if azi < 0: azi += 360 elif azi > 360: azi -= 360 return azi
[ "def", "parse_quadrant_measurement", "(", "quad_azimuth", ")", ":", "def", "rotation_direction", "(", "first", ",", "second", ")", ":", "return", "np", ".", "cross", "(", "_azimuth2vec", "(", "first", ")", ",", "_azimuth2vec", "(", "second", ")", ")", "# Parse measurement", "quad_azimuth", "=", "quad_azimuth", ".", "strip", "(", ")", "try", ":", "first_dir", "=", "quadrantletter_to_azimuth", "(", "quad_azimuth", "[", "0", "]", ".", "upper", "(", ")", ")", "sec_dir", "=", "quadrantletter_to_azimuth", "(", "quad_azimuth", "[", "-", "1", "]", ".", "upper", "(", ")", ")", "except", "KeyError", ":", "raise", "ValueError", "(", "'{} is not a valid azimuth'", ".", "format", "(", "quad_azimuth", ")", ")", "angle", "=", "float", "(", "quad_azimuth", "[", "1", ":", "-", "1", "]", ")", "# Convert quadrant measurement into an azimuth", "direc", "=", "rotation_direction", "(", "first_dir", ",", "sec_dir", ")", "azi", "=", "first_dir", "+", "direc", "*", "angle", "# Catch ambiguous measurements such as N10S and raise an error", "if", "abs", "(", "direc", ")", "<", "0.9", ":", "raise", "ValueError", "(", "'{} is not a valid azimuth'", ".", "format", "(", "quad_azimuth", ")", ")", "# Ensure that 0 <= azi <= 360", "if", "azi", "<", "0", ":", "azi", "+=", "360", "elif", "azi", ">", "360", ":", "azi", "-=", "360", "return", "azi" ]
Parses a quadrant measurement of the form "AxxB", where A and B are cardinal directions and xx is an angle measured relative to those directions. In other words, it converts a measurement such as E30N into an azimuth of 60 degrees, or W10S into an azimuth of 260 degrees. For ambiguous quadrant measurements such as "N30S", a ValueError is raised. Parameters ----------- quad_azimuth : string An azimuth measurement in quadrant form. Returns ------- azi : float An azimuth in degrees clockwise from north. See Also -------- parse_azimuth
[ "Parses", "a", "quadrant", "measurement", "of", "the", "form", "AxxB", "where", "A", "and", "B", "are", "cardinal", "directions", "and", "xx", "is", "an", "angle", "measured", "relative", "to", "those", "directions", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/utilities.py#L251-L302
joferkington/mplstereonet
mplstereonet/stereonet_transforms.py
BaseStereonetTransform.inverted
def inverted(self): """Return the inverse of the transform.""" # This is a bit of hackery so that we can put a single "inverse" # function here. If we just made "self._inverse_type" point to the class # in question, it wouldn't be defined yet. This way, it's done at # at runtime and we avoid the definition problem. Hackish, but better # than repeating code everywhere or making a relatively complex # metaclass. inverse_type = globals()[self._inverse_type] return inverse_type(self._center_longitude, self._center_latitude, self._resolution)
python
def inverted(self): """Return the inverse of the transform.""" # This is a bit of hackery so that we can put a single "inverse" # function here. If we just made "self._inverse_type" point to the class # in question, it wouldn't be defined yet. This way, it's done at # at runtime and we avoid the definition problem. Hackish, but better # than repeating code everywhere or making a relatively complex # metaclass. inverse_type = globals()[self._inverse_type] return inverse_type(self._center_longitude, self._center_latitude, self._resolution)
[ "def", "inverted", "(", "self", ")", ":", "# This is a bit of hackery so that we can put a single \"inverse\"", "# function here. If we just made \"self._inverse_type\" point to the class", "# in question, it wouldn't be defined yet. This way, it's done at", "# at runtime and we avoid the definition problem. Hackish, but better", "# than repeating code everywhere or making a relatively complex", "# metaclass.", "inverse_type", "=", "globals", "(", ")", "[", "self", ".", "_inverse_type", "]", "return", "inverse_type", "(", "self", ".", "_center_longitude", ",", "self", ".", "_center_latitude", ",", "self", ".", "_resolution", ")" ]
Return the inverse of the transform.
[ "Return", "the", "inverse", "of", "the", "transform", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_transforms.py#L54-L64
joferkington/mplstereonet
mplstereonet/stereonet_math.py
sph2cart
def sph2cart(lon, lat): """ Converts a longitude and latitude (or sequence of lons and lats) given in _radians_ to cartesian coordinates, `x`, `y`, `z`, where x=0, y=0, z=0 is the center of the globe. Parameters ---------- lon : array-like Longitude in radians lat : array-like Latitude in radians Returns ------- `x`, `y`, `z` : Arrays of cartesian coordinates """ x = np.cos(lat)*np.cos(lon) y = np.cos(lat)*np.sin(lon) z = np.sin(lat) return x, y, z
python
def sph2cart(lon, lat): """ Converts a longitude and latitude (or sequence of lons and lats) given in _radians_ to cartesian coordinates, `x`, `y`, `z`, where x=0, y=0, z=0 is the center of the globe. Parameters ---------- lon : array-like Longitude in radians lat : array-like Latitude in radians Returns ------- `x`, `y`, `z` : Arrays of cartesian coordinates """ x = np.cos(lat)*np.cos(lon) y = np.cos(lat)*np.sin(lon) z = np.sin(lat) return x, y, z
[ "def", "sph2cart", "(", "lon", ",", "lat", ")", ":", "x", "=", "np", ".", "cos", "(", "lat", ")", "*", "np", ".", "cos", "(", "lon", ")", "y", "=", "np", ".", "cos", "(", "lat", ")", "*", "np", ".", "sin", "(", "lon", ")", "z", "=", "np", ".", "sin", "(", "lat", ")", "return", "x", ",", "y", ",", "z" ]
Converts a longitude and latitude (or sequence of lons and lats) given in _radians_ to cartesian coordinates, `x`, `y`, `z`, where x=0, y=0, z=0 is the center of the globe. Parameters ---------- lon : array-like Longitude in radians lat : array-like Latitude in radians Returns ------- `x`, `y`, `z` : Arrays of cartesian coordinates
[ "Converts", "a", "longitude", "and", "latitude", "(", "or", "sequence", "of", "lons", "and", "lats", ")", "given", "in", "_radians_", "to", "cartesian", "coordinates", "x", "y", "z", "where", "x", "=", "0", "y", "=", "0", "z", "=", "0", "is", "the", "center", "of", "the", "globe", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L28-L48
joferkington/mplstereonet
mplstereonet/stereonet_math.py
cart2sph
def cart2sph(x, y, z): """ Converts cartesian coordinates `x`, `y`, `z` into a longitude and latitude. x=0, y=0, z=0 is assumed to correspond to the center of the globe. Returns lon and lat in radians. Parameters ---------- `x`, `y`, `z` : Arrays of cartesian coordinates Returns ------- lon : Longitude in radians lat : Latitude in radians """ r = np.sqrt(x**2 + y**2 + z**2) lat = np.arcsin(z/r) lon = np.arctan2(y, x) return lon, lat
python
def cart2sph(x, y, z): """ Converts cartesian coordinates `x`, `y`, `z` into a longitude and latitude. x=0, y=0, z=0 is assumed to correspond to the center of the globe. Returns lon and lat in radians. Parameters ---------- `x`, `y`, `z` : Arrays of cartesian coordinates Returns ------- lon : Longitude in radians lat : Latitude in radians """ r = np.sqrt(x**2 + y**2 + z**2) lat = np.arcsin(z/r) lon = np.arctan2(y, x) return lon, lat
[ "def", "cart2sph", "(", "x", ",", "y", ",", "z", ")", ":", "r", "=", "np", ".", "sqrt", "(", "x", "**", "2", "+", "y", "**", "2", "+", "z", "**", "2", ")", "lat", "=", "np", ".", "arcsin", "(", "z", "/", "r", ")", "lon", "=", "np", ".", "arctan2", "(", "y", ",", "x", ")", "return", "lon", ",", "lat" ]
Converts cartesian coordinates `x`, `y`, `z` into a longitude and latitude. x=0, y=0, z=0 is assumed to correspond to the center of the globe. Returns lon and lat in radians. Parameters ---------- `x`, `y`, `z` : Arrays of cartesian coordinates Returns ------- lon : Longitude in radians lat : Latitude in radians
[ "Converts", "cartesian", "coordinates", "x", "y", "z", "into", "a", "longitude", "and", "latitude", ".", "x", "=", "0", "y", "=", "0", "z", "=", "0", "is", "assumed", "to", "correspond", "to", "the", "center", "of", "the", "globe", ".", "Returns", "lon", "and", "lat", "in", "radians", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L50-L68
joferkington/mplstereonet
mplstereonet/stereonet_math.py
_rotate
def _rotate(lon, lat, theta, axis='x'): """ Rotate "lon", "lat" coords (in _degrees_) about the X-axis by "theta" degrees. This effectively simulates rotating a physical stereonet. Returns rotated lon, lat coords in _radians_). """ # Convert input to numpy arrays in radians lon, lat = np.atleast_1d(lon, lat) lon, lat = map(np.radians, [lon, lat]) theta = np.radians(theta) # Convert to cartesian coords for the rotation x, y, z = sph2cart(lon, lat) lookup = {'x':_rotate_x, 'y':_rotate_y, 'z':_rotate_z} X, Y, Z = lookup[axis](x, y, z, theta) # Now convert back to spherical coords (longitude and latitude, ignore R) lon, lat = cart2sph(X,Y,Z) return lon, lat
python
def _rotate(lon, lat, theta, axis='x'): """ Rotate "lon", "lat" coords (in _degrees_) about the X-axis by "theta" degrees. This effectively simulates rotating a physical stereonet. Returns rotated lon, lat coords in _radians_). """ # Convert input to numpy arrays in radians lon, lat = np.atleast_1d(lon, lat) lon, lat = map(np.radians, [lon, lat]) theta = np.radians(theta) # Convert to cartesian coords for the rotation x, y, z = sph2cart(lon, lat) lookup = {'x':_rotate_x, 'y':_rotate_y, 'z':_rotate_z} X, Y, Z = lookup[axis](x, y, z, theta) # Now convert back to spherical coords (longitude and latitude, ignore R) lon, lat = cart2sph(X,Y,Z) return lon, lat
[ "def", "_rotate", "(", "lon", ",", "lat", ",", "theta", ",", "axis", "=", "'x'", ")", ":", "# Convert input to numpy arrays in radians", "lon", ",", "lat", "=", "np", ".", "atleast_1d", "(", "lon", ",", "lat", ")", "lon", ",", "lat", "=", "map", "(", "np", ".", "radians", ",", "[", "lon", ",", "lat", "]", ")", "theta", "=", "np", ".", "radians", "(", "theta", ")", "# Convert to cartesian coords for the rotation", "x", ",", "y", ",", "z", "=", "sph2cart", "(", "lon", ",", "lat", ")", "lookup", "=", "{", "'x'", ":", "_rotate_x", ",", "'y'", ":", "_rotate_y", ",", "'z'", ":", "_rotate_z", "}", "X", ",", "Y", ",", "Z", "=", "lookup", "[", "axis", "]", "(", "x", ",", "y", ",", "z", ",", "theta", ")", "# Now convert back to spherical coords (longitude and latitude, ignore R)", "lon", ",", "lat", "=", "cart2sph", "(", "X", ",", "Y", ",", "Z", ")", "return", "lon", ",", "lat" ]
Rotate "lon", "lat" coords (in _degrees_) about the X-axis by "theta" degrees. This effectively simulates rotating a physical stereonet. Returns rotated lon, lat coords in _radians_).
[ "Rotate", "lon", "lat", "coords", "(", "in", "_degrees_", ")", "about", "the", "X", "-", "axis", "by", "theta", "degrees", ".", "This", "effectively", "simulates", "rotating", "a", "physical", "stereonet", ".", "Returns", "rotated", "lon", "lat", "coords", "in", "_radians_", ")", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L70-L89
joferkington/mplstereonet
mplstereonet/stereonet_math.py
antipode
def antipode(lon, lat): """ Calculates the antipode (opposite point on the globe) of the given point or points. Input and output is expected to be in radians. Parameters ---------- lon : number or sequence of numbers Longitude in radians lat : number or sequence of numbers Latitude in radians Returns ------- lon, lat : arrays Sequences (regardless of whether or not the input was a single value or a sequence) of longitude and latitude in radians. """ x, y, z = sph2cart(lon, lat) return cart2sph(-x, -y, -z)
python
def antipode(lon, lat): """ Calculates the antipode (opposite point on the globe) of the given point or points. Input and output is expected to be in radians. Parameters ---------- lon : number or sequence of numbers Longitude in radians lat : number or sequence of numbers Latitude in radians Returns ------- lon, lat : arrays Sequences (regardless of whether or not the input was a single value or a sequence) of longitude and latitude in radians. """ x, y, z = sph2cart(lon, lat) return cart2sph(-x, -y, -z)
[ "def", "antipode", "(", "lon", ",", "lat", ")", ":", "x", ",", "y", ",", "z", "=", "sph2cart", "(", "lon", ",", "lat", ")", "return", "cart2sph", "(", "-", "x", ",", "-", "y", ",", "-", "z", ")" ]
Calculates the antipode (opposite point on the globe) of the given point or points. Input and output is expected to be in radians. Parameters ---------- lon : number or sequence of numbers Longitude in radians lat : number or sequence of numbers Latitude in radians Returns ------- lon, lat : arrays Sequences (regardless of whether or not the input was a single value or a sequence) of longitude and latitude in radians.
[ "Calculates", "the", "antipode", "(", "opposite", "point", "on", "the", "globe", ")", "of", "the", "given", "point", "or", "points", ".", "Input", "and", "output", "is", "expected", "to", "be", "in", "radians", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L109-L128
joferkington/mplstereonet
mplstereonet/stereonet_math.py
plane
def plane(strike, dip, segments=100, center=(0, 0)): """ Calculates the longitude and latitude of `segments` points along the stereonet projection of each plane with a given `strike` and `dip` in degrees. Returns points for one hemisphere only. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. segments : number or sequence of numbers The number of points in the returned `lon` and `lat` arrays. Defaults to 100 segments. center : sequence of two numbers (lon, lat) The longitude and latitude of the center of the hemisphere that the returned points will be in. Defaults to 0,0 (approriate for a typical stereonet). Returns ------- lon, lat : arrays `num_segments` x `num_strikes` arrays of longitude and latitude in radians. """ lon0, lat0 = center strikes, dips = np.atleast_1d(strike, dip) lons = np.zeros((segments, strikes.size), dtype=np.float) lats = lons.copy() for i, (strike, dip) in enumerate(zip(strikes, dips)): # We just plot a line of constant longitude and rotate it by the strike. dip = 90 - dip lon = dip * np.ones(segments) lat = np.linspace(-90, 90, segments) lon, lat = _rotate(lon, lat, strike) if lat0 != 0 or lon0 != 0: dist = angular_distance([lon, lat], [lon0, lat0], False) mask = dist > (np.pi / 2) lon[mask], lat[mask] = antipode(lon[mask], lat[mask]) change = np.diff(mask.astype(int)) ind = np.flatnonzero(change) + 1 lat = np.hstack(np.split(lat, ind)[::-1]) lon = np.hstack(np.split(lon, ind)[::-1]) lons[:,i] = lon lats[:,i] = lat return lons, lats
python
def plane(strike, dip, segments=100, center=(0, 0)): """ Calculates the longitude and latitude of `segments` points along the stereonet projection of each plane with a given `strike` and `dip` in degrees. Returns points for one hemisphere only. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. segments : number or sequence of numbers The number of points in the returned `lon` and `lat` arrays. Defaults to 100 segments. center : sequence of two numbers (lon, lat) The longitude and latitude of the center of the hemisphere that the returned points will be in. Defaults to 0,0 (approriate for a typical stereonet). Returns ------- lon, lat : arrays `num_segments` x `num_strikes` arrays of longitude and latitude in radians. """ lon0, lat0 = center strikes, dips = np.atleast_1d(strike, dip) lons = np.zeros((segments, strikes.size), dtype=np.float) lats = lons.copy() for i, (strike, dip) in enumerate(zip(strikes, dips)): # We just plot a line of constant longitude and rotate it by the strike. dip = 90 - dip lon = dip * np.ones(segments) lat = np.linspace(-90, 90, segments) lon, lat = _rotate(lon, lat, strike) if lat0 != 0 or lon0 != 0: dist = angular_distance([lon, lat], [lon0, lat0], False) mask = dist > (np.pi / 2) lon[mask], lat[mask] = antipode(lon[mask], lat[mask]) change = np.diff(mask.astype(int)) ind = np.flatnonzero(change) + 1 lat = np.hstack(np.split(lat, ind)[::-1]) lon = np.hstack(np.split(lon, ind)[::-1]) lons[:,i] = lon lats[:,i] = lat return lons, lats
[ "def", "plane", "(", "strike", ",", "dip", ",", "segments", "=", "100", ",", "center", "=", "(", "0", ",", "0", ")", ")", ":", "lon0", ",", "lat0", "=", "center", "strikes", ",", "dips", "=", "np", ".", "atleast_1d", "(", "strike", ",", "dip", ")", "lons", "=", "np", ".", "zeros", "(", "(", "segments", ",", "strikes", ".", "size", ")", ",", "dtype", "=", "np", ".", "float", ")", "lats", "=", "lons", ".", "copy", "(", ")", "for", "i", ",", "(", "strike", ",", "dip", ")", "in", "enumerate", "(", "zip", "(", "strikes", ",", "dips", ")", ")", ":", "# We just plot a line of constant longitude and rotate it by the strike.", "dip", "=", "90", "-", "dip", "lon", "=", "dip", "*", "np", ".", "ones", "(", "segments", ")", "lat", "=", "np", ".", "linspace", "(", "-", "90", ",", "90", ",", "segments", ")", "lon", ",", "lat", "=", "_rotate", "(", "lon", ",", "lat", ",", "strike", ")", "if", "lat0", "!=", "0", "or", "lon0", "!=", "0", ":", "dist", "=", "angular_distance", "(", "[", "lon", ",", "lat", "]", ",", "[", "lon0", ",", "lat0", "]", ",", "False", ")", "mask", "=", "dist", ">", "(", "np", ".", "pi", "/", "2", ")", "lon", "[", "mask", "]", ",", "lat", "[", "mask", "]", "=", "antipode", "(", "lon", "[", "mask", "]", ",", "lat", "[", "mask", "]", ")", "change", "=", "np", ".", "diff", "(", "mask", ".", "astype", "(", "int", ")", ")", "ind", "=", "np", ".", "flatnonzero", "(", "change", ")", "+", "1", "lat", "=", "np", ".", "hstack", "(", "np", ".", "split", "(", "lat", ",", "ind", ")", "[", ":", ":", "-", "1", "]", ")", "lon", "=", "np", ".", "hstack", "(", "np", ".", "split", "(", "lon", ",", "ind", ")", "[", ":", ":", "-", "1", "]", ")", "lons", "[", ":", ",", "i", "]", "=", "lon", "lats", "[", ":", ",", "i", "]", "=", "lat", "return", "lons", ",", "lats" ]
Calculates the longitude and latitude of `segments` points along the stereonet projection of each plane with a given `strike` and `dip` in degrees. Returns points for one hemisphere only. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. segments : number or sequence of numbers The number of points in the returned `lon` and `lat` arrays. Defaults to 100 segments. center : sequence of two numbers (lon, lat) The longitude and latitude of the center of the hemisphere that the returned points will be in. Defaults to 0,0 (approriate for a typical stereonet). Returns ------- lon, lat : arrays `num_segments` x `num_strikes` arrays of longitude and latitude in radians.
[ "Calculates", "the", "longitude", "and", "latitude", "of", "segments", "points", "along", "the", "stereonet", "projection", "of", "each", "plane", "with", "a", "given", "strike", "and", "dip", "in", "degrees", ".", "Returns", "points", "for", "one", "hemisphere", "only", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L130-L181
joferkington/mplstereonet
mplstereonet/stereonet_math.py
pole
def pole(strike, dip): """ Calculates the longitude and latitude of the pole(s) to the plane(s) specified by `strike` and `dip`, given in degrees. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. Returns ------- lon, lat : Arrays of longitude and latitude in radians. """ strike, dip = np.atleast_1d(strike, dip) mask = dip > 90 dip[mask] = 180 - dip[mask] strike[mask] += 180 # Plot the approriate point for a strike of 0 and rotate it lon, lat = -dip, 0.0 lon, lat = _rotate(lon, lat, strike) return lon, lat
python
def pole(strike, dip): """ Calculates the longitude and latitude of the pole(s) to the plane(s) specified by `strike` and `dip`, given in degrees. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. Returns ------- lon, lat : Arrays of longitude and latitude in radians. """ strike, dip = np.atleast_1d(strike, dip) mask = dip > 90 dip[mask] = 180 - dip[mask] strike[mask] += 180 # Plot the approriate point for a strike of 0 and rotate it lon, lat = -dip, 0.0 lon, lat = _rotate(lon, lat, strike) return lon, lat
[ "def", "pole", "(", "strike", ",", "dip", ")", ":", "strike", ",", "dip", "=", "np", ".", "atleast_1d", "(", "strike", ",", "dip", ")", "mask", "=", "dip", ">", "90", "dip", "[", "mask", "]", "=", "180", "-", "dip", "[", "mask", "]", "strike", "[", "mask", "]", "+=", "180", "# Plot the approriate point for a strike of 0 and rotate it", "lon", ",", "lat", "=", "-", "dip", ",", "0.0", "lon", ",", "lat", "=", "_rotate", "(", "lon", ",", "lat", ",", "strike", ")", "return", "lon", ",", "lat" ]
Calculates the longitude and latitude of the pole(s) to the plane(s) specified by `strike` and `dip`, given in degrees. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. Returns ------- lon, lat : Arrays of longitude and latitude in radians.
[ "Calculates", "the", "longitude", "and", "latitude", "of", "the", "pole", "(", "s", ")", "to", "the", "plane", "(", "s", ")", "specified", "by", "strike", "and", "dip", "given", "in", "degrees", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L183-L208
joferkington/mplstereonet
mplstereonet/stereonet_math.py
rake
def rake(strike, dip, rake_angle): """ Calculates the longitude and latitude of the linear feature(s) specified by `strike`, `dip`, and `rake_angle`. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. rake_angle : number or sequence of numbers The angle of the lineation on the plane measured in degrees downward from horizontal. Zero degrees corresponds to the "right- hand" direction indicated by the strike, while 180 degrees or a negative angle corresponds to the opposite direction. Returns ------- lon, lat : Arrays of longitude and latitude in radians. """ strike, dip, rake_angle = np.atleast_1d(strike, dip, rake_angle) # Plot the approriate point for a strike of 0 and rotate it dip = 90 - dip lon = dip rake_angle = rake_angle.copy() rake_angle[rake_angle < 0] += 180 lat = 90 - rake_angle lon, lat = _rotate(lon, lat, strike) return lon, lat
python
def rake(strike, dip, rake_angle): """ Calculates the longitude and latitude of the linear feature(s) specified by `strike`, `dip`, and `rake_angle`. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. rake_angle : number or sequence of numbers The angle of the lineation on the plane measured in degrees downward from horizontal. Zero degrees corresponds to the "right- hand" direction indicated by the strike, while 180 degrees or a negative angle corresponds to the opposite direction. Returns ------- lon, lat : Arrays of longitude and latitude in radians. """ strike, dip, rake_angle = np.atleast_1d(strike, dip, rake_angle) # Plot the approriate point for a strike of 0 and rotate it dip = 90 - dip lon = dip rake_angle = rake_angle.copy() rake_angle[rake_angle < 0] += 180 lat = 90 - rake_angle lon, lat = _rotate(lon, lat, strike) return lon, lat
[ "def", "rake", "(", "strike", ",", "dip", ",", "rake_angle", ")", ":", "strike", ",", "dip", ",", "rake_angle", "=", "np", ".", "atleast_1d", "(", "strike", ",", "dip", ",", "rake_angle", ")", "# Plot the approriate point for a strike of 0 and rotate it", "dip", "=", "90", "-", "dip", "lon", "=", "dip", "rake_angle", "=", "rake_angle", ".", "copy", "(", ")", "rake_angle", "[", "rake_angle", "<", "0", "]", "+=", "180", "lat", "=", "90", "-", "rake_angle", "lon", ",", "lat", "=", "_rotate", "(", "lon", ",", "lat", ",", "strike", ")", "return", "lon", ",", "lat" ]
Calculates the longitude and latitude of the linear feature(s) specified by `strike`, `dip`, and `rake_angle`. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. rake_angle : number or sequence of numbers The angle of the lineation on the plane measured in degrees downward from horizontal. Zero degrees corresponds to the "right- hand" direction indicated by the strike, while 180 degrees or a negative angle corresponds to the opposite direction. Returns ------- lon, lat : Arrays of longitude and latitude in radians.
[ "Calculates", "the", "longitude", "and", "latitude", "of", "the", "linear", "feature", "(", "s", ")", "specified", "by", "strike", "dip", "and", "rake_angle", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L210-L243
joferkington/mplstereonet
mplstereonet/stereonet_math.py
line
def line(plunge, bearing): """ Calculates the longitude and latitude of the linear feature(s) specified by `plunge` and `bearing`. Parameters ---------- plunge : number or sequence of numbers The plunge of the line(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the line(s) in degrees. Returns ------- lon, lat : Arrays of longitude and latitude in radians. """ plunge, bearing = np.atleast_1d(plunge, bearing) # Plot the approriate point for a bearing of 0 and rotate it lat = 90 - plunge lon = 0 lon, lat = _rotate(lon, lat, bearing) return lon, lat
python
def line(plunge, bearing): """ Calculates the longitude and latitude of the linear feature(s) specified by `plunge` and `bearing`. Parameters ---------- plunge : number or sequence of numbers The plunge of the line(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the line(s) in degrees. Returns ------- lon, lat : Arrays of longitude and latitude in radians. """ plunge, bearing = np.atleast_1d(plunge, bearing) # Plot the approriate point for a bearing of 0 and rotate it lat = 90 - plunge lon = 0 lon, lat = _rotate(lon, lat, bearing) return lon, lat
[ "def", "line", "(", "plunge", ",", "bearing", ")", ":", "plunge", ",", "bearing", "=", "np", ".", "atleast_1d", "(", "plunge", ",", "bearing", ")", "# Plot the approriate point for a bearing of 0 and rotate it", "lat", "=", "90", "-", "plunge", "lon", "=", "0", "lon", ",", "lat", "=", "_rotate", "(", "lon", ",", "lat", ",", "bearing", ")", "return", "lon", ",", "lat" ]
Calculates the longitude and latitude of the linear feature(s) specified by `plunge` and `bearing`. Parameters ---------- plunge : number or sequence of numbers The plunge of the line(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the line(s) in degrees. Returns ------- lon, lat : Arrays of longitude and latitude in radians.
[ "Calculates", "the", "longitude", "and", "latitude", "of", "the", "linear", "feature", "(", "s", ")", "specified", "by", "plunge", "and", "bearing", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L245-L267
joferkington/mplstereonet
mplstereonet/stereonet_math.py
cone
def cone(plunge, bearing, angle, segments=100): """ Calculates the longitude and latitude of the small circle (i.e. a cone) centered at the given *plunge* and *bearing* with an apical angle of *angle*, all in degrees. Parameters ---------- plunge : number or sequence of numbers The plunge of the center of the cone(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the center of the cone(s) in degrees. angle : number or sequence of numbers The apical angle (i.e. radius) of the cone(s) in degrees. segments : int, optional The number of vertices in the small circle. Returns ------- lon, lat : arrays `num_measurements` x `num_segments` arrays of longitude and latitude in radians. """ plunges, bearings, angles = np.atleast_1d(plunge, bearing, angle) lons, lats = [], [] for plunge, bearing, angle in zip(plunges, bearings, angles): lat = (90 - angle) * np.ones(segments, dtype=float) lon = np.linspace(-180, 180, segments) lon, lat = _rotate(lon, lat, -plunge, axis='y') lon, lat = _rotate(np.degrees(lon), np.degrees(lat), bearing, axis='x') lons.append(lon) lats.append(lat) return np.vstack(lons), np.vstack(lats)
python
def cone(plunge, bearing, angle, segments=100): """ Calculates the longitude and latitude of the small circle (i.e. a cone) centered at the given *plunge* and *bearing* with an apical angle of *angle*, all in degrees. Parameters ---------- plunge : number or sequence of numbers The plunge of the center of the cone(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the center of the cone(s) in degrees. angle : number or sequence of numbers The apical angle (i.e. radius) of the cone(s) in degrees. segments : int, optional The number of vertices in the small circle. Returns ------- lon, lat : arrays `num_measurements` x `num_segments` arrays of longitude and latitude in radians. """ plunges, bearings, angles = np.atleast_1d(plunge, bearing, angle) lons, lats = [], [] for plunge, bearing, angle in zip(plunges, bearings, angles): lat = (90 - angle) * np.ones(segments, dtype=float) lon = np.linspace(-180, 180, segments) lon, lat = _rotate(lon, lat, -plunge, axis='y') lon, lat = _rotate(np.degrees(lon), np.degrees(lat), bearing, axis='x') lons.append(lon) lats.append(lat) return np.vstack(lons), np.vstack(lats)
[ "def", "cone", "(", "plunge", ",", "bearing", ",", "angle", ",", "segments", "=", "100", ")", ":", "plunges", ",", "bearings", ",", "angles", "=", "np", ".", "atleast_1d", "(", "plunge", ",", "bearing", ",", "angle", ")", "lons", ",", "lats", "=", "[", "]", ",", "[", "]", "for", "plunge", ",", "bearing", ",", "angle", "in", "zip", "(", "plunges", ",", "bearings", ",", "angles", ")", ":", "lat", "=", "(", "90", "-", "angle", ")", "*", "np", ".", "ones", "(", "segments", ",", "dtype", "=", "float", ")", "lon", "=", "np", ".", "linspace", "(", "-", "180", ",", "180", ",", "segments", ")", "lon", ",", "lat", "=", "_rotate", "(", "lon", ",", "lat", ",", "-", "plunge", ",", "axis", "=", "'y'", ")", "lon", ",", "lat", "=", "_rotate", "(", "np", ".", "degrees", "(", "lon", ")", ",", "np", ".", "degrees", "(", "lat", ")", ",", "bearing", ",", "axis", "=", "'x'", ")", "lons", ".", "append", "(", "lon", ")", "lats", ".", "append", "(", "lat", ")", "return", "np", ".", "vstack", "(", "lons", ")", ",", "np", ".", "vstack", "(", "lats", ")" ]
Calculates the longitude and latitude of the small circle (i.e. a cone) centered at the given *plunge* and *bearing* with an apical angle of *angle*, all in degrees. Parameters ---------- plunge : number or sequence of numbers The plunge of the center of the cone(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the center of the cone(s) in degrees. angle : number or sequence of numbers The apical angle (i.e. radius) of the cone(s) in degrees. segments : int, optional The number of vertices in the small circle. Returns ------- lon, lat : arrays `num_measurements` x `num_segments` arrays of longitude and latitude in radians.
[ "Calculates", "the", "longitude", "and", "latitude", "of", "the", "small", "circle", "(", "i", ".", "e", ".", "a", "cone", ")", "centered", "at", "the", "given", "*", "plunge", "*", "and", "*", "bearing", "*", "with", "an", "apical", "angle", "of", "*", "angle", "*", "all", "in", "degrees", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L269-L303
joferkington/mplstereonet
mplstereonet/stereonet_math.py
plunge_bearing2pole
def plunge_bearing2pole(plunge, bearing): """ Converts the given `plunge` and `bearing` in degrees to a strike and dip of the plane whose pole would be parallel to the line specified. (i.e. The pole to the plane returned would plot at the same point as the specified plunge and bearing.) Parameters ---------- plunge : number or sequence of numbers The plunge of the line(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the line(s) in degrees. Returns ------- strike, dip : arrays Arrays of strikes and dips in degrees following the right-hand-rule. """ plunge, bearing = np.atleast_1d(plunge, bearing) strike = bearing + 90 dip = 90 - plunge strike[strike >= 360] -= 360 return strike, dip
python
def plunge_bearing2pole(plunge, bearing): """ Converts the given `plunge` and `bearing` in degrees to a strike and dip of the plane whose pole would be parallel to the line specified. (i.e. The pole to the plane returned would plot at the same point as the specified plunge and bearing.) Parameters ---------- plunge : number or sequence of numbers The plunge of the line(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the line(s) in degrees. Returns ------- strike, dip : arrays Arrays of strikes and dips in degrees following the right-hand-rule. """ plunge, bearing = np.atleast_1d(plunge, bearing) strike = bearing + 90 dip = 90 - plunge strike[strike >= 360] -= 360 return strike, dip
[ "def", "plunge_bearing2pole", "(", "plunge", ",", "bearing", ")", ":", "plunge", ",", "bearing", "=", "np", ".", "atleast_1d", "(", "plunge", ",", "bearing", ")", "strike", "=", "bearing", "+", "90", "dip", "=", "90", "-", "plunge", "strike", "[", "strike", ">=", "360", "]", "-=", "360", "return", "strike", ",", "dip" ]
Converts the given `plunge` and `bearing` in degrees to a strike and dip of the plane whose pole would be parallel to the line specified. (i.e. The pole to the plane returned would plot at the same point as the specified plunge and bearing.) Parameters ---------- plunge : number or sequence of numbers The plunge of the line(s) in degrees. The plunge is measured in degrees downward from the end of the feature specified by the bearing. bearing : number or sequence of numbers The bearing (azimuth) of the line(s) in degrees. Returns ------- strike, dip : arrays Arrays of strikes and dips in degrees following the right-hand-rule.
[ "Converts", "the", "given", "plunge", "and", "bearing", "in", "degrees", "to", "a", "strike", "and", "dip", "of", "the", "plane", "whose", "pole", "would", "be", "parallel", "to", "the", "line", "specified", ".", "(", "i", ".", "e", ".", "The", "pole", "to", "the", "plane", "returned", "would", "plot", "at", "the", "same", "point", "as", "the", "specified", "plunge", "and", "bearing", ".", ")" ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L305-L329
joferkington/mplstereonet
mplstereonet/stereonet_math.py
pole2plunge_bearing
def pole2plunge_bearing(strike, dip): """ Converts the given *strike* and *dip* in dgrees of a plane(s) to a plunge and bearing of its pole. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. Returns ------- plunge, bearing : arrays Arrays of plunges and bearings of the pole to the plane(s) in degrees. """ strike, dip = np.atleast_1d(strike, dip) bearing = strike - 90 plunge = 90 - dip bearing[bearing < 0] += 360 return plunge, bearing
python
def pole2plunge_bearing(strike, dip): """ Converts the given *strike* and *dip* in dgrees of a plane(s) to a plunge and bearing of its pole. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. Returns ------- plunge, bearing : arrays Arrays of plunges and bearings of the pole to the plane(s) in degrees. """ strike, dip = np.atleast_1d(strike, dip) bearing = strike - 90 plunge = 90 - dip bearing[bearing < 0] += 360 return plunge, bearing
[ "def", "pole2plunge_bearing", "(", "strike", ",", "dip", ")", ":", "strike", ",", "dip", "=", "np", ".", "atleast_1d", "(", "strike", ",", "dip", ")", "bearing", "=", "strike", "-", "90", "plunge", "=", "90", "-", "dip", "bearing", "[", "bearing", "<", "0", "]", "+=", "360", "return", "plunge", ",", "bearing" ]
Converts the given *strike* and *dip* in dgrees of a plane(s) to a plunge and bearing of its pole. Parameters ---------- strike : number or sequence of numbers The strike of the plane(s) in degrees, with dip direction indicated by the azimuth (e.g. 315 vs. 135) specified following the "right hand rule". dip : number or sequence of numbers The dip of the plane(s) in degrees. Returns ------- plunge, bearing : arrays Arrays of plunges and bearings of the pole to the plane(s) in degrees.
[ "Converts", "the", "given", "*", "strike", "*", "and", "*", "dip", "*", "in", "dgrees", "of", "a", "plane", "(", "s", ")", "to", "a", "plunge", "and", "bearing", "of", "its", "pole", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L331-L354
joferkington/mplstereonet
mplstereonet/stereonet_math.py
mean_vector
def mean_vector(lons, lats): """ Returns the resultant vector from a series of longitudes and latitudes Parameters ---------- lons : array-like A sequence of longitudes (in radians) lats : array-like A sequence of latitudes (in radians) Returns ------- mean_vec : tuple (lon, lat) in radians r_value : number The magnitude of the resultant vector (between 0 and 1) This represents the degree of clustering in the data. """ xyz = sph2cart(lons, lats) xyz = np.vstack(xyz).T mean_vec = xyz.mean(axis=0) r_value = np.linalg.norm(mean_vec) mean_vec = cart2sph(*mean_vec) return mean_vec, r_value
python
def mean_vector(lons, lats): """ Returns the resultant vector from a series of longitudes and latitudes Parameters ---------- lons : array-like A sequence of longitudes (in radians) lats : array-like A sequence of latitudes (in radians) Returns ------- mean_vec : tuple (lon, lat) in radians r_value : number The magnitude of the resultant vector (between 0 and 1) This represents the degree of clustering in the data. """ xyz = sph2cart(lons, lats) xyz = np.vstack(xyz).T mean_vec = xyz.mean(axis=0) r_value = np.linalg.norm(mean_vec) mean_vec = cart2sph(*mean_vec) return mean_vec, r_value
[ "def", "mean_vector", "(", "lons", ",", "lats", ")", ":", "xyz", "=", "sph2cart", "(", "lons", ",", "lats", ")", "xyz", "=", "np", ".", "vstack", "(", "xyz", ")", ".", "T", "mean_vec", "=", "xyz", ".", "mean", "(", "axis", "=", "0", ")", "r_value", "=", "np", ".", "linalg", ".", "norm", "(", "mean_vec", ")", "mean_vec", "=", "cart2sph", "(", "*", "mean_vec", ")", "return", "mean_vec", ",", "r_value" ]
Returns the resultant vector from a series of longitudes and latitudes Parameters ---------- lons : array-like A sequence of longitudes (in radians) lats : array-like A sequence of latitudes (in radians) Returns ------- mean_vec : tuple (lon, lat) in radians r_value : number The magnitude of the resultant vector (between 0 and 1) This represents the degree of clustering in the data.
[ "Returns", "the", "resultant", "vector", "from", "a", "series", "of", "longitudes", "and", "latitudes" ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L356-L380
joferkington/mplstereonet
mplstereonet/stereonet_math.py
fisher_stats
def fisher_stats(lons, lats, conf=95): """ Returns the resultant vector from a series of longitudes and latitudes. If a confidence is set the function additionally returns the opening angle of the confidence small circle (Fisher, 19..) and the dispersion factor (kappa). Parameters ---------- lons : array-like A sequence of longitudes (in radians) lats : array-like A sequence of latitudes (in radians) conf : confidence value The confidence used for the calculation (float). Defaults to None. Returns ------- mean vector: tuple The point that lies in the center of a set of vectors. (Longitude, Latitude) in radians. If 1 vector is passed to the function it returns two None-values. For more than one vector the following 3 values are returned as a tuple: r_value: float The magnitude of the resultant vector (between 0 and 1) This represents the degree of clustering in the data. angle: float The opening angle of the small circle that corresponds to confidence of the calculated direction. kappa: float A measure for the amount of dispersion of a group of layers. For one vector the factor is undefined. Approaches infinity for nearly parallel vectors and zero for highly dispersed vectors. """ xyz = sph2cart(lons, lats) xyz = np.vstack(xyz).T mean_vec = xyz.mean(axis=0) r_value = np.linalg.norm(mean_vec) num = xyz.shape[0] mean_vec = cart2sph(*mean_vec) if num > 1: p = (100.0 - conf) / 100.0 vector_sum = xyz.sum(axis=0) result_vect = np.sqrt(np.sum(np.square(vector_sum))) fract1 = (num - result_vect) / result_vect fract3 = 1.0 / (num - 1.0) angle = np.arccos(1 - fract1 * ((1 / p) ** fract3 - 1)) angle = np.degrees(angle) kappa = (num - 1.0) / (num - result_vect) return mean_vec, (r_value, angle, kappa) else: return None, None
python
def fisher_stats(lons, lats, conf=95): """ Returns the resultant vector from a series of longitudes and latitudes. If a confidence is set the function additionally returns the opening angle of the confidence small circle (Fisher, 19..) and the dispersion factor (kappa). Parameters ---------- lons : array-like A sequence of longitudes (in radians) lats : array-like A sequence of latitudes (in radians) conf : confidence value The confidence used for the calculation (float). Defaults to None. Returns ------- mean vector: tuple The point that lies in the center of a set of vectors. (Longitude, Latitude) in radians. If 1 vector is passed to the function it returns two None-values. For more than one vector the following 3 values are returned as a tuple: r_value: float The magnitude of the resultant vector (between 0 and 1) This represents the degree of clustering in the data. angle: float The opening angle of the small circle that corresponds to confidence of the calculated direction. kappa: float A measure for the amount of dispersion of a group of layers. For one vector the factor is undefined. Approaches infinity for nearly parallel vectors and zero for highly dispersed vectors. """ xyz = sph2cart(lons, lats) xyz = np.vstack(xyz).T mean_vec = xyz.mean(axis=0) r_value = np.linalg.norm(mean_vec) num = xyz.shape[0] mean_vec = cart2sph(*mean_vec) if num > 1: p = (100.0 - conf) / 100.0 vector_sum = xyz.sum(axis=0) result_vect = np.sqrt(np.sum(np.square(vector_sum))) fract1 = (num - result_vect) / result_vect fract3 = 1.0 / (num - 1.0) angle = np.arccos(1 - fract1 * ((1 / p) ** fract3 - 1)) angle = np.degrees(angle) kappa = (num - 1.0) / (num - result_vect) return mean_vec, (r_value, angle, kappa) else: return None, None
[ "def", "fisher_stats", "(", "lons", ",", "lats", ",", "conf", "=", "95", ")", ":", "xyz", "=", "sph2cart", "(", "lons", ",", "lats", ")", "xyz", "=", "np", ".", "vstack", "(", "xyz", ")", ".", "T", "mean_vec", "=", "xyz", ".", "mean", "(", "axis", "=", "0", ")", "r_value", "=", "np", ".", "linalg", ".", "norm", "(", "mean_vec", ")", "num", "=", "xyz", ".", "shape", "[", "0", "]", "mean_vec", "=", "cart2sph", "(", "*", "mean_vec", ")", "if", "num", ">", "1", ":", "p", "=", "(", "100.0", "-", "conf", ")", "/", "100.0", "vector_sum", "=", "xyz", ".", "sum", "(", "axis", "=", "0", ")", "result_vect", "=", "np", ".", "sqrt", "(", "np", ".", "sum", "(", "np", ".", "square", "(", "vector_sum", ")", ")", ")", "fract1", "=", "(", "num", "-", "result_vect", ")", "/", "result_vect", "fract3", "=", "1.0", "/", "(", "num", "-", "1.0", ")", "angle", "=", "np", ".", "arccos", "(", "1", "-", "fract1", "*", "(", "(", "1", "/", "p", ")", "**", "fract3", "-", "1", ")", ")", "angle", "=", "np", ".", "degrees", "(", "angle", ")", "kappa", "=", "(", "num", "-", "1.0", ")", "/", "(", "num", "-", "result_vect", ")", "return", "mean_vec", ",", "(", "r_value", ",", "angle", ",", "kappa", ")", "else", ":", "return", "None", ",", "None" ]
Returns the resultant vector from a series of longitudes and latitudes. If a confidence is set the function additionally returns the opening angle of the confidence small circle (Fisher, 19..) and the dispersion factor (kappa). Parameters ---------- lons : array-like A sequence of longitudes (in radians) lats : array-like A sequence of latitudes (in radians) conf : confidence value The confidence used for the calculation (float). Defaults to None. Returns ------- mean vector: tuple The point that lies in the center of a set of vectors. (Longitude, Latitude) in radians. If 1 vector is passed to the function it returns two None-values. For more than one vector the following 3 values are returned as a tuple: r_value: float The magnitude of the resultant vector (between 0 and 1) This represents the degree of clustering in the data. angle: float The opening angle of the small circle that corresponds to confidence of the calculated direction. kappa: float A measure for the amount of dispersion of a group of layers. For one vector the factor is undefined. Approaches infinity for nearly parallel vectors and zero for highly dispersed vectors.
[ "Returns", "the", "resultant", "vector", "from", "a", "series", "of", "longitudes", "and", "latitudes", ".", "If", "a", "confidence", "is", "set", "the", "function", "additionally", "returns", "the", "opening", "angle", "of", "the", "confidence", "small", "circle", "(", "Fisher", "19", "..", ")", "and", "the", "dispersion", "factor", "(", "kappa", ")", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L382-L437
joferkington/mplstereonet
mplstereonet/stereonet_math.py
geographic2pole
def geographic2pole(lon, lat): """ Converts a longitude and latitude (from a stereonet) into the strike and dip of the plane whose pole lies at the given longitude(s) and latitude(s). Parameters ---------- lon : array-like A sequence of longitudes (or a single longitude) in radians lat : array-like A sequence of latitudes (or a single latitude) in radians Returns ------- strike : array A sequence of strikes in degrees dip : array A sequence of dips in degrees """ plunge, bearing = geographic2plunge_bearing(lon, lat) strike = bearing + 90 strike[strike >= 360] -= 360 dip = 90 - plunge return strike, dip
python
def geographic2pole(lon, lat): """ Converts a longitude and latitude (from a stereonet) into the strike and dip of the plane whose pole lies at the given longitude(s) and latitude(s). Parameters ---------- lon : array-like A sequence of longitudes (or a single longitude) in radians lat : array-like A sequence of latitudes (or a single latitude) in radians Returns ------- strike : array A sequence of strikes in degrees dip : array A sequence of dips in degrees """ plunge, bearing = geographic2plunge_bearing(lon, lat) strike = bearing + 90 strike[strike >= 360] -= 360 dip = 90 - plunge return strike, dip
[ "def", "geographic2pole", "(", "lon", ",", "lat", ")", ":", "plunge", ",", "bearing", "=", "geographic2plunge_bearing", "(", "lon", ",", "lat", ")", "strike", "=", "bearing", "+", "90", "strike", "[", "strike", ">=", "360", "]", "-=", "360", "dip", "=", "90", "-", "plunge", "return", "strike", ",", "dip" ]
Converts a longitude and latitude (from a stereonet) into the strike and dip of the plane whose pole lies at the given longitude(s) and latitude(s). Parameters ---------- lon : array-like A sequence of longitudes (or a single longitude) in radians lat : array-like A sequence of latitudes (or a single latitude) in radians Returns ------- strike : array A sequence of strikes in degrees dip : array A sequence of dips in degrees
[ "Converts", "a", "longitude", "and", "latitude", "(", "from", "a", "stereonet", ")", "into", "the", "strike", "and", "dip", "of", "the", "plane", "whose", "pole", "lies", "at", "the", "given", "longitude", "(", "s", ")", "and", "latitude", "(", "s", ")", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L439-L462
joferkington/mplstereonet
mplstereonet/stereonet_math.py
geographic2plunge_bearing
def geographic2plunge_bearing(lon, lat): """ Converts longitude and latitude in stereonet coordinates into a plunge/bearing. Parameters ---------- lon, lat : numbers or sequences of numbers Longitudes and latitudes in radians as measured from a lower-hemisphere stereonet Returns ------- plunge : array The plunge of the vector in degrees downward from horizontal. bearing : array The bearing of the vector in degrees clockwise from north. """ lon, lat = np.atleast_1d(lon, lat) x, y, z = sph2cart(lon, lat) # Bearing will be in the y-z plane... bearing = np.arctan2(z, y) # Plunge is the angle between the line and the y-z plane r = np.sqrt(x*x + y*y + z*z) r[r == 0] = 1e-15 plunge = np.arcsin(x / r) # Convert back to azimuths in degrees.. plunge, bearing = np.degrees(plunge), np.degrees(bearing) bearing = 90 - bearing bearing[bearing < 0] += 360 # If the plunge angle is upwards, get the opposite end of the line upwards = plunge < 0 plunge[upwards] *= -1 bearing[upwards] -= 180 bearing[upwards & (bearing < 0)] += 360 return plunge, bearing
python
def geographic2plunge_bearing(lon, lat): """ Converts longitude and latitude in stereonet coordinates into a plunge/bearing. Parameters ---------- lon, lat : numbers or sequences of numbers Longitudes and latitudes in radians as measured from a lower-hemisphere stereonet Returns ------- plunge : array The plunge of the vector in degrees downward from horizontal. bearing : array The bearing of the vector in degrees clockwise from north. """ lon, lat = np.atleast_1d(lon, lat) x, y, z = sph2cart(lon, lat) # Bearing will be in the y-z plane... bearing = np.arctan2(z, y) # Plunge is the angle between the line and the y-z plane r = np.sqrt(x*x + y*y + z*z) r[r == 0] = 1e-15 plunge = np.arcsin(x / r) # Convert back to azimuths in degrees.. plunge, bearing = np.degrees(plunge), np.degrees(bearing) bearing = 90 - bearing bearing[bearing < 0] += 360 # If the plunge angle is upwards, get the opposite end of the line upwards = plunge < 0 plunge[upwards] *= -1 bearing[upwards] -= 180 bearing[upwards & (bearing < 0)] += 360 return plunge, bearing
[ "def", "geographic2plunge_bearing", "(", "lon", ",", "lat", ")", ":", "lon", ",", "lat", "=", "np", ".", "atleast_1d", "(", "lon", ",", "lat", ")", "x", ",", "y", ",", "z", "=", "sph2cart", "(", "lon", ",", "lat", ")", "# Bearing will be in the y-z plane...", "bearing", "=", "np", ".", "arctan2", "(", "z", ",", "y", ")", "# Plunge is the angle between the line and the y-z plane", "r", "=", "np", ".", "sqrt", "(", "x", "*", "x", "+", "y", "*", "y", "+", "z", "*", "z", ")", "r", "[", "r", "==", "0", "]", "=", "1e-15", "plunge", "=", "np", ".", "arcsin", "(", "x", "/", "r", ")", "# Convert back to azimuths in degrees..", "plunge", ",", "bearing", "=", "np", ".", "degrees", "(", "plunge", ")", ",", "np", ".", "degrees", "(", "bearing", ")", "bearing", "=", "90", "-", "bearing", "bearing", "[", "bearing", "<", "0", "]", "+=", "360", "# If the plunge angle is upwards, get the opposite end of the line", "upwards", "=", "plunge", "<", "0", "plunge", "[", "upwards", "]", "*=", "-", "1", "bearing", "[", "upwards", "]", "-=", "180", "bearing", "[", "upwards", "&", "(", "bearing", "<", "0", ")", "]", "+=", "360", "return", "plunge", ",", "bearing" ]
Converts longitude and latitude in stereonet coordinates into a plunge/bearing. Parameters ---------- lon, lat : numbers or sequences of numbers Longitudes and latitudes in radians as measured from a lower-hemisphere stereonet Returns ------- plunge : array The plunge of the vector in degrees downward from horizontal. bearing : array The bearing of the vector in degrees clockwise from north.
[ "Converts", "longitude", "and", "latitude", "in", "stereonet", "coordinates", "into", "a", "plunge", "/", "bearing", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L464-L504
joferkington/mplstereonet
mplstereonet/stereonet_math.py
plane_intersection
def plane_intersection(strike1, dip1, strike2, dip2): """ Finds the intersection of two planes. Returns a plunge/bearing of the linear intersection of the two planes. Also accepts sequences of strike1s, dip1s, strike2s, dip2s. Parameters ---------- strike1, dip1 : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the first plane(s). strike2, dip2 : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the second plane(s). Returns ------- plunge, bearing : arrays The plunge and bearing(s) (in degrees) of the line representing the intersection of the two planes. """ norm1 = sph2cart(*pole(strike1, dip1)) norm2 = sph2cart(*pole(strike2, dip2)) norm1, norm2 = np.array(norm1), np.array(norm2) lon, lat = cart2sph(*np.cross(norm1, norm2, axis=0)) return geographic2plunge_bearing(lon, lat)
python
def plane_intersection(strike1, dip1, strike2, dip2): """ Finds the intersection of two planes. Returns a plunge/bearing of the linear intersection of the two planes. Also accepts sequences of strike1s, dip1s, strike2s, dip2s. Parameters ---------- strike1, dip1 : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the first plane(s). strike2, dip2 : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the second plane(s). Returns ------- plunge, bearing : arrays The plunge and bearing(s) (in degrees) of the line representing the intersection of the two planes. """ norm1 = sph2cart(*pole(strike1, dip1)) norm2 = sph2cart(*pole(strike2, dip2)) norm1, norm2 = np.array(norm1), np.array(norm2) lon, lat = cart2sph(*np.cross(norm1, norm2, axis=0)) return geographic2plunge_bearing(lon, lat)
[ "def", "plane_intersection", "(", "strike1", ",", "dip1", ",", "strike2", ",", "dip2", ")", ":", "norm1", "=", "sph2cart", "(", "*", "pole", "(", "strike1", ",", "dip1", ")", ")", "norm2", "=", "sph2cart", "(", "*", "pole", "(", "strike2", ",", "dip2", ")", ")", "norm1", ",", "norm2", "=", "np", ".", "array", "(", "norm1", ")", ",", "np", ".", "array", "(", "norm2", ")", "lon", ",", "lat", "=", "cart2sph", "(", "*", "np", ".", "cross", "(", "norm1", ",", "norm2", ",", "axis", "=", "0", ")", ")", "return", "geographic2plunge_bearing", "(", "lon", ",", "lat", ")" ]
Finds the intersection of two planes. Returns a plunge/bearing of the linear intersection of the two planes. Also accepts sequences of strike1s, dip1s, strike2s, dip2s. Parameters ---------- strike1, dip1 : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the first plane(s). strike2, dip2 : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the second plane(s). Returns ------- plunge, bearing : arrays The plunge and bearing(s) (in degrees) of the line representing the intersection of the two planes.
[ "Finds", "the", "intersection", "of", "two", "planes", ".", "Returns", "a", "plunge", "/", "bearing", "of", "the", "linear", "intersection", "of", "the", "two", "planes", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L506-L532
joferkington/mplstereonet
mplstereonet/stereonet_math.py
project_onto_plane
def project_onto_plane(strike, dip, plunge, bearing): """ Projects a linear feature(s) onto the surface of a plane. Returns a rake angle(s) along the plane. This is also useful for finding the rake angle of a feature that already intersects the plane in question. Parameters ---------- strike, dip : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the plane(s). plunge, bearing : numbers or sequences of numbers The plunge and bearing (in degrees) or of the linear feature(s) to be projected onto the plane. Returns ------- rake : array A sequence of rake angles measured downwards from horizontal in degrees. Zero degrees corresponds to the "right- hand" direction indicated by the strike, while a negative angle corresponds to the opposite direction. Rakes returned by this function will always be between -90 and 90 (inclusive). """ # Project the line onto the plane norm = sph2cart(*pole(strike, dip)) feature = sph2cart(*line(plunge, bearing)) norm, feature = np.array(norm), np.array(feature) perp = np.cross(norm, feature, axis=0) on_plane = np.cross(perp, norm, axis=0) on_plane /= np.sqrt(np.sum(on_plane**2, axis=0)) # Calculate the angle between the projected feature and horizontal # This is just a dot product, but we need to work with multiple measurements # at once, so einsum is quicker than apply_along_axis. strike_vec = sph2cart(*line(0, strike)) dot = np.einsum('ij,ij->j', on_plane, strike_vec) rake = np.degrees(np.arccos(dot)) # Convert rakes over 90 to negative rakes... rake[rake > 90] -= 180 rake[rake < -90] += 180 return rake
python
def project_onto_plane(strike, dip, plunge, bearing): """ Projects a linear feature(s) onto the surface of a plane. Returns a rake angle(s) along the plane. This is also useful for finding the rake angle of a feature that already intersects the plane in question. Parameters ---------- strike, dip : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the plane(s). plunge, bearing : numbers or sequences of numbers The plunge and bearing (in degrees) or of the linear feature(s) to be projected onto the plane. Returns ------- rake : array A sequence of rake angles measured downwards from horizontal in degrees. Zero degrees corresponds to the "right- hand" direction indicated by the strike, while a negative angle corresponds to the opposite direction. Rakes returned by this function will always be between -90 and 90 (inclusive). """ # Project the line onto the plane norm = sph2cart(*pole(strike, dip)) feature = sph2cart(*line(plunge, bearing)) norm, feature = np.array(norm), np.array(feature) perp = np.cross(norm, feature, axis=0) on_plane = np.cross(perp, norm, axis=0) on_plane /= np.sqrt(np.sum(on_plane**2, axis=0)) # Calculate the angle between the projected feature and horizontal # This is just a dot product, but we need to work with multiple measurements # at once, so einsum is quicker than apply_along_axis. strike_vec = sph2cart(*line(0, strike)) dot = np.einsum('ij,ij->j', on_plane, strike_vec) rake = np.degrees(np.arccos(dot)) # Convert rakes over 90 to negative rakes... rake[rake > 90] -= 180 rake[rake < -90] += 180 return rake
[ "def", "project_onto_plane", "(", "strike", ",", "dip", ",", "plunge", ",", "bearing", ")", ":", "# Project the line onto the plane", "norm", "=", "sph2cart", "(", "*", "pole", "(", "strike", ",", "dip", ")", ")", "feature", "=", "sph2cart", "(", "*", "line", "(", "plunge", ",", "bearing", ")", ")", "norm", ",", "feature", "=", "np", ".", "array", "(", "norm", ")", ",", "np", ".", "array", "(", "feature", ")", "perp", "=", "np", ".", "cross", "(", "norm", ",", "feature", ",", "axis", "=", "0", ")", "on_plane", "=", "np", ".", "cross", "(", "perp", ",", "norm", ",", "axis", "=", "0", ")", "on_plane", "/=", "np", ".", "sqrt", "(", "np", ".", "sum", "(", "on_plane", "**", "2", ",", "axis", "=", "0", ")", ")", "# Calculate the angle between the projected feature and horizontal", "# This is just a dot product, but we need to work with multiple measurements", "# at once, so einsum is quicker than apply_along_axis.", "strike_vec", "=", "sph2cart", "(", "*", "line", "(", "0", ",", "strike", ")", ")", "dot", "=", "np", ".", "einsum", "(", "'ij,ij->j'", ",", "on_plane", ",", "strike_vec", ")", "rake", "=", "np", ".", "degrees", "(", "np", ".", "arccos", "(", "dot", ")", ")", "# Convert rakes over 90 to negative rakes...", "rake", "[", "rake", ">", "90", "]", "-=", "180", "rake", "[", "rake", "<", "-", "90", "]", "+=", "180", "return", "rake" ]
Projects a linear feature(s) onto the surface of a plane. Returns a rake angle(s) along the plane. This is also useful for finding the rake angle of a feature that already intersects the plane in question. Parameters ---------- strike, dip : numbers or sequences of numbers The strike and dip (in degrees, following the right-hand-rule) of the plane(s). plunge, bearing : numbers or sequences of numbers The plunge and bearing (in degrees) or of the linear feature(s) to be projected onto the plane. Returns ------- rake : array A sequence of rake angles measured downwards from horizontal in degrees. Zero degrees corresponds to the "right- hand" direction indicated by the strike, while a negative angle corresponds to the opposite direction. Rakes returned by this function will always be between -90 and 90 (inclusive).
[ "Projects", "a", "linear", "feature", "(", "s", ")", "onto", "the", "surface", "of", "a", "plane", ".", "Returns", "a", "rake", "angle", "(", "s", ")", "along", "the", "plane", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L534-L578
joferkington/mplstereonet
mplstereonet/stereonet_math.py
azimuth2rake
def azimuth2rake(strike, dip, azimuth): """ Projects an azimuth of a linear feature onto a plane as a rake angle. Parameters ---------- strike, dip : numbers The strike and dip of the plane in degrees following the right-hand-rule. azimuth : numbers The azimuth of the linear feature in degrees clockwise from north (i.e. a 0-360 azimuth). Returns ------- rake : number A rake angle in degrees measured downwards from horizontal. Negative values correspond to the opposite end of the strike. """ plunge, bearing = plane_intersection(strike, dip, azimuth, 90) rake = project_onto_plane(strike, dip, plunge, bearing) return rake
python
def azimuth2rake(strike, dip, azimuth): """ Projects an azimuth of a linear feature onto a plane as a rake angle. Parameters ---------- strike, dip : numbers The strike and dip of the plane in degrees following the right-hand-rule. azimuth : numbers The azimuth of the linear feature in degrees clockwise from north (i.e. a 0-360 azimuth). Returns ------- rake : number A rake angle in degrees measured downwards from horizontal. Negative values correspond to the opposite end of the strike. """ plunge, bearing = plane_intersection(strike, dip, azimuth, 90) rake = project_onto_plane(strike, dip, plunge, bearing) return rake
[ "def", "azimuth2rake", "(", "strike", ",", "dip", ",", "azimuth", ")", ":", "plunge", ",", "bearing", "=", "plane_intersection", "(", "strike", ",", "dip", ",", "azimuth", ",", "90", ")", "rake", "=", "project_onto_plane", "(", "strike", ",", "dip", ",", "plunge", ",", "bearing", ")", "return", "rake" ]
Projects an azimuth of a linear feature onto a plane as a rake angle. Parameters ---------- strike, dip : numbers The strike and dip of the plane in degrees following the right-hand-rule. azimuth : numbers The azimuth of the linear feature in degrees clockwise from north (i.e. a 0-360 azimuth). Returns ------- rake : number A rake angle in degrees measured downwards from horizontal. Negative values correspond to the opposite end of the strike.
[ "Projects", "an", "azimuth", "of", "a", "linear", "feature", "onto", "a", "plane", "as", "a", "rake", "angle", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L580-L601
joferkington/mplstereonet
mplstereonet/stereonet_math.py
xyz2stereonet
def xyz2stereonet(x, y, z): """ Converts x, y, z in _world_ cartesian coordinates into lower-hemisphere stereonet coordinates. Parameters ---------- x, y, z : array-likes Sequences of world coordinates Returns ------- lon, lat : arrays Sequences of longitudes and latitudes (in radians) """ x, y, z = np.atleast_1d(x, y, z) return cart2sph(-z, x, y)
python
def xyz2stereonet(x, y, z): """ Converts x, y, z in _world_ cartesian coordinates into lower-hemisphere stereonet coordinates. Parameters ---------- x, y, z : array-likes Sequences of world coordinates Returns ------- lon, lat : arrays Sequences of longitudes and latitudes (in radians) """ x, y, z = np.atleast_1d(x, y, z) return cart2sph(-z, x, y)
[ "def", "xyz2stereonet", "(", "x", ",", "y", ",", "z", ")", ":", "x", ",", "y", ",", "z", "=", "np", ".", "atleast_1d", "(", "x", ",", "y", ",", "z", ")", "return", "cart2sph", "(", "-", "z", ",", "x", ",", "y", ")" ]
Converts x, y, z in _world_ cartesian coordinates into lower-hemisphere stereonet coordinates. Parameters ---------- x, y, z : array-likes Sequences of world coordinates Returns ------- lon, lat : arrays Sequences of longitudes and latitudes (in radians)
[ "Converts", "x", "y", "z", "in", "_world_", "cartesian", "coordinates", "into", "lower", "-", "hemisphere", "stereonet", "coordinates", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L603-L619
joferkington/mplstereonet
mplstereonet/stereonet_math.py
stereonet2xyz
def stereonet2xyz(lon, lat): """ Converts a sequence of longitudes and latitudes from a lower-hemisphere stereonet into _world_ x,y,z coordinates. Parameters ---------- lon, lat : array-likes Sequences of longitudes and latitudes (in radians) from a lower-hemisphere stereonet Returns ------- x, y, z : arrays The world x,y,z components of the vectors represented by the lon, lat coordinates on the stereonet. """ lon, lat = np.atleast_1d(lon, lat) x, y, z = sph2cart(lon, lat) return y, z, -x
python
def stereonet2xyz(lon, lat): """ Converts a sequence of longitudes and latitudes from a lower-hemisphere stereonet into _world_ x,y,z coordinates. Parameters ---------- lon, lat : array-likes Sequences of longitudes and latitudes (in radians) from a lower-hemisphere stereonet Returns ------- x, y, z : arrays The world x,y,z components of the vectors represented by the lon, lat coordinates on the stereonet. """ lon, lat = np.atleast_1d(lon, lat) x, y, z = sph2cart(lon, lat) return y, z, -x
[ "def", "stereonet2xyz", "(", "lon", ",", "lat", ")", ":", "lon", ",", "lat", "=", "np", ".", "atleast_1d", "(", "lon", ",", "lat", ")", "x", ",", "y", ",", "z", "=", "sph2cart", "(", "lon", ",", "lat", ")", "return", "y", ",", "z", ",", "-", "x" ]
Converts a sequence of longitudes and latitudes from a lower-hemisphere stereonet into _world_ x,y,z coordinates. Parameters ---------- lon, lat : array-likes Sequences of longitudes and latitudes (in radians) from a lower-hemisphere stereonet Returns ------- x, y, z : arrays The world x,y,z components of the vectors represented by the lon, lat coordinates on the stereonet.
[ "Converts", "a", "sequence", "of", "longitudes", "and", "latitudes", "from", "a", "lower", "-", "hemisphere", "stereonet", "into", "_world_", "x", "y", "z", "coordinates", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L621-L640
joferkington/mplstereonet
mplstereonet/stereonet_math.py
angular_distance
def angular_distance(first, second, bidirectional=True): """ Calculate the angular distance between two linear features or elementwise angular distance between two sets of linear features. (Note: a linear feature in this context is a point on a stereonet represented by a single latitude and longitude.) Parameters ---------- first : (lon, lat) 2xN array-like or sequence of two numbers The longitudes and latitudes of the first measurements in radians. second : (lon, lat) 2xN array-like or sequence of two numbers The longitudes and latitudes of the second measurements in radians. bidirectional : boolean If True, only "inner" angles will be returned. In other words, all angles returned by this function will be in the range [0, pi/2] (0 to 90 in degrees). Otherwise, ``first`` and ``second`` will be treated as vectors going from the origin outwards instead of bidirectional infinite lines. Therefore, with ``bidirectional=False``, angles returned by this function will be in the range [0, pi] (zero to 180 degrees). Returns ------- dist : array The elementwise angular distance between each pair of measurements in (lon1, lat1) and (lon2, lat2). Examples -------- Calculate the angle between two lines specified as a plunge/bearing >>> angle = angular_distance(line(30, 270), line(40, 90)) >>> np.degrees(angle) array([ 70.]) Let's do the same, but change the "bidirectional" argument: >>> first, second = line(30, 270), line(40, 90) >>> angle = angular_distance(first, second, bidirectional=False) >>> np.degrees(angle) array([ 110.]) Calculate the angle between two planes. >>> angle = angular_distance(pole(0, 10), pole(180, 10)) >>> np.degrees(angle) array([ 20.]) """ lon1, lat1 = first lon2, lat2 = second lon1, lat1, lon2, lat2 = np.atleast_1d(lon1, lat1, lon2, lat2) xyz1 = sph2cart(lon1, lat1) xyz2 = sph2cart(lon2, lat2) # This is just a dot product, but we need to work with multiple measurements # at once, so einsum is quicker than apply_along_axis. dot = np.einsum('ij,ij->j', xyz1, xyz2) angle = np.arccos(dot) # There are numerical sensitivity issues around 180 and 0 degrees... # Sometimes a result will have an absolute value slighly over 1. if np.any(np.isnan(angle)): rtol = 1e-4 angle[np.isclose(dot, -1, rtol)] = np.pi angle[np.isclose(dot, 1, rtol)] = 0 if bidirectional: mask = angle > np.pi / 2 angle[mask] = np.pi - angle[mask] return angle
python
def angular_distance(first, second, bidirectional=True): """ Calculate the angular distance between two linear features or elementwise angular distance between two sets of linear features. (Note: a linear feature in this context is a point on a stereonet represented by a single latitude and longitude.) Parameters ---------- first : (lon, lat) 2xN array-like or sequence of two numbers The longitudes and latitudes of the first measurements in radians. second : (lon, lat) 2xN array-like or sequence of two numbers The longitudes and latitudes of the second measurements in radians. bidirectional : boolean If True, only "inner" angles will be returned. In other words, all angles returned by this function will be in the range [0, pi/2] (0 to 90 in degrees). Otherwise, ``first`` and ``second`` will be treated as vectors going from the origin outwards instead of bidirectional infinite lines. Therefore, with ``bidirectional=False``, angles returned by this function will be in the range [0, pi] (zero to 180 degrees). Returns ------- dist : array The elementwise angular distance between each pair of measurements in (lon1, lat1) and (lon2, lat2). Examples -------- Calculate the angle between two lines specified as a plunge/bearing >>> angle = angular_distance(line(30, 270), line(40, 90)) >>> np.degrees(angle) array([ 70.]) Let's do the same, but change the "bidirectional" argument: >>> first, second = line(30, 270), line(40, 90) >>> angle = angular_distance(first, second, bidirectional=False) >>> np.degrees(angle) array([ 110.]) Calculate the angle between two planes. >>> angle = angular_distance(pole(0, 10), pole(180, 10)) >>> np.degrees(angle) array([ 20.]) """ lon1, lat1 = first lon2, lat2 = second lon1, lat1, lon2, lat2 = np.atleast_1d(lon1, lat1, lon2, lat2) xyz1 = sph2cart(lon1, lat1) xyz2 = sph2cart(lon2, lat2) # This is just a dot product, but we need to work with multiple measurements # at once, so einsum is quicker than apply_along_axis. dot = np.einsum('ij,ij->j', xyz1, xyz2) angle = np.arccos(dot) # There are numerical sensitivity issues around 180 and 0 degrees... # Sometimes a result will have an absolute value slighly over 1. if np.any(np.isnan(angle)): rtol = 1e-4 angle[np.isclose(dot, -1, rtol)] = np.pi angle[np.isclose(dot, 1, rtol)] = 0 if bidirectional: mask = angle > np.pi / 2 angle[mask] = np.pi - angle[mask] return angle
[ "def", "angular_distance", "(", "first", ",", "second", ",", "bidirectional", "=", "True", ")", ":", "lon1", ",", "lat1", "=", "first", "lon2", ",", "lat2", "=", "second", "lon1", ",", "lat1", ",", "lon2", ",", "lat2", "=", "np", ".", "atleast_1d", "(", "lon1", ",", "lat1", ",", "lon2", ",", "lat2", ")", "xyz1", "=", "sph2cart", "(", "lon1", ",", "lat1", ")", "xyz2", "=", "sph2cart", "(", "lon2", ",", "lat2", ")", "# This is just a dot product, but we need to work with multiple measurements", "# at once, so einsum is quicker than apply_along_axis.", "dot", "=", "np", ".", "einsum", "(", "'ij,ij->j'", ",", "xyz1", ",", "xyz2", ")", "angle", "=", "np", ".", "arccos", "(", "dot", ")", "# There are numerical sensitivity issues around 180 and 0 degrees...", "# Sometimes a result will have an absolute value slighly over 1.", "if", "np", ".", "any", "(", "np", ".", "isnan", "(", "angle", ")", ")", ":", "rtol", "=", "1e-4", "angle", "[", "np", ".", "isclose", "(", "dot", ",", "-", "1", ",", "rtol", ")", "]", "=", "np", ".", "pi", "angle", "[", "np", ".", "isclose", "(", "dot", ",", "1", ",", "rtol", ")", "]", "=", "0", "if", "bidirectional", ":", "mask", "=", "angle", ">", "np", ".", "pi", "/", "2", "angle", "[", "mask", "]", "=", "np", ".", "pi", "-", "angle", "[", "mask", "]", "return", "angle" ]
Calculate the angular distance between two linear features or elementwise angular distance between two sets of linear features. (Note: a linear feature in this context is a point on a stereonet represented by a single latitude and longitude.) Parameters ---------- first : (lon, lat) 2xN array-like or sequence of two numbers The longitudes and latitudes of the first measurements in radians. second : (lon, lat) 2xN array-like or sequence of two numbers The longitudes and latitudes of the second measurements in radians. bidirectional : boolean If True, only "inner" angles will be returned. In other words, all angles returned by this function will be in the range [0, pi/2] (0 to 90 in degrees). Otherwise, ``first`` and ``second`` will be treated as vectors going from the origin outwards instead of bidirectional infinite lines. Therefore, with ``bidirectional=False``, angles returned by this function will be in the range [0, pi] (zero to 180 degrees). Returns ------- dist : array The elementwise angular distance between each pair of measurements in (lon1, lat1) and (lon2, lat2). Examples -------- Calculate the angle between two lines specified as a plunge/bearing >>> angle = angular_distance(line(30, 270), line(40, 90)) >>> np.degrees(angle) array([ 70.]) Let's do the same, but change the "bidirectional" argument: >>> first, second = line(30, 270), line(40, 90) >>> angle = angular_distance(first, second, bidirectional=False) >>> np.degrees(angle) array([ 110.]) Calculate the angle between two planes. >>> angle = angular_distance(pole(0, 10), pole(180, 10)) >>> np.degrees(angle) array([ 20.])
[ "Calculate", "the", "angular", "distance", "between", "two", "linear", "features", "or", "elementwise", "angular", "distance", "between", "two", "sets", "of", "linear", "features", ".", "(", "Note", ":", "a", "linear", "feature", "in", "this", "context", "is", "a", "point", "on", "a", "stereonet", "represented", "by", "a", "single", "latitude", "and", "longitude", ".", ")" ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L692-L763
joferkington/mplstereonet
mplstereonet/stereonet_math.py
_repole
def _repole(lon, lat, center): """ Reproject data such that ``center`` is the north pole. Returns lon, lat in the new, rotated reference frame. This is currently a sketch for a later function. Do not assume it works correctly. """ vec3 = sph2cart(*center) vec3 = np.squeeze(vec3) if not np.allclose(vec3, [0, 0, 1]): vec1 = np.cross(vec3, [0, 0, 1]) else: vec1 = np.cross(vec3, [1, 0, 0]) vec2 = np.cross(vec3, vec1) vecs = [item / np.linalg.norm(item) for item in [vec1, vec2, vec3]] basis = np.column_stack(vecs) xyz = sph2cart(lon, lat) xyz = np.column_stack(xyz) prime = xyz.dot(np.linalg.inv(basis)) lon, lat = cart2sph(*prime.T) return lon[:,None], lat[:,None]
python
def _repole(lon, lat, center): """ Reproject data such that ``center`` is the north pole. Returns lon, lat in the new, rotated reference frame. This is currently a sketch for a later function. Do not assume it works correctly. """ vec3 = sph2cart(*center) vec3 = np.squeeze(vec3) if not np.allclose(vec3, [0, 0, 1]): vec1 = np.cross(vec3, [0, 0, 1]) else: vec1 = np.cross(vec3, [1, 0, 0]) vec2 = np.cross(vec3, vec1) vecs = [item / np.linalg.norm(item) for item in [vec1, vec2, vec3]] basis = np.column_stack(vecs) xyz = sph2cart(lon, lat) xyz = np.column_stack(xyz) prime = xyz.dot(np.linalg.inv(basis)) lon, lat = cart2sph(*prime.T) return lon[:,None], lat[:,None]
[ "def", "_repole", "(", "lon", ",", "lat", ",", "center", ")", ":", "vec3", "=", "sph2cart", "(", "*", "center", ")", "vec3", "=", "np", ".", "squeeze", "(", "vec3", ")", "if", "not", "np", ".", "allclose", "(", "vec3", ",", "[", "0", ",", "0", ",", "1", "]", ")", ":", "vec1", "=", "np", ".", "cross", "(", "vec3", ",", "[", "0", ",", "0", ",", "1", "]", ")", "else", ":", "vec1", "=", "np", ".", "cross", "(", "vec3", ",", "[", "1", ",", "0", ",", "0", "]", ")", "vec2", "=", "np", ".", "cross", "(", "vec3", ",", "vec1", ")", "vecs", "=", "[", "item", "/", "np", ".", "linalg", ".", "norm", "(", "item", ")", "for", "item", "in", "[", "vec1", ",", "vec2", ",", "vec3", "]", "]", "basis", "=", "np", ".", "column_stack", "(", "vecs", ")", "xyz", "=", "sph2cart", "(", "lon", ",", "lat", ")", "xyz", "=", "np", ".", "column_stack", "(", "xyz", ")", "prime", "=", "xyz", ".", "dot", "(", "np", ".", "linalg", ".", "inv", "(", "basis", ")", ")", "lon", ",", "lat", "=", "cart2sph", "(", "*", "prime", ".", "T", ")", "return", "lon", "[", ":", ",", "None", "]", ",", "lat", "[", ":", ",", "None", "]" ]
Reproject data such that ``center`` is the north pole. Returns lon, lat in the new, rotated reference frame. This is currently a sketch for a later function. Do not assume it works correctly.
[ "Reproject", "data", "such", "that", "center", "is", "the", "north", "pole", ".", "Returns", "lon", "lat", "in", "the", "new", "rotated", "reference", "frame", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/stereonet_math.py#L765-L787
joferkington/mplstereonet
mplstereonet/analysis.py
_sd_of_eigenvector
def _sd_of_eigenvector(data, vec, measurement='poles', bidirectional=True): """Unifies ``fit_pole`` and ``fit_girdle``.""" lon, lat = _convert_measurements(data, measurement) vals, vecs = cov_eig(lon, lat, bidirectional) x, y, z = vecs[:, vec] s, d = stereonet_math.geographic2pole(*stereonet_math.cart2sph(x, y, z)) return s[0], d[0]
python
def _sd_of_eigenvector(data, vec, measurement='poles', bidirectional=True): """Unifies ``fit_pole`` and ``fit_girdle``.""" lon, lat = _convert_measurements(data, measurement) vals, vecs = cov_eig(lon, lat, bidirectional) x, y, z = vecs[:, vec] s, d = stereonet_math.geographic2pole(*stereonet_math.cart2sph(x, y, z)) return s[0], d[0]
[ "def", "_sd_of_eigenvector", "(", "data", ",", "vec", ",", "measurement", "=", "'poles'", ",", "bidirectional", "=", "True", ")", ":", "lon", ",", "lat", "=", "_convert_measurements", "(", "data", ",", "measurement", ")", "vals", ",", "vecs", "=", "cov_eig", "(", "lon", ",", "lat", ",", "bidirectional", ")", "x", ",", "y", ",", "z", "=", "vecs", "[", ":", ",", "vec", "]", "s", ",", "d", "=", "stereonet_math", ".", "geographic2pole", "(", "*", "stereonet_math", ".", "cart2sph", "(", "x", ",", "y", ",", "z", ")", ")", "return", "s", "[", "0", "]", ",", "d", "[", "0", "]" ]
Unifies ``fit_pole`` and ``fit_girdle``.
[ "Unifies", "fit_pole", "and", "fit_girdle", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/analysis.py#L126-L132
joferkington/mplstereonet
mplstereonet/analysis.py
eigenvectors
def eigenvectors(*args, **kwargs): """ Finds the 3 eigenvectors and eigenvalues of the 3D covariance matrix of a series of geometries. This can be used to fit a plane/pole to a dataset or for shape fabric analysis (e.g. Flinn/Hsu plots). Input arguments will be interpreted as poles, lines, rakes, or "raw" longitudes and latitudes based on the *measurement* keyword argument. (Defaults to ``"poles"``.) Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``strike`` & ``dip``, both array-like sequences representing poles to planes. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. measurement : {'poles', 'lines', 'rakes', 'radians'}, optional Controls how the input arguments are interpreted. Defaults to ``"poles"``. May be one of the following: ``"poles"`` : Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for density contouring. ``"lines"`` : Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : Arguments are assumed to be "raw" longitudes and latitudes in the underlying projection's coordinate system. bidirectional : boolean, optional Whether or not the antipode of each measurement will be used in the calculation. For almost all use cases, it should. Defaults to True. Returns ------- plunges, bearings, values : sequences of 3 floats each The plunges, bearings, and eigenvalues of the three eigenvectors of the covariance matrix of the input data. The measurements are returned sorted in descending order relative to the eigenvalues. (i.e. The largest eigenvector/eigenvalue is first.) Examples -------- Find the eigenvectors as plunge/bearing and eigenvalues of the 3D covariance matrix of a series of planar measurements: >>> strikes = [270, 65, 280, 300] >>> dips = [20, 15, 10, 5] >>> plu, azi, vals = mplstereonet.eigenvectors(strikes, dips) """ lon, lat = _convert_measurements(args, kwargs.get('measurement', 'poles')) vals, vecs = cov_eig(lon, lat, kwargs.get('bidirectional', True)) lon, lat = stereonet_math.cart2sph(*vecs) plunges, bearings = stereonet_math.geographic2plunge_bearing(lon, lat) # Largest eigenvalue first... return plunges[::-1], bearings[::-1], vals[::-1]
python
def eigenvectors(*args, **kwargs): """ Finds the 3 eigenvectors and eigenvalues of the 3D covariance matrix of a series of geometries. This can be used to fit a plane/pole to a dataset or for shape fabric analysis (e.g. Flinn/Hsu plots). Input arguments will be interpreted as poles, lines, rakes, or "raw" longitudes and latitudes based on the *measurement* keyword argument. (Defaults to ``"poles"``.) Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``strike`` & ``dip``, both array-like sequences representing poles to planes. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. measurement : {'poles', 'lines', 'rakes', 'radians'}, optional Controls how the input arguments are interpreted. Defaults to ``"poles"``. May be one of the following: ``"poles"`` : Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for density contouring. ``"lines"`` : Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : Arguments are assumed to be "raw" longitudes and latitudes in the underlying projection's coordinate system. bidirectional : boolean, optional Whether or not the antipode of each measurement will be used in the calculation. For almost all use cases, it should. Defaults to True. Returns ------- plunges, bearings, values : sequences of 3 floats each The plunges, bearings, and eigenvalues of the three eigenvectors of the covariance matrix of the input data. The measurements are returned sorted in descending order relative to the eigenvalues. (i.e. The largest eigenvector/eigenvalue is first.) Examples -------- Find the eigenvectors as plunge/bearing and eigenvalues of the 3D covariance matrix of a series of planar measurements: >>> strikes = [270, 65, 280, 300] >>> dips = [20, 15, 10, 5] >>> plu, azi, vals = mplstereonet.eigenvectors(strikes, dips) """ lon, lat = _convert_measurements(args, kwargs.get('measurement', 'poles')) vals, vecs = cov_eig(lon, lat, kwargs.get('bidirectional', True)) lon, lat = stereonet_math.cart2sph(*vecs) plunges, bearings = stereonet_math.geographic2plunge_bearing(lon, lat) # Largest eigenvalue first... return plunges[::-1], bearings[::-1], vals[::-1]
[ "def", "eigenvectors", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "lon", ",", "lat", "=", "_convert_measurements", "(", "args", ",", "kwargs", ".", "get", "(", "'measurement'", ",", "'poles'", ")", ")", "vals", ",", "vecs", "=", "cov_eig", "(", "lon", ",", "lat", ",", "kwargs", ".", "get", "(", "'bidirectional'", ",", "True", ")", ")", "lon", ",", "lat", "=", "stereonet_math", ".", "cart2sph", "(", "*", "vecs", ")", "plunges", ",", "bearings", "=", "stereonet_math", ".", "geographic2plunge_bearing", "(", "lon", ",", "lat", ")", "# Largest eigenvalue first...", "return", "plunges", "[", ":", ":", "-", "1", "]", ",", "bearings", "[", ":", ":", "-", "1", "]", ",", "vals", "[", ":", ":", "-", "1", "]" ]
Finds the 3 eigenvectors and eigenvalues of the 3D covariance matrix of a series of geometries. This can be used to fit a plane/pole to a dataset or for shape fabric analysis (e.g. Flinn/Hsu plots). Input arguments will be interpreted as poles, lines, rakes, or "raw" longitudes and latitudes based on the *measurement* keyword argument. (Defaults to ``"poles"``.) Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``strike`` & ``dip``, both array-like sequences representing poles to planes. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. measurement : {'poles', 'lines', 'rakes', 'radians'}, optional Controls how the input arguments are interpreted. Defaults to ``"poles"``. May be one of the following: ``"poles"`` : Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for density contouring. ``"lines"`` : Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : Arguments are assumed to be "raw" longitudes and latitudes in the underlying projection's coordinate system. bidirectional : boolean, optional Whether or not the antipode of each measurement will be used in the calculation. For almost all use cases, it should. Defaults to True. Returns ------- plunges, bearings, values : sequences of 3 floats each The plunges, bearings, and eigenvalues of the three eigenvectors of the covariance matrix of the input data. The measurements are returned sorted in descending order relative to the eigenvalues. (i.e. The largest eigenvector/eigenvalue is first.) Examples -------- Find the eigenvectors as plunge/bearing and eigenvalues of the 3D covariance matrix of a series of planar measurements: >>> strikes = [270, 65, 280, 300] >>> dips = [20, 15, 10, 5] >>> plu, azi, vals = mplstereonet.eigenvectors(strikes, dips)
[ "Finds", "the", "3", "eigenvectors", "and", "eigenvalues", "of", "the", "3D", "covariance", "matrix", "of", "a", "series", "of", "geometries", ".", "This", "can", "be", "used", "to", "fit", "a", "plane", "/", "pole", "to", "a", "dataset", "or", "for", "shape", "fabric", "analysis", "(", "e", ".", "g", ".", "Flinn", "/", "Hsu", "plots", ")", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/analysis.py#L134-L195
joferkington/mplstereonet
mplstereonet/analysis.py
find_mean_vector
def find_mean_vector(*args, **kwargs): """ Returns the mean vector for a set of measurments. By default, this expects the input to be plunges and bearings, but the type of input can be controlled through the ``measurement`` kwarg. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``plunge`` & ``bearing``, both array-like sequences representing linear features. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"lines"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- mean_vector : tuple of two floats The plunge and bearing of the mean vector (in degrees). r_value : float The length of the mean vector (a value between 0 and 1). """ lon, lat = _convert_measurements(args, kwargs.get('measurement', 'lines')) vector, r_value = stereonet_math.mean_vector(lon, lat) plunge, bearing = stereonet_math.geographic2plunge_bearing(*vector) return (plunge[0], bearing[0]), r_value
python
def find_mean_vector(*args, **kwargs): """ Returns the mean vector for a set of measurments. By default, this expects the input to be plunges and bearings, but the type of input can be controlled through the ``measurement`` kwarg. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``plunge`` & ``bearing``, both array-like sequences representing linear features. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"lines"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- mean_vector : tuple of two floats The plunge and bearing of the mean vector (in degrees). r_value : float The length of the mean vector (a value between 0 and 1). """ lon, lat = _convert_measurements(args, kwargs.get('measurement', 'lines')) vector, r_value = stereonet_math.mean_vector(lon, lat) plunge, bearing = stereonet_math.geographic2plunge_bearing(*vector) return (plunge[0], bearing[0]), r_value
[ "def", "find_mean_vector", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "lon", ",", "lat", "=", "_convert_measurements", "(", "args", ",", "kwargs", ".", "get", "(", "'measurement'", ",", "'lines'", ")", ")", "vector", ",", "r_value", "=", "stereonet_math", ".", "mean_vector", "(", "lon", ",", "lat", ")", "plunge", ",", "bearing", "=", "stereonet_math", ".", "geographic2plunge_bearing", "(", "*", "vector", ")", "return", "(", "plunge", "[", "0", "]", ",", "bearing", "[", "0", "]", ")", ",", "r_value" ]
Returns the mean vector for a set of measurments. By default, this expects the input to be plunges and bearings, but the type of input can be controlled through the ``measurement`` kwarg. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``plunge`` & ``bearing``, both array-like sequences representing linear features. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"lines"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- mean_vector : tuple of two floats The plunge and bearing of the mean vector (in degrees). r_value : float The length of the mean vector (a value between 0 and 1).
[ "Returns", "the", "mean", "vector", "for", "a", "set", "of", "measurments", ".", "By", "default", "this", "expects", "the", "input", "to", "be", "plunges", "and", "bearings", "but", "the", "type", "of", "input", "can", "be", "controlled", "through", "the", "measurement", "kwarg", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/analysis.py#L219-L264
joferkington/mplstereonet
mplstereonet/analysis.py
find_fisher_stats
def find_fisher_stats(*args, **kwargs): """ Returns the mean vector and summary statistics for a set of measurements. By default, this expects the input to be plunges and bearings, but the type of input can be controlled through the ``measurement`` kwarg. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``plunge`` & ``bearing``, both array-like sequences representing linear features. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. conf : number The confidence level (0-100). Defaults to 95%, similar to 2 sigma. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"lines"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- mean_vector: tuple of two floats A set consisting of the plunge and bearing of the mean vector (in degrees). stats : tuple of three floats ``(r_value, confidence, kappa)`` The ``r_value`` is the magnitude of the mean vector as a number between 0 and 1. The ``confidence`` radius is the opening angle of a small circle that corresponds to the confidence in the calculated direction, and is dependent on the input ``conf``. The ``kappa`` value is the dispersion factor that quantifies the amount of dispersion of the given vectors, analgous to a variance/stddev. """ # How the heck did this wind up as a separate function? lon, lat = _convert_measurements(args, kwargs.get('measurement', 'lines')) conf = kwargs.get('conf', 95) center, stats = stereonet_math.fisher_stats(lon, lat, conf) plunge, bearing = stereonet_math.geographic2plunge_bearing(*center) mean_vector = (plunge[0], bearing[0]) return mean_vector, stats
python
def find_fisher_stats(*args, **kwargs): """ Returns the mean vector and summary statistics for a set of measurements. By default, this expects the input to be plunges and bearings, but the type of input can be controlled through the ``measurement`` kwarg. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``plunge`` & ``bearing``, both array-like sequences representing linear features. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. conf : number The confidence level (0-100). Defaults to 95%, similar to 2 sigma. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"lines"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- mean_vector: tuple of two floats A set consisting of the plunge and bearing of the mean vector (in degrees). stats : tuple of three floats ``(r_value, confidence, kappa)`` The ``r_value`` is the magnitude of the mean vector as a number between 0 and 1. The ``confidence`` radius is the opening angle of a small circle that corresponds to the confidence in the calculated direction, and is dependent on the input ``conf``. The ``kappa`` value is the dispersion factor that quantifies the amount of dispersion of the given vectors, analgous to a variance/stddev. """ # How the heck did this wind up as a separate function? lon, lat = _convert_measurements(args, kwargs.get('measurement', 'lines')) conf = kwargs.get('conf', 95) center, stats = stereonet_math.fisher_stats(lon, lat, conf) plunge, bearing = stereonet_math.geographic2plunge_bearing(*center) mean_vector = (plunge[0], bearing[0]) return mean_vector, stats
[ "def", "find_fisher_stats", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "# How the heck did this wind up as a separate function?", "lon", ",", "lat", "=", "_convert_measurements", "(", "args", ",", "kwargs", ".", "get", "(", "'measurement'", ",", "'lines'", ")", ")", "conf", "=", "kwargs", ".", "get", "(", "'conf'", ",", "95", ")", "center", ",", "stats", "=", "stereonet_math", ".", "fisher_stats", "(", "lon", ",", "lat", ",", "conf", ")", "plunge", ",", "bearing", "=", "stereonet_math", ".", "geographic2plunge_bearing", "(", "*", "center", ")", "mean_vector", "=", "(", "plunge", "[", "0", "]", ",", "bearing", "[", "0", "]", ")", "return", "mean_vector", ",", "stats" ]
Returns the mean vector and summary statistics for a set of measurements. By default, this expects the input to be plunges and bearings, but the type of input can be controlled through the ``measurement`` kwarg. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``plunge`` & ``bearing``, both array-like sequences representing linear features. (Rake measurements require three parameters, thus the variable number of arguments.) The *measurement* kwarg controls how these arguments are interpreted. conf : number The confidence level (0-100). Defaults to 95%, similar to 2 sigma. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"lines"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- mean_vector: tuple of two floats A set consisting of the plunge and bearing of the mean vector (in degrees). stats : tuple of three floats ``(r_value, confidence, kappa)`` The ``r_value`` is the magnitude of the mean vector as a number between 0 and 1. The ``confidence`` radius is the opening angle of a small circle that corresponds to the confidence in the calculated direction, and is dependent on the input ``conf``. The ``kappa`` value is the dispersion factor that quantifies the amount of dispersion of the given vectors, analgous to a variance/stddev.
[ "Returns", "the", "mean", "vector", "and", "summary", "statistics", "for", "a", "set", "of", "measurements", ".", "By", "default", "this", "expects", "the", "input", "to", "be", "plunges", "and", "bearings", "but", "the", "type", "of", "input", "can", "be", "controlled", "through", "the", "measurement", "kwarg", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/analysis.py#L266-L323
joferkington/mplstereonet
mplstereonet/analysis.py
kmeans
def kmeans(*args, **kwargs): """ Find centers of multi-modal clusters of data using a kmeans approach modified for spherical measurements. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``strike`` & ``dip``, both array-like sequences representing poles to planes. (Rake measurements require three parameters, thus the variable number of arguments.) The ``measurement`` kwarg controls how these arguments are interpreted. num : int The number of clusters to find. Defaults to 2. bidirectional : bool Whether or not the measurements are bi-directional linear/planar features or directed vectors. Defaults to True. tolerance : float Iteration will continue until the centers have not changed by more than this amount. Defaults to 1e-5. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"poles"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- centers : An Nx2 array-like Longitude and latitude in radians of the centers of each cluster. """ lon, lat = _convert_measurements(args, kwargs.get('measurement', 'poles')) num = kwargs.get('num', 2) bidirectional = kwargs.get('bidirectional', True) tolerance = kwargs.get('tolerance', 1e-5) points = lon, lat dist = lambda x: stereonet_math.angular_distance(x, points, bidirectional) center_lon = np.random.choice(lon, num) center_lat = np.random.choice(lat, num) centers = np.column_stack([center_lon, center_lat]) while True: dists = np.array([dist(item) for item in centers]).T closest = dists.argmin(axis=1) new_centers = [] for i in range(num): mask = mask = closest == i _, vecs = cov_eig(lon[mask], lat[mask], bidirectional) new_centers.append(stereonet_math.cart2sph(*vecs[:,-1])) if np.allclose(centers, new_centers, atol=tolerance): break else: centers = new_centers return centers
python
def kmeans(*args, **kwargs): """ Find centers of multi-modal clusters of data using a kmeans approach modified for spherical measurements. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``strike`` & ``dip``, both array-like sequences representing poles to planes. (Rake measurements require three parameters, thus the variable number of arguments.) The ``measurement`` kwarg controls how these arguments are interpreted. num : int The number of clusters to find. Defaults to 2. bidirectional : bool Whether or not the measurements are bi-directional linear/planar features or directed vectors. Defaults to True. tolerance : float Iteration will continue until the centers have not changed by more than this amount. Defaults to 1e-5. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"poles"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- centers : An Nx2 array-like Longitude and latitude in radians of the centers of each cluster. """ lon, lat = _convert_measurements(args, kwargs.get('measurement', 'poles')) num = kwargs.get('num', 2) bidirectional = kwargs.get('bidirectional', True) tolerance = kwargs.get('tolerance', 1e-5) points = lon, lat dist = lambda x: stereonet_math.angular_distance(x, points, bidirectional) center_lon = np.random.choice(lon, num) center_lat = np.random.choice(lat, num) centers = np.column_stack([center_lon, center_lat]) while True: dists = np.array([dist(item) for item in centers]).T closest = dists.argmin(axis=1) new_centers = [] for i in range(num): mask = mask = closest == i _, vecs = cov_eig(lon[mask], lat[mask], bidirectional) new_centers.append(stereonet_math.cart2sph(*vecs[:,-1])) if np.allclose(centers, new_centers, atol=tolerance): break else: centers = new_centers return centers
[ "def", "kmeans", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "lon", ",", "lat", "=", "_convert_measurements", "(", "args", ",", "kwargs", ".", "get", "(", "'measurement'", ",", "'poles'", ")", ")", "num", "=", "kwargs", ".", "get", "(", "'num'", ",", "2", ")", "bidirectional", "=", "kwargs", ".", "get", "(", "'bidirectional'", ",", "True", ")", "tolerance", "=", "kwargs", ".", "get", "(", "'tolerance'", ",", "1e-5", ")", "points", "=", "lon", ",", "lat", "dist", "=", "lambda", "x", ":", "stereonet_math", ".", "angular_distance", "(", "x", ",", "points", ",", "bidirectional", ")", "center_lon", "=", "np", ".", "random", ".", "choice", "(", "lon", ",", "num", ")", "center_lat", "=", "np", ".", "random", ".", "choice", "(", "lat", ",", "num", ")", "centers", "=", "np", ".", "column_stack", "(", "[", "center_lon", ",", "center_lat", "]", ")", "while", "True", ":", "dists", "=", "np", ".", "array", "(", "[", "dist", "(", "item", ")", "for", "item", "in", "centers", "]", ")", ".", "T", "closest", "=", "dists", ".", "argmin", "(", "axis", "=", "1", ")", "new_centers", "=", "[", "]", "for", "i", "in", "range", "(", "num", ")", ":", "mask", "=", "mask", "=", "closest", "==", "i", "_", ",", "vecs", "=", "cov_eig", "(", "lon", "[", "mask", "]", ",", "lat", "[", "mask", "]", ",", "bidirectional", ")", "new_centers", ".", "append", "(", "stereonet_math", ".", "cart2sph", "(", "*", "vecs", "[", ":", ",", "-", "1", "]", ")", ")", "if", "np", ".", "allclose", "(", "centers", ",", "new_centers", ",", "atol", "=", "tolerance", ")", ":", "break", "else", ":", "centers", "=", "new_centers", "return", "centers" ]
Find centers of multi-modal clusters of data using a kmeans approach modified for spherical measurements. Parameters ---------- *args : 2 or 3 sequences of measurements By default, this will be expected to be ``strike`` & ``dip``, both array-like sequences representing poles to planes. (Rake measurements require three parameters, thus the variable number of arguments.) The ``measurement`` kwarg controls how these arguments are interpreted. num : int The number of clusters to find. Defaults to 2. bidirectional : bool Whether or not the measurements are bi-directional linear/planar features or directed vectors. Defaults to True. tolerance : float Iteration will continue until the centers have not changed by more than this amount. Defaults to 1e-5. measurement : string, optional Controls how the input arguments are interpreted. Defaults to ``"poles"``. May be one of the following: ``"poles"`` : strikes, dips Arguments are assumed to be sequences of strikes and dips of planes. Poles to these planes are used for analysis. ``"lines"`` : plunges, bearings Arguments are assumed to be sequences of plunges and bearings of linear features. ``"rakes"`` : strikes, dips, rakes Arguments are assumed to be sequences of strikes, dips, and rakes along the plane. ``"radians"`` : lon, lat Arguments are assumed to be "raw" longitudes and latitudes in the stereonet's underlying coordinate system. Returns ------- centers : An Nx2 array-like Longitude and latitude in radians of the centers of each cluster.
[ "Find", "centers", "of", "multi", "-", "modal", "clusters", "of", "data", "using", "a", "kmeans", "approach", "modified", "for", "spherical", "measurements", "." ]
train
https://github.com/joferkington/mplstereonet/blob/f6d78ca49807915d4223e864e12bb24d497cc2d6/mplstereonet/analysis.py#L325-L400
heroku-python/django-postgrespool
django_postgrespool/base.py
is_disconnect
def is_disconnect(e, connection, cursor): """ Connection state check from SQLAlchemy: https://bitbucket.org/sqlalchemy/sqlalchemy/src/tip/lib/sqlalchemy/dialects/postgresql/psycopg2.py """ if isinstance(e, OperationalError): # these error messages from libpq: interfaces/libpq/fe-misc.c. # TODO: these are sent through gettext in libpq and we can't # check within other locales - consider using connection.closed return 'terminating connection' in str(e) or \ 'closed the connection' in str(e) or \ 'connection not open' in str(e) or \ 'could not receive data from server' in str(e) elif isinstance(e, InterfaceError): # psycopg2 client errors, psycopg2/conenction.h, psycopg2/cursor.h return 'connection already closed' in str(e) or \ 'cursor already closed' in str(e) elif isinstance(e, ProgrammingError): # not sure where this path is originally from, it may # be obsolete. It really says "losed", not "closed". return "closed the connection unexpectedly" in str(e) else: return False
python
def is_disconnect(e, connection, cursor): """ Connection state check from SQLAlchemy: https://bitbucket.org/sqlalchemy/sqlalchemy/src/tip/lib/sqlalchemy/dialects/postgresql/psycopg2.py """ if isinstance(e, OperationalError): # these error messages from libpq: interfaces/libpq/fe-misc.c. # TODO: these are sent through gettext in libpq and we can't # check within other locales - consider using connection.closed return 'terminating connection' in str(e) or \ 'closed the connection' in str(e) or \ 'connection not open' in str(e) or \ 'could not receive data from server' in str(e) elif isinstance(e, InterfaceError): # psycopg2 client errors, psycopg2/conenction.h, psycopg2/cursor.h return 'connection already closed' in str(e) or \ 'cursor already closed' in str(e) elif isinstance(e, ProgrammingError): # not sure where this path is originally from, it may # be obsolete. It really says "losed", not "closed". return "closed the connection unexpectedly" in str(e) else: return False
[ "def", "is_disconnect", "(", "e", ",", "connection", ",", "cursor", ")", ":", "if", "isinstance", "(", "e", ",", "OperationalError", ")", ":", "# these error messages from libpq: interfaces/libpq/fe-misc.c.", "# TODO: these are sent through gettext in libpq and we can't", "# check within other locales - consider using connection.closed", "return", "'terminating connection'", "in", "str", "(", "e", ")", "or", "'closed the connection'", "in", "str", "(", "e", ")", "or", "'connection not open'", "in", "str", "(", "e", ")", "or", "'could not receive data from server'", "in", "str", "(", "e", ")", "elif", "isinstance", "(", "e", ",", "InterfaceError", ")", ":", "# psycopg2 client errors, psycopg2/conenction.h, psycopg2/cursor.h", "return", "'connection already closed'", "in", "str", "(", "e", ")", "or", "'cursor already closed'", "in", "str", "(", "e", ")", "elif", "isinstance", "(", "e", ",", "ProgrammingError", ")", ":", "# not sure where this path is originally from, it may", "# be obsolete. It really says \"losed\", not \"closed\".", "return", "\"closed the connection unexpectedly\"", "in", "str", "(", "e", ")", "else", ":", "return", "False" ]
Connection state check from SQLAlchemy: https://bitbucket.org/sqlalchemy/sqlalchemy/src/tip/lib/sqlalchemy/dialects/postgresql/psycopg2.py
[ "Connection", "state", "check", "from", "SQLAlchemy", ":", "https", ":", "//", "bitbucket", ".", "org", "/", "sqlalchemy", "/", "sqlalchemy", "/", "src", "/", "tip", "/", "lib", "/", "sqlalchemy", "/", "dialects", "/", "postgresql", "/", "psycopg2", ".", "py" ]
train
https://github.com/heroku-python/django-postgrespool/blob/ce83a4d49c19eded86d86d5fcfa8daaeea5ef662/django_postgrespool/base.py#L42-L64
heroku-python/django-postgrespool
django_postgrespool/base.py
DatabaseWrapper._dispose
def _dispose(self): """Dispose of the pool for this instance, closing all connections.""" self.close() # _DBProxy.dispose doesn't actually call dispose on the pool conn_params = self.get_connection_params() key = db_pool._serialize(**conn_params) try: pool = db_pool.pools[key] except KeyError: pass else: pool.dispose() del db_pool.pools[key]
python
def _dispose(self): """Dispose of the pool for this instance, closing all connections.""" self.close() # _DBProxy.dispose doesn't actually call dispose on the pool conn_params = self.get_connection_params() key = db_pool._serialize(**conn_params) try: pool = db_pool.pools[key] except KeyError: pass else: pool.dispose() del db_pool.pools[key]
[ "def", "_dispose", "(", "self", ")", ":", "self", ".", "close", "(", ")", "# _DBProxy.dispose doesn't actually call dispose on the pool", "conn_params", "=", "self", ".", "get_connection_params", "(", ")", "key", "=", "db_pool", ".", "_serialize", "(", "*", "*", "conn_params", ")", "try", ":", "pool", "=", "db_pool", ".", "pools", "[", "key", "]", "except", "KeyError", ":", "pass", "else", ":", "pool", ".", "dispose", "(", ")", "del", "db_pool", ".", "pools", "[", "key", "]" ]
Dispose of the pool for this instance, closing all connections.
[ "Dispose", "of", "the", "pool", "for", "this", "instance", "closing", "all", "connections", "." ]
train
https://github.com/heroku-python/django-postgrespool/blob/ce83a4d49c19eded86d86d5fcfa8daaeea5ef662/django_postgrespool/base.py#L91-L103
stitchfix/fauxtograph
fauxtograph/vaegan.py
calc_fc_size
def calc_fc_size(img_height, img_width): '''Calculates shape of data after encoding. Parameters ---------- img_height : int Height of input image. img_width : int Width of input image. Returns ------- encoded_shape : tuple(int) Gives back 3-tuple with new dims. ''' height, width = img_height, img_width for _ in range(5): height, width = _get_conv_outsize( (height, width), 4, 2, 1) conv_out_layers = 512 return conv_out_layers, height, width
python
def calc_fc_size(img_height, img_width): '''Calculates shape of data after encoding. Parameters ---------- img_height : int Height of input image. img_width : int Width of input image. Returns ------- encoded_shape : tuple(int) Gives back 3-tuple with new dims. ''' height, width = img_height, img_width for _ in range(5): height, width = _get_conv_outsize( (height, width), 4, 2, 1) conv_out_layers = 512 return conv_out_layers, height, width
[ "def", "calc_fc_size", "(", "img_height", ",", "img_width", ")", ":", "height", ",", "width", "=", "img_height", ",", "img_width", "for", "_", "in", "range", "(", "5", ")", ":", "height", ",", "width", "=", "_get_conv_outsize", "(", "(", "height", ",", "width", ")", ",", "4", ",", "2", ",", "1", ")", "conv_out_layers", "=", "512", "return", "conv_out_layers", ",", "height", ",", "width" ]
Calculates shape of data after encoding. Parameters ---------- img_height : int Height of input image. img_width : int Width of input image. Returns ------- encoded_shape : tuple(int) Gives back 3-tuple with new dims.
[ "Calculates", "shape", "of", "data", "after", "encoding", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/vaegan.py#L540-L562
stitchfix/fauxtograph
fauxtograph/vaegan.py
calc_im_size
def calc_im_size(img_height, img_width): '''Calculates shape of data after decoding. Parameters ---------- img_height : int Height of encoded data. img_width : int Width of encoded data. Returns ------- encoded_shape : tuple(int) Gives back 2-tuple with decoded image dimensions. ''' height, width = img_height, img_width for _ in range(5): height, width = _get_deconv_outsize((height, width), 4, 2, 1) return height, width
python
def calc_im_size(img_height, img_width): '''Calculates shape of data after decoding. Parameters ---------- img_height : int Height of encoded data. img_width : int Width of encoded data. Returns ------- encoded_shape : tuple(int) Gives back 2-tuple with decoded image dimensions. ''' height, width = img_height, img_width for _ in range(5): height, width = _get_deconv_outsize((height, width), 4, 2, 1) return height, width
[ "def", "calc_im_size", "(", "img_height", ",", "img_width", ")", ":", "height", ",", "width", "=", "img_height", ",", "img_width", "for", "_", "in", "range", "(", "5", ")", ":", "height", ",", "width", "=", "_get_deconv_outsize", "(", "(", "height", ",", "width", ")", ",", "4", ",", "2", ",", "1", ")", "return", "height", ",", "width" ]
Calculates shape of data after decoding. Parameters ---------- img_height : int Height of encoded data. img_width : int Width of encoded data. Returns ------- encoded_shape : tuple(int) Gives back 2-tuple with decoded image dimensions.
[ "Calculates", "shape", "of", "data", "after", "decoding", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/vaegan.py#L565-L585
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
get_paths
def get_paths(directory): '''Gets all the paths of non-hidden files in a directory and returns a list of those paths. Parameters ---------- directory : str The directory whose contents you wish to grab. Returns ------- paths : List[str] ''' fnames = [os.path.join(directory, f) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f)) and not f.startswith('.')] return fnames
python
def get_paths(directory): '''Gets all the paths of non-hidden files in a directory and returns a list of those paths. Parameters ---------- directory : str The directory whose contents you wish to grab. Returns ------- paths : List[str] ''' fnames = [os.path.join(directory, f) for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f)) and not f.startswith('.')] return fnames
[ "def", "get_paths", "(", "directory", ")", ":", "fnames", "=", "[", "os", ".", "path", ".", "join", "(", "directory", ",", "f", ")", "for", "f", "in", "os", ".", "listdir", "(", "directory", ")", "if", "os", ".", "path", ".", "isfile", "(", "os", ".", "path", ".", "join", "(", "directory", ",", "f", ")", ")", "and", "not", "f", ".", "startswith", "(", "'.'", ")", "]", "return", "fnames" ]
Gets all the paths of non-hidden files in a directory and returns a list of those paths. Parameters ---------- directory : str The directory whose contents you wish to grab. Returns ------- paths : List[str]
[ "Gets", "all", "the", "paths", "of", "non", "-", "hidden", "files", "in", "a", "directory", "and", "returns", "a", "list", "of", "those", "paths", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L1570-L1587
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
image_resize
def image_resize(file_paths, new_dir, width, height): '''Resizes all images with given paths to new dimensions. Uses up/downscaling with antialiasing. Parameters ---------- file_paths : List[str] List of path strings for image to resize. new_dir : str Directory to place resized images. width : int Target width of new resized images. height : int Target height of new resized images. ''' if new_dir[-1] != '/': new_dir += '/' if not os.path.exists(os.path.dirname(new_dir)): os.makedirs(os.path.dirname(new_dir)) for f in tqdm.tqdm(file_paths): img = Image.open(f).resize((width, height), Image.ANTIALIAS).convert('RGB') new = os.path.join(new_dir, os.path.basename(f)) img.save(new)
python
def image_resize(file_paths, new_dir, width, height): '''Resizes all images with given paths to new dimensions. Uses up/downscaling with antialiasing. Parameters ---------- file_paths : List[str] List of path strings for image to resize. new_dir : str Directory to place resized images. width : int Target width of new resized images. height : int Target height of new resized images. ''' if new_dir[-1] != '/': new_dir += '/' if not os.path.exists(os.path.dirname(new_dir)): os.makedirs(os.path.dirname(new_dir)) for f in tqdm.tqdm(file_paths): img = Image.open(f).resize((width, height), Image.ANTIALIAS).convert('RGB') new = os.path.join(new_dir, os.path.basename(f)) img.save(new)
[ "def", "image_resize", "(", "file_paths", ",", "new_dir", ",", "width", ",", "height", ")", ":", "if", "new_dir", "[", "-", "1", "]", "!=", "'/'", ":", "new_dir", "+=", "'/'", "if", "not", "os", ".", "path", ".", "exists", "(", "os", ".", "path", ".", "dirname", "(", "new_dir", ")", ")", ":", "os", ".", "makedirs", "(", "os", ".", "path", ".", "dirname", "(", "new_dir", ")", ")", "for", "f", "in", "tqdm", ".", "tqdm", "(", "file_paths", ")", ":", "img", "=", "Image", ".", "open", "(", "f", ")", ".", "resize", "(", "(", "width", ",", "height", ")", ",", "Image", ".", "ANTIALIAS", ")", ".", "convert", "(", "'RGB'", ")", "new", "=", "os", ".", "path", ".", "join", "(", "new_dir", ",", "os", ".", "path", ".", "basename", "(", "f", ")", ")", "img", ".", "save", "(", "new", ")" ]
Resizes all images with given paths to new dimensions. Uses up/downscaling with antialiasing. Parameters ---------- file_paths : List[str] List of path strings for image to resize. new_dir : str Directory to place resized images. width : int Target width of new resized images. height : int Target height of new resized images.
[ "Resizes", "all", "images", "with", "given", "paths", "to", "new", "dimensions", ".", "Uses", "up", "/", "downscaling", "with", "antialiasing", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L1590-L1614
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAE.inverse_transform
def inverse_transform(self, data, test=False): '''Transform latent vectors into images. Parameters ---------- data : array-like shape (n_images, latent_width) Input numpy array of images. test [optional] : bool Controls the test boolean for batch normalization. Returns ------- images : array-like shape (n_images, image_width, image_height, n_colors) ''' if not type(data) == Variable: if len(data.shape) < 2: data = data[np.newaxis] if len(data.shape) != 2: raise TypeError("Invalid dimensions for latent data. Dim = %s.\ Must be a 2d array." % str(data.shape)) data = Variable(data) else: if len(data.data.shape) < 2: data.data = data.data[np.newaxis] if len(data.data.shape) != 2: raise TypeError("Invalid dimensions for latent data. Dim = %s.\ Must be a 2d array." % str(data.data.shape)) assert data.data.shape[-1] == self.latent_width,\ "Latent shape %d != %d" % (data.data.shape[-1], self.latent_width) if self.flag_gpu: data.to_gpu() out = self.model.decode(data, test=test) out.to_cpu() if self.mode == 'linear': final = out.data else: final = out.data.transpose(0, 2, 3, 1) return final
python
def inverse_transform(self, data, test=False): '''Transform latent vectors into images. Parameters ---------- data : array-like shape (n_images, latent_width) Input numpy array of images. test [optional] : bool Controls the test boolean for batch normalization. Returns ------- images : array-like shape (n_images, image_width, image_height, n_colors) ''' if not type(data) == Variable: if len(data.shape) < 2: data = data[np.newaxis] if len(data.shape) != 2: raise TypeError("Invalid dimensions for latent data. Dim = %s.\ Must be a 2d array." % str(data.shape)) data = Variable(data) else: if len(data.data.shape) < 2: data.data = data.data[np.newaxis] if len(data.data.shape) != 2: raise TypeError("Invalid dimensions for latent data. Dim = %s.\ Must be a 2d array." % str(data.data.shape)) assert data.data.shape[-1] == self.latent_width,\ "Latent shape %d != %d" % (data.data.shape[-1], self.latent_width) if self.flag_gpu: data.to_gpu() out = self.model.decode(data, test=test) out.to_cpu() if self.mode == 'linear': final = out.data else: final = out.data.transpose(0, 2, 3, 1) return final
[ "def", "inverse_transform", "(", "self", ",", "data", ",", "test", "=", "False", ")", ":", "if", "not", "type", "(", "data", ")", "==", "Variable", ":", "if", "len", "(", "data", ".", "shape", ")", "<", "2", ":", "data", "=", "data", "[", "np", ".", "newaxis", "]", "if", "len", "(", "data", ".", "shape", ")", "!=", "2", ":", "raise", "TypeError", "(", "\"Invalid dimensions for latent data. Dim = %s.\\\n Must be a 2d array.\"", "%", "str", "(", "data", ".", "shape", ")", ")", "data", "=", "Variable", "(", "data", ")", "else", ":", "if", "len", "(", "data", ".", "data", ".", "shape", ")", "<", "2", ":", "data", ".", "data", "=", "data", ".", "data", "[", "np", ".", "newaxis", "]", "if", "len", "(", "data", ".", "data", ".", "shape", ")", "!=", "2", ":", "raise", "TypeError", "(", "\"Invalid dimensions for latent data. Dim = %s.\\\n Must be a 2d array.\"", "%", "str", "(", "data", ".", "data", ".", "shape", ")", ")", "assert", "data", ".", "data", ".", "shape", "[", "-", "1", "]", "==", "self", ".", "latent_width", ",", "\"Latent shape %d != %d\"", "%", "(", "data", ".", "data", ".", "shape", "[", "-", "1", "]", ",", "self", ".", "latent_width", ")", "if", "self", ".", "flag_gpu", ":", "data", ".", "to_gpu", "(", ")", "out", "=", "self", ".", "model", ".", "decode", "(", "data", ",", "test", "=", "test", ")", "out", ".", "to_cpu", "(", ")", "if", "self", ".", "mode", "==", "'linear'", ":", "final", "=", "out", ".", "data", "else", ":", "final", "=", "out", ".", "data", ".", "transpose", "(", "0", ",", "2", ",", "3", ",", "1", ")", "return", "final" ]
Transform latent vectors into images. Parameters ---------- data : array-like shape (n_images, latent_width) Input numpy array of images. test [optional] : bool Controls the test boolean for batch normalization. Returns ------- images : array-like shape (n_images, image_width, image_height, n_colors)
[ "Transform", "latent", "vectors", "into", "images", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L175-L218
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAE.load_images
def load_images(self, filepaths): '''Load in image files from list of paths. Parameters ---------- filepaths : List[str] List of file paths of images to be loaded. Returns ------- images : array-like shape (n_images, n_colors, image_width, image_height) Images normalized to have pixel data range [0,1]. ''' def read(fname): im = Image.open(fname) im = np.float32(im) return im/255. x_all = np.array([read(fname) for fname in tqdm.tqdm(filepaths)]) x_all = x_all.astype('float32') if self.mode == 'convolution': x_all = x_all.transpose(0, 3, 1, 2) print("Image Files Loaded!") return x_all
python
def load_images(self, filepaths): '''Load in image files from list of paths. Parameters ---------- filepaths : List[str] List of file paths of images to be loaded. Returns ------- images : array-like shape (n_images, n_colors, image_width, image_height) Images normalized to have pixel data range [0,1]. ''' def read(fname): im = Image.open(fname) im = np.float32(im) return im/255. x_all = np.array([read(fname) for fname in tqdm.tqdm(filepaths)]) x_all = x_all.astype('float32') if self.mode == 'convolution': x_all = x_all.transpose(0, 3, 1, 2) print("Image Files Loaded!") return x_all
[ "def", "load_images", "(", "self", ",", "filepaths", ")", ":", "def", "read", "(", "fname", ")", ":", "im", "=", "Image", ".", "open", "(", "fname", ")", "im", "=", "np", ".", "float32", "(", "im", ")", "return", "im", "/", "255.", "x_all", "=", "np", ".", "array", "(", "[", "read", "(", "fname", ")", "for", "fname", "in", "tqdm", ".", "tqdm", "(", "filepaths", ")", "]", ")", "x_all", "=", "x_all", ".", "astype", "(", "'float32'", ")", "if", "self", ".", "mode", "==", "'convolution'", ":", "x_all", "=", "x_all", ".", "transpose", "(", "0", ",", "3", ",", "1", ",", "2", ")", "print", "(", "\"Image Files Loaded!\"", ")", "return", "x_all" ]
Load in image files from list of paths. Parameters ---------- filepaths : List[str] List of file paths of images to be loaded. Returns ------- images : array-like shape (n_images, n_colors, image_width, image_height) Images normalized to have pixel data range [0,1].
[ "Load", "in", "image", "files", "from", "list", "of", "paths", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L220-L244
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAE.fit
def fit( self, img_data, save_freq=-1, pic_freq=-1, n_epochs=100, batch_size=50, weight_decay=True, model_path='./VAE_training_model/', img_path='./VAE_training_images/', img_out_width=10 ): '''Fit the VAE model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. ''' width = img_out_width self.opt.setup(self.model) if weight_decay: self.opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) n_data = img_data.shape[0] batch_iter = list(range(0, n_data, batch_size)) n_batches = len(batch_iter) save_counter = 0 for epoch in range(1, n_epochs + 1): print('epoch: %i' % epoch) t1 = time.time() indexes = np.random.permutation(n_data) last_loss_kl = 0. last_loss_rec = 0. count = 0 for i in tqdm.tqdm(batch_iter): x_batch = Variable(img_data[indexes[i: i + batch_size]]) if self.flag_gpu: x_batch.to_gpu() out, kl_loss, rec_loss = self.model.forward(x_batch) total_loss = rec_loss + kl_loss*self.kl_ratio self.opt.zero_grads() total_loss.backward() self.opt.update() last_loss_kl += kl_loss.data last_loss_rec += rec_loss.data plot_pics = Variable(img_data[indexes[:width]]) count += 1 if pic_freq > 0: assert type(pic_freq) == int, "pic_freq must be an integer." if count % pic_freq == 0: fig = self._plot_img( plot_pics, img_path=img_path, epoch=epoch ) display(fig) if save_freq > 0: save_counter += 1 assert type(save_freq) == int, "save_freq must be an integer." if epoch % save_freq == 0: name = "vae_epoch%s" % str(epoch) if save_counter == 1: save_meta = True else: save_meta = False self.save(model_path, name, save_meta=save_meta) fig = self._plot_img( plot_pics, img_path=img_path, epoch=epoch, batch=n_batches, save=True ) msg = "rec_loss = {0} , kl_loss = {1}" print(msg.format(last_loss_rec/n_batches, last_loss_kl/n_batches)) t_diff = time.time()-t1 print("time: %f\n\n" % t_diff)
python
def fit( self, img_data, save_freq=-1, pic_freq=-1, n_epochs=100, batch_size=50, weight_decay=True, model_path='./VAE_training_model/', img_path='./VAE_training_images/', img_out_width=10 ): '''Fit the VAE model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. ''' width = img_out_width self.opt.setup(self.model) if weight_decay: self.opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) n_data = img_data.shape[0] batch_iter = list(range(0, n_data, batch_size)) n_batches = len(batch_iter) save_counter = 0 for epoch in range(1, n_epochs + 1): print('epoch: %i' % epoch) t1 = time.time() indexes = np.random.permutation(n_data) last_loss_kl = 0. last_loss_rec = 0. count = 0 for i in tqdm.tqdm(batch_iter): x_batch = Variable(img_data[indexes[i: i + batch_size]]) if self.flag_gpu: x_batch.to_gpu() out, kl_loss, rec_loss = self.model.forward(x_batch) total_loss = rec_loss + kl_loss*self.kl_ratio self.opt.zero_grads() total_loss.backward() self.opt.update() last_loss_kl += kl_loss.data last_loss_rec += rec_loss.data plot_pics = Variable(img_data[indexes[:width]]) count += 1 if pic_freq > 0: assert type(pic_freq) == int, "pic_freq must be an integer." if count % pic_freq == 0: fig = self._plot_img( plot_pics, img_path=img_path, epoch=epoch ) display(fig) if save_freq > 0: save_counter += 1 assert type(save_freq) == int, "save_freq must be an integer." if epoch % save_freq == 0: name = "vae_epoch%s" % str(epoch) if save_counter == 1: save_meta = True else: save_meta = False self.save(model_path, name, save_meta=save_meta) fig = self._plot_img( plot_pics, img_path=img_path, epoch=epoch, batch=n_batches, save=True ) msg = "rec_loss = {0} , kl_loss = {1}" print(msg.format(last_loss_rec/n_batches, last_loss_kl/n_batches)) t_diff = time.time()-t1 print("time: %f\n\n" % t_diff)
[ "def", "fit", "(", "self", ",", "img_data", ",", "save_freq", "=", "-", "1", ",", "pic_freq", "=", "-", "1", ",", "n_epochs", "=", "100", ",", "batch_size", "=", "50", ",", "weight_decay", "=", "True", ",", "model_path", "=", "'./VAE_training_model/'", ",", "img_path", "=", "'./VAE_training_images/'", ",", "img_out_width", "=", "10", ")", ":", "width", "=", "img_out_width", "self", ".", "opt", ".", "setup", "(", "self", ".", "model", ")", "if", "weight_decay", ":", "self", ".", "opt", ".", "add_hook", "(", "chainer", ".", "optimizer", ".", "WeightDecay", "(", "0.00001", ")", ")", "n_data", "=", "img_data", ".", "shape", "[", "0", "]", "batch_iter", "=", "list", "(", "range", "(", "0", ",", "n_data", ",", "batch_size", ")", ")", "n_batches", "=", "len", "(", "batch_iter", ")", "save_counter", "=", "0", "for", "epoch", "in", "range", "(", "1", ",", "n_epochs", "+", "1", ")", ":", "print", "(", "'epoch: %i'", "%", "epoch", ")", "t1", "=", "time", ".", "time", "(", ")", "indexes", "=", "np", ".", "random", ".", "permutation", "(", "n_data", ")", "last_loss_kl", "=", "0.", "last_loss_rec", "=", "0.", "count", "=", "0", "for", "i", "in", "tqdm", ".", "tqdm", "(", "batch_iter", ")", ":", "x_batch", "=", "Variable", "(", "img_data", "[", "indexes", "[", "i", ":", "i", "+", "batch_size", "]", "]", ")", "if", "self", ".", "flag_gpu", ":", "x_batch", ".", "to_gpu", "(", ")", "out", ",", "kl_loss", ",", "rec_loss", "=", "self", ".", "model", ".", "forward", "(", "x_batch", ")", "total_loss", "=", "rec_loss", "+", "kl_loss", "*", "self", ".", "kl_ratio", "self", ".", "opt", ".", "zero_grads", "(", ")", "total_loss", ".", "backward", "(", ")", "self", ".", "opt", ".", "update", "(", ")", "last_loss_kl", "+=", "kl_loss", ".", "data", "last_loss_rec", "+=", "rec_loss", ".", "data", "plot_pics", "=", "Variable", "(", "img_data", "[", "indexes", "[", ":", "width", "]", "]", ")", "count", "+=", "1", "if", "pic_freq", ">", "0", ":", "assert", "type", "(", "pic_freq", ")", "==", "int", ",", "\"pic_freq must be an integer.\"", "if", "count", "%", "pic_freq", "==", "0", ":", "fig", "=", "self", ".", "_plot_img", "(", "plot_pics", ",", "img_path", "=", "img_path", ",", "epoch", "=", "epoch", ")", "display", "(", "fig", ")", "if", "save_freq", ">", "0", ":", "save_counter", "+=", "1", "assert", "type", "(", "save_freq", ")", "==", "int", ",", "\"save_freq must be an integer.\"", "if", "epoch", "%", "save_freq", "==", "0", ":", "name", "=", "\"vae_epoch%s\"", "%", "str", "(", "epoch", ")", "if", "save_counter", "==", "1", ":", "save_meta", "=", "True", "else", ":", "save_meta", "=", "False", "self", ".", "save", "(", "model_path", ",", "name", ",", "save_meta", "=", "save_meta", ")", "fig", "=", "self", ".", "_plot_img", "(", "plot_pics", ",", "img_path", "=", "img_path", ",", "epoch", "=", "epoch", ",", "batch", "=", "n_batches", ",", "save", "=", "True", ")", "msg", "=", "\"rec_loss = {0} , kl_loss = {1}\"", "print", "(", "msg", ".", "format", "(", "last_loss_rec", "/", "n_batches", ",", "last_loss_kl", "/", "n_batches", ")", ")", "t_diff", "=", "time", ".", "time", "(", ")", "-", "t1", "print", "(", "\"time: %f\\n\\n\"", "%", "t_diff", ")" ]
Fit the VAE model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags.
[ "Fit", "the", "VAE", "model", "to", "the", "image", "data", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L246-L359
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAE.save
def save(self, path, name, save_meta=True): '''Saves model as a sequence of files in the format: {path}/{name}_{'model', 'opt', 'meta'}.h5 Parameters ---------- path : str The directory of the file you wish to save the model to. name : str The name prefix of the model and optimizer files you wish to save. save_meta [optional] : bool Flag that controls whether to save the class metadata along with the encoder, decoder, and respective optimizer states. ''' _save_model(self.model, str(path), "%s_model" % str(name)) _save_model(self.opt, str(path), "%s_opt" % str(name)) if save_meta: self._save_meta(os.path.join(path, "%s_meta" % str(name)))
python
def save(self, path, name, save_meta=True): '''Saves model as a sequence of files in the format: {path}/{name}_{'model', 'opt', 'meta'}.h5 Parameters ---------- path : str The directory of the file you wish to save the model to. name : str The name prefix of the model and optimizer files you wish to save. save_meta [optional] : bool Flag that controls whether to save the class metadata along with the encoder, decoder, and respective optimizer states. ''' _save_model(self.model, str(path), "%s_model" % str(name)) _save_model(self.opt, str(path), "%s_opt" % str(name)) if save_meta: self._save_meta(os.path.join(path, "%s_meta" % str(name)))
[ "def", "save", "(", "self", ",", "path", ",", "name", ",", "save_meta", "=", "True", ")", ":", "_save_model", "(", "self", ".", "model", ",", "str", "(", "path", ")", ",", "\"%s_model\"", "%", "str", "(", "name", ")", ")", "_save_model", "(", "self", ".", "opt", ",", "str", "(", "path", ")", ",", "\"%s_opt\"", "%", "str", "(", "name", ")", ")", "if", "save_meta", ":", "self", ".", "_save_meta", "(", "os", ".", "path", ".", "join", "(", "path", ",", "\"%s_meta\"", "%", "str", "(", "name", ")", ")", ")" ]
Saves model as a sequence of files in the format: {path}/{name}_{'model', 'opt', 'meta'}.h5 Parameters ---------- path : str The directory of the file you wish to save the model to. name : str The name prefix of the model and optimizer files you wish to save. save_meta [optional] : bool Flag that controls whether to save the class metadata along with the encoder, decoder, and respective optimizer states.
[ "Saves", "model", "as", "a", "sequence", "of", "files", "in", "the", "format", ":", "{", "path", "}", "/", "{", "name", "}", "_", "{", "model", "opt", "meta", "}", ".", "h5" ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L396-L414
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAE.load
def load(cls, model, opt, meta, flag_gpu=None): '''Loads in model as a class instance with with the specified model and optimizer states. Parameters ---------- model : str Path to the model state file. opt : str Path to the optimizer state file. meta : str Path to the class metadata state file. flag_gpu : bool Specifies whether to load the model to use gpu capabilities. Returns ------- class instance of self. ''' mess = "Model file {0} does not exist. Please check the file path." assert os.path.exists(model), mess.format(model) assert os.path.exists(opt), mess.format(opt) assert os.path.exists(meta), mess.format(meta) with open(meta, 'r') as f: meta = json.load(f) if flag_gpu is not None: meta['flag_gpu'] = flag_gpu loaded_class = cls(**meta) serializers.load_hdf5(model, loaded_class.model) loaded_class.opt.setup(loaded_class.model) serializers.load_hdf5(opt, loaded_class.opt) if meta['flag_gpu']: loaded_class.model = loaded_class.model.to_gpu() return loaded_class
python
def load(cls, model, opt, meta, flag_gpu=None): '''Loads in model as a class instance with with the specified model and optimizer states. Parameters ---------- model : str Path to the model state file. opt : str Path to the optimizer state file. meta : str Path to the class metadata state file. flag_gpu : bool Specifies whether to load the model to use gpu capabilities. Returns ------- class instance of self. ''' mess = "Model file {0} does not exist. Please check the file path." assert os.path.exists(model), mess.format(model) assert os.path.exists(opt), mess.format(opt) assert os.path.exists(meta), mess.format(meta) with open(meta, 'r') as f: meta = json.load(f) if flag_gpu is not None: meta['flag_gpu'] = flag_gpu loaded_class = cls(**meta) serializers.load_hdf5(model, loaded_class.model) loaded_class.opt.setup(loaded_class.model) serializers.load_hdf5(opt, loaded_class.opt) if meta['flag_gpu']: loaded_class.model = loaded_class.model.to_gpu() return loaded_class
[ "def", "load", "(", "cls", ",", "model", ",", "opt", ",", "meta", ",", "flag_gpu", "=", "None", ")", ":", "mess", "=", "\"Model file {0} does not exist. Please check the file path.\"", "assert", "os", ".", "path", ".", "exists", "(", "model", ")", ",", "mess", ".", "format", "(", "model", ")", "assert", "os", ".", "path", ".", "exists", "(", "opt", ")", ",", "mess", ".", "format", "(", "opt", ")", "assert", "os", ".", "path", ".", "exists", "(", "meta", ")", ",", "mess", ".", "format", "(", "meta", ")", "with", "open", "(", "meta", ",", "'r'", ")", "as", "f", ":", "meta", "=", "json", ".", "load", "(", "f", ")", "if", "flag_gpu", "is", "not", "None", ":", "meta", "[", "'flag_gpu'", "]", "=", "flag_gpu", "loaded_class", "=", "cls", "(", "*", "*", "meta", ")", "serializers", ".", "load_hdf5", "(", "model", ",", "loaded_class", ".", "model", ")", "loaded_class", ".", "opt", ".", "setup", "(", "loaded_class", ".", "model", ")", "serializers", ".", "load_hdf5", "(", "opt", ",", "loaded_class", ".", "opt", ")", "if", "meta", "[", "'flag_gpu'", "]", ":", "loaded_class", ".", "model", "=", "loaded_class", ".", "model", ".", "to_gpu", "(", ")", "return", "loaded_class" ]
Loads in model as a class instance with with the specified model and optimizer states. Parameters ---------- model : str Path to the model state file. opt : str Path to the optimizer state file. meta : str Path to the class metadata state file. flag_gpu : bool Specifies whether to load the model to use gpu capabilities. Returns ------- class instance of self.
[ "Loads", "in", "model", "as", "a", "class", "instance", "with", "with", "the", "specified", "model", "and", "optimizer", "states", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L417-L455
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
GAN.fit
def fit( self, img_data, save_freq=-1, pic_freq=-1, n_epochs=100, batch_size=50, weight_decay=True, model_path='./GAN_training_model/', img_path='./GAN_training_images/', img_out_width=10, mirroring=False ): '''Fit the GAN model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. mirroring [optional] : bool Controls whether images are randomly mirrored along the verical axis with a .5 probability. Artificially increases images variance for training set. ''' width = img_out_width self.dec_opt.setup(self.dec) self.disc_opt.setup(self.disc) if weight_decay: self.dec_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) self.disc_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) n_data = img_data.shape[0] batch_iter = list(range(0, n_data, batch_size)) n_batches = len(batch_iter) c_samples = np.random.standard_normal((width, self.latent_width)).astype(np.float32) save_counter = 0 for epoch in range(1, n_epochs + 1): print('epoch: %i' % epoch) t1 = time.time() indexes = np.random.permutation(n_data) last_loss_dec = 0. last_loss_disc = 0. count = 0 for i in tqdm.tqdm(batch_iter): x = img_data[indexes[i: i + batch_size]] size = x.shape[0] if mirroring: for j in range(size): if np.random.randint(2): x[j, :, :, :] = x[j, :, :, ::-1] x_batch = Variable(x) zeros = Variable(np.zeros(size, dtype=np.int32)) ones = Variable(np.ones(size, dtype=np.int32)) if self.flag_gpu: x_batch.to_gpu() zeros.to_gpu() ones.to_gpu() disc_samp, disc_batch = self._forward(x_batch) L_dec = F.softmax_cross_entropy(disc_samp, ones) L_disc = F.softmax_cross_entropy(disc_samp, zeros) L_disc += F.softmax_cross_entropy(disc_batch, ones) L_disc /= 2. self.dec_opt.zero_grads() L_dec.backward() self.dec_opt.update() self.disc_opt.zero_grads() L_disc.backward() self.disc_opt.update() last_loss_dec += L_dec.data last_loss_disc += L_disc.data count += 1 if pic_freq > 0: assert type(pic_freq) == int, "pic_freq must be an integer." if count % pic_freq == 0: fig = self._plot_img( c_samples, img_path=img_path, epoch=epoch ) display(fig) if save_freq > 0: save_counter += 1 assert type(save_freq) == int, "save_freq must be an integer." if epoch % save_freq == 0: name = "gan_epoch%s" % str(epoch) if save_counter == 1: save_meta = True else: save_meta = False self.save(model_path, name, save_meta=save_meta) fig = self._plot_img( c_samples, img_path=img_path, epoch=epoch, batch=n_batches, save_pic=True ) msg = "dec_loss = {0} , disc_loss = {1}" print(msg.format(last_loss_dec/n_batches, last_loss_disc/n_batches)) t_diff = time.time()-t1 print("time: %f\n\n" % t_diff)
python
def fit( self, img_data, save_freq=-1, pic_freq=-1, n_epochs=100, batch_size=50, weight_decay=True, model_path='./GAN_training_model/', img_path='./GAN_training_images/', img_out_width=10, mirroring=False ): '''Fit the GAN model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. mirroring [optional] : bool Controls whether images are randomly mirrored along the verical axis with a .5 probability. Artificially increases images variance for training set. ''' width = img_out_width self.dec_opt.setup(self.dec) self.disc_opt.setup(self.disc) if weight_decay: self.dec_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) self.disc_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) n_data = img_data.shape[0] batch_iter = list(range(0, n_data, batch_size)) n_batches = len(batch_iter) c_samples = np.random.standard_normal((width, self.latent_width)).astype(np.float32) save_counter = 0 for epoch in range(1, n_epochs + 1): print('epoch: %i' % epoch) t1 = time.time() indexes = np.random.permutation(n_data) last_loss_dec = 0. last_loss_disc = 0. count = 0 for i in tqdm.tqdm(batch_iter): x = img_data[indexes[i: i + batch_size]] size = x.shape[0] if mirroring: for j in range(size): if np.random.randint(2): x[j, :, :, :] = x[j, :, :, ::-1] x_batch = Variable(x) zeros = Variable(np.zeros(size, dtype=np.int32)) ones = Variable(np.ones(size, dtype=np.int32)) if self.flag_gpu: x_batch.to_gpu() zeros.to_gpu() ones.to_gpu() disc_samp, disc_batch = self._forward(x_batch) L_dec = F.softmax_cross_entropy(disc_samp, ones) L_disc = F.softmax_cross_entropy(disc_samp, zeros) L_disc += F.softmax_cross_entropy(disc_batch, ones) L_disc /= 2. self.dec_opt.zero_grads() L_dec.backward() self.dec_opt.update() self.disc_opt.zero_grads() L_disc.backward() self.disc_opt.update() last_loss_dec += L_dec.data last_loss_disc += L_disc.data count += 1 if pic_freq > 0: assert type(pic_freq) == int, "pic_freq must be an integer." if count % pic_freq == 0: fig = self._plot_img( c_samples, img_path=img_path, epoch=epoch ) display(fig) if save_freq > 0: save_counter += 1 assert type(save_freq) == int, "save_freq must be an integer." if epoch % save_freq == 0: name = "gan_epoch%s" % str(epoch) if save_counter == 1: save_meta = True else: save_meta = False self.save(model_path, name, save_meta=save_meta) fig = self._plot_img( c_samples, img_path=img_path, epoch=epoch, batch=n_batches, save_pic=True ) msg = "dec_loss = {0} , disc_loss = {1}" print(msg.format(last_loss_dec/n_batches, last_loss_disc/n_batches)) t_diff = time.time()-t1 print("time: %f\n\n" % t_diff)
[ "def", "fit", "(", "self", ",", "img_data", ",", "save_freq", "=", "-", "1", ",", "pic_freq", "=", "-", "1", ",", "n_epochs", "=", "100", ",", "batch_size", "=", "50", ",", "weight_decay", "=", "True", ",", "model_path", "=", "'./GAN_training_model/'", ",", "img_path", "=", "'./GAN_training_images/'", ",", "img_out_width", "=", "10", ",", "mirroring", "=", "False", ")", ":", "width", "=", "img_out_width", "self", ".", "dec_opt", ".", "setup", "(", "self", ".", "dec", ")", "self", ".", "disc_opt", ".", "setup", "(", "self", ".", "disc", ")", "if", "weight_decay", ":", "self", ".", "dec_opt", ".", "add_hook", "(", "chainer", ".", "optimizer", ".", "WeightDecay", "(", "0.00001", ")", ")", "self", ".", "disc_opt", ".", "add_hook", "(", "chainer", ".", "optimizer", ".", "WeightDecay", "(", "0.00001", ")", ")", "n_data", "=", "img_data", ".", "shape", "[", "0", "]", "batch_iter", "=", "list", "(", "range", "(", "0", ",", "n_data", ",", "batch_size", ")", ")", "n_batches", "=", "len", "(", "batch_iter", ")", "c_samples", "=", "np", ".", "random", ".", "standard_normal", "(", "(", "width", ",", "self", ".", "latent_width", ")", ")", ".", "astype", "(", "np", ".", "float32", ")", "save_counter", "=", "0", "for", "epoch", "in", "range", "(", "1", ",", "n_epochs", "+", "1", ")", ":", "print", "(", "'epoch: %i'", "%", "epoch", ")", "t1", "=", "time", ".", "time", "(", ")", "indexes", "=", "np", ".", "random", ".", "permutation", "(", "n_data", ")", "last_loss_dec", "=", "0.", "last_loss_disc", "=", "0.", "count", "=", "0", "for", "i", "in", "tqdm", ".", "tqdm", "(", "batch_iter", ")", ":", "x", "=", "img_data", "[", "indexes", "[", "i", ":", "i", "+", "batch_size", "]", "]", "size", "=", "x", ".", "shape", "[", "0", "]", "if", "mirroring", ":", "for", "j", "in", "range", "(", "size", ")", ":", "if", "np", ".", "random", ".", "randint", "(", "2", ")", ":", "x", "[", "j", ",", ":", ",", ":", ",", ":", "]", "=", "x", "[", "j", ",", ":", ",", ":", ",", ":", ":", "-", "1", "]", "x_batch", "=", "Variable", "(", "x", ")", "zeros", "=", "Variable", "(", "np", ".", "zeros", "(", "size", ",", "dtype", "=", "np", ".", "int32", ")", ")", "ones", "=", "Variable", "(", "np", ".", "ones", "(", "size", ",", "dtype", "=", "np", ".", "int32", ")", ")", "if", "self", ".", "flag_gpu", ":", "x_batch", ".", "to_gpu", "(", ")", "zeros", ".", "to_gpu", "(", ")", "ones", ".", "to_gpu", "(", ")", "disc_samp", ",", "disc_batch", "=", "self", ".", "_forward", "(", "x_batch", ")", "L_dec", "=", "F", ".", "softmax_cross_entropy", "(", "disc_samp", ",", "ones", ")", "L_disc", "=", "F", ".", "softmax_cross_entropy", "(", "disc_samp", ",", "zeros", ")", "L_disc", "+=", "F", ".", "softmax_cross_entropy", "(", "disc_batch", ",", "ones", ")", "L_disc", "/=", "2.", "self", ".", "dec_opt", ".", "zero_grads", "(", ")", "L_dec", ".", "backward", "(", ")", "self", ".", "dec_opt", ".", "update", "(", ")", "self", ".", "disc_opt", ".", "zero_grads", "(", ")", "L_disc", ".", "backward", "(", ")", "self", ".", "disc_opt", ".", "update", "(", ")", "last_loss_dec", "+=", "L_dec", ".", "data", "last_loss_disc", "+=", "L_disc", ".", "data", "count", "+=", "1", "if", "pic_freq", ">", "0", ":", "assert", "type", "(", "pic_freq", ")", "==", "int", ",", "\"pic_freq must be an integer.\"", "if", "count", "%", "pic_freq", "==", "0", ":", "fig", "=", "self", ".", "_plot_img", "(", "c_samples", ",", "img_path", "=", "img_path", ",", "epoch", "=", "epoch", ")", "display", "(", "fig", ")", "if", "save_freq", ">", "0", ":", "save_counter", "+=", "1", "assert", "type", "(", "save_freq", ")", "==", "int", ",", "\"save_freq must be an integer.\"", "if", "epoch", "%", "save_freq", "==", "0", ":", "name", "=", "\"gan_epoch%s\"", "%", "str", "(", "epoch", ")", "if", "save_counter", "==", "1", ":", "save_meta", "=", "True", "else", ":", "save_meta", "=", "False", "self", ".", "save", "(", "model_path", ",", "name", ",", "save_meta", "=", "save_meta", ")", "fig", "=", "self", ".", "_plot_img", "(", "c_samples", ",", "img_path", "=", "img_path", ",", "epoch", "=", "epoch", ",", "batch", "=", "n_batches", ",", "save_pic", "=", "True", ")", "msg", "=", "\"dec_loss = {0} , disc_loss = {1}\"", "print", "(", "msg", ".", "format", "(", "last_loss_dec", "/", "n_batches", ",", "last_loss_disc", "/", "n_batches", ")", ")", "t_diff", "=", "time", ".", "time", "(", ")", "-", "t1", "print", "(", "\"time: %f\\n\\n\"", "%", "t_diff", ")" ]
Fit the GAN model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. mirroring [optional] : bool Controls whether images are randomly mirrored along the verical axis with a .5 probability. Artificially increases images variance for training set.
[ "Fit", "the", "GAN", "model", "to", "the", "image", "data", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L667-L804
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
GAN.save
def save(self, path, name, save_meta=True): '''Saves model as a sequence of files in the format: {path}/{name}_{'dec', 'disc', 'dec_opt', 'disc_opt', 'meta'}.h5 Parameters ---------- path : str The directory of the file you wish to save the model to. name : str The name prefix of the model and optimizer files you wish to save. save_meta [optional] : bool Flag that controls whether to save the class metadata along with the generator, discriminator, and respective optimizer states. ''' _save_model(self.dec, str(path), "%s_dec" % str(name)) _save_model(self.disc, str(path), "%s_disc" % str(name)) _save_model(self.dec_opt, str(path), "%s_dec_opt" % str(name)) _save_model(self.disc_opt, str(path), "%s_disc_opt" % str(name)) if save_meta: self._save_meta(os.path.join(path, "%s_meta" % str(name)))
python
def save(self, path, name, save_meta=True): '''Saves model as a sequence of files in the format: {path}/{name}_{'dec', 'disc', 'dec_opt', 'disc_opt', 'meta'}.h5 Parameters ---------- path : str The directory of the file you wish to save the model to. name : str The name prefix of the model and optimizer files you wish to save. save_meta [optional] : bool Flag that controls whether to save the class metadata along with the generator, discriminator, and respective optimizer states. ''' _save_model(self.dec, str(path), "%s_dec" % str(name)) _save_model(self.disc, str(path), "%s_disc" % str(name)) _save_model(self.dec_opt, str(path), "%s_dec_opt" % str(name)) _save_model(self.disc_opt, str(path), "%s_disc_opt" % str(name)) if save_meta: self._save_meta(os.path.join(path, "%s_meta" % str(name)))
[ "def", "save", "(", "self", ",", "path", ",", "name", ",", "save_meta", "=", "True", ")", ":", "_save_model", "(", "self", ".", "dec", ",", "str", "(", "path", ")", ",", "\"%s_dec\"", "%", "str", "(", "name", ")", ")", "_save_model", "(", "self", ".", "disc", ",", "str", "(", "path", ")", ",", "\"%s_disc\"", "%", "str", "(", "name", ")", ")", "_save_model", "(", "self", ".", "dec_opt", ",", "str", "(", "path", ")", ",", "\"%s_dec_opt\"", "%", "str", "(", "name", ")", ")", "_save_model", "(", "self", ".", "disc_opt", ",", "str", "(", "path", ")", ",", "\"%s_disc_opt\"", "%", "str", "(", "name", ")", ")", "if", "save_meta", ":", "self", ".", "_save_meta", "(", "os", ".", "path", ".", "join", "(", "path", ",", "\"%s_meta\"", "%", "str", "(", "name", ")", ")", ")" ]
Saves model as a sequence of files in the format: {path}/{name}_{'dec', 'disc', 'dec_opt', 'disc_opt', 'meta'}.h5 Parameters ---------- path : str The directory of the file you wish to save the model to. name : str The name prefix of the model and optimizer files you wish to save. save_meta [optional] : bool Flag that controls whether to save the class metadata along with the generator, discriminator, and respective optimizer states.
[ "Saves", "model", "as", "a", "sequence", "of", "files", "in", "the", "format", ":", "{", "path", "}", "/", "{", "name", "}", "_", "{", "dec", "disc", "dec_opt", "disc_opt", "meta", "}", ".", "h5" ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L846-L867
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAEGAN.transform
def transform(self, data, test=False): '''Transform image data to latent space. Parameters ---------- data : array-like shape (n_images, image_width, image_height, n_colors) Input numpy array of images. test [optional] : bool Controls the test boolean for batch normalization. Returns ------- latent_vec : array-like shape (n_images, latent_width) ''' #make sure that data has the right shape. if not type(data) == Variable: if len(data.shape) < 4: data = data[np.newaxis] if len(data.shape) != 4: raise TypeError("Invalid dimensions for image data. Dim = %s.\ Must be 4d array." % str(data.shape)) if data.shape[1] != self.color_channels: if data.shape[-1] == self.color_channels: data = data.transpose(0, 3, 1, 2) else: raise TypeError("Invalid dimensions for image data. Dim = %s" % str(data.shape)) data = Variable(data) else: if len(data.data.shape) < 4: data.data = data.data[np.newaxis] if len(data.data.shape) != 4: raise TypeError("Invalid dimensions for image data. Dim = %s.\ Must be 4d array." % str(data.data.shape)) if data.data.shape[1] != self.color_channels: if data.data.shape[-1] == self.color_channels: data.data = data.data.transpose(0, 3, 1, 2) else: raise TypeError("Invalid dimensions for image data. Dim = %s" % str(data.shape)) # Actual transformation. if self.flag_gpu: data.to_gpu() z = self._encode(data, test=test)[0] z.to_cpu() return z.data
python
def transform(self, data, test=False): '''Transform image data to latent space. Parameters ---------- data : array-like shape (n_images, image_width, image_height, n_colors) Input numpy array of images. test [optional] : bool Controls the test boolean for batch normalization. Returns ------- latent_vec : array-like shape (n_images, latent_width) ''' #make sure that data has the right shape. if not type(data) == Variable: if len(data.shape) < 4: data = data[np.newaxis] if len(data.shape) != 4: raise TypeError("Invalid dimensions for image data. Dim = %s.\ Must be 4d array." % str(data.shape)) if data.shape[1] != self.color_channels: if data.shape[-1] == self.color_channels: data = data.transpose(0, 3, 1, 2) else: raise TypeError("Invalid dimensions for image data. Dim = %s" % str(data.shape)) data = Variable(data) else: if len(data.data.shape) < 4: data.data = data.data[np.newaxis] if len(data.data.shape) != 4: raise TypeError("Invalid dimensions for image data. Dim = %s.\ Must be 4d array." % str(data.data.shape)) if data.data.shape[1] != self.color_channels: if data.data.shape[-1] == self.color_channels: data.data = data.data.transpose(0, 3, 1, 2) else: raise TypeError("Invalid dimensions for image data. Dim = %s" % str(data.shape)) # Actual transformation. if self.flag_gpu: data.to_gpu() z = self._encode(data, test=test)[0] z.to_cpu() return z.data
[ "def", "transform", "(", "self", ",", "data", ",", "test", "=", "False", ")", ":", "#make sure that data has the right shape.", "if", "not", "type", "(", "data", ")", "==", "Variable", ":", "if", "len", "(", "data", ".", "shape", ")", "<", "4", ":", "data", "=", "data", "[", "np", ".", "newaxis", "]", "if", "len", "(", "data", ".", "shape", ")", "!=", "4", ":", "raise", "TypeError", "(", "\"Invalid dimensions for image data. Dim = %s.\\\n Must be 4d array.\"", "%", "str", "(", "data", ".", "shape", ")", ")", "if", "data", ".", "shape", "[", "1", "]", "!=", "self", ".", "color_channels", ":", "if", "data", ".", "shape", "[", "-", "1", "]", "==", "self", ".", "color_channels", ":", "data", "=", "data", ".", "transpose", "(", "0", ",", "3", ",", "1", ",", "2", ")", "else", ":", "raise", "TypeError", "(", "\"Invalid dimensions for image data. Dim = %s\"", "%", "str", "(", "data", ".", "shape", ")", ")", "data", "=", "Variable", "(", "data", ")", "else", ":", "if", "len", "(", "data", ".", "data", ".", "shape", ")", "<", "4", ":", "data", ".", "data", "=", "data", ".", "data", "[", "np", ".", "newaxis", "]", "if", "len", "(", "data", ".", "data", ".", "shape", ")", "!=", "4", ":", "raise", "TypeError", "(", "\"Invalid dimensions for image data. Dim = %s.\\\n Must be 4d array.\"", "%", "str", "(", "data", ".", "data", ".", "shape", ")", ")", "if", "data", ".", "data", ".", "shape", "[", "1", "]", "!=", "self", ".", "color_channels", ":", "if", "data", ".", "data", ".", "shape", "[", "-", "1", "]", "==", "self", ".", "color_channels", ":", "data", ".", "data", "=", "data", ".", "data", ".", "transpose", "(", "0", ",", "3", ",", "1", ",", "2", ")", "else", ":", "raise", "TypeError", "(", "\"Invalid dimensions for image data. Dim = %s\"", "%", "str", "(", "data", ".", "shape", ")", ")", "# Actual transformation.", "if", "self", ".", "flag_gpu", ":", "data", ".", "to_gpu", "(", ")", "z", "=", "self", ".", "_encode", "(", "data", ",", "test", "=", "test", ")", "[", "0", "]", "z", ".", "to_cpu", "(", ")", "return", "z", ".", "data" ]
Transform image data to latent space. Parameters ---------- data : array-like shape (n_images, image_width, image_height, n_colors) Input numpy array of images. test [optional] : bool Controls the test boolean for batch normalization. Returns ------- latent_vec : array-like shape (n_images, latent_width)
[ "Transform", "image", "data", "to", "latent", "space", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L1122-L1171
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAEGAN.fit
def fit( self, img_data, gamma=1.0, save_freq=-1, pic_freq=-1, n_epochs=100, batch_size=50, weight_decay=True, model_path='./VAEGAN_training_model/', img_path='./VAEGAN_training_images/', img_out_width=10, mirroring=False ): '''Fit the VAE/GAN model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. gamma [optional] : float Sets the multiplicative factor that weights the relative importance of reconstruction loss vs. ability to fool the discriminator. Higher weight means greater focus on faithful reconstruction. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. mirroring [optional] : bool Controls whether images are randomly mirrored along the verical axis with a .5 probability. Artificially increases images variance for training set. ''' width = img_out_width self.enc_opt.setup(self.enc) self.dec_opt.setup(self.dec) self.disc_opt.setup(self.disc) if weight_decay: self.enc_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) self.dec_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) self.disc_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) n_data = img_data.shape[0] batch_iter = list(range(0, n_data, batch_size)) n_batches = len(batch_iter) c_samples = np.random.standard_normal((width, self.latent_width)).astype(np.float32) save_counter = 0 for epoch in range(1, n_epochs + 1): print('epoch: %i' % epoch) t1 = time.time() indexes = np.random.permutation(n_data) sum_l_enc = 0. sum_l_dec = 0. sum_l_disc = 0. sum_l_gan = 0. sum_l_like = 0. sum_l_prior = 0. count = 0 for i in tqdm.tqdm(batch_iter): x = img_data[indexes[i: i + batch_size]] size = x.shape[0] if mirroring: for j in range(size): if np.random.randint(2): x[j, :, :, :] = x[j, :, :, ::-1] x_batch = Variable(x) zeros = Variable(np.zeros(size, dtype=np.int32)) ones = Variable(np.ones(size, dtype=np.int32)) if self.flag_gpu: x_batch.to_gpu() zeros.to_gpu() ones.to_gpu() kl_loss, dif_l, disc_rec, disc_batch, disc_samp = self._forward(x_batch) L_batch_GAN = F.softmax_cross_entropy(disc_batch, ones) L_rec_GAN = F.softmax_cross_entropy(disc_rec, zeros) L_samp_GAN = F.softmax_cross_entropy(disc_samp, zeros) l_gan = (L_batch_GAN + L_rec_GAN + L_samp_GAN)/3. l_like = dif_l l_prior = kl_loss enc_loss = self.kl_ratio*l_prior + l_like dec_loss = gamma*l_like - l_gan disc_loss = l_gan self.enc_opt.zero_grads() enc_loss.backward() self.enc_opt.update() self.dec_opt.zero_grads() dec_loss.backward() self.dec_opt.update() self.disc_opt.zero_grads() disc_loss.backward() self.disc_opt.update() sum_l_enc += enc_loss.data sum_l_dec += dec_loss.data sum_l_disc += disc_loss.data sum_l_gan += l_gan.data sum_l_like += l_like.data sum_l_prior += l_prior.data count += 1 plot_data = img_data[indexes[:width]] if pic_freq > 0: assert type(pic_freq) == int, "pic_freq must be an integer." if count % pic_freq == 0: fig = self._plot_img( plot_data, c_samples, img_path=img_path, epoch=epoch ) display(fig) if save_freq > 0: save_counter += 1 assert type(save_freq) == int, "save_freq must be an integer." if epoch % save_freq == 0: name = "vaegan_epoch%s" % str(epoch) if save_counter == 1: save_meta = True else: save_meta = False self.save(model_path, name, save_meta=save_meta) fig = self._plot_img( plot_data, c_samples, img_path=img_path, epoch=epoch, batch=n_batches, save_pic=True ) sum_l_enc /= n_batches sum_l_dec /= n_batches sum_l_disc /= n_batches sum_l_gan /= n_batches sum_l_like /= n_batches sum_l_prior /= n_batches msg = "enc_loss = {0}, dec_loss = {1} , disc_loss = {2}" msg2 = "gan_loss = {0}, sim_loss = {1}, kl_loss = {2}" print(msg.format(sum_l_enc, sum_l_dec, sum_l_disc)) print(msg2.format(sum_l_gan, sum_l_like, sum_l_prior)) t_diff = time.time()-t1 print("time: %f\n\n" % t_diff)
python
def fit( self, img_data, gamma=1.0, save_freq=-1, pic_freq=-1, n_epochs=100, batch_size=50, weight_decay=True, model_path='./VAEGAN_training_model/', img_path='./VAEGAN_training_images/', img_out_width=10, mirroring=False ): '''Fit the VAE/GAN model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. gamma [optional] : float Sets the multiplicative factor that weights the relative importance of reconstruction loss vs. ability to fool the discriminator. Higher weight means greater focus on faithful reconstruction. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. mirroring [optional] : bool Controls whether images are randomly mirrored along the verical axis with a .5 probability. Artificially increases images variance for training set. ''' width = img_out_width self.enc_opt.setup(self.enc) self.dec_opt.setup(self.dec) self.disc_opt.setup(self.disc) if weight_decay: self.enc_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) self.dec_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) self.disc_opt.add_hook(chainer.optimizer.WeightDecay(0.00001)) n_data = img_data.shape[0] batch_iter = list(range(0, n_data, batch_size)) n_batches = len(batch_iter) c_samples = np.random.standard_normal((width, self.latent_width)).astype(np.float32) save_counter = 0 for epoch in range(1, n_epochs + 1): print('epoch: %i' % epoch) t1 = time.time() indexes = np.random.permutation(n_data) sum_l_enc = 0. sum_l_dec = 0. sum_l_disc = 0. sum_l_gan = 0. sum_l_like = 0. sum_l_prior = 0. count = 0 for i in tqdm.tqdm(batch_iter): x = img_data[indexes[i: i + batch_size]] size = x.shape[0] if mirroring: for j in range(size): if np.random.randint(2): x[j, :, :, :] = x[j, :, :, ::-1] x_batch = Variable(x) zeros = Variable(np.zeros(size, dtype=np.int32)) ones = Variable(np.ones(size, dtype=np.int32)) if self.flag_gpu: x_batch.to_gpu() zeros.to_gpu() ones.to_gpu() kl_loss, dif_l, disc_rec, disc_batch, disc_samp = self._forward(x_batch) L_batch_GAN = F.softmax_cross_entropy(disc_batch, ones) L_rec_GAN = F.softmax_cross_entropy(disc_rec, zeros) L_samp_GAN = F.softmax_cross_entropy(disc_samp, zeros) l_gan = (L_batch_GAN + L_rec_GAN + L_samp_GAN)/3. l_like = dif_l l_prior = kl_loss enc_loss = self.kl_ratio*l_prior + l_like dec_loss = gamma*l_like - l_gan disc_loss = l_gan self.enc_opt.zero_grads() enc_loss.backward() self.enc_opt.update() self.dec_opt.zero_grads() dec_loss.backward() self.dec_opt.update() self.disc_opt.zero_grads() disc_loss.backward() self.disc_opt.update() sum_l_enc += enc_loss.data sum_l_dec += dec_loss.data sum_l_disc += disc_loss.data sum_l_gan += l_gan.data sum_l_like += l_like.data sum_l_prior += l_prior.data count += 1 plot_data = img_data[indexes[:width]] if pic_freq > 0: assert type(pic_freq) == int, "pic_freq must be an integer." if count % pic_freq == 0: fig = self._plot_img( plot_data, c_samples, img_path=img_path, epoch=epoch ) display(fig) if save_freq > 0: save_counter += 1 assert type(save_freq) == int, "save_freq must be an integer." if epoch % save_freq == 0: name = "vaegan_epoch%s" % str(epoch) if save_counter == 1: save_meta = True else: save_meta = False self.save(model_path, name, save_meta=save_meta) fig = self._plot_img( plot_data, c_samples, img_path=img_path, epoch=epoch, batch=n_batches, save_pic=True ) sum_l_enc /= n_batches sum_l_dec /= n_batches sum_l_disc /= n_batches sum_l_gan /= n_batches sum_l_like /= n_batches sum_l_prior /= n_batches msg = "enc_loss = {0}, dec_loss = {1} , disc_loss = {2}" msg2 = "gan_loss = {0}, sim_loss = {1}, kl_loss = {2}" print(msg.format(sum_l_enc, sum_l_dec, sum_l_disc)) print(msg2.format(sum_l_gan, sum_l_like, sum_l_prior)) t_diff = time.time()-t1 print("time: %f\n\n" % t_diff)
[ "def", "fit", "(", "self", ",", "img_data", ",", "gamma", "=", "1.0", ",", "save_freq", "=", "-", "1", ",", "pic_freq", "=", "-", "1", ",", "n_epochs", "=", "100", ",", "batch_size", "=", "50", ",", "weight_decay", "=", "True", ",", "model_path", "=", "'./VAEGAN_training_model/'", ",", "img_path", "=", "'./VAEGAN_training_images/'", ",", "img_out_width", "=", "10", ",", "mirroring", "=", "False", ")", ":", "width", "=", "img_out_width", "self", ".", "enc_opt", ".", "setup", "(", "self", ".", "enc", ")", "self", ".", "dec_opt", ".", "setup", "(", "self", ".", "dec", ")", "self", ".", "disc_opt", ".", "setup", "(", "self", ".", "disc", ")", "if", "weight_decay", ":", "self", ".", "enc_opt", ".", "add_hook", "(", "chainer", ".", "optimizer", ".", "WeightDecay", "(", "0.00001", ")", ")", "self", ".", "dec_opt", ".", "add_hook", "(", "chainer", ".", "optimizer", ".", "WeightDecay", "(", "0.00001", ")", ")", "self", ".", "disc_opt", ".", "add_hook", "(", "chainer", ".", "optimizer", ".", "WeightDecay", "(", "0.00001", ")", ")", "n_data", "=", "img_data", ".", "shape", "[", "0", "]", "batch_iter", "=", "list", "(", "range", "(", "0", ",", "n_data", ",", "batch_size", ")", ")", "n_batches", "=", "len", "(", "batch_iter", ")", "c_samples", "=", "np", ".", "random", ".", "standard_normal", "(", "(", "width", ",", "self", ".", "latent_width", ")", ")", ".", "astype", "(", "np", ".", "float32", ")", "save_counter", "=", "0", "for", "epoch", "in", "range", "(", "1", ",", "n_epochs", "+", "1", ")", ":", "print", "(", "'epoch: %i'", "%", "epoch", ")", "t1", "=", "time", ".", "time", "(", ")", "indexes", "=", "np", ".", "random", ".", "permutation", "(", "n_data", ")", "sum_l_enc", "=", "0.", "sum_l_dec", "=", "0.", "sum_l_disc", "=", "0.", "sum_l_gan", "=", "0.", "sum_l_like", "=", "0.", "sum_l_prior", "=", "0.", "count", "=", "0", "for", "i", "in", "tqdm", ".", "tqdm", "(", "batch_iter", ")", ":", "x", "=", "img_data", "[", "indexes", "[", "i", ":", "i", "+", "batch_size", "]", "]", "size", "=", "x", ".", "shape", "[", "0", "]", "if", "mirroring", ":", "for", "j", "in", "range", "(", "size", ")", ":", "if", "np", ".", "random", ".", "randint", "(", "2", ")", ":", "x", "[", "j", ",", ":", ",", ":", ",", ":", "]", "=", "x", "[", "j", ",", ":", ",", ":", ",", ":", ":", "-", "1", "]", "x_batch", "=", "Variable", "(", "x", ")", "zeros", "=", "Variable", "(", "np", ".", "zeros", "(", "size", ",", "dtype", "=", "np", ".", "int32", ")", ")", "ones", "=", "Variable", "(", "np", ".", "ones", "(", "size", ",", "dtype", "=", "np", ".", "int32", ")", ")", "if", "self", ".", "flag_gpu", ":", "x_batch", ".", "to_gpu", "(", ")", "zeros", ".", "to_gpu", "(", ")", "ones", ".", "to_gpu", "(", ")", "kl_loss", ",", "dif_l", ",", "disc_rec", ",", "disc_batch", ",", "disc_samp", "=", "self", ".", "_forward", "(", "x_batch", ")", "L_batch_GAN", "=", "F", ".", "softmax_cross_entropy", "(", "disc_batch", ",", "ones", ")", "L_rec_GAN", "=", "F", ".", "softmax_cross_entropy", "(", "disc_rec", ",", "zeros", ")", "L_samp_GAN", "=", "F", ".", "softmax_cross_entropy", "(", "disc_samp", ",", "zeros", ")", "l_gan", "=", "(", "L_batch_GAN", "+", "L_rec_GAN", "+", "L_samp_GAN", ")", "/", "3.", "l_like", "=", "dif_l", "l_prior", "=", "kl_loss", "enc_loss", "=", "self", ".", "kl_ratio", "*", "l_prior", "+", "l_like", "dec_loss", "=", "gamma", "*", "l_like", "-", "l_gan", "disc_loss", "=", "l_gan", "self", ".", "enc_opt", ".", "zero_grads", "(", ")", "enc_loss", ".", "backward", "(", ")", "self", ".", "enc_opt", ".", "update", "(", ")", "self", ".", "dec_opt", ".", "zero_grads", "(", ")", "dec_loss", ".", "backward", "(", ")", "self", ".", "dec_opt", ".", "update", "(", ")", "self", ".", "disc_opt", ".", "zero_grads", "(", ")", "disc_loss", ".", "backward", "(", ")", "self", ".", "disc_opt", ".", "update", "(", ")", "sum_l_enc", "+=", "enc_loss", ".", "data", "sum_l_dec", "+=", "dec_loss", ".", "data", "sum_l_disc", "+=", "disc_loss", ".", "data", "sum_l_gan", "+=", "l_gan", ".", "data", "sum_l_like", "+=", "l_like", ".", "data", "sum_l_prior", "+=", "l_prior", ".", "data", "count", "+=", "1", "plot_data", "=", "img_data", "[", "indexes", "[", ":", "width", "]", "]", "if", "pic_freq", ">", "0", ":", "assert", "type", "(", "pic_freq", ")", "==", "int", ",", "\"pic_freq must be an integer.\"", "if", "count", "%", "pic_freq", "==", "0", ":", "fig", "=", "self", ".", "_plot_img", "(", "plot_data", ",", "c_samples", ",", "img_path", "=", "img_path", ",", "epoch", "=", "epoch", ")", "display", "(", "fig", ")", "if", "save_freq", ">", "0", ":", "save_counter", "+=", "1", "assert", "type", "(", "save_freq", ")", "==", "int", ",", "\"save_freq must be an integer.\"", "if", "epoch", "%", "save_freq", "==", "0", ":", "name", "=", "\"vaegan_epoch%s\"", "%", "str", "(", "epoch", ")", "if", "save_counter", "==", "1", ":", "save_meta", "=", "True", "else", ":", "save_meta", "=", "False", "self", ".", "save", "(", "model_path", ",", "name", ",", "save_meta", "=", "save_meta", ")", "fig", "=", "self", ".", "_plot_img", "(", "plot_data", ",", "c_samples", ",", "img_path", "=", "img_path", ",", "epoch", "=", "epoch", ",", "batch", "=", "n_batches", ",", "save_pic", "=", "True", ")", "sum_l_enc", "/=", "n_batches", "sum_l_dec", "/=", "n_batches", "sum_l_disc", "/=", "n_batches", "sum_l_gan", "/=", "n_batches", "sum_l_like", "/=", "n_batches", "sum_l_prior", "/=", "n_batches", "msg", "=", "\"enc_loss = {0}, dec_loss = {1} , disc_loss = {2}\"", "msg2", "=", "\"gan_loss = {0}, sim_loss = {1}, kl_loss = {2}\"", "print", "(", "msg", ".", "format", "(", "sum_l_enc", ",", "sum_l_dec", ",", "sum_l_disc", ")", ")", "print", "(", "msg2", ".", "format", "(", "sum_l_gan", ",", "sum_l_like", ",", "sum_l_prior", ")", ")", "t_diff", "=", "time", ".", "time", "(", ")", "-", "t1", "print", "(", "\"time: %f\\n\\n\"", "%", "t_diff", ")" ]
Fit the VAE/GAN model to the image data. Parameters ---------- img_data : array-like shape (n_images, n_colors, image_width, image_height) Images used to fit VAE model. gamma [optional] : float Sets the multiplicative factor that weights the relative importance of reconstruction loss vs. ability to fool the discriminator. Higher weight means greater focus on faithful reconstruction. save_freq [optional] : int Sets the number of epochs to wait before saving the model and optimizer states. Also saves image files of randomly generated images using those states in a separate directory. Does not save if negative valued. pic_freq [optional] : int Sets the number of batches to wait before displaying a picture or randomly generated images using the current model state. Does not display if negative valued. n_epochs [optional] : int Gives the number of training epochs to run through for the fitting process. batch_size [optional] : int The size of the batch to use when training. Note: generally larger batch sizes will result in fater epoch iteration, but at the const of lower granulatity when updating the layer weights. weight_decay [optional] : bool Flag that controls adding weight decay hooks to the optimizer. model_path [optional] : str Directory where the model and optimizer state files will be saved. img_path [optional] : str Directory where the end of epoch training image files will be saved. img_out_width : int Controls the number of randomly genreated images per row in the output saved imags. mirroring [optional] : bool Controls whether images are randomly mirrored along the verical axis with a .5 probability. Artificially increases images variance for training set.
[ "Fit", "the", "VAE", "/", "GAN", "model", "to", "the", "image", "data", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L1243-L1418
stitchfix/fauxtograph
fauxtograph/fauxtograph.py
VAEGAN.load
def load(cls, enc, dec, disc, enc_opt, dec_opt, disc_opt, meta, flag_gpu=None): '''Loads in model as a class instance with with the specified model and optimizer states. Parameters ---------- enc : str Path to the encoder state file. dec : str Path to the decoder/generator state file. disc : str Path to the discriminator state file. enc_opt : str Path to the encoder optimizer state file. dec_opt : str Path to the decoder/generator optimizer state file. disc_opt : str Path to the discriminator optimizer state file. meta : str Path to the class metadata state file. flag_gpu : bool Specifies whether to load the model to use gpu capabilities. Returns ------- class instance of self. ''' mess = "Model file {0} does not exist. Please check the file path." assert os.path.exists(enc), mess.format(enc) assert os.path.exists(dec), mess.format(dec) assert os.path.exists(disc), mess.format(disc) assert os.path.exists(dec_opt), mess.format(dec_opt) assert os.path.exists(disc_opt), mess.format(disc_opt) assert os.path.exists(meta), mess.format(meta) with open(meta, 'r') as f: meta = json.load(f) if flag_gpu is not None: meta['flag_gpu'] = flag_gpu loaded_class = cls(**meta) serializers.load_hdf5(enc, loaded_class.enc) serializers.load_hdf5(dec, loaded_class.dec) serializers.load_hdf5(disc, loaded_class.disc) loaded_class.enc_opt.setup(loaded_class.enc) loaded_class.dec_opt.setup(loaded_class.dec) loaded_class.disc_opt.setup(loaded_class.disc) serializers.load_hdf5(enc_opt, loaded_class.enc_opt) serializers.load_hdf5(dec_opt, loaded_class.dec_opt) serializers.load_hdf5(disc_opt, loaded_class.disc_opt) if meta['flag_gpu']: loaded_class.enc.to_gpu() loaded_class.dec.to_gpu() loaded_class.disc.to_gpu() return loaded_class
python
def load(cls, enc, dec, disc, enc_opt, dec_opt, disc_opt, meta, flag_gpu=None): '''Loads in model as a class instance with with the specified model and optimizer states. Parameters ---------- enc : str Path to the encoder state file. dec : str Path to the decoder/generator state file. disc : str Path to the discriminator state file. enc_opt : str Path to the encoder optimizer state file. dec_opt : str Path to the decoder/generator optimizer state file. disc_opt : str Path to the discriminator optimizer state file. meta : str Path to the class metadata state file. flag_gpu : bool Specifies whether to load the model to use gpu capabilities. Returns ------- class instance of self. ''' mess = "Model file {0} does not exist. Please check the file path." assert os.path.exists(enc), mess.format(enc) assert os.path.exists(dec), mess.format(dec) assert os.path.exists(disc), mess.format(disc) assert os.path.exists(dec_opt), mess.format(dec_opt) assert os.path.exists(disc_opt), mess.format(disc_opt) assert os.path.exists(meta), mess.format(meta) with open(meta, 'r') as f: meta = json.load(f) if flag_gpu is not None: meta['flag_gpu'] = flag_gpu loaded_class = cls(**meta) serializers.load_hdf5(enc, loaded_class.enc) serializers.load_hdf5(dec, loaded_class.dec) serializers.load_hdf5(disc, loaded_class.disc) loaded_class.enc_opt.setup(loaded_class.enc) loaded_class.dec_opt.setup(loaded_class.dec) loaded_class.disc_opt.setup(loaded_class.disc) serializers.load_hdf5(enc_opt, loaded_class.enc_opt) serializers.load_hdf5(dec_opt, loaded_class.dec_opt) serializers.load_hdf5(disc_opt, loaded_class.disc_opt) if meta['flag_gpu']: loaded_class.enc.to_gpu() loaded_class.dec.to_gpu() loaded_class.disc.to_gpu() return loaded_class
[ "def", "load", "(", "cls", ",", "enc", ",", "dec", ",", "disc", ",", "enc_opt", ",", "dec_opt", ",", "disc_opt", ",", "meta", ",", "flag_gpu", "=", "None", ")", ":", "mess", "=", "\"Model file {0} does not exist. Please check the file path.\"", "assert", "os", ".", "path", ".", "exists", "(", "enc", ")", ",", "mess", ".", "format", "(", "enc", ")", "assert", "os", ".", "path", ".", "exists", "(", "dec", ")", ",", "mess", ".", "format", "(", "dec", ")", "assert", "os", ".", "path", ".", "exists", "(", "disc", ")", ",", "mess", ".", "format", "(", "disc", ")", "assert", "os", ".", "path", ".", "exists", "(", "dec_opt", ")", ",", "mess", ".", "format", "(", "dec_opt", ")", "assert", "os", ".", "path", ".", "exists", "(", "disc_opt", ")", ",", "mess", ".", "format", "(", "disc_opt", ")", "assert", "os", ".", "path", ".", "exists", "(", "meta", ")", ",", "mess", ".", "format", "(", "meta", ")", "with", "open", "(", "meta", ",", "'r'", ")", "as", "f", ":", "meta", "=", "json", ".", "load", "(", "f", ")", "if", "flag_gpu", "is", "not", "None", ":", "meta", "[", "'flag_gpu'", "]", "=", "flag_gpu", "loaded_class", "=", "cls", "(", "*", "*", "meta", ")", "serializers", ".", "load_hdf5", "(", "enc", ",", "loaded_class", ".", "enc", ")", "serializers", ".", "load_hdf5", "(", "dec", ",", "loaded_class", ".", "dec", ")", "serializers", ".", "load_hdf5", "(", "disc", ",", "loaded_class", ".", "disc", ")", "loaded_class", ".", "enc_opt", ".", "setup", "(", "loaded_class", ".", "enc", ")", "loaded_class", ".", "dec_opt", ".", "setup", "(", "loaded_class", ".", "dec", ")", "loaded_class", ".", "disc_opt", ".", "setup", "(", "loaded_class", ".", "disc", ")", "serializers", ".", "load_hdf5", "(", "enc_opt", ",", "loaded_class", ".", "enc_opt", ")", "serializers", ".", "load_hdf5", "(", "dec_opt", ",", "loaded_class", ".", "dec_opt", ")", "serializers", ".", "load_hdf5", "(", "disc_opt", ",", "loaded_class", ".", "disc_opt", ")", "if", "meta", "[", "'flag_gpu'", "]", ":", "loaded_class", ".", "enc", ".", "to_gpu", "(", ")", "loaded_class", ".", "dec", ".", "to_gpu", "(", ")", "loaded_class", ".", "disc", ".", "to_gpu", "(", ")", "return", "loaded_class" ]
Loads in model as a class instance with with the specified model and optimizer states. Parameters ---------- enc : str Path to the encoder state file. dec : str Path to the decoder/generator state file. disc : str Path to the discriminator state file. enc_opt : str Path to the encoder optimizer state file. dec_opt : str Path to the decoder/generator optimizer state file. disc_opt : str Path to the discriminator optimizer state file. meta : str Path to the class metadata state file. flag_gpu : bool Specifies whether to load the model to use gpu capabilities. Returns ------- class instance of self.
[ "Loads", "in", "model", "as", "a", "class", "instance", "with", "with", "the", "specified", "model", "and", "optimizer", "states", "." ]
train
https://github.com/stitchfix/fauxtograph/blob/393f402151126991dac1f2ee4cdd4c6aba817a5d/fauxtograph/fauxtograph.py#L1498-L1555
dhermes/bezier
src/bezier/_surface_helpers.py
polynomial_sign
def polynomial_sign(poly_surface, degree): r"""Determine the "sign" of a polynomial on the reference triangle. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Checks if a polynomial :math:`p(s, t)` is positive, negative or mixed sign on the reference triangle. Does this by utilizing the B |eacute| zier form of :math:`p`: it is a convex combination of the Bernstein basis (real numbers) hence if the Bernstein basis is all positive, the polynomial must be. If the values are mixed, then we can recursively subdivide until we are in a region where the coefficients are all one sign. Args: poly_surface (numpy.ndarray): 2D array (with 1 row) of control points for a "surface", i.e. a bivariate polynomial. degree (int): The degree of the surface / polynomial given by ``poly_surface``. Returns: int: The sign of the polynomial. Will be one of ``-1``, ``1`` or ``0``. A value of ``0`` indicates a mixed sign or the zero polynomial. Raises: ValueError: If no conclusion is reached after the maximum number of subdivisions. """ # The indices where the corner nodes in a surface are. corner_indices = (0, degree, -1) sub_polys = [poly_surface] signs = set() for _ in six.moves.xrange(_MAX_POLY_SUBDIVISIONS): undecided = [] for poly in sub_polys: # First add all the signs of the corner nodes. signs.update(_SIGN(poly[0, corner_indices]).astype(int)) # Then check if the ``poly`` nodes are **uniformly** one sign. if np.all(poly == 0.0): signs.add(0) elif np.all(poly > 0.0): signs.add(1) elif np.all(poly < 0.0): signs.add(-1) else: undecided.append(poly) if len(signs) > 1: return 0 sub_polys = functools.reduce( operator.add, [subdivide_nodes(poly, degree) for poly in undecided], (), ) if not sub_polys: break if sub_polys: raise ValueError( "Did not reach a conclusion after max subdivisions", _MAX_POLY_SUBDIVISIONS, ) else: # NOTE: We are guaranteed that ``len(signs) <= 1``. return signs.pop()
python
def polynomial_sign(poly_surface, degree): r"""Determine the "sign" of a polynomial on the reference triangle. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Checks if a polynomial :math:`p(s, t)` is positive, negative or mixed sign on the reference triangle. Does this by utilizing the B |eacute| zier form of :math:`p`: it is a convex combination of the Bernstein basis (real numbers) hence if the Bernstein basis is all positive, the polynomial must be. If the values are mixed, then we can recursively subdivide until we are in a region where the coefficients are all one sign. Args: poly_surface (numpy.ndarray): 2D array (with 1 row) of control points for a "surface", i.e. a bivariate polynomial. degree (int): The degree of the surface / polynomial given by ``poly_surface``. Returns: int: The sign of the polynomial. Will be one of ``-1``, ``1`` or ``0``. A value of ``0`` indicates a mixed sign or the zero polynomial. Raises: ValueError: If no conclusion is reached after the maximum number of subdivisions. """ # The indices where the corner nodes in a surface are. corner_indices = (0, degree, -1) sub_polys = [poly_surface] signs = set() for _ in six.moves.xrange(_MAX_POLY_SUBDIVISIONS): undecided = [] for poly in sub_polys: # First add all the signs of the corner nodes. signs.update(_SIGN(poly[0, corner_indices]).astype(int)) # Then check if the ``poly`` nodes are **uniformly** one sign. if np.all(poly == 0.0): signs.add(0) elif np.all(poly > 0.0): signs.add(1) elif np.all(poly < 0.0): signs.add(-1) else: undecided.append(poly) if len(signs) > 1: return 0 sub_polys = functools.reduce( operator.add, [subdivide_nodes(poly, degree) for poly in undecided], (), ) if not sub_polys: break if sub_polys: raise ValueError( "Did not reach a conclusion after max subdivisions", _MAX_POLY_SUBDIVISIONS, ) else: # NOTE: We are guaranteed that ``len(signs) <= 1``. return signs.pop()
[ "def", "polynomial_sign", "(", "poly_surface", ",", "degree", ")", ":", "# The indices where the corner nodes in a surface are.", "corner_indices", "=", "(", "0", ",", "degree", ",", "-", "1", ")", "sub_polys", "=", "[", "poly_surface", "]", "signs", "=", "set", "(", ")", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "_MAX_POLY_SUBDIVISIONS", ")", ":", "undecided", "=", "[", "]", "for", "poly", "in", "sub_polys", ":", "# First add all the signs of the corner nodes.", "signs", ".", "update", "(", "_SIGN", "(", "poly", "[", "0", ",", "corner_indices", "]", ")", ".", "astype", "(", "int", ")", ")", "# Then check if the ``poly`` nodes are **uniformly** one sign.", "if", "np", ".", "all", "(", "poly", "==", "0.0", ")", ":", "signs", ".", "add", "(", "0", ")", "elif", "np", ".", "all", "(", "poly", ">", "0.0", ")", ":", "signs", ".", "add", "(", "1", ")", "elif", "np", ".", "all", "(", "poly", "<", "0.0", ")", ":", "signs", ".", "add", "(", "-", "1", ")", "else", ":", "undecided", ".", "append", "(", "poly", ")", "if", "len", "(", "signs", ")", ">", "1", ":", "return", "0", "sub_polys", "=", "functools", ".", "reduce", "(", "operator", ".", "add", ",", "[", "subdivide_nodes", "(", "poly", ",", "degree", ")", "for", "poly", "in", "undecided", "]", ",", "(", ")", ",", ")", "if", "not", "sub_polys", ":", "break", "if", "sub_polys", ":", "raise", "ValueError", "(", "\"Did not reach a conclusion after max subdivisions\"", ",", "_MAX_POLY_SUBDIVISIONS", ",", ")", "else", ":", "# NOTE: We are guaranteed that ``len(signs) <= 1``.", "return", "signs", ".", "pop", "(", ")" ]
r"""Determine the "sign" of a polynomial on the reference triangle. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Checks if a polynomial :math:`p(s, t)` is positive, negative or mixed sign on the reference triangle. Does this by utilizing the B |eacute| zier form of :math:`p`: it is a convex combination of the Bernstein basis (real numbers) hence if the Bernstein basis is all positive, the polynomial must be. If the values are mixed, then we can recursively subdivide until we are in a region where the coefficients are all one sign. Args: poly_surface (numpy.ndarray): 2D array (with 1 row) of control points for a "surface", i.e. a bivariate polynomial. degree (int): The degree of the surface / polynomial given by ``poly_surface``. Returns: int: The sign of the polynomial. Will be one of ``-1``, ``1`` or ``0``. A value of ``0`` indicates a mixed sign or the zero polynomial. Raises: ValueError: If no conclusion is reached after the maximum number of subdivisions.
[ "r", "Determine", "the", "sign", "of", "a", "polynomial", "on", "the", "reference", "triangle", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L719-L791
dhermes/bezier
src/bezier/_surface_helpers.py
quadratic_jacobian_polynomial
def quadratic_jacobian_polynomial(nodes): r"""Compute the Jacobian determinant of a quadratic surface. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Converts :math:`\det(J(s, t))` to a polynomial on the reference triangle and represents it as a surface object. .. note:: This assumes that ``nodes`` is ``2 x 6`` but doesn't verify this. (However, the right multiplication by ``_QUADRATIC_JACOBIAN_HELPER`` would fail if ``nodes`` wasn't ``R x 6`` and then the ensuing determinants would fail if there weren't 2 rows.) Args: nodes (numpy.ndarray): A 2 x 6 array of nodes in a surface. Returns: numpy.ndarray: 1 x 6 array, coefficients in Bernstein basis. """ # First evaluate the Jacobian at each of the 6 nodes. jac_parts = _helpers.matrix_product(nodes, _QUADRATIC_JACOBIAN_HELPER) jac_at_nodes = np.empty((1, 6), order="F") jac_at_nodes[0, 0] = two_by_two_det(jac_parts[:, :2]) jac_at_nodes[0, 1] = two_by_two_det(jac_parts[:, 2:4]) jac_at_nodes[0, 2] = two_by_two_det(jac_parts[:, 4:6]) jac_at_nodes[0, 3] = two_by_two_det(jac_parts[:, 6:8]) jac_at_nodes[0, 4] = two_by_two_det(jac_parts[:, 8:10]) jac_at_nodes[0, 5] = two_by_two_det(jac_parts[:, 10:]) # Convert the nodal values to the Bernstein basis... bernstein = _helpers.matrix_product(jac_at_nodes, _QUADRATIC_TO_BERNSTEIN) return bernstein
python
def quadratic_jacobian_polynomial(nodes): r"""Compute the Jacobian determinant of a quadratic surface. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Converts :math:`\det(J(s, t))` to a polynomial on the reference triangle and represents it as a surface object. .. note:: This assumes that ``nodes`` is ``2 x 6`` but doesn't verify this. (However, the right multiplication by ``_QUADRATIC_JACOBIAN_HELPER`` would fail if ``nodes`` wasn't ``R x 6`` and then the ensuing determinants would fail if there weren't 2 rows.) Args: nodes (numpy.ndarray): A 2 x 6 array of nodes in a surface. Returns: numpy.ndarray: 1 x 6 array, coefficients in Bernstein basis. """ # First evaluate the Jacobian at each of the 6 nodes. jac_parts = _helpers.matrix_product(nodes, _QUADRATIC_JACOBIAN_HELPER) jac_at_nodes = np.empty((1, 6), order="F") jac_at_nodes[0, 0] = two_by_two_det(jac_parts[:, :2]) jac_at_nodes[0, 1] = two_by_two_det(jac_parts[:, 2:4]) jac_at_nodes[0, 2] = two_by_two_det(jac_parts[:, 4:6]) jac_at_nodes[0, 3] = two_by_two_det(jac_parts[:, 6:8]) jac_at_nodes[0, 4] = two_by_two_det(jac_parts[:, 8:10]) jac_at_nodes[0, 5] = two_by_two_det(jac_parts[:, 10:]) # Convert the nodal values to the Bernstein basis... bernstein = _helpers.matrix_product(jac_at_nodes, _QUADRATIC_TO_BERNSTEIN) return bernstein
[ "def", "quadratic_jacobian_polynomial", "(", "nodes", ")", ":", "# First evaluate the Jacobian at each of the 6 nodes.", "jac_parts", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_QUADRATIC_JACOBIAN_HELPER", ")", "jac_at_nodes", "=", "np", ".", "empty", "(", "(", "1", ",", "6", ")", ",", "order", "=", "\"F\"", ")", "jac_at_nodes", "[", "0", ",", "0", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", ":", "2", "]", ")", "jac_at_nodes", "[", "0", ",", "1", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "2", ":", "4", "]", ")", "jac_at_nodes", "[", "0", ",", "2", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "4", ":", "6", "]", ")", "jac_at_nodes", "[", "0", ",", "3", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "6", ":", "8", "]", ")", "jac_at_nodes", "[", "0", ",", "4", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "8", ":", "10", "]", ")", "jac_at_nodes", "[", "0", ",", "5", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "10", ":", "]", ")", "# Convert the nodal values to the Bernstein basis...", "bernstein", "=", "_helpers", ".", "matrix_product", "(", "jac_at_nodes", ",", "_QUADRATIC_TO_BERNSTEIN", ")", "return", "bernstein" ]
r"""Compute the Jacobian determinant of a quadratic surface. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Converts :math:`\det(J(s, t))` to a polynomial on the reference triangle and represents it as a surface object. .. note:: This assumes that ``nodes`` is ``2 x 6`` but doesn't verify this. (However, the right multiplication by ``_QUADRATIC_JACOBIAN_HELPER`` would fail if ``nodes`` wasn't ``R x 6`` and then the ensuing determinants would fail if there weren't 2 rows.) Args: nodes (numpy.ndarray): A 2 x 6 array of nodes in a surface. Returns: numpy.ndarray: 1 x 6 array, coefficients in Bernstein basis.
[ "r", "Compute", "the", "Jacobian", "determinant", "of", "a", "quadratic", "surface", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L815-L851
dhermes/bezier
src/bezier/_surface_helpers.py
cubic_jacobian_polynomial
def cubic_jacobian_polynomial(nodes): r"""Compute the Jacobian determinant of a cubic surface. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Converts :math:`\det(J(s, t))` to a polynomial on the reference triangle and represents it as a surface object. .. note:: This assumes that ``nodes`` is ``2 x 10`` but doesn't verify this. (However, the right multiplication by ``_CUBIC_JACOBIAN_HELPER`` would fail if ``nodes`` wasn't ``R x 10`` and then the ensuing determinants would fail if there weren't 2 rows.) Args: nodes (numpy.ndarray): A 2 x 10 array of nodes in a surface. Returns: numpy.ndarray: 1 x 15 array, coefficients in Bernstein basis. """ # First evaluate the Jacobian at each of the 15 nodes # in the quartic triangle. jac_parts = _helpers.matrix_product(nodes, _CUBIC_JACOBIAN_HELPER) jac_at_nodes = np.empty((1, 15), order="F") jac_at_nodes[0, 0] = two_by_two_det(jac_parts[:, :2]) jac_at_nodes[0, 1] = two_by_two_det(jac_parts[:, 2:4]) jac_at_nodes[0, 2] = two_by_two_det(jac_parts[:, 4:6]) jac_at_nodes[0, 3] = two_by_two_det(jac_parts[:, 6:8]) jac_at_nodes[0, 4] = two_by_two_det(jac_parts[:, 8:10]) jac_at_nodes[0, 5] = two_by_two_det(jac_parts[:, 10:12]) jac_at_nodes[0, 6] = two_by_two_det(jac_parts[:, 12:14]) jac_at_nodes[0, 7] = two_by_two_det(jac_parts[:, 14:16]) jac_at_nodes[0, 8] = two_by_two_det(jac_parts[:, 16:18]) jac_at_nodes[0, 9] = two_by_two_det(jac_parts[:, 18:20]) jac_at_nodes[0, 10] = two_by_two_det(jac_parts[:, 20:22]) jac_at_nodes[0, 11] = two_by_two_det(jac_parts[:, 22:24]) jac_at_nodes[0, 12] = two_by_two_det(jac_parts[:, 24:26]) jac_at_nodes[0, 13] = two_by_two_det(jac_parts[:, 26:28]) jac_at_nodes[0, 14] = two_by_two_det(jac_parts[:, 28:]) # Convert the nodal values to the Bernstein basis... bernstein = _helpers.matrix_product(jac_at_nodes, _QUARTIC_TO_BERNSTEIN) bernstein /= _QUARTIC_BERNSTEIN_FACTOR return bernstein
python
def cubic_jacobian_polynomial(nodes): r"""Compute the Jacobian determinant of a cubic surface. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Converts :math:`\det(J(s, t))` to a polynomial on the reference triangle and represents it as a surface object. .. note:: This assumes that ``nodes`` is ``2 x 10`` but doesn't verify this. (However, the right multiplication by ``_CUBIC_JACOBIAN_HELPER`` would fail if ``nodes`` wasn't ``R x 10`` and then the ensuing determinants would fail if there weren't 2 rows.) Args: nodes (numpy.ndarray): A 2 x 10 array of nodes in a surface. Returns: numpy.ndarray: 1 x 15 array, coefficients in Bernstein basis. """ # First evaluate the Jacobian at each of the 15 nodes # in the quartic triangle. jac_parts = _helpers.matrix_product(nodes, _CUBIC_JACOBIAN_HELPER) jac_at_nodes = np.empty((1, 15), order="F") jac_at_nodes[0, 0] = two_by_two_det(jac_parts[:, :2]) jac_at_nodes[0, 1] = two_by_two_det(jac_parts[:, 2:4]) jac_at_nodes[0, 2] = two_by_two_det(jac_parts[:, 4:6]) jac_at_nodes[0, 3] = two_by_two_det(jac_parts[:, 6:8]) jac_at_nodes[0, 4] = two_by_two_det(jac_parts[:, 8:10]) jac_at_nodes[0, 5] = two_by_two_det(jac_parts[:, 10:12]) jac_at_nodes[0, 6] = two_by_two_det(jac_parts[:, 12:14]) jac_at_nodes[0, 7] = two_by_two_det(jac_parts[:, 14:16]) jac_at_nodes[0, 8] = two_by_two_det(jac_parts[:, 16:18]) jac_at_nodes[0, 9] = two_by_two_det(jac_parts[:, 18:20]) jac_at_nodes[0, 10] = two_by_two_det(jac_parts[:, 20:22]) jac_at_nodes[0, 11] = two_by_two_det(jac_parts[:, 22:24]) jac_at_nodes[0, 12] = two_by_two_det(jac_parts[:, 24:26]) jac_at_nodes[0, 13] = two_by_two_det(jac_parts[:, 26:28]) jac_at_nodes[0, 14] = two_by_two_det(jac_parts[:, 28:]) # Convert the nodal values to the Bernstein basis... bernstein = _helpers.matrix_product(jac_at_nodes, _QUARTIC_TO_BERNSTEIN) bernstein /= _QUARTIC_BERNSTEIN_FACTOR return bernstein
[ "def", "cubic_jacobian_polynomial", "(", "nodes", ")", ":", "# First evaluate the Jacobian at each of the 15 nodes", "# in the quartic triangle.", "jac_parts", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_CUBIC_JACOBIAN_HELPER", ")", "jac_at_nodes", "=", "np", ".", "empty", "(", "(", "1", ",", "15", ")", ",", "order", "=", "\"F\"", ")", "jac_at_nodes", "[", "0", ",", "0", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", ":", "2", "]", ")", "jac_at_nodes", "[", "0", ",", "1", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "2", ":", "4", "]", ")", "jac_at_nodes", "[", "0", ",", "2", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "4", ":", "6", "]", ")", "jac_at_nodes", "[", "0", ",", "3", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "6", ":", "8", "]", ")", "jac_at_nodes", "[", "0", ",", "4", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "8", ":", "10", "]", ")", "jac_at_nodes", "[", "0", ",", "5", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "10", ":", "12", "]", ")", "jac_at_nodes", "[", "0", ",", "6", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "12", ":", "14", "]", ")", "jac_at_nodes", "[", "0", ",", "7", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "14", ":", "16", "]", ")", "jac_at_nodes", "[", "0", ",", "8", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "16", ":", "18", "]", ")", "jac_at_nodes", "[", "0", ",", "9", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "18", ":", "20", "]", ")", "jac_at_nodes", "[", "0", ",", "10", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "20", ":", "22", "]", ")", "jac_at_nodes", "[", "0", ",", "11", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "22", ":", "24", "]", ")", "jac_at_nodes", "[", "0", ",", "12", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "24", ":", "26", "]", ")", "jac_at_nodes", "[", "0", ",", "13", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "26", ":", "28", "]", ")", "jac_at_nodes", "[", "0", ",", "14", "]", "=", "two_by_two_det", "(", "jac_parts", "[", ":", ",", "28", ":", "]", ")", "# Convert the nodal values to the Bernstein basis...", "bernstein", "=", "_helpers", ".", "matrix_product", "(", "jac_at_nodes", ",", "_QUARTIC_TO_BERNSTEIN", ")", "bernstein", "/=", "_QUARTIC_BERNSTEIN_FACTOR", "return", "bernstein" ]
r"""Compute the Jacobian determinant of a cubic surface. .. note:: This is used **only** by :meth:`Surface._compute_valid` (which is in turn used to compute / cache the :attr:`Surface.is_valid` property). Converts :math:`\det(J(s, t))` to a polynomial on the reference triangle and represents it as a surface object. .. note:: This assumes that ``nodes`` is ``2 x 10`` but doesn't verify this. (However, the right multiplication by ``_CUBIC_JACOBIAN_HELPER`` would fail if ``nodes`` wasn't ``R x 10`` and then the ensuing determinants would fail if there weren't 2 rows.) Args: nodes (numpy.ndarray): A 2 x 10 array of nodes in a surface. Returns: numpy.ndarray: 1 x 15 array, coefficients in Bernstein basis.
[ "r", "Compute", "the", "Jacobian", "determinant", "of", "a", "cubic", "surface", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L854-L901
dhermes/bezier
src/bezier/_surface_helpers.py
_de_casteljau_one_round
def _de_casteljau_one_round(nodes, degree, lambda1, lambda2, lambda3): r"""Performs one "round" of the de Casteljau algorithm for surfaces. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This is a helper function, used by :func:`make_transform` and :func:`_specialize_surface` (and :func:`make_transform` is **only** used by :func:`_specialize_surface`). Converts the ``nodes`` into a basis for a surface one degree smaller by using the barycentric weights: .. math:: q_{i, j, k} = \lambda_1 \cdot p_{i + 1, j, k} + \lambda_2 \cdot p_{i, j + 1, k} + \lambda_2 \cdot p_{i, j, k + 1} .. note: For degree :math:`d`, the number of nodes should be :math:`(d + 1)(d + 2)/2`, but we don't verify this. Args: nodes (numpy.ndarray): The nodes to reduce. degree (int): The degree of the surface. lambda1 (float): Parameter along the reference triangle. lambda2 (float): Parameter along the reference triangle. lambda3 (float): Parameter along the reference triangle. Returns: numpy.ndarray: The converted nodes. """ dimension, num_nodes = nodes.shape num_new_nodes = num_nodes - degree - 1 new_nodes = np.empty((dimension, num_new_nodes), order="F") index = 0 # parent_i1 = index + k # parent_i2 = index + k + 1 # parent_i3 = index + degree + 1 parent_i1 = 0 parent_i2 = 1 parent_i3 = degree + 1 for k in six.moves.xrange(degree): for unused_j in six.moves.xrange(degree - k): # NOTE: i = (degree - 1) - j - k new_nodes[:, index] = ( lambda1 * nodes[:, parent_i1] + lambda2 * nodes[:, parent_i2] + lambda3 * nodes[:, parent_i3] ) # Update all the indices. parent_i1 += 1 parent_i2 += 1 parent_i3 += 1 index += 1 # Update the indices that depend on k. parent_i1 += 1 parent_i2 += 1 return new_nodes
python
def _de_casteljau_one_round(nodes, degree, lambda1, lambda2, lambda3): r"""Performs one "round" of the de Casteljau algorithm for surfaces. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This is a helper function, used by :func:`make_transform` and :func:`_specialize_surface` (and :func:`make_transform` is **only** used by :func:`_specialize_surface`). Converts the ``nodes`` into a basis for a surface one degree smaller by using the barycentric weights: .. math:: q_{i, j, k} = \lambda_1 \cdot p_{i + 1, j, k} + \lambda_2 \cdot p_{i, j + 1, k} + \lambda_2 \cdot p_{i, j, k + 1} .. note: For degree :math:`d`, the number of nodes should be :math:`(d + 1)(d + 2)/2`, but we don't verify this. Args: nodes (numpy.ndarray): The nodes to reduce. degree (int): The degree of the surface. lambda1 (float): Parameter along the reference triangle. lambda2 (float): Parameter along the reference triangle. lambda3 (float): Parameter along the reference triangle. Returns: numpy.ndarray: The converted nodes. """ dimension, num_nodes = nodes.shape num_new_nodes = num_nodes - degree - 1 new_nodes = np.empty((dimension, num_new_nodes), order="F") index = 0 # parent_i1 = index + k # parent_i2 = index + k + 1 # parent_i3 = index + degree + 1 parent_i1 = 0 parent_i2 = 1 parent_i3 = degree + 1 for k in six.moves.xrange(degree): for unused_j in six.moves.xrange(degree - k): # NOTE: i = (degree - 1) - j - k new_nodes[:, index] = ( lambda1 * nodes[:, parent_i1] + lambda2 * nodes[:, parent_i2] + lambda3 * nodes[:, parent_i3] ) # Update all the indices. parent_i1 += 1 parent_i2 += 1 parent_i3 += 1 index += 1 # Update the indices that depend on k. parent_i1 += 1 parent_i2 += 1 return new_nodes
[ "def", "_de_casteljau_one_round", "(", "nodes", ",", "degree", ",", "lambda1", ",", "lambda2", ",", "lambda3", ")", ":", "dimension", ",", "num_nodes", "=", "nodes", ".", "shape", "num_new_nodes", "=", "num_nodes", "-", "degree", "-", "1", "new_nodes", "=", "np", ".", "empty", "(", "(", "dimension", ",", "num_new_nodes", ")", ",", "order", "=", "\"F\"", ")", "index", "=", "0", "# parent_i1 = index + k", "# parent_i2 = index + k + 1", "# parent_i3 = index + degree + 1", "parent_i1", "=", "0", "parent_i2", "=", "1", "parent_i3", "=", "degree", "+", "1", "for", "k", "in", "six", ".", "moves", ".", "xrange", "(", "degree", ")", ":", "for", "unused_j", "in", "six", ".", "moves", ".", "xrange", "(", "degree", "-", "k", ")", ":", "# NOTE: i = (degree - 1) - j - k", "new_nodes", "[", ":", ",", "index", "]", "=", "(", "lambda1", "*", "nodes", "[", ":", ",", "parent_i1", "]", "+", "lambda2", "*", "nodes", "[", ":", ",", "parent_i2", "]", "+", "lambda3", "*", "nodes", "[", ":", ",", "parent_i3", "]", ")", "# Update all the indices.", "parent_i1", "+=", "1", "parent_i2", "+=", "1", "parent_i3", "+=", "1", "index", "+=", "1", "# Update the indices that depend on k.", "parent_i1", "+=", "1", "parent_i2", "+=", "1", "return", "new_nodes" ]
r"""Performs one "round" of the de Casteljau algorithm for surfaces. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This is a helper function, used by :func:`make_transform` and :func:`_specialize_surface` (and :func:`make_transform` is **only** used by :func:`_specialize_surface`). Converts the ``nodes`` into a basis for a surface one degree smaller by using the barycentric weights: .. math:: q_{i, j, k} = \lambda_1 \cdot p_{i + 1, j, k} + \lambda_2 \cdot p_{i, j + 1, k} + \lambda_2 \cdot p_{i, j, k + 1} .. note: For degree :math:`d`, the number of nodes should be :math:`(d + 1)(d + 2)/2`, but we don't verify this. Args: nodes (numpy.ndarray): The nodes to reduce. degree (int): The degree of the surface. lambda1 (float): Parameter along the reference triangle. lambda2 (float): Parameter along the reference triangle. lambda3 (float): Parameter along the reference triangle. Returns: numpy.ndarray: The converted nodes.
[ "r", "Performs", "one", "round", "of", "the", "de", "Casteljau", "algorithm", "for", "surfaces", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L904-L967
dhermes/bezier
src/bezier/_surface_helpers.py
make_transform
def make_transform(degree, weights_a, weights_b, weights_c): """Compute matrices corresponding to the de Casteljau algorithm. .. note:: This is a helper used only by :func:`_specialize_surface`. Applies the de Casteljau to the identity matrix, thus effectively caching the algorithm in a transformation matrix. .. note:: This is premature optimization. It's unclear if the time saved from "caching" one round of de Casteljau is cancelled out by the extra storage required for the 3 matrices. Args: degree (int): The degree of a candidate surface. weights_a (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_b (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_c (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle Returns: Mapping[int, numpy.ndarray]: Mapping from keys to the de Casteljau transformation mappings. The keys are ``0`` corresponding to ``weights_a``, ``1`` to ``weights_b`` and ``2`` to ``weights_c``. """ num_nodes = ((degree + 1) * (degree + 2)) // 2 id_mat = np.eye(num_nodes, order="F") # Pre-compute the matrices that do the reduction so we don't # have to **actually** perform the de Casteljau algorithm # every time. transform = { 0: de_casteljau_one_round(id_mat, degree, *weights_a), 1: de_casteljau_one_round(id_mat, degree, *weights_b), 2: de_casteljau_one_round(id_mat, degree, *weights_c), } return transform
python
def make_transform(degree, weights_a, weights_b, weights_c): """Compute matrices corresponding to the de Casteljau algorithm. .. note:: This is a helper used only by :func:`_specialize_surface`. Applies the de Casteljau to the identity matrix, thus effectively caching the algorithm in a transformation matrix. .. note:: This is premature optimization. It's unclear if the time saved from "caching" one round of de Casteljau is cancelled out by the extra storage required for the 3 matrices. Args: degree (int): The degree of a candidate surface. weights_a (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_b (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_c (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle Returns: Mapping[int, numpy.ndarray]: Mapping from keys to the de Casteljau transformation mappings. The keys are ``0`` corresponding to ``weights_a``, ``1`` to ``weights_b`` and ``2`` to ``weights_c``. """ num_nodes = ((degree + 1) * (degree + 2)) // 2 id_mat = np.eye(num_nodes, order="F") # Pre-compute the matrices that do the reduction so we don't # have to **actually** perform the de Casteljau algorithm # every time. transform = { 0: de_casteljau_one_round(id_mat, degree, *weights_a), 1: de_casteljau_one_round(id_mat, degree, *weights_b), 2: de_casteljau_one_round(id_mat, degree, *weights_c), } return transform
[ "def", "make_transform", "(", "degree", ",", "weights_a", ",", "weights_b", ",", "weights_c", ")", ":", "num_nodes", "=", "(", "(", "degree", "+", "1", ")", "*", "(", "degree", "+", "2", ")", ")", "//", "2", "id_mat", "=", "np", ".", "eye", "(", "num_nodes", ",", "order", "=", "\"F\"", ")", "# Pre-compute the matrices that do the reduction so we don't", "# have to **actually** perform the de Casteljau algorithm", "# every time.", "transform", "=", "{", "0", ":", "de_casteljau_one_round", "(", "id_mat", ",", "degree", ",", "*", "weights_a", ")", ",", "1", ":", "de_casteljau_one_round", "(", "id_mat", ",", "degree", ",", "*", "weights_b", ")", ",", "2", ":", "de_casteljau_one_round", "(", "id_mat", ",", "degree", ",", "*", "weights_c", ")", ",", "}", "return", "transform" ]
Compute matrices corresponding to the de Casteljau algorithm. .. note:: This is a helper used only by :func:`_specialize_surface`. Applies the de Casteljau to the identity matrix, thus effectively caching the algorithm in a transformation matrix. .. note:: This is premature optimization. It's unclear if the time saved from "caching" one round of de Casteljau is cancelled out by the extra storage required for the 3 matrices. Args: degree (int): The degree of a candidate surface. weights_a (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_b (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_c (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle Returns: Mapping[int, numpy.ndarray]: Mapping from keys to the de Casteljau transformation mappings. The keys are ``0`` corresponding to ``weights_a``, ``1`` to ``weights_b`` and ``2`` to ``weights_c``.
[ "Compute", "matrices", "corresponding", "to", "the", "de", "Casteljau", "algorithm", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L970-L1010
dhermes/bezier
src/bezier/_surface_helpers.py
reduced_to_matrix
def reduced_to_matrix(shape, degree, vals_by_weight): r"""Converts a reduced values dictionary into a matrix. .. note:: This is a helper used only by :func:`_specialize_surface`. The ``vals_by_weight`` mapping has keys of the form: ``(0, ..., 1, ..., 2, ...)`` where the ``0`` corresponds to the number of times the first set of barycentric weights was used in the reduction process, and similarly for ``1`` and ``2``. These points correspond to barycentric weights in their own right. For example ``(0, 0, 0, 1, 2, 2)`` corresponds to the barycentric weight :math:`\left(\frac{3}{6}, \frac{1}{6}, \frac{2}{6}\right)`. Once the keys in ``vals_by_weight`` have been converted to barycentric coordinates, we order them according to our rule (bottom to top, left to right) and then return them in a single matrix. Args: shape (tuple): The shape of the result matrix. degree (int): The degree of the surface. vals_by_weight (Mapping[tuple, numpy.ndarray]): Dictionary of reduced nodes according to blending of each of the three sets of weights in a reduction. Returns: numpy.ndarray: The newly created reduced control points. """ result = np.empty(shape, order="F") index = 0 for k in six.moves.xrange(degree + 1): for j in six.moves.xrange(degree + 1 - k): i = degree - j - k key = (0,) * i + (1,) * j + (2,) * k result[:, index] = vals_by_weight[key][:, 0] index += 1 return result
python
def reduced_to_matrix(shape, degree, vals_by_weight): r"""Converts a reduced values dictionary into a matrix. .. note:: This is a helper used only by :func:`_specialize_surface`. The ``vals_by_weight`` mapping has keys of the form: ``(0, ..., 1, ..., 2, ...)`` where the ``0`` corresponds to the number of times the first set of barycentric weights was used in the reduction process, and similarly for ``1`` and ``2``. These points correspond to barycentric weights in their own right. For example ``(0, 0, 0, 1, 2, 2)`` corresponds to the barycentric weight :math:`\left(\frac{3}{6}, \frac{1}{6}, \frac{2}{6}\right)`. Once the keys in ``vals_by_weight`` have been converted to barycentric coordinates, we order them according to our rule (bottom to top, left to right) and then return them in a single matrix. Args: shape (tuple): The shape of the result matrix. degree (int): The degree of the surface. vals_by_weight (Mapping[tuple, numpy.ndarray]): Dictionary of reduced nodes according to blending of each of the three sets of weights in a reduction. Returns: numpy.ndarray: The newly created reduced control points. """ result = np.empty(shape, order="F") index = 0 for k in six.moves.xrange(degree + 1): for j in six.moves.xrange(degree + 1 - k): i = degree - j - k key = (0,) * i + (1,) * j + (2,) * k result[:, index] = vals_by_weight[key][:, 0] index += 1 return result
[ "def", "reduced_to_matrix", "(", "shape", ",", "degree", ",", "vals_by_weight", ")", ":", "result", "=", "np", ".", "empty", "(", "shape", ",", "order", "=", "\"F\"", ")", "index", "=", "0", "for", "k", "in", "six", ".", "moves", ".", "xrange", "(", "degree", "+", "1", ")", ":", "for", "j", "in", "six", ".", "moves", ".", "xrange", "(", "degree", "+", "1", "-", "k", ")", ":", "i", "=", "degree", "-", "j", "-", "k", "key", "=", "(", "0", ",", ")", "*", "i", "+", "(", "1", ",", ")", "*", "j", "+", "(", "2", ",", ")", "*", "k", "result", "[", ":", ",", "index", "]", "=", "vals_by_weight", "[", "key", "]", "[", ":", ",", "0", "]", "index", "+=", "1", "return", "result" ]
r"""Converts a reduced values dictionary into a matrix. .. note:: This is a helper used only by :func:`_specialize_surface`. The ``vals_by_weight`` mapping has keys of the form: ``(0, ..., 1, ..., 2, ...)`` where the ``0`` corresponds to the number of times the first set of barycentric weights was used in the reduction process, and similarly for ``1`` and ``2``. These points correspond to barycentric weights in their own right. For example ``(0, 0, 0, 1, 2, 2)`` corresponds to the barycentric weight :math:`\left(\frac{3}{6}, \frac{1}{6}, \frac{2}{6}\right)`. Once the keys in ``vals_by_weight`` have been converted to barycentric coordinates, we order them according to our rule (bottom to top, left to right) and then return them in a single matrix. Args: shape (tuple): The shape of the result matrix. degree (int): The degree of the surface. vals_by_weight (Mapping[tuple, numpy.ndarray]): Dictionary of reduced nodes according to blending of each of the three sets of weights in a reduction. Returns: numpy.ndarray: The newly created reduced control points.
[ "r", "Converts", "a", "reduced", "values", "dictionary", "into", "a", "matrix", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1013-L1054
dhermes/bezier
src/bezier/_surface_helpers.py
_specialize_surface
def _specialize_surface(nodes, degree, weights_a, weights_b, weights_c): """Specialize a surface to a reparameterization .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Does so by taking three points (in barycentric form) within the reference triangle and then reparameterizing the surface onto the triangle formed by those three points. .. note:: This assumes the surface is degree 1 or greater but doesn't check. .. note:: This is used **only** as a helper for :func:`_subdivide_nodes`, however it may be worth adding this to :class:`Surface` as an analogue to :meth:`Curve.specialize`. Args: nodes (numpy.ndarray): Control points for a surface. degree (int): The degree of the surface. weights_a (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_b (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_c (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle Returns: numpy.ndarray: The control points for the specialized surface. """ # Uses A-->0, B-->1, C-->2 to represent the specialization used. partial_vals = { (0,): de_casteljau_one_round(nodes, degree, *weights_a), (1,): de_casteljau_one_round(nodes, degree, *weights_b), (2,): de_casteljau_one_round(nodes, degree, *weights_c), } for reduced_deg in six.moves.xrange(degree - 1, 0, -1): new_partial = {} transform = make_transform( reduced_deg, weights_a, weights_b, weights_c ) for key, sub_nodes in six.iteritems(partial_vals): # Our keys are ascending so we increment from the last value. for next_id in six.moves.xrange(key[-1], 2 + 1): new_key = key + (next_id,) new_partial[new_key] = _helpers.matrix_product( sub_nodes, transform[next_id] ) partial_vals = new_partial return reduced_to_matrix(nodes.shape, degree, partial_vals)
python
def _specialize_surface(nodes, degree, weights_a, weights_b, weights_c): """Specialize a surface to a reparameterization .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Does so by taking three points (in barycentric form) within the reference triangle and then reparameterizing the surface onto the triangle formed by those three points. .. note:: This assumes the surface is degree 1 or greater but doesn't check. .. note:: This is used **only** as a helper for :func:`_subdivide_nodes`, however it may be worth adding this to :class:`Surface` as an analogue to :meth:`Curve.specialize`. Args: nodes (numpy.ndarray): Control points for a surface. degree (int): The degree of the surface. weights_a (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_b (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_c (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle Returns: numpy.ndarray: The control points for the specialized surface. """ # Uses A-->0, B-->1, C-->2 to represent the specialization used. partial_vals = { (0,): de_casteljau_one_round(nodes, degree, *weights_a), (1,): de_casteljau_one_round(nodes, degree, *weights_b), (2,): de_casteljau_one_round(nodes, degree, *weights_c), } for reduced_deg in six.moves.xrange(degree - 1, 0, -1): new_partial = {} transform = make_transform( reduced_deg, weights_a, weights_b, weights_c ) for key, sub_nodes in six.iteritems(partial_vals): # Our keys are ascending so we increment from the last value. for next_id in six.moves.xrange(key[-1], 2 + 1): new_key = key + (next_id,) new_partial[new_key] = _helpers.matrix_product( sub_nodes, transform[next_id] ) partial_vals = new_partial return reduced_to_matrix(nodes.shape, degree, partial_vals)
[ "def", "_specialize_surface", "(", "nodes", ",", "degree", ",", "weights_a", ",", "weights_b", ",", "weights_c", ")", ":", "# Uses A-->0, B-->1, C-->2 to represent the specialization used.", "partial_vals", "=", "{", "(", "0", ",", ")", ":", "de_casteljau_one_round", "(", "nodes", ",", "degree", ",", "*", "weights_a", ")", ",", "(", "1", ",", ")", ":", "de_casteljau_one_round", "(", "nodes", ",", "degree", ",", "*", "weights_b", ")", ",", "(", "2", ",", ")", ":", "de_casteljau_one_round", "(", "nodes", ",", "degree", ",", "*", "weights_c", ")", ",", "}", "for", "reduced_deg", "in", "six", ".", "moves", ".", "xrange", "(", "degree", "-", "1", ",", "0", ",", "-", "1", ")", ":", "new_partial", "=", "{", "}", "transform", "=", "make_transform", "(", "reduced_deg", ",", "weights_a", ",", "weights_b", ",", "weights_c", ")", "for", "key", ",", "sub_nodes", "in", "six", ".", "iteritems", "(", "partial_vals", ")", ":", "# Our keys are ascending so we increment from the last value.", "for", "next_id", "in", "six", ".", "moves", ".", "xrange", "(", "key", "[", "-", "1", "]", ",", "2", "+", "1", ")", ":", "new_key", "=", "key", "+", "(", "next_id", ",", ")", "new_partial", "[", "new_key", "]", "=", "_helpers", ".", "matrix_product", "(", "sub_nodes", ",", "transform", "[", "next_id", "]", ")", "partial_vals", "=", "new_partial", "return", "reduced_to_matrix", "(", "nodes", ".", "shape", ",", "degree", ",", "partial_vals", ")" ]
Specialize a surface to a reparameterization .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Does so by taking three points (in barycentric form) within the reference triangle and then reparameterizing the surface onto the triangle formed by those three points. .. note:: This assumes the surface is degree 1 or greater but doesn't check. .. note:: This is used **only** as a helper for :func:`_subdivide_nodes`, however it may be worth adding this to :class:`Surface` as an analogue to :meth:`Curve.specialize`. Args: nodes (numpy.ndarray): Control points for a surface. degree (int): The degree of the surface. weights_a (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_b (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle weights_c (numpy.ndarray): Triple (1D array) of barycentric weights for a point in the reference triangle Returns: numpy.ndarray: The control points for the specialized surface.
[ "Specialize", "a", "surface", "to", "a", "reparameterization" ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1057-L1111
dhermes/bezier
src/bezier/_surface_helpers.py
_subdivide_nodes
def _subdivide_nodes(nodes, degree): """Subdivide a surface into four sub-surfaces. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Does so by taking the unit triangle (i.e. the domain of the surface) and splitting it into four sub-triangles by connecting the midpoints of each side. Args: nodes (numpy.ndarray): Control points for a surface. degree (int): The degree of the surface. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]: The nodes for the four sub-surfaces. """ if degree == 1: nodes_a = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_D) elif degree == 2: nodes_a = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_D) elif degree == 3: nodes_a = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_D) elif degree == 4: nodes_a = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_D) else: nodes_a = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE0, _WEIGHTS_SUBDIVIDE1, _WEIGHTS_SUBDIVIDE2, ) nodes_b = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE3, _WEIGHTS_SUBDIVIDE2, _WEIGHTS_SUBDIVIDE1, ) nodes_c = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE1, _WEIGHTS_SUBDIVIDE4, _WEIGHTS_SUBDIVIDE3, ) nodes_d = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE2, _WEIGHTS_SUBDIVIDE3, _WEIGHTS_SUBDIVIDE5, ) return nodes_a, nodes_b, nodes_c, nodes_d
python
def _subdivide_nodes(nodes, degree): """Subdivide a surface into four sub-surfaces. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Does so by taking the unit triangle (i.e. the domain of the surface) and splitting it into four sub-triangles by connecting the midpoints of each side. Args: nodes (numpy.ndarray): Control points for a surface. degree (int): The degree of the surface. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]: The nodes for the four sub-surfaces. """ if degree == 1: nodes_a = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, LINEAR_SUBDIVIDE_D) elif degree == 2: nodes_a = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, QUADRATIC_SUBDIVIDE_D) elif degree == 3: nodes_a = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, CUBIC_SUBDIVIDE_D) elif degree == 4: nodes_a = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_A) nodes_b = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_B) nodes_c = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_C) nodes_d = _helpers.matrix_product(nodes, QUARTIC_SUBDIVIDE_D) else: nodes_a = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE0, _WEIGHTS_SUBDIVIDE1, _WEIGHTS_SUBDIVIDE2, ) nodes_b = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE3, _WEIGHTS_SUBDIVIDE2, _WEIGHTS_SUBDIVIDE1, ) nodes_c = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE1, _WEIGHTS_SUBDIVIDE4, _WEIGHTS_SUBDIVIDE3, ) nodes_d = specialize_surface( nodes, degree, _WEIGHTS_SUBDIVIDE2, _WEIGHTS_SUBDIVIDE3, _WEIGHTS_SUBDIVIDE5, ) return nodes_a, nodes_b, nodes_c, nodes_d
[ "def", "_subdivide_nodes", "(", "nodes", ",", "degree", ")", ":", "if", "degree", "==", "1", ":", "nodes_a", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "LINEAR_SUBDIVIDE_A", ")", "nodes_b", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "LINEAR_SUBDIVIDE_B", ")", "nodes_c", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "LINEAR_SUBDIVIDE_C", ")", "nodes_d", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "LINEAR_SUBDIVIDE_D", ")", "elif", "degree", "==", "2", ":", "nodes_a", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUADRATIC_SUBDIVIDE_A", ")", "nodes_b", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUADRATIC_SUBDIVIDE_B", ")", "nodes_c", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUADRATIC_SUBDIVIDE_C", ")", "nodes_d", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUADRATIC_SUBDIVIDE_D", ")", "elif", "degree", "==", "3", ":", "nodes_a", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "CUBIC_SUBDIVIDE_A", ")", "nodes_b", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "CUBIC_SUBDIVIDE_B", ")", "nodes_c", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "CUBIC_SUBDIVIDE_C", ")", "nodes_d", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "CUBIC_SUBDIVIDE_D", ")", "elif", "degree", "==", "4", ":", "nodes_a", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUARTIC_SUBDIVIDE_A", ")", "nodes_b", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUARTIC_SUBDIVIDE_B", ")", "nodes_c", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUARTIC_SUBDIVIDE_C", ")", "nodes_d", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "QUARTIC_SUBDIVIDE_D", ")", "else", ":", "nodes_a", "=", "specialize_surface", "(", "nodes", ",", "degree", ",", "_WEIGHTS_SUBDIVIDE0", ",", "_WEIGHTS_SUBDIVIDE1", ",", "_WEIGHTS_SUBDIVIDE2", ",", ")", "nodes_b", "=", "specialize_surface", "(", "nodes", ",", "degree", ",", "_WEIGHTS_SUBDIVIDE3", ",", "_WEIGHTS_SUBDIVIDE2", ",", "_WEIGHTS_SUBDIVIDE1", ",", ")", "nodes_c", "=", "specialize_surface", "(", "nodes", ",", "degree", ",", "_WEIGHTS_SUBDIVIDE1", ",", "_WEIGHTS_SUBDIVIDE4", ",", "_WEIGHTS_SUBDIVIDE3", ",", ")", "nodes_d", "=", "specialize_surface", "(", "nodes", ",", "degree", ",", "_WEIGHTS_SUBDIVIDE2", ",", "_WEIGHTS_SUBDIVIDE3", ",", "_WEIGHTS_SUBDIVIDE5", ",", ")", "return", "nodes_a", ",", "nodes_b", ",", "nodes_c", ",", "nodes_d" ]
Subdivide a surface into four sub-surfaces. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Does so by taking the unit triangle (i.e. the domain of the surface) and splitting it into four sub-triangles by connecting the midpoints of each side. Args: nodes (numpy.ndarray): Control points for a surface. degree (int): The degree of the surface. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]: The nodes for the four sub-surfaces.
[ "Subdivide", "a", "surface", "into", "four", "sub", "-", "surfaces", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1114-L1183
dhermes/bezier
src/bezier/_surface_helpers.py
jacobian_s
def jacobian_s(nodes, degree, dimension): r"""Compute :math:`\frac{\partial B}{\partial s}`. .. note:: This is a helper for :func:`_jacobian_both`, which has an equivalent Fortran implementation. Args: nodes (numpy.ndarray): Array of nodes in a surface. degree (int): The degree of the surface. dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: Nodes of the Jacobian surface in B |eacute| zier form. """ num_nodes = (degree * (degree + 1)) // 2 result = np.empty((dimension, num_nodes), order="F") index = 0 i = 0 for num_vals in six.moves.xrange(degree, 0, -1): for _ in six.moves.xrange(num_vals): result[:, index] = nodes[:, i + 1] - nodes[:, i] # Update the indices index += 1 i += 1 # In between each row, the index gains an extra value. i += 1 return float(degree) * result
python
def jacobian_s(nodes, degree, dimension): r"""Compute :math:`\frac{\partial B}{\partial s}`. .. note:: This is a helper for :func:`_jacobian_both`, which has an equivalent Fortran implementation. Args: nodes (numpy.ndarray): Array of nodes in a surface. degree (int): The degree of the surface. dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: Nodes of the Jacobian surface in B |eacute| zier form. """ num_nodes = (degree * (degree + 1)) // 2 result = np.empty((dimension, num_nodes), order="F") index = 0 i = 0 for num_vals in six.moves.xrange(degree, 0, -1): for _ in six.moves.xrange(num_vals): result[:, index] = nodes[:, i + 1] - nodes[:, i] # Update the indices index += 1 i += 1 # In between each row, the index gains an extra value. i += 1 return float(degree) * result
[ "def", "jacobian_s", "(", "nodes", ",", "degree", ",", "dimension", ")", ":", "num_nodes", "=", "(", "degree", "*", "(", "degree", "+", "1", ")", ")", "//", "2", "result", "=", "np", ".", "empty", "(", "(", "dimension", ",", "num_nodes", ")", ",", "order", "=", "\"F\"", ")", "index", "=", "0", "i", "=", "0", "for", "num_vals", "in", "six", ".", "moves", ".", "xrange", "(", "degree", ",", "0", ",", "-", "1", ")", ":", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "num_vals", ")", ":", "result", "[", ":", ",", "index", "]", "=", "nodes", "[", ":", ",", "i", "+", "1", "]", "-", "nodes", "[", ":", ",", "i", "]", "# Update the indices", "index", "+=", "1", "i", "+=", "1", "# In between each row, the index gains an extra value.", "i", "+=", "1", "return", "float", "(", "degree", ")", "*", "result" ]
r"""Compute :math:`\frac{\partial B}{\partial s}`. .. note:: This is a helper for :func:`_jacobian_both`, which has an equivalent Fortran implementation. Args: nodes (numpy.ndarray): Array of nodes in a surface. degree (int): The degree of the surface. dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: Nodes of the Jacobian surface in B |eacute| zier form.
[ "r", "Compute", ":", "math", ":", "\\", "frac", "{", "\\", "partial", "B", "}", "{", "\\", "partial", "s", "}", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1186-L1215
dhermes/bezier
src/bezier/_surface_helpers.py
_jacobian_both
def _jacobian_both(nodes, degree, dimension): r"""Compute :math:`s` and :math:`t` partial of :math:`B`. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Array of nodes in a surface. degree (int): The degree of the surface. dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: Nodes of the Jacobian surfaces in B |eacute| zier form. """ _, num_nodes = nodes.shape result = np.empty((2 * dimension, num_nodes - degree - 1), order="F") result[:dimension, :] = jacobian_s(nodes, degree, dimension) result[dimension:, :] = jacobian_t(nodes, degree, dimension) return result
python
def _jacobian_both(nodes, degree, dimension): r"""Compute :math:`s` and :math:`t` partial of :math:`B`. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Array of nodes in a surface. degree (int): The degree of the surface. dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: Nodes of the Jacobian surfaces in B |eacute| zier form. """ _, num_nodes = nodes.shape result = np.empty((2 * dimension, num_nodes - degree - 1), order="F") result[:dimension, :] = jacobian_s(nodes, degree, dimension) result[dimension:, :] = jacobian_t(nodes, degree, dimension) return result
[ "def", "_jacobian_both", "(", "nodes", ",", "degree", ",", "dimension", ")", ":", "_", ",", "num_nodes", "=", "nodes", ".", "shape", "result", "=", "np", ".", "empty", "(", "(", "2", "*", "dimension", ",", "num_nodes", "-", "degree", "-", "1", ")", ",", "order", "=", "\"F\"", ")", "result", "[", ":", "dimension", ",", ":", "]", "=", "jacobian_s", "(", "nodes", ",", "degree", ",", "dimension", ")", "result", "[", "dimension", ":", ",", ":", "]", "=", "jacobian_t", "(", "nodes", ",", "degree", ",", "dimension", ")", "return", "result" ]
r"""Compute :math:`s` and :math:`t` partial of :math:`B`. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Array of nodes in a surface. degree (int): The degree of the surface. dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: Nodes of the Jacobian surfaces in B |eacute| zier form.
[ "r", "Compute", ":", "math", ":", "s", "and", ":", "math", ":", "t", "partial", "of", ":", "math", ":", "B", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1252-L1273
dhermes/bezier
src/bezier/_surface_helpers.py
_jacobian_det
def _jacobian_det(nodes, degree, st_vals): r"""Compute :math:`\det(D B)` at a set of values. This requires that :math:`B \in \mathbf{R}^2`. .. note:: This assumes but does not check that each ``(s, t)`` in ``st_vals`` is inside the reference triangle. .. warning:: This relies on helpers in :mod:`bezier` for computing the Jacobian of the surface. However, these helpers are not part of the public surface and may change or be removed. .. testsetup:: jacobian-det import numpy as np import bezier from bezier._surface_helpers import jacobian_det .. doctest:: jacobian-det :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.0, 2.0, 0.0, 1.5, 0.0], ... [0.0, 0.0, 0.0, 1.0, 1.5, 2.0], ... ]) >>> surface = bezier.Surface(nodes, degree=2) >>> st_vals = np.asfortranarray([ ... [0.25, 0.0 ], ... [0.75, 0.125], ... [0.5 , 0.5 ], ... ]) >>> s_vals, t_vals = st_vals.T >>> surface.evaluate_cartesian_multi(st_vals) array([[0.5 , 1.59375, 1.25 ], [0. , 0.34375, 1.25 ]]) >>> # B(s, t) = [s(t + 2), t(s + 2)] >>> s_vals * (t_vals + 2) array([0.5 , 1.59375, 1.25 ]) >>> t_vals * (s_vals + 2) array([0. , 0.34375, 1.25 ]) >>> jacobian_det(nodes, 2, st_vals) array([4.5 , 5.75, 6. ]) >>> # det(DB) = 2(s + t + 2) >>> 2 * (s_vals + t_vals + 2) array([4.5 , 5.75, 6. ]) .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Nodes defining a B |eacute| zier surface :math:`B(s, t)`. degree (int): The degree of the surface :math:`B`. st_vals (numpy.ndarray): ``N x 2`` array of Cartesian inputs to B |eacute| zier surfaces defined by :math:`B_s` and :math:`B_t`. Returns: numpy.ndarray: Array of all determinant values, one for each row in ``st_vals``. """ jac_nodes = jacobian_both(nodes, degree, 2) if degree == 1: num_vals, _ = st_vals.shape bs_bt_vals = np.repeat(jac_nodes, num_vals, axis=1) else: bs_bt_vals = evaluate_cartesian_multi( jac_nodes, degree - 1, st_vals, 4 ) # Take the determinant for each (s, t). return ( bs_bt_vals[0, :] * bs_bt_vals[3, :] - bs_bt_vals[1, :] * bs_bt_vals[2, :] )
python
def _jacobian_det(nodes, degree, st_vals): r"""Compute :math:`\det(D B)` at a set of values. This requires that :math:`B \in \mathbf{R}^2`. .. note:: This assumes but does not check that each ``(s, t)`` in ``st_vals`` is inside the reference triangle. .. warning:: This relies on helpers in :mod:`bezier` for computing the Jacobian of the surface. However, these helpers are not part of the public surface and may change or be removed. .. testsetup:: jacobian-det import numpy as np import bezier from bezier._surface_helpers import jacobian_det .. doctest:: jacobian-det :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.0, 2.0, 0.0, 1.5, 0.0], ... [0.0, 0.0, 0.0, 1.0, 1.5, 2.0], ... ]) >>> surface = bezier.Surface(nodes, degree=2) >>> st_vals = np.asfortranarray([ ... [0.25, 0.0 ], ... [0.75, 0.125], ... [0.5 , 0.5 ], ... ]) >>> s_vals, t_vals = st_vals.T >>> surface.evaluate_cartesian_multi(st_vals) array([[0.5 , 1.59375, 1.25 ], [0. , 0.34375, 1.25 ]]) >>> # B(s, t) = [s(t + 2), t(s + 2)] >>> s_vals * (t_vals + 2) array([0.5 , 1.59375, 1.25 ]) >>> t_vals * (s_vals + 2) array([0. , 0.34375, 1.25 ]) >>> jacobian_det(nodes, 2, st_vals) array([4.5 , 5.75, 6. ]) >>> # det(DB) = 2(s + t + 2) >>> 2 * (s_vals + t_vals + 2) array([4.5 , 5.75, 6. ]) .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Nodes defining a B |eacute| zier surface :math:`B(s, t)`. degree (int): The degree of the surface :math:`B`. st_vals (numpy.ndarray): ``N x 2`` array of Cartesian inputs to B |eacute| zier surfaces defined by :math:`B_s` and :math:`B_t`. Returns: numpy.ndarray: Array of all determinant values, one for each row in ``st_vals``. """ jac_nodes = jacobian_both(nodes, degree, 2) if degree == 1: num_vals, _ = st_vals.shape bs_bt_vals = np.repeat(jac_nodes, num_vals, axis=1) else: bs_bt_vals = evaluate_cartesian_multi( jac_nodes, degree - 1, st_vals, 4 ) # Take the determinant for each (s, t). return ( bs_bt_vals[0, :] * bs_bt_vals[3, :] - bs_bt_vals[1, :] * bs_bt_vals[2, :] )
[ "def", "_jacobian_det", "(", "nodes", ",", "degree", ",", "st_vals", ")", ":", "jac_nodes", "=", "jacobian_both", "(", "nodes", ",", "degree", ",", "2", ")", "if", "degree", "==", "1", ":", "num_vals", ",", "_", "=", "st_vals", ".", "shape", "bs_bt_vals", "=", "np", ".", "repeat", "(", "jac_nodes", ",", "num_vals", ",", "axis", "=", "1", ")", "else", ":", "bs_bt_vals", "=", "evaluate_cartesian_multi", "(", "jac_nodes", ",", "degree", "-", "1", ",", "st_vals", ",", "4", ")", "# Take the determinant for each (s, t).", "return", "(", "bs_bt_vals", "[", "0", ",", ":", "]", "*", "bs_bt_vals", "[", "3", ",", ":", "]", "-", "bs_bt_vals", "[", "1", ",", ":", "]", "*", "bs_bt_vals", "[", "2", ",", ":", "]", ")" ]
r"""Compute :math:`\det(D B)` at a set of values. This requires that :math:`B \in \mathbf{R}^2`. .. note:: This assumes but does not check that each ``(s, t)`` in ``st_vals`` is inside the reference triangle. .. warning:: This relies on helpers in :mod:`bezier` for computing the Jacobian of the surface. However, these helpers are not part of the public surface and may change or be removed. .. testsetup:: jacobian-det import numpy as np import bezier from bezier._surface_helpers import jacobian_det .. doctest:: jacobian-det :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.0, 2.0, 0.0, 1.5, 0.0], ... [0.0, 0.0, 0.0, 1.0, 1.5, 2.0], ... ]) >>> surface = bezier.Surface(nodes, degree=2) >>> st_vals = np.asfortranarray([ ... [0.25, 0.0 ], ... [0.75, 0.125], ... [0.5 , 0.5 ], ... ]) >>> s_vals, t_vals = st_vals.T >>> surface.evaluate_cartesian_multi(st_vals) array([[0.5 , 1.59375, 1.25 ], [0. , 0.34375, 1.25 ]]) >>> # B(s, t) = [s(t + 2), t(s + 2)] >>> s_vals * (t_vals + 2) array([0.5 , 1.59375, 1.25 ]) >>> t_vals * (s_vals + 2) array([0. , 0.34375, 1.25 ]) >>> jacobian_det(nodes, 2, st_vals) array([4.5 , 5.75, 6. ]) >>> # det(DB) = 2(s + t + 2) >>> 2 * (s_vals + t_vals + 2) array([4.5 , 5.75, 6. ]) .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Nodes defining a B |eacute| zier surface :math:`B(s, t)`. degree (int): The degree of the surface :math:`B`. st_vals (numpy.ndarray): ``N x 2`` array of Cartesian inputs to B |eacute| zier surfaces defined by :math:`B_s` and :math:`B_t`. Returns: numpy.ndarray: Array of all determinant values, one for each row in ``st_vals``.
[ "r", "Compute", ":", "math", ":", "\\", "det", "(", "D", "B", ")", "at", "a", "set", "of", "values", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1276-L1356
dhermes/bezier
src/bezier/_surface_helpers.py
classify_tangent_intersection
def classify_tangent_intersection( intersection, nodes1, tangent1, nodes2, tangent2 ): """Helper for func:`classify_intersection` at tangencies. .. note:: This is a helper used only by :func:`classify_intersection`. Args: intersection (.Intersection): An intersection object. nodes1 (numpy.ndarray): Control points for the first curve at the intersection. tangent1 (numpy.ndarray): The tangent vector to the first curve at the intersection (``2 x 1`` array). nodes2 (numpy.ndarray): Control points for the second curve at the intersection. tangent2 (numpy.ndarray): The tangent vector to the second curve at the intersection (``2 x 1`` array). Returns: IntersectionClassification: The "inside" curve type, based on the classification enum. Will either be ``opposed`` or one of the ``tangent`` values. Raises: NotImplementedError: If the curves are tangent at the intersection and have the same curvature. """ # Each array is 2 x 1 (i.e. a column vector), we want the vector # dot product. dot_prod = np.vdot(tangent1[:, 0], tangent2[:, 0]) # NOTE: When computing curvatures we assume that we don't have lines # here, because lines that are tangent at an intersection are # parallel and we don't handle that case. curvature1 = _curve_helpers.get_curvature(nodes1, tangent1, intersection.s) curvature2 = _curve_helpers.get_curvature(nodes2, tangent2, intersection.t) if dot_prod < 0: # If the tangent vectors are pointing in the opposite direction, # then the curves are facing opposite directions. sign1, sign2 = _SIGN([curvature1, curvature2]) if sign1 == sign2: # If both curvatures are positive, since the curves are # moving in opposite directions, the tangency isn't part of # the surface intersection. if sign1 == 1.0: return CLASSIFICATION_T.OPPOSED else: return CLASSIFICATION_T.TANGENT_BOTH else: delta_c = abs(curvature1) - abs(curvature2) if delta_c == 0.0: raise NotImplementedError(_SAME_CURVATURE) elif sign1 == _SIGN(delta_c): return CLASSIFICATION_T.OPPOSED else: return CLASSIFICATION_T.TANGENT_BOTH else: if curvature1 > curvature2: return CLASSIFICATION_T.TANGENT_FIRST elif curvature1 < curvature2: return CLASSIFICATION_T.TANGENT_SECOND else: raise NotImplementedError(_SAME_CURVATURE)
python
def classify_tangent_intersection( intersection, nodes1, tangent1, nodes2, tangent2 ): """Helper for func:`classify_intersection` at tangencies. .. note:: This is a helper used only by :func:`classify_intersection`. Args: intersection (.Intersection): An intersection object. nodes1 (numpy.ndarray): Control points for the first curve at the intersection. tangent1 (numpy.ndarray): The tangent vector to the first curve at the intersection (``2 x 1`` array). nodes2 (numpy.ndarray): Control points for the second curve at the intersection. tangent2 (numpy.ndarray): The tangent vector to the second curve at the intersection (``2 x 1`` array). Returns: IntersectionClassification: The "inside" curve type, based on the classification enum. Will either be ``opposed`` or one of the ``tangent`` values. Raises: NotImplementedError: If the curves are tangent at the intersection and have the same curvature. """ # Each array is 2 x 1 (i.e. a column vector), we want the vector # dot product. dot_prod = np.vdot(tangent1[:, 0], tangent2[:, 0]) # NOTE: When computing curvatures we assume that we don't have lines # here, because lines that are tangent at an intersection are # parallel and we don't handle that case. curvature1 = _curve_helpers.get_curvature(nodes1, tangent1, intersection.s) curvature2 = _curve_helpers.get_curvature(nodes2, tangent2, intersection.t) if dot_prod < 0: # If the tangent vectors are pointing in the opposite direction, # then the curves are facing opposite directions. sign1, sign2 = _SIGN([curvature1, curvature2]) if sign1 == sign2: # If both curvatures are positive, since the curves are # moving in opposite directions, the tangency isn't part of # the surface intersection. if sign1 == 1.0: return CLASSIFICATION_T.OPPOSED else: return CLASSIFICATION_T.TANGENT_BOTH else: delta_c = abs(curvature1) - abs(curvature2) if delta_c == 0.0: raise NotImplementedError(_SAME_CURVATURE) elif sign1 == _SIGN(delta_c): return CLASSIFICATION_T.OPPOSED else: return CLASSIFICATION_T.TANGENT_BOTH else: if curvature1 > curvature2: return CLASSIFICATION_T.TANGENT_FIRST elif curvature1 < curvature2: return CLASSIFICATION_T.TANGENT_SECOND else: raise NotImplementedError(_SAME_CURVATURE)
[ "def", "classify_tangent_intersection", "(", "intersection", ",", "nodes1", ",", "tangent1", ",", "nodes2", ",", "tangent2", ")", ":", "# Each array is 2 x 1 (i.e. a column vector), we want the vector", "# dot product.", "dot_prod", "=", "np", ".", "vdot", "(", "tangent1", "[", ":", ",", "0", "]", ",", "tangent2", "[", ":", ",", "0", "]", ")", "# NOTE: When computing curvatures we assume that we don't have lines", "# here, because lines that are tangent at an intersection are", "# parallel and we don't handle that case.", "curvature1", "=", "_curve_helpers", ".", "get_curvature", "(", "nodes1", ",", "tangent1", ",", "intersection", ".", "s", ")", "curvature2", "=", "_curve_helpers", ".", "get_curvature", "(", "nodes2", ",", "tangent2", ",", "intersection", ".", "t", ")", "if", "dot_prod", "<", "0", ":", "# If the tangent vectors are pointing in the opposite direction,", "# then the curves are facing opposite directions.", "sign1", ",", "sign2", "=", "_SIGN", "(", "[", "curvature1", ",", "curvature2", "]", ")", "if", "sign1", "==", "sign2", ":", "# If both curvatures are positive, since the curves are", "# moving in opposite directions, the tangency isn't part of", "# the surface intersection.", "if", "sign1", "==", "1.0", ":", "return", "CLASSIFICATION_T", ".", "OPPOSED", "else", ":", "return", "CLASSIFICATION_T", ".", "TANGENT_BOTH", "else", ":", "delta_c", "=", "abs", "(", "curvature1", ")", "-", "abs", "(", "curvature2", ")", "if", "delta_c", "==", "0.0", ":", "raise", "NotImplementedError", "(", "_SAME_CURVATURE", ")", "elif", "sign1", "==", "_SIGN", "(", "delta_c", ")", ":", "return", "CLASSIFICATION_T", ".", "OPPOSED", "else", ":", "return", "CLASSIFICATION_T", ".", "TANGENT_BOTH", "else", ":", "if", "curvature1", ">", "curvature2", ":", "return", "CLASSIFICATION_T", ".", "TANGENT_FIRST", "elif", "curvature1", "<", "curvature2", ":", "return", "CLASSIFICATION_T", ".", "TANGENT_SECOND", "else", ":", "raise", "NotImplementedError", "(", "_SAME_CURVATURE", ")" ]
Helper for func:`classify_intersection` at tangencies. .. note:: This is a helper used only by :func:`classify_intersection`. Args: intersection (.Intersection): An intersection object. nodes1 (numpy.ndarray): Control points for the first curve at the intersection. tangent1 (numpy.ndarray): The tangent vector to the first curve at the intersection (``2 x 1`` array). nodes2 (numpy.ndarray): Control points for the second curve at the intersection. tangent2 (numpy.ndarray): The tangent vector to the second curve at the intersection (``2 x 1`` array). Returns: IntersectionClassification: The "inside" curve type, based on the classification enum. Will either be ``opposed`` or one of the ``tangent`` values. Raises: NotImplementedError: If the curves are tangent at the intersection and have the same curvature.
[ "Helper", "for", "func", ":", "classify_intersection", "at", "tangencies", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1359-L1429
dhermes/bezier
src/bezier/_surface_helpers.py
ignored_edge_corner
def ignored_edge_corner(edge_tangent, corner_tangent, corner_previous_edge): """Check ignored when a corner lies **inside** another edge. .. note:: This is a helper used only by :func:`ignored_corner`, which in turn is only used by :func:`classify_intersection`. Helper for :func:`ignored_corner` where one of ``s`` and ``t`` are ``0``, but **not both**. Args: edge_tangent (numpy.ndarray): Tangent vector (``2 x 1`` array) along the edge that the intersection occurs in the middle of. corner_tangent (numpy.ndarray): Tangent vector (``2 x 1`` array) at the corner where intersection occurs (at the beginning of edge). corner_previous_edge (numpy.ndarray): Edge that ends at the corner intersection (whereas ``corner_tangent`` comes from the edge that **begins** at the corner intersection). This is a ``2 x N`` array where ``N`` is the number of nodes on the edge. Returns: bool: Indicates if the corner intersection should be ignored. """ cross_prod = _helpers.cross_product( edge_tangent.ravel(order="F"), corner_tangent.ravel(order="F") ) # A negative cross product indicates that ``edge_tangent`` is # "inside" / "to the left" of ``corner_tangent`` (due to right-hand rule). if cross_prod > 0.0: return False # Do the same for the **other** tangent at the corner. alt_corner_tangent = _curve_helpers.evaluate_hodograph( 1.0, corner_previous_edge ) # Change the direction of the "in" tangent so that it points "out". alt_corner_tangent *= -1.0 cross_prod = _helpers.cross_product( edge_tangent.ravel(order="F"), alt_corner_tangent.ravel(order="F") ) return cross_prod <= 0.0
python
def ignored_edge_corner(edge_tangent, corner_tangent, corner_previous_edge): """Check ignored when a corner lies **inside** another edge. .. note:: This is a helper used only by :func:`ignored_corner`, which in turn is only used by :func:`classify_intersection`. Helper for :func:`ignored_corner` where one of ``s`` and ``t`` are ``0``, but **not both**. Args: edge_tangent (numpy.ndarray): Tangent vector (``2 x 1`` array) along the edge that the intersection occurs in the middle of. corner_tangent (numpy.ndarray): Tangent vector (``2 x 1`` array) at the corner where intersection occurs (at the beginning of edge). corner_previous_edge (numpy.ndarray): Edge that ends at the corner intersection (whereas ``corner_tangent`` comes from the edge that **begins** at the corner intersection). This is a ``2 x N`` array where ``N`` is the number of nodes on the edge. Returns: bool: Indicates if the corner intersection should be ignored. """ cross_prod = _helpers.cross_product( edge_tangent.ravel(order="F"), corner_tangent.ravel(order="F") ) # A negative cross product indicates that ``edge_tangent`` is # "inside" / "to the left" of ``corner_tangent`` (due to right-hand rule). if cross_prod > 0.0: return False # Do the same for the **other** tangent at the corner. alt_corner_tangent = _curve_helpers.evaluate_hodograph( 1.0, corner_previous_edge ) # Change the direction of the "in" tangent so that it points "out". alt_corner_tangent *= -1.0 cross_prod = _helpers.cross_product( edge_tangent.ravel(order="F"), alt_corner_tangent.ravel(order="F") ) return cross_prod <= 0.0
[ "def", "ignored_edge_corner", "(", "edge_tangent", ",", "corner_tangent", ",", "corner_previous_edge", ")", ":", "cross_prod", "=", "_helpers", ".", "cross_product", "(", "edge_tangent", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "corner_tangent", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "# A negative cross product indicates that ``edge_tangent`` is", "# \"inside\" / \"to the left\" of ``corner_tangent`` (due to right-hand rule).", "if", "cross_prod", ">", "0.0", ":", "return", "False", "# Do the same for the **other** tangent at the corner.", "alt_corner_tangent", "=", "_curve_helpers", ".", "evaluate_hodograph", "(", "1.0", ",", "corner_previous_edge", ")", "# Change the direction of the \"in\" tangent so that it points \"out\".", "alt_corner_tangent", "*=", "-", "1.0", "cross_prod", "=", "_helpers", ".", "cross_product", "(", "edge_tangent", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "alt_corner_tangent", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "return", "cross_prod", "<=", "0.0" ]
Check ignored when a corner lies **inside** another edge. .. note:: This is a helper used only by :func:`ignored_corner`, which in turn is only used by :func:`classify_intersection`. Helper for :func:`ignored_corner` where one of ``s`` and ``t`` are ``0``, but **not both**. Args: edge_tangent (numpy.ndarray): Tangent vector (``2 x 1`` array) along the edge that the intersection occurs in the middle of. corner_tangent (numpy.ndarray): Tangent vector (``2 x 1`` array) at the corner where intersection occurs (at the beginning of edge). corner_previous_edge (numpy.ndarray): Edge that ends at the corner intersection (whereas ``corner_tangent`` comes from the edge that **begins** at the corner intersection). This is a ``2 x N`` array where ``N`` is the number of nodes on the edge. Returns: bool: Indicates if the corner intersection should be ignored.
[ "Check", "ignored", "when", "a", "corner", "lies", "**", "inside", "**", "another", "edge", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1432-L1473
dhermes/bezier
src/bezier/_surface_helpers.py
ignored_double_corner
def ignored_double_corner( intersection, tangent_s, tangent_t, edge_nodes1, edge_nodes2 ): """Check if an intersection is an "ignored" double corner. .. note:: This is a helper used only by :func:`ignored_corner`, which in turn is only used by :func:`classify_intersection`. Helper for :func:`ignored_corner` where both ``s`` and ``t`` are ``0``. Does so by checking if either edge through the ``t`` corner goes through the interior of the other surface. An interior check is done by checking that a few cross products are positive. Args: intersection (.Intersection): An intersection to "diagnose". tangent_s (numpy.ndarray): The tangent vector (``2 x 1`` array) to the first curve at the intersection. tangent_t (numpy.ndarray): The tangent vector (``2 x 1`` array) to the second curve at the intersection. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: bool: Indicates if the corner is to be ignored. """ # Compute the other edge for the ``s`` surface. prev_index = (intersection.index_first - 1) % 3 prev_edge = edge_nodes1[prev_index] alt_tangent_s = _curve_helpers.evaluate_hodograph(1.0, prev_edge) # First check if ``tangent_t`` is interior to the ``s`` surface. cross_prod1 = _helpers.cross_product( tangent_s.ravel(order="F"), tangent_t.ravel(order="F") ) # A positive cross product indicates that ``tangent_t`` is # interior to ``tangent_s``. Similar for ``alt_tangent_s``. # If ``tangent_t`` is interior to both, then the surfaces # do more than just "kiss" at the corner, so the corner should # not be ignored. if cross_prod1 >= 0.0: # Only compute ``cross_prod2`` if we need to. cross_prod2 = _helpers.cross_product( alt_tangent_s.ravel(order="F"), tangent_t.ravel(order="F") ) if cross_prod2 >= 0.0: return False # If ``tangent_t`` is not interior, we check the other ``t`` # edge that ends at the corner. prev_index = (intersection.index_second - 1) % 3 prev_edge = edge_nodes2[prev_index] alt_tangent_t = _curve_helpers.evaluate_hodograph(1.0, prev_edge) # Change the direction of the "in" tangent so that it points "out". alt_tangent_t *= -1.0 cross_prod3 = _helpers.cross_product( tangent_s.ravel(order="F"), alt_tangent_t.ravel(order="F") ) if cross_prod3 >= 0.0: # Only compute ``cross_prod4`` if we need to. cross_prod4 = _helpers.cross_product( alt_tangent_s.ravel(order="F"), alt_tangent_t.ravel(order="F") ) if cross_prod4 >= 0.0: return False # If neither of ``tangent_t`` or ``alt_tangent_t`` are interior # to the ``s`` surface, one of two things is true. Either # the two surfaces have no interior intersection (1) or the # ``s`` surface is bounded by both edges of the ``t`` surface # at the corner intersection (2). To detect (2), we only need # check if ``tangent_s`` is interior to both ``tangent_t`` # and ``alt_tangent_t``. ``cross_prod1`` contains # (tangent_s) x (tangent_t), so it's negative will tell if # ``tangent_s`` is interior. Similarly, ``cross_prod3`` # contains (tangent_s) x (alt_tangent_t), but we also reversed # the sign on ``alt_tangent_t`` so switching the sign back # and reversing the arguments in the cross product cancel out. return cross_prod1 > 0.0 or cross_prod3 < 0.0
python
def ignored_double_corner( intersection, tangent_s, tangent_t, edge_nodes1, edge_nodes2 ): """Check if an intersection is an "ignored" double corner. .. note:: This is a helper used only by :func:`ignored_corner`, which in turn is only used by :func:`classify_intersection`. Helper for :func:`ignored_corner` where both ``s`` and ``t`` are ``0``. Does so by checking if either edge through the ``t`` corner goes through the interior of the other surface. An interior check is done by checking that a few cross products are positive. Args: intersection (.Intersection): An intersection to "diagnose". tangent_s (numpy.ndarray): The tangent vector (``2 x 1`` array) to the first curve at the intersection. tangent_t (numpy.ndarray): The tangent vector (``2 x 1`` array) to the second curve at the intersection. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: bool: Indicates if the corner is to be ignored. """ # Compute the other edge for the ``s`` surface. prev_index = (intersection.index_first - 1) % 3 prev_edge = edge_nodes1[prev_index] alt_tangent_s = _curve_helpers.evaluate_hodograph(1.0, prev_edge) # First check if ``tangent_t`` is interior to the ``s`` surface. cross_prod1 = _helpers.cross_product( tangent_s.ravel(order="F"), tangent_t.ravel(order="F") ) # A positive cross product indicates that ``tangent_t`` is # interior to ``tangent_s``. Similar for ``alt_tangent_s``. # If ``tangent_t`` is interior to both, then the surfaces # do more than just "kiss" at the corner, so the corner should # not be ignored. if cross_prod1 >= 0.0: # Only compute ``cross_prod2`` if we need to. cross_prod2 = _helpers.cross_product( alt_tangent_s.ravel(order="F"), tangent_t.ravel(order="F") ) if cross_prod2 >= 0.0: return False # If ``tangent_t`` is not interior, we check the other ``t`` # edge that ends at the corner. prev_index = (intersection.index_second - 1) % 3 prev_edge = edge_nodes2[prev_index] alt_tangent_t = _curve_helpers.evaluate_hodograph(1.0, prev_edge) # Change the direction of the "in" tangent so that it points "out". alt_tangent_t *= -1.0 cross_prod3 = _helpers.cross_product( tangent_s.ravel(order="F"), alt_tangent_t.ravel(order="F") ) if cross_prod3 >= 0.0: # Only compute ``cross_prod4`` if we need to. cross_prod4 = _helpers.cross_product( alt_tangent_s.ravel(order="F"), alt_tangent_t.ravel(order="F") ) if cross_prod4 >= 0.0: return False # If neither of ``tangent_t`` or ``alt_tangent_t`` are interior # to the ``s`` surface, one of two things is true. Either # the two surfaces have no interior intersection (1) or the # ``s`` surface is bounded by both edges of the ``t`` surface # at the corner intersection (2). To detect (2), we only need # check if ``tangent_s`` is interior to both ``tangent_t`` # and ``alt_tangent_t``. ``cross_prod1`` contains # (tangent_s) x (tangent_t), so it's negative will tell if # ``tangent_s`` is interior. Similarly, ``cross_prod3`` # contains (tangent_s) x (alt_tangent_t), but we also reversed # the sign on ``alt_tangent_t`` so switching the sign back # and reversing the arguments in the cross product cancel out. return cross_prod1 > 0.0 or cross_prod3 < 0.0
[ "def", "ignored_double_corner", "(", "intersection", ",", "tangent_s", ",", "tangent_t", ",", "edge_nodes1", ",", "edge_nodes2", ")", ":", "# Compute the other edge for the ``s`` surface.", "prev_index", "=", "(", "intersection", ".", "index_first", "-", "1", ")", "%", "3", "prev_edge", "=", "edge_nodes1", "[", "prev_index", "]", "alt_tangent_s", "=", "_curve_helpers", ".", "evaluate_hodograph", "(", "1.0", ",", "prev_edge", ")", "# First check if ``tangent_t`` is interior to the ``s`` surface.", "cross_prod1", "=", "_helpers", ".", "cross_product", "(", "tangent_s", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "tangent_t", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "# A positive cross product indicates that ``tangent_t`` is", "# interior to ``tangent_s``. Similar for ``alt_tangent_s``.", "# If ``tangent_t`` is interior to both, then the surfaces", "# do more than just \"kiss\" at the corner, so the corner should", "# not be ignored.", "if", "cross_prod1", ">=", "0.0", ":", "# Only compute ``cross_prod2`` if we need to.", "cross_prod2", "=", "_helpers", ".", "cross_product", "(", "alt_tangent_s", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "tangent_t", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "if", "cross_prod2", ">=", "0.0", ":", "return", "False", "# If ``tangent_t`` is not interior, we check the other ``t``", "# edge that ends at the corner.", "prev_index", "=", "(", "intersection", ".", "index_second", "-", "1", ")", "%", "3", "prev_edge", "=", "edge_nodes2", "[", "prev_index", "]", "alt_tangent_t", "=", "_curve_helpers", ".", "evaluate_hodograph", "(", "1.0", ",", "prev_edge", ")", "# Change the direction of the \"in\" tangent so that it points \"out\".", "alt_tangent_t", "*=", "-", "1.0", "cross_prod3", "=", "_helpers", ".", "cross_product", "(", "tangent_s", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "alt_tangent_t", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "if", "cross_prod3", ">=", "0.0", ":", "# Only compute ``cross_prod4`` if we need to.", "cross_prod4", "=", "_helpers", ".", "cross_product", "(", "alt_tangent_s", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "alt_tangent_t", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "if", "cross_prod4", ">=", "0.0", ":", "return", "False", "# If neither of ``tangent_t`` or ``alt_tangent_t`` are interior", "# to the ``s`` surface, one of two things is true. Either", "# the two surfaces have no interior intersection (1) or the", "# ``s`` surface is bounded by both edges of the ``t`` surface", "# at the corner intersection (2). To detect (2), we only need", "# check if ``tangent_s`` is interior to both ``tangent_t``", "# and ``alt_tangent_t``. ``cross_prod1`` contains", "# (tangent_s) x (tangent_t), so it's negative will tell if", "# ``tangent_s`` is interior. Similarly, ``cross_prod3``", "# contains (tangent_s) x (alt_tangent_t), but we also reversed", "# the sign on ``alt_tangent_t`` so switching the sign back", "# and reversing the arguments in the cross product cancel out.", "return", "cross_prod1", ">", "0.0", "or", "cross_prod3", "<", "0.0" ]
Check if an intersection is an "ignored" double corner. .. note:: This is a helper used only by :func:`ignored_corner`, which in turn is only used by :func:`classify_intersection`. Helper for :func:`ignored_corner` where both ``s`` and ``t`` are ``0``. Does so by checking if either edge through the ``t`` corner goes through the interior of the other surface. An interior check is done by checking that a few cross products are positive. Args: intersection (.Intersection): An intersection to "diagnose". tangent_s (numpy.ndarray): The tangent vector (``2 x 1`` array) to the first curve at the intersection. tangent_t (numpy.ndarray): The tangent vector (``2 x 1`` array) to the second curve at the intersection. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: bool: Indicates if the corner is to be ignored.
[ "Check", "if", "an", "intersection", "is", "an", "ignored", "double", "corner", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1476-L1558
dhermes/bezier
src/bezier/_surface_helpers.py
ignored_corner
def ignored_corner( intersection, tangent_s, tangent_t, edge_nodes1, edge_nodes2 ): """Check if an intersection is an "ignored" corner. .. note:: This is a helper used only by :func:`classify_intersection`. An "ignored" corner is one where the surfaces just "kiss" at the point of intersection but their interiors do not meet. We can determine this by comparing the tangent lines from the point of intersection. .. note:: This assumes the ``intersection`` has been shifted to the beginning of a curve so only checks if ``s == 0.0`` or ``t == 0.0`` (rather than also checking for ``1.0``). Args: intersection (.Intersection): An intersection to "diagnose". tangent_s (numpy.ndarray): The tangent vector (``2 x 1`` array) to the first curve at the intersection. tangent_t (numpy.ndarray): The tangent vector (``2 x 1`` array) to the second curve at the intersection. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: bool: Indicates if the corner is to be ignored. """ if intersection.s == 0.0: if intersection.t == 0.0: # Double corner. return ignored_double_corner( intersection, tangent_s, tangent_t, edge_nodes1, edge_nodes2 ) else: # s-only corner. prev_index = (intersection.index_first - 1) % 3 prev_edge = edge_nodes1[prev_index] return ignored_edge_corner(tangent_t, tangent_s, prev_edge) elif intersection.t == 0.0: # t-only corner. prev_index = (intersection.index_second - 1) % 3 prev_edge = edge_nodes2[prev_index] return ignored_edge_corner(tangent_s, tangent_t, prev_edge) else: # Not a corner. return False
python
def ignored_corner( intersection, tangent_s, tangent_t, edge_nodes1, edge_nodes2 ): """Check if an intersection is an "ignored" corner. .. note:: This is a helper used only by :func:`classify_intersection`. An "ignored" corner is one where the surfaces just "kiss" at the point of intersection but their interiors do not meet. We can determine this by comparing the tangent lines from the point of intersection. .. note:: This assumes the ``intersection`` has been shifted to the beginning of a curve so only checks if ``s == 0.0`` or ``t == 0.0`` (rather than also checking for ``1.0``). Args: intersection (.Intersection): An intersection to "diagnose". tangent_s (numpy.ndarray): The tangent vector (``2 x 1`` array) to the first curve at the intersection. tangent_t (numpy.ndarray): The tangent vector (``2 x 1`` array) to the second curve at the intersection. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: bool: Indicates if the corner is to be ignored. """ if intersection.s == 0.0: if intersection.t == 0.0: # Double corner. return ignored_double_corner( intersection, tangent_s, tangent_t, edge_nodes1, edge_nodes2 ) else: # s-only corner. prev_index = (intersection.index_first - 1) % 3 prev_edge = edge_nodes1[prev_index] return ignored_edge_corner(tangent_t, tangent_s, prev_edge) elif intersection.t == 0.0: # t-only corner. prev_index = (intersection.index_second - 1) % 3 prev_edge = edge_nodes2[prev_index] return ignored_edge_corner(tangent_s, tangent_t, prev_edge) else: # Not a corner. return False
[ "def", "ignored_corner", "(", "intersection", ",", "tangent_s", ",", "tangent_t", ",", "edge_nodes1", ",", "edge_nodes2", ")", ":", "if", "intersection", ".", "s", "==", "0.0", ":", "if", "intersection", ".", "t", "==", "0.0", ":", "# Double corner.", "return", "ignored_double_corner", "(", "intersection", ",", "tangent_s", ",", "tangent_t", ",", "edge_nodes1", ",", "edge_nodes2", ")", "else", ":", "# s-only corner.", "prev_index", "=", "(", "intersection", ".", "index_first", "-", "1", ")", "%", "3", "prev_edge", "=", "edge_nodes1", "[", "prev_index", "]", "return", "ignored_edge_corner", "(", "tangent_t", ",", "tangent_s", ",", "prev_edge", ")", "elif", "intersection", ".", "t", "==", "0.0", ":", "# t-only corner.", "prev_index", "=", "(", "intersection", ".", "index_second", "-", "1", ")", "%", "3", "prev_edge", "=", "edge_nodes2", "[", "prev_index", "]", "return", "ignored_edge_corner", "(", "tangent_s", ",", "tangent_t", ",", "prev_edge", ")", "else", ":", "# Not a corner.", "return", "False" ]
Check if an intersection is an "ignored" corner. .. note:: This is a helper used only by :func:`classify_intersection`. An "ignored" corner is one where the surfaces just "kiss" at the point of intersection but their interiors do not meet. We can determine this by comparing the tangent lines from the point of intersection. .. note:: This assumes the ``intersection`` has been shifted to the beginning of a curve so only checks if ``s == 0.0`` or ``t == 0.0`` (rather than also checking for ``1.0``). Args: intersection (.Intersection): An intersection to "diagnose". tangent_s (numpy.ndarray): The tangent vector (``2 x 1`` array) to the first curve at the intersection. tangent_t (numpy.ndarray): The tangent vector (``2 x 1`` array) to the second curve at the intersection. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: bool: Indicates if the corner is to be ignored.
[ "Check", "if", "an", "intersection", "is", "an", "ignored", "corner", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1561-L1617
dhermes/bezier
src/bezier/_surface_helpers.py
classify_intersection
def classify_intersection(intersection, edge_nodes1, edge_nodes2): r"""Determine which curve is on the "inside of the intersection". .. note:: This is a helper used only by :meth:`.Surface.intersect`. This is intended to be a helper for forming a :class:`.CurvedPolygon` from the edge intersections of two :class:`.Surface`-s. In order to move from one intersection to another (or to the end of an edge), the interior edge must be determined at the point of intersection. The "typical" case is on the interior of both edges: .. image:: ../images/classify_intersection1.png :align: center .. testsetup:: classify-intersection1, classify-intersection2, classify-intersection3, classify-intersection4, classify-intersection5, classify-intersection6, classify-intersection7, classify-intersection8, classify-intersection9 import numpy as np import bezier from bezier import _curve_helpers from bezier._intersection_helpers import Intersection from bezier._surface_helpers import classify_intersection def hodograph(curve, s): return _curve_helpers.evaluate_hodograph( s, curve._nodes) def curvature(curve, s): nodes = curve._nodes tangent = _curve_helpers.evaluate_hodograph( s, nodes) return _curve_helpers.get_curvature( nodes, tangent, s) .. doctest:: classify-intersection1 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.75, 2.0], ... [0.0, 0.25, 1.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.6875, 2.0], ... [0.0, 0.0625, 0.5], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.25, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> tangent1 = hodograph(curve1, s) >>> tangent1 array([[1.25], [0.75]]) >>> tangent2 = hodograph(curve2, t) >>> tangent2 array([[2. ], [0.5]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.FIRST: 0> .. testcleanup:: classify-intersection1 import make_images make_images.classify_intersection1( s, curve1, tangent1, curve2, tangent2) We determine the interior (i.e. left) one by using the `right-hand rule`_: by embedding the tangent vectors in :math:`\mathbf{R}^3`, we compute .. _right-hand rule: https://en.wikipedia.org/wiki/Right-hand_rule .. math:: \left[\begin{array}{c} x_1'(s) \\ y_1'(s) \\ 0 \end{array}\right] \times \left[\begin{array}{c} x_2'(t) \\ y_2'(t) \\ 0 \end{array}\right] = \left[\begin{array}{c} 0 \\ 0 \\ x_1'(s) y_2'(t) - x_2'(t) y_1'(s) \end{array}\right]. If the cross product quantity :math:`B_1'(s) \times B_2'(t) = x_1'(s) y_2'(t) - x_2'(t) y_1'(s)` is positive, then the first curve is "outside" / "to the right", i.e. the second curve is interior. If the cross product is negative, the first curve is interior. When :math:`B_1'(s) \times B_2'(t) = 0`, the tangent vectors are parallel, i.e. the intersection is a point of tangency: .. image:: ../images/classify_intersection2.png :align: center .. doctest:: classify-intersection2 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.5, 2.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_SECOND: 4> .. testcleanup:: classify-intersection2 import make_images make_images.classify_intersection2(s, curve1, curve2) Depending on the direction of the parameterizations, the interior curve may change, but we can use the (signed) `curvature`_ of each curve at that point to determine which is on the interior: .. _curvature: https://en.wikipedia.org/wiki/Curvature .. image:: ../images/classify_intersection3.png :align: center .. doctest:: classify-intersection3 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [2.0, 1.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [3.0, 1.5, 0.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_FIRST: 3> .. testcleanup:: classify-intersection3 import make_images make_images.classify_intersection3(s, curve1, curve2) When the curves are moving in opposite directions at a point of tangency, there is no side to choose. Either the point of tangency is not part of any :class:`.CurvedPolygon` intersection .. image:: ../images/classify_intersection4.png :align: center .. doctest:: classify-intersection4 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [2.0, 1.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.OPPOSED: 2> .. testcleanup:: classify-intersection4 import make_images make_images.classify_intersection4(s, curve1, curve2) or the point of tangency is a "degenerate" part of two :class:`.CurvedPolygon` intersections. It is "degenerate" because from one direction, the point should be classified as :attr:`~.IntersectionClassification.FIRST` and from another as :attr:`~.IntersectionClassification.SECOND`. .. image:: ../images/classify_intersection5.png :align: center .. doctest:: classify-intersection5 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.5, 2.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [3.0, 1.5, 0.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_BOTH: 6> .. testcleanup:: classify-intersection5 import make_images make_images.classify_intersection5(s, curve1, curve2) The :attr:`~.IntersectionClassification.TANGENT_BOTH` classification can also occur if the curves are "kissing" but share a zero width interior at the point of tangency: .. image:: ../images/classify_intersection9.png :align: center .. doctest:: classify-intersection9 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.0, 20.0, 40.0], ... [0.0, 40.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [40.0, 20.0, 0.0], ... [40.0, 0.0, 40.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_BOTH: 6> .. testcleanup:: classify-intersection9 import make_images make_images.classify_intersection9(s, curve1, curve2) However, if the `curvature`_ of each curve is identical, we don't try to distinguish further: .. image:: ../images/classify_intersection6.png :align: center .. doctest:: classify-intersection6 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [-0.125 , -0.125 , 0.375 ], ... [ 0.0625, -0.0625, 0.0625], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [-0.25, -0.25, 0.75], ... [ 0.25, -0.25, 0.25], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> hodograph(curve1, s) array([[0.5], [0. ]]) >>> hodograph(curve2, t) array([[1.], [0.]]) >>> curvature(curve1, s) 2.0 >>> curvature(curve2, t) 2.0 >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) Traceback (most recent call last): ... NotImplementedError: Tangent curves have same curvature. .. testcleanup:: classify-intersection6 import make_images make_images.classify_intersection6(s, curve1, curve2) In addition to points of tangency, intersections that happen at the end of an edge need special handling: .. image:: ../images/classify_intersection7.png :align: center .. doctest:: classify-intersection7 :options: +NORMALIZE_WHITESPACE >>> nodes1a = np.asfortranarray([ ... [0.0, 4.5, 9.0 ], ... [0.0, 0.0, 2.25], ... ]) >>> curve1a = bezier.Curve(nodes1a, degree=2) >>> nodes2 = np.asfortranarray([ ... [11.25, 9.0, 2.75], ... [ 0.0 , 4.5, 1.0 ], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 1.0, 0.375 >>> curve1a.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1a, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) Traceback (most recent call last): ... ValueError: ('Intersection occurs at the end of an edge', 's', 1.0, 't', 0.375) >>> >>> nodes1b = np.asfortranarray([ ... [9.0, 4.5, 0.0], ... [2.25, 2.375, 2.5], ... ]) >>> curve1b = bezier.Curve(nodes1b, degree=2) >>> curve1b.evaluate(0.0) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(1, 0.0, 0, t) >>> edge_nodes1 = (nodes1a, nodes1b, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.FIRST: 0> .. testcleanup:: classify-intersection7 import make_images make_images.classify_intersection7(s, curve1a, curve1b, curve2) As above, some intersections at the end of an edge are part of an actual intersection. However, some surfaces may just "kiss" at a corner intersection: .. image:: ../images/classify_intersection8.png :align: center .. doctest:: classify-intersection8 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.25, 0.0, 0.0, 0.625, 0.5 , 1.0 ], ... [1.0 , 0.5, 0.0, 0.875, 0.375, 0.75], ... ]) >>> surface1 = bezier.Surface(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0625, -0.25, -1.0, -0.5 , -1.0, -1.0], ... [0.5 , 1.0 , 1.0, 0.125, 0.5, 0.0], ... ]) >>> surface2 = bezier.Surface(nodes2, degree=2) >>> curve1, _, _ = surface1.edges >>> edge_nodes1 = [curve.nodes for curve in surface1.edges] >>> curve2, _, _ = surface2.edges >>> edge_nodes2 = [curve.nodes for curve in surface2.edges] >>> s, t = 0.5, 0.0 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.IGNORED_CORNER: 5> .. testcleanup:: classify-intersection8 import make_images make_images.classify_intersection8( s, curve1, surface1, curve2, surface2) .. note:: This assumes the intersection occurs in :math:`\mathbf{R}^2` but doesn't check this. .. note:: This function doesn't allow wiggle room / round-off when checking endpoints, nor when checking if the cross product is near zero, nor when curvatures are compared. However, the most "correct" version of this function likely should allow for some round off. Args: intersection (.Intersection): An intersection object. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: IntersectionClassification: The "inside" curve type, based on the classification enum. Raises: ValueError: If the intersection occurs at the end of either curve involved. This is because we want to classify which curve to **move forward** on, and we can't move past the end of a segment. """ if intersection.s == 1.0 or intersection.t == 1.0: raise ValueError( "Intersection occurs at the end of an edge", "s", intersection.s, "t", intersection.t, ) nodes1 = edge_nodes1[intersection.index_first] tangent1 = _curve_helpers.evaluate_hodograph(intersection.s, nodes1) nodes2 = edge_nodes2[intersection.index_second] tangent2 = _curve_helpers.evaluate_hodograph(intersection.t, nodes2) if ignored_corner( intersection, tangent1, tangent2, edge_nodes1, edge_nodes2 ): return CLASSIFICATION_T.IGNORED_CORNER # Take the cross product of tangent vectors to determine which one # is more "inside" / "to the left". cross_prod = _helpers.cross_product( tangent1.ravel(order="F"), tangent2.ravel(order="F") ) if cross_prod < -ALMOST_TANGENT: return CLASSIFICATION_T.FIRST elif cross_prod > ALMOST_TANGENT: return CLASSIFICATION_T.SECOND else: # NOTE: A more robust approach would take ||tangent1|| and ||tangent2|| # into account when comparing (tangent1 x tangent2) to the # "almost zero" threshold. We (for now) avoid doing this because # normalizing the tangent vectors has a "cost" of ~6 flops each # and that cost would happen for **every** single intersection. return classify_tangent_intersection( intersection, nodes1, tangent1, nodes2, tangent2 )
python
def classify_intersection(intersection, edge_nodes1, edge_nodes2): r"""Determine which curve is on the "inside of the intersection". .. note:: This is a helper used only by :meth:`.Surface.intersect`. This is intended to be a helper for forming a :class:`.CurvedPolygon` from the edge intersections of two :class:`.Surface`-s. In order to move from one intersection to another (or to the end of an edge), the interior edge must be determined at the point of intersection. The "typical" case is on the interior of both edges: .. image:: ../images/classify_intersection1.png :align: center .. testsetup:: classify-intersection1, classify-intersection2, classify-intersection3, classify-intersection4, classify-intersection5, classify-intersection6, classify-intersection7, classify-intersection8, classify-intersection9 import numpy as np import bezier from bezier import _curve_helpers from bezier._intersection_helpers import Intersection from bezier._surface_helpers import classify_intersection def hodograph(curve, s): return _curve_helpers.evaluate_hodograph( s, curve._nodes) def curvature(curve, s): nodes = curve._nodes tangent = _curve_helpers.evaluate_hodograph( s, nodes) return _curve_helpers.get_curvature( nodes, tangent, s) .. doctest:: classify-intersection1 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.75, 2.0], ... [0.0, 0.25, 1.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.6875, 2.0], ... [0.0, 0.0625, 0.5], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.25, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> tangent1 = hodograph(curve1, s) >>> tangent1 array([[1.25], [0.75]]) >>> tangent2 = hodograph(curve2, t) >>> tangent2 array([[2. ], [0.5]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.FIRST: 0> .. testcleanup:: classify-intersection1 import make_images make_images.classify_intersection1( s, curve1, tangent1, curve2, tangent2) We determine the interior (i.e. left) one by using the `right-hand rule`_: by embedding the tangent vectors in :math:`\mathbf{R}^3`, we compute .. _right-hand rule: https://en.wikipedia.org/wiki/Right-hand_rule .. math:: \left[\begin{array}{c} x_1'(s) \\ y_1'(s) \\ 0 \end{array}\right] \times \left[\begin{array}{c} x_2'(t) \\ y_2'(t) \\ 0 \end{array}\right] = \left[\begin{array}{c} 0 \\ 0 \\ x_1'(s) y_2'(t) - x_2'(t) y_1'(s) \end{array}\right]. If the cross product quantity :math:`B_1'(s) \times B_2'(t) = x_1'(s) y_2'(t) - x_2'(t) y_1'(s)` is positive, then the first curve is "outside" / "to the right", i.e. the second curve is interior. If the cross product is negative, the first curve is interior. When :math:`B_1'(s) \times B_2'(t) = 0`, the tangent vectors are parallel, i.e. the intersection is a point of tangency: .. image:: ../images/classify_intersection2.png :align: center .. doctest:: classify-intersection2 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.5, 2.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_SECOND: 4> .. testcleanup:: classify-intersection2 import make_images make_images.classify_intersection2(s, curve1, curve2) Depending on the direction of the parameterizations, the interior curve may change, but we can use the (signed) `curvature`_ of each curve at that point to determine which is on the interior: .. _curvature: https://en.wikipedia.org/wiki/Curvature .. image:: ../images/classify_intersection3.png :align: center .. doctest:: classify-intersection3 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [2.0, 1.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [3.0, 1.5, 0.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_FIRST: 3> .. testcleanup:: classify-intersection3 import make_images make_images.classify_intersection3(s, curve1, curve2) When the curves are moving in opposite directions at a point of tangency, there is no side to choose. Either the point of tangency is not part of any :class:`.CurvedPolygon` intersection .. image:: ../images/classify_intersection4.png :align: center .. doctest:: classify-intersection4 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [2.0, 1.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.OPPOSED: 2> .. testcleanup:: classify-intersection4 import make_images make_images.classify_intersection4(s, curve1, curve2) or the point of tangency is a "degenerate" part of two :class:`.CurvedPolygon` intersections. It is "degenerate" because from one direction, the point should be classified as :attr:`~.IntersectionClassification.FIRST` and from another as :attr:`~.IntersectionClassification.SECOND`. .. image:: ../images/classify_intersection5.png :align: center .. doctest:: classify-intersection5 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.5, 2.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [3.0, 1.5, 0.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_BOTH: 6> .. testcleanup:: classify-intersection5 import make_images make_images.classify_intersection5(s, curve1, curve2) The :attr:`~.IntersectionClassification.TANGENT_BOTH` classification can also occur if the curves are "kissing" but share a zero width interior at the point of tangency: .. image:: ../images/classify_intersection9.png :align: center .. doctest:: classify-intersection9 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.0, 20.0, 40.0], ... [0.0, 40.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [40.0, 20.0, 0.0], ... [40.0, 0.0, 40.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_BOTH: 6> .. testcleanup:: classify-intersection9 import make_images make_images.classify_intersection9(s, curve1, curve2) However, if the `curvature`_ of each curve is identical, we don't try to distinguish further: .. image:: ../images/classify_intersection6.png :align: center .. doctest:: classify-intersection6 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [-0.125 , -0.125 , 0.375 ], ... [ 0.0625, -0.0625, 0.0625], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [-0.25, -0.25, 0.75], ... [ 0.25, -0.25, 0.25], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> hodograph(curve1, s) array([[0.5], [0. ]]) >>> hodograph(curve2, t) array([[1.], [0.]]) >>> curvature(curve1, s) 2.0 >>> curvature(curve2, t) 2.0 >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) Traceback (most recent call last): ... NotImplementedError: Tangent curves have same curvature. .. testcleanup:: classify-intersection6 import make_images make_images.classify_intersection6(s, curve1, curve2) In addition to points of tangency, intersections that happen at the end of an edge need special handling: .. image:: ../images/classify_intersection7.png :align: center .. doctest:: classify-intersection7 :options: +NORMALIZE_WHITESPACE >>> nodes1a = np.asfortranarray([ ... [0.0, 4.5, 9.0 ], ... [0.0, 0.0, 2.25], ... ]) >>> curve1a = bezier.Curve(nodes1a, degree=2) >>> nodes2 = np.asfortranarray([ ... [11.25, 9.0, 2.75], ... [ 0.0 , 4.5, 1.0 ], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 1.0, 0.375 >>> curve1a.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1a, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) Traceback (most recent call last): ... ValueError: ('Intersection occurs at the end of an edge', 's', 1.0, 't', 0.375) >>> >>> nodes1b = np.asfortranarray([ ... [9.0, 4.5, 0.0], ... [2.25, 2.375, 2.5], ... ]) >>> curve1b = bezier.Curve(nodes1b, degree=2) >>> curve1b.evaluate(0.0) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(1, 0.0, 0, t) >>> edge_nodes1 = (nodes1a, nodes1b, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.FIRST: 0> .. testcleanup:: classify-intersection7 import make_images make_images.classify_intersection7(s, curve1a, curve1b, curve2) As above, some intersections at the end of an edge are part of an actual intersection. However, some surfaces may just "kiss" at a corner intersection: .. image:: ../images/classify_intersection8.png :align: center .. doctest:: classify-intersection8 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.25, 0.0, 0.0, 0.625, 0.5 , 1.0 ], ... [1.0 , 0.5, 0.0, 0.875, 0.375, 0.75], ... ]) >>> surface1 = bezier.Surface(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0625, -0.25, -1.0, -0.5 , -1.0, -1.0], ... [0.5 , 1.0 , 1.0, 0.125, 0.5, 0.0], ... ]) >>> surface2 = bezier.Surface(nodes2, degree=2) >>> curve1, _, _ = surface1.edges >>> edge_nodes1 = [curve.nodes for curve in surface1.edges] >>> curve2, _, _ = surface2.edges >>> edge_nodes2 = [curve.nodes for curve in surface2.edges] >>> s, t = 0.5, 0.0 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.IGNORED_CORNER: 5> .. testcleanup:: classify-intersection8 import make_images make_images.classify_intersection8( s, curve1, surface1, curve2, surface2) .. note:: This assumes the intersection occurs in :math:`\mathbf{R}^2` but doesn't check this. .. note:: This function doesn't allow wiggle room / round-off when checking endpoints, nor when checking if the cross product is near zero, nor when curvatures are compared. However, the most "correct" version of this function likely should allow for some round off. Args: intersection (.Intersection): An intersection object. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: IntersectionClassification: The "inside" curve type, based on the classification enum. Raises: ValueError: If the intersection occurs at the end of either curve involved. This is because we want to classify which curve to **move forward** on, and we can't move past the end of a segment. """ if intersection.s == 1.0 or intersection.t == 1.0: raise ValueError( "Intersection occurs at the end of an edge", "s", intersection.s, "t", intersection.t, ) nodes1 = edge_nodes1[intersection.index_first] tangent1 = _curve_helpers.evaluate_hodograph(intersection.s, nodes1) nodes2 = edge_nodes2[intersection.index_second] tangent2 = _curve_helpers.evaluate_hodograph(intersection.t, nodes2) if ignored_corner( intersection, tangent1, tangent2, edge_nodes1, edge_nodes2 ): return CLASSIFICATION_T.IGNORED_CORNER # Take the cross product of tangent vectors to determine which one # is more "inside" / "to the left". cross_prod = _helpers.cross_product( tangent1.ravel(order="F"), tangent2.ravel(order="F") ) if cross_prod < -ALMOST_TANGENT: return CLASSIFICATION_T.FIRST elif cross_prod > ALMOST_TANGENT: return CLASSIFICATION_T.SECOND else: # NOTE: A more robust approach would take ||tangent1|| and ||tangent2|| # into account when comparing (tangent1 x tangent2) to the # "almost zero" threshold. We (for now) avoid doing this because # normalizing the tangent vectors has a "cost" of ~6 flops each # and that cost would happen for **every** single intersection. return classify_tangent_intersection( intersection, nodes1, tangent1, nodes2, tangent2 )
[ "def", "classify_intersection", "(", "intersection", ",", "edge_nodes1", ",", "edge_nodes2", ")", ":", "if", "intersection", ".", "s", "==", "1.0", "or", "intersection", ".", "t", "==", "1.0", ":", "raise", "ValueError", "(", "\"Intersection occurs at the end of an edge\"", ",", "\"s\"", ",", "intersection", ".", "s", ",", "\"t\"", ",", "intersection", ".", "t", ",", ")", "nodes1", "=", "edge_nodes1", "[", "intersection", ".", "index_first", "]", "tangent1", "=", "_curve_helpers", ".", "evaluate_hodograph", "(", "intersection", ".", "s", ",", "nodes1", ")", "nodes2", "=", "edge_nodes2", "[", "intersection", ".", "index_second", "]", "tangent2", "=", "_curve_helpers", ".", "evaluate_hodograph", "(", "intersection", ".", "t", ",", "nodes2", ")", "if", "ignored_corner", "(", "intersection", ",", "tangent1", ",", "tangent2", ",", "edge_nodes1", ",", "edge_nodes2", ")", ":", "return", "CLASSIFICATION_T", ".", "IGNORED_CORNER", "# Take the cross product of tangent vectors to determine which one", "# is more \"inside\" / \"to the left\".", "cross_prod", "=", "_helpers", ".", "cross_product", "(", "tangent1", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "tangent2", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "if", "cross_prod", "<", "-", "ALMOST_TANGENT", ":", "return", "CLASSIFICATION_T", ".", "FIRST", "elif", "cross_prod", ">", "ALMOST_TANGENT", ":", "return", "CLASSIFICATION_T", ".", "SECOND", "else", ":", "# NOTE: A more robust approach would take ||tangent1|| and ||tangent2||", "# into account when comparing (tangent1 x tangent2) to the", "# \"almost zero\" threshold. We (for now) avoid doing this because", "# normalizing the tangent vectors has a \"cost\" of ~6 flops each", "# and that cost would happen for **every** single intersection.", "return", "classify_tangent_intersection", "(", "intersection", ",", "nodes1", ",", "tangent1", ",", "nodes2", ",", "tangent2", ")" ]
r"""Determine which curve is on the "inside of the intersection". .. note:: This is a helper used only by :meth:`.Surface.intersect`. This is intended to be a helper for forming a :class:`.CurvedPolygon` from the edge intersections of two :class:`.Surface`-s. In order to move from one intersection to another (or to the end of an edge), the interior edge must be determined at the point of intersection. The "typical" case is on the interior of both edges: .. image:: ../images/classify_intersection1.png :align: center .. testsetup:: classify-intersection1, classify-intersection2, classify-intersection3, classify-intersection4, classify-intersection5, classify-intersection6, classify-intersection7, classify-intersection8, classify-intersection9 import numpy as np import bezier from bezier import _curve_helpers from bezier._intersection_helpers import Intersection from bezier._surface_helpers import classify_intersection def hodograph(curve, s): return _curve_helpers.evaluate_hodograph( s, curve._nodes) def curvature(curve, s): nodes = curve._nodes tangent = _curve_helpers.evaluate_hodograph( s, nodes) return _curve_helpers.get_curvature( nodes, tangent, s) .. doctest:: classify-intersection1 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.75, 2.0], ... [0.0, 0.25, 1.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.6875, 2.0], ... [0.0, 0.0625, 0.5], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.25, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> tangent1 = hodograph(curve1, s) >>> tangent1 array([[1.25], [0.75]]) >>> tangent2 = hodograph(curve2, t) >>> tangent2 array([[2. ], [0.5]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.FIRST: 0> .. testcleanup:: classify-intersection1 import make_images make_images.classify_intersection1( s, curve1, tangent1, curve2, tangent2) We determine the interior (i.e. left) one by using the `right-hand rule`_: by embedding the tangent vectors in :math:`\mathbf{R}^3`, we compute .. _right-hand rule: https://en.wikipedia.org/wiki/Right-hand_rule .. math:: \left[\begin{array}{c} x_1'(s) \\ y_1'(s) \\ 0 \end{array}\right] \times \left[\begin{array}{c} x_2'(t) \\ y_2'(t) \\ 0 \end{array}\right] = \left[\begin{array}{c} 0 \\ 0 \\ x_1'(s) y_2'(t) - x_2'(t) y_1'(s) \end{array}\right]. If the cross product quantity :math:`B_1'(s) \times B_2'(t) = x_1'(s) y_2'(t) - x_2'(t) y_1'(s)` is positive, then the first curve is "outside" / "to the right", i.e. the second curve is interior. If the cross product is negative, the first curve is interior. When :math:`B_1'(s) \times B_2'(t) = 0`, the tangent vectors are parallel, i.e. the intersection is a point of tangency: .. image:: ../images/classify_intersection2.png :align: center .. doctest:: classify-intersection2 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.5, 2.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_SECOND: 4> .. testcleanup:: classify-intersection2 import make_images make_images.classify_intersection2(s, curve1, curve2) Depending on the direction of the parameterizations, the interior curve may change, but we can use the (signed) `curvature`_ of each curve at that point to determine which is on the interior: .. _curvature: https://en.wikipedia.org/wiki/Curvature .. image:: ../images/classify_intersection3.png :align: center .. doctest:: classify-intersection3 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [2.0, 1.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [3.0, 1.5, 0.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_FIRST: 3> .. testcleanup:: classify-intersection3 import make_images make_images.classify_intersection3(s, curve1, curve2) When the curves are moving in opposite directions at a point of tangency, there is no side to choose. Either the point of tangency is not part of any :class:`.CurvedPolygon` intersection .. image:: ../images/classify_intersection4.png :align: center .. doctest:: classify-intersection4 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [2.0, 1.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.OPPOSED: 2> .. testcleanup:: classify-intersection4 import make_images make_images.classify_intersection4(s, curve1, curve2) or the point of tangency is a "degenerate" part of two :class:`.CurvedPolygon` intersections. It is "degenerate" because from one direction, the point should be classified as :attr:`~.IntersectionClassification.FIRST` and from another as :attr:`~.IntersectionClassification.SECOND`. .. image:: ../images/classify_intersection5.png :align: center .. doctest:: classify-intersection5 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [1.0, 1.5, 2.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [3.0, 1.5, 0.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_BOTH: 6> .. testcleanup:: classify-intersection5 import make_images make_images.classify_intersection5(s, curve1, curve2) The :attr:`~.IntersectionClassification.TANGENT_BOTH` classification can also occur if the curves are "kissing" but share a zero width interior at the point of tangency: .. image:: ../images/classify_intersection9.png :align: center .. doctest:: classify-intersection9 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.0, 20.0, 40.0], ... [0.0, 40.0, 0.0], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [40.0, 20.0, 0.0], ... [40.0, 0.0, 40.0], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.TANGENT_BOTH: 6> .. testcleanup:: classify-intersection9 import make_images make_images.classify_intersection9(s, curve1, curve2) However, if the `curvature`_ of each curve is identical, we don't try to distinguish further: .. image:: ../images/classify_intersection6.png :align: center .. doctest:: classify-intersection6 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [-0.125 , -0.125 , 0.375 ], ... [ 0.0625, -0.0625, 0.0625], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [-0.25, -0.25, 0.75], ... [ 0.25, -0.25, 0.25], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 0.5, 0.5 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> hodograph(curve1, s) array([[0.5], [0. ]]) >>> hodograph(curve2, t) array([[1.], [0.]]) >>> curvature(curve1, s) 2.0 >>> curvature(curve2, t) 2.0 >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) Traceback (most recent call last): ... NotImplementedError: Tangent curves have same curvature. .. testcleanup:: classify-intersection6 import make_images make_images.classify_intersection6(s, curve1, curve2) In addition to points of tangency, intersections that happen at the end of an edge need special handling: .. image:: ../images/classify_intersection7.png :align: center .. doctest:: classify-intersection7 :options: +NORMALIZE_WHITESPACE >>> nodes1a = np.asfortranarray([ ... [0.0, 4.5, 9.0 ], ... [0.0, 0.0, 2.25], ... ]) >>> curve1a = bezier.Curve(nodes1a, degree=2) >>> nodes2 = np.asfortranarray([ ... [11.25, 9.0, 2.75], ... [ 0.0 , 4.5, 1.0 ], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=2) >>> s, t = 1.0, 0.375 >>> curve1a.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> edge_nodes1 = (nodes1a, None, None) >>> edge_nodes2 = (nodes2, None, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) Traceback (most recent call last): ... ValueError: ('Intersection occurs at the end of an edge', 's', 1.0, 't', 0.375) >>> >>> nodes1b = np.asfortranarray([ ... [9.0, 4.5, 0.0], ... [2.25, 2.375, 2.5], ... ]) >>> curve1b = bezier.Curve(nodes1b, degree=2) >>> curve1b.evaluate(0.0) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(1, 0.0, 0, t) >>> edge_nodes1 = (nodes1a, nodes1b, None) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.FIRST: 0> .. testcleanup:: classify-intersection7 import make_images make_images.classify_intersection7(s, curve1a, curve1b, curve2) As above, some intersections at the end of an edge are part of an actual intersection. However, some surfaces may just "kiss" at a corner intersection: .. image:: ../images/classify_intersection8.png :align: center .. doctest:: classify-intersection8 :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.25, 0.0, 0.0, 0.625, 0.5 , 1.0 ], ... [1.0 , 0.5, 0.0, 0.875, 0.375, 0.75], ... ]) >>> surface1 = bezier.Surface(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.0625, -0.25, -1.0, -0.5 , -1.0, -1.0], ... [0.5 , 1.0 , 1.0, 0.125, 0.5, 0.0], ... ]) >>> surface2 = bezier.Surface(nodes2, degree=2) >>> curve1, _, _ = surface1.edges >>> edge_nodes1 = [curve.nodes for curve in surface1.edges] >>> curve2, _, _ = surface2.edges >>> edge_nodes2 = [curve.nodes for curve in surface2.edges] >>> s, t = 0.5, 0.0 >>> curve1.evaluate(s) == curve2.evaluate(t) array([[ True], [ True]]) >>> intersection = Intersection(0, s, 0, t) >>> classify_intersection(intersection, edge_nodes1, edge_nodes2) <IntersectionClassification.IGNORED_CORNER: 5> .. testcleanup:: classify-intersection8 import make_images make_images.classify_intersection8( s, curve1, surface1, curve2, surface2) .. note:: This assumes the intersection occurs in :math:`\mathbf{R}^2` but doesn't check this. .. note:: This function doesn't allow wiggle room / round-off when checking endpoints, nor when checking if the cross product is near zero, nor when curvatures are compared. However, the most "correct" version of this function likely should allow for some round off. Args: intersection (.Intersection): An intersection object. edge_nodes1 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the first surface being intersected. edge_nodes2 (Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]): The nodes of the three edges of the second surface being intersected. Returns: IntersectionClassification: The "inside" curve type, based on the classification enum. Raises: ValueError: If the intersection occurs at the end of either curve involved. This is because we want to classify which curve to **move forward** on, and we can't move past the end of a segment.
[ "r", "Determine", "which", "curve", "is", "on", "the", "inside", "of", "the", "intersection", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L1620-L2096
dhermes/bezier
src/bezier/_surface_helpers.py
handle_ends
def handle_ends(index1, s, index2, t): """Updates intersection parameters if it is on the end of an edge. .. note:: This is a helper used only by :meth:`.Surface.intersect`. Does nothing if the intersection happens in the middle of two edges. If the intersection occurs at the end of the first curve, moves it to the beginning of the next edge. Similar for the second curve. This function is used as a pre-processing step before passing an intersection to :func:`classify_intersection`. There, only corners that **begin** an edge are considered, since that function is trying to determine which edge to **move forward** on. Args: index1 (int): The index (among 0, 1, 2) of the first edge in the intersection. s (float): The parameter along the first curve of the intersection. index2 (int): The index (among 0, 1, 2) of the second edge in the intersection. t (float): The parameter along the second curve of the intersection. Returns: Tuple[bool, bool, Tuple[int, float, int, float]]: A triple of: * flag indicating if the intersection is at the end of an edge * flag indicating if the intersection is a "corner" * 4-tuple of the "updated" values ``(index1, s, index2, t)`` """ edge_end = False if s == 1.0: s = 0.0 index1 = (index1 + 1) % 3 edge_end = True # NOTE: This is not a typo, the values can be updated twice if both ``s`` # and ``t`` are ``1.0`` if t == 1.0: t = 0.0 index2 = (index2 + 1) % 3 edge_end = True is_corner = s == 0.0 or t == 0.0 return edge_end, is_corner, (index1, s, index2, t)
python
def handle_ends(index1, s, index2, t): """Updates intersection parameters if it is on the end of an edge. .. note:: This is a helper used only by :meth:`.Surface.intersect`. Does nothing if the intersection happens in the middle of two edges. If the intersection occurs at the end of the first curve, moves it to the beginning of the next edge. Similar for the second curve. This function is used as a pre-processing step before passing an intersection to :func:`classify_intersection`. There, only corners that **begin** an edge are considered, since that function is trying to determine which edge to **move forward** on. Args: index1 (int): The index (among 0, 1, 2) of the first edge in the intersection. s (float): The parameter along the first curve of the intersection. index2 (int): The index (among 0, 1, 2) of the second edge in the intersection. t (float): The parameter along the second curve of the intersection. Returns: Tuple[bool, bool, Tuple[int, float, int, float]]: A triple of: * flag indicating if the intersection is at the end of an edge * flag indicating if the intersection is a "corner" * 4-tuple of the "updated" values ``(index1, s, index2, t)`` """ edge_end = False if s == 1.0: s = 0.0 index1 = (index1 + 1) % 3 edge_end = True # NOTE: This is not a typo, the values can be updated twice if both ``s`` # and ``t`` are ``1.0`` if t == 1.0: t = 0.0 index2 = (index2 + 1) % 3 edge_end = True is_corner = s == 0.0 or t == 0.0 return edge_end, is_corner, (index1, s, index2, t)
[ "def", "handle_ends", "(", "index1", ",", "s", ",", "index2", ",", "t", ")", ":", "edge_end", "=", "False", "if", "s", "==", "1.0", ":", "s", "=", "0.0", "index1", "=", "(", "index1", "+", "1", ")", "%", "3", "edge_end", "=", "True", "# NOTE: This is not a typo, the values can be updated twice if both ``s``", "# and ``t`` are ``1.0``", "if", "t", "==", "1.0", ":", "t", "=", "0.0", "index2", "=", "(", "index2", "+", "1", ")", "%", "3", "edge_end", "=", "True", "is_corner", "=", "s", "==", "0.0", "or", "t", "==", "0.0", "return", "edge_end", ",", "is_corner", ",", "(", "index1", ",", "s", ",", "index2", ",", "t", ")" ]
Updates intersection parameters if it is on the end of an edge. .. note:: This is a helper used only by :meth:`.Surface.intersect`. Does nothing if the intersection happens in the middle of two edges. If the intersection occurs at the end of the first curve, moves it to the beginning of the next edge. Similar for the second curve. This function is used as a pre-processing step before passing an intersection to :func:`classify_intersection`. There, only corners that **begin** an edge are considered, since that function is trying to determine which edge to **move forward** on. Args: index1 (int): The index (among 0, 1, 2) of the first edge in the intersection. s (float): The parameter along the first curve of the intersection. index2 (int): The index (among 0, 1, 2) of the second edge in the intersection. t (float): The parameter along the second curve of the intersection. Returns: Tuple[bool, bool, Tuple[int, float, int, float]]: A triple of: * flag indicating if the intersection is at the end of an edge * flag indicating if the intersection is a "corner" * 4-tuple of the "updated" values ``(index1, s, index2, t)``
[ "Updates", "intersection", "parameters", "if", "it", "is", "on", "the", "end", "of", "an", "edge", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2099-L2145
dhermes/bezier
src/bezier/_surface_helpers.py
to_front
def to_front(intersection, intersections, unused): """Rotates a node to the "front". .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. If a node is at the end of a segment, moves it to the beginning of the next segment (at the exact same point). We assume that callers have pruned ``intersections`` so that there are none with ``s == 1.0`` or ``t == 1.0``. Hence, any such intersection will be an "artificial" intersection added by :func:`get_next`. .. note:: This method checks for **exact** endpoints, i.e. parameter bitwise identical to ``1.0``. But it may make sense to allow some wiggle room. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. unused (List[.Intersection]): List of nodes that haven't been used yet in an intersection curved polygon Returns: .Intersection: An intersection to (maybe) move to the beginning of the next edge of the surface. """ if intersection.s == 1.0: next_index = (intersection.index_first + 1) % 3 # Make sure we haven't accidentally ignored an existing intersection. for other_int in intersections: if other_int.s == 0.0 and other_int.index_first == next_index: if other_int in unused: unused.remove(other_int) return other_int # If we haven't already returned, create **another** artificial # intersection. return _intersection_helpers.Intersection( next_index, 0.0, None, None, interior_curve=CLASSIFICATION_T.FIRST ) elif intersection.t == 1.0: # NOTE: We assume, but do not check, that ``s == 1.0`` and ``t == 1.0`` # are mutually exclusive. next_index = (intersection.index_second + 1) % 3 # Make sure we haven't accidentally ignored an existing intersection. for other_int in intersections: if other_int.t == 0.0 and other_int.index_second == next_index: if other_int in unused: unused.remove(other_int) return other_int # If we haven't already returned, create **another** artificial # intersection. return _intersection_helpers.Intersection( None, None, next_index, 0.0, interior_curve=CLASSIFICATION_T.SECOND ) else: return intersection
python
def to_front(intersection, intersections, unused): """Rotates a node to the "front". .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. If a node is at the end of a segment, moves it to the beginning of the next segment (at the exact same point). We assume that callers have pruned ``intersections`` so that there are none with ``s == 1.0`` or ``t == 1.0``. Hence, any such intersection will be an "artificial" intersection added by :func:`get_next`. .. note:: This method checks for **exact** endpoints, i.e. parameter bitwise identical to ``1.0``. But it may make sense to allow some wiggle room. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. unused (List[.Intersection]): List of nodes that haven't been used yet in an intersection curved polygon Returns: .Intersection: An intersection to (maybe) move to the beginning of the next edge of the surface. """ if intersection.s == 1.0: next_index = (intersection.index_first + 1) % 3 # Make sure we haven't accidentally ignored an existing intersection. for other_int in intersections: if other_int.s == 0.0 and other_int.index_first == next_index: if other_int in unused: unused.remove(other_int) return other_int # If we haven't already returned, create **another** artificial # intersection. return _intersection_helpers.Intersection( next_index, 0.0, None, None, interior_curve=CLASSIFICATION_T.FIRST ) elif intersection.t == 1.0: # NOTE: We assume, but do not check, that ``s == 1.0`` and ``t == 1.0`` # are mutually exclusive. next_index = (intersection.index_second + 1) % 3 # Make sure we haven't accidentally ignored an existing intersection. for other_int in intersections: if other_int.t == 0.0 and other_int.index_second == next_index: if other_int in unused: unused.remove(other_int) return other_int # If we haven't already returned, create **another** artificial # intersection. return _intersection_helpers.Intersection( None, None, next_index, 0.0, interior_curve=CLASSIFICATION_T.SECOND ) else: return intersection
[ "def", "to_front", "(", "intersection", ",", "intersections", ",", "unused", ")", ":", "if", "intersection", ".", "s", "==", "1.0", ":", "next_index", "=", "(", "intersection", ".", "index_first", "+", "1", ")", "%", "3", "# Make sure we haven't accidentally ignored an existing intersection.", "for", "other_int", "in", "intersections", ":", "if", "other_int", ".", "s", "==", "0.0", "and", "other_int", ".", "index_first", "==", "next_index", ":", "if", "other_int", "in", "unused", ":", "unused", ".", "remove", "(", "other_int", ")", "return", "other_int", "# If we haven't already returned, create **another** artificial", "# intersection.", "return", "_intersection_helpers", ".", "Intersection", "(", "next_index", ",", "0.0", ",", "None", ",", "None", ",", "interior_curve", "=", "CLASSIFICATION_T", ".", "FIRST", ")", "elif", "intersection", ".", "t", "==", "1.0", ":", "# NOTE: We assume, but do not check, that ``s == 1.0`` and ``t == 1.0``", "# are mutually exclusive.", "next_index", "=", "(", "intersection", ".", "index_second", "+", "1", ")", "%", "3", "# Make sure we haven't accidentally ignored an existing intersection.", "for", "other_int", "in", "intersections", ":", "if", "other_int", ".", "t", "==", "0.0", "and", "other_int", ".", "index_second", "==", "next_index", ":", "if", "other_int", "in", "unused", ":", "unused", ".", "remove", "(", "other_int", ")", "return", "other_int", "# If we haven't already returned, create **another** artificial", "# intersection.", "return", "_intersection_helpers", ".", "Intersection", "(", "None", ",", "None", ",", "next_index", ",", "0.0", ",", "interior_curve", "=", "CLASSIFICATION_T", ".", "SECOND", ")", "else", ":", "return", "intersection" ]
Rotates a node to the "front". .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. If a node is at the end of a segment, moves it to the beginning of the next segment (at the exact same point). We assume that callers have pruned ``intersections`` so that there are none with ``s == 1.0`` or ``t == 1.0``. Hence, any such intersection will be an "artificial" intersection added by :func:`get_next`. .. note:: This method checks for **exact** endpoints, i.e. parameter bitwise identical to ``1.0``. But it may make sense to allow some wiggle room. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. unused (List[.Intersection]): List of nodes that haven't been used yet in an intersection curved polygon Returns: .Intersection: An intersection to (maybe) move to the beginning of the next edge of the surface.
[ "Rotates", "a", "node", "to", "the", "front", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2148-L2213
dhermes/bezier
src/bezier/_surface_helpers.py
get_next_first
def get_next_first(intersection, intersections, to_end=True): """Gets the next node along the current (first) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_second`, this function does the majority of the heavy lifting in :func:`get_next`. **Very** similar to :func:`get_next_second`, but this works with the first curve while the other function works with the second. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. to_end (Optional[bool]): Indicates if the next node should just be the end of the first edge or :data:`None`. Returns: Optional[.Intersection]: The "next" point along a surface of intersection. This will produce the next intersection along the current (first) edge or the end of the same edge. If ``to_end`` is :data:`False` and there are no other intersections along the current edge, will return :data:`None` (rather than the end of the same edge). """ along_edge = None index_first = intersection.index_first s = intersection.s for other_int in intersections: other_s = other_int.s if other_int.index_first == index_first and other_s > s: if along_edge is None or other_s < along_edge.s: along_edge = other_int if along_edge is None: if to_end: # If there is no other intersection on the edge, just return # the segment end. return _intersection_helpers.Intersection( index_first, 1.0, None, None, interior_curve=CLASSIFICATION_T.FIRST, ) else: return None else: return along_edge
python
def get_next_first(intersection, intersections, to_end=True): """Gets the next node along the current (first) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_second`, this function does the majority of the heavy lifting in :func:`get_next`. **Very** similar to :func:`get_next_second`, but this works with the first curve while the other function works with the second. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. to_end (Optional[bool]): Indicates if the next node should just be the end of the first edge or :data:`None`. Returns: Optional[.Intersection]: The "next" point along a surface of intersection. This will produce the next intersection along the current (first) edge or the end of the same edge. If ``to_end`` is :data:`False` and there are no other intersections along the current edge, will return :data:`None` (rather than the end of the same edge). """ along_edge = None index_first = intersection.index_first s = intersection.s for other_int in intersections: other_s = other_int.s if other_int.index_first == index_first and other_s > s: if along_edge is None or other_s < along_edge.s: along_edge = other_int if along_edge is None: if to_end: # If there is no other intersection on the edge, just return # the segment end. return _intersection_helpers.Intersection( index_first, 1.0, None, None, interior_curve=CLASSIFICATION_T.FIRST, ) else: return None else: return along_edge
[ "def", "get_next_first", "(", "intersection", ",", "intersections", ",", "to_end", "=", "True", ")", ":", "along_edge", "=", "None", "index_first", "=", "intersection", ".", "index_first", "s", "=", "intersection", ".", "s", "for", "other_int", "in", "intersections", ":", "other_s", "=", "other_int", ".", "s", "if", "other_int", ".", "index_first", "==", "index_first", "and", "other_s", ">", "s", ":", "if", "along_edge", "is", "None", "or", "other_s", "<", "along_edge", ".", "s", ":", "along_edge", "=", "other_int", "if", "along_edge", "is", "None", ":", "if", "to_end", ":", "# If there is no other intersection on the edge, just return", "# the segment end.", "return", "_intersection_helpers", ".", "Intersection", "(", "index_first", ",", "1.0", ",", "None", ",", "None", ",", "interior_curve", "=", "CLASSIFICATION_T", ".", "FIRST", ",", ")", "else", ":", "return", "None", "else", ":", "return", "along_edge" ]
Gets the next node along the current (first) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_second`, this function does the majority of the heavy lifting in :func:`get_next`. **Very** similar to :func:`get_next_second`, but this works with the first curve while the other function works with the second. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. to_end (Optional[bool]): Indicates if the next node should just be the end of the first edge or :data:`None`. Returns: Optional[.Intersection]: The "next" point along a surface of intersection. This will produce the next intersection along the current (first) edge or the end of the same edge. If ``to_end`` is :data:`False` and there are no other intersections along the current edge, will return :data:`None` (rather than the end of the same edge).
[ "Gets", "the", "next", "node", "along", "the", "current", "(", "first", ")", "edge", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2216-L2269
dhermes/bezier
src/bezier/_surface_helpers.py
get_next_second
def get_next_second(intersection, intersections, to_end=True): """Gets the next node along the current (second) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_first`, this function does the majority of the heavy lifting in :func:`get_next`. **Very** similar to :func:`get_next_first`, but this works with the second curve while the other function works with the first. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. to_end (Optional[bool]): Indicates if the next node should just be the end of the first edge or :data:`None`. Returns: Optional[.Intersection]: The "next" point along a surface of intersection. This will produce the next intersection along the current (second) edge or the end of the same edge. If ``to_end`` is :data:`False` and there are no other intersections along the current edge, will return :data:`None` (rather than the end of the same edge). """ along_edge = None index_second = intersection.index_second t = intersection.t for other_int in intersections: other_t = other_int.t if other_int.index_second == index_second and other_t > t: if along_edge is None or other_t < along_edge.t: along_edge = other_int if along_edge is None: if to_end: # If there is no other intersection on the edge, just return # the segment end. return _intersection_helpers.Intersection( None, None, index_second, 1.0, interior_curve=CLASSIFICATION_T.SECOND, ) else: return None else: return along_edge
python
def get_next_second(intersection, intersections, to_end=True): """Gets the next node along the current (second) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_first`, this function does the majority of the heavy lifting in :func:`get_next`. **Very** similar to :func:`get_next_first`, but this works with the second curve while the other function works with the first. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. to_end (Optional[bool]): Indicates if the next node should just be the end of the first edge or :data:`None`. Returns: Optional[.Intersection]: The "next" point along a surface of intersection. This will produce the next intersection along the current (second) edge or the end of the same edge. If ``to_end`` is :data:`False` and there are no other intersections along the current edge, will return :data:`None` (rather than the end of the same edge). """ along_edge = None index_second = intersection.index_second t = intersection.t for other_int in intersections: other_t = other_int.t if other_int.index_second == index_second and other_t > t: if along_edge is None or other_t < along_edge.t: along_edge = other_int if along_edge is None: if to_end: # If there is no other intersection on the edge, just return # the segment end. return _intersection_helpers.Intersection( None, None, index_second, 1.0, interior_curve=CLASSIFICATION_T.SECOND, ) else: return None else: return along_edge
[ "def", "get_next_second", "(", "intersection", ",", "intersections", ",", "to_end", "=", "True", ")", ":", "along_edge", "=", "None", "index_second", "=", "intersection", ".", "index_second", "t", "=", "intersection", ".", "t", "for", "other_int", "in", "intersections", ":", "other_t", "=", "other_int", ".", "t", "if", "other_int", ".", "index_second", "==", "index_second", "and", "other_t", ">", "t", ":", "if", "along_edge", "is", "None", "or", "other_t", "<", "along_edge", ".", "t", ":", "along_edge", "=", "other_int", "if", "along_edge", "is", "None", ":", "if", "to_end", ":", "# If there is no other intersection on the edge, just return", "# the segment end.", "return", "_intersection_helpers", ".", "Intersection", "(", "None", ",", "None", ",", "index_second", ",", "1.0", ",", "interior_curve", "=", "CLASSIFICATION_T", ".", "SECOND", ",", ")", "else", ":", "return", "None", "else", ":", "return", "along_edge" ]
Gets the next node along the current (second) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_first`, this function does the majority of the heavy lifting in :func:`get_next`. **Very** similar to :func:`get_next_first`, but this works with the second curve while the other function works with the first. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. to_end (Optional[bool]): Indicates if the next node should just be the end of the first edge or :data:`None`. Returns: Optional[.Intersection]: The "next" point along a surface of intersection. This will produce the next intersection along the current (second) edge or the end of the same edge. If ``to_end`` is :data:`False` and there are no other intersections along the current edge, will return :data:`None` (rather than the end of the same edge).
[ "Gets", "the", "next", "node", "along", "the", "current", "(", "second", ")", "edge", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2272-L2325
dhermes/bezier
src/bezier/_surface_helpers.py
get_next_coincident
def get_next_coincident(intersection, intersections): """Gets the next node along the current (coincident) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_first` and :func:`get_next_second`, this function does the majority of the heavy lifting in :func:`get_next`. This function moves immediately to the "next" intersection along the "current" edge. An intersection labeled as ``COINCIDENT`` can only occur at the beginning of a segment. The end will correspond to the ``s`` or ``t`` parameter being equal to ``1.0`` and so it will get "rotated" to the front of the next edge, where it will be classified according to a different edge pair. It's also worth noting that some coincident segments will correspond to curves moving in opposite directions. In that case, there is no "interior" intersection (the curves are facing away) and so that segment won't be part of an intersection. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. Returns: .Intersection: The "next" point along a surface of intersection. This will produce the next intersection along the current (second) edge or the end of the same edge. """ # Moving along the first or second edge will produce the same result, but # since we "rotate" all intersections to the beginning of an edge, the # index may not match the first or second (or both). along_first = get_next_first(intersection, intersections, to_end=False) if along_first is None: along_second = get_next_second( intersection, intersections, to_end=False ) else: return along_first if along_second is None: return _intersection_helpers.Intersection( intersection.index_first, 1.0, intersection.index_second, 1.0, interior_curve=CLASSIFICATION_T.COINCIDENT, ) else: return along_second
python
def get_next_coincident(intersection, intersections): """Gets the next node along the current (coincident) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_first` and :func:`get_next_second`, this function does the majority of the heavy lifting in :func:`get_next`. This function moves immediately to the "next" intersection along the "current" edge. An intersection labeled as ``COINCIDENT`` can only occur at the beginning of a segment. The end will correspond to the ``s`` or ``t`` parameter being equal to ``1.0`` and so it will get "rotated" to the front of the next edge, where it will be classified according to a different edge pair. It's also worth noting that some coincident segments will correspond to curves moving in opposite directions. In that case, there is no "interior" intersection (the curves are facing away) and so that segment won't be part of an intersection. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. Returns: .Intersection: The "next" point along a surface of intersection. This will produce the next intersection along the current (second) edge or the end of the same edge. """ # Moving along the first or second edge will produce the same result, but # since we "rotate" all intersections to the beginning of an edge, the # index may not match the first or second (or both). along_first = get_next_first(intersection, intersections, to_end=False) if along_first is None: along_second = get_next_second( intersection, intersections, to_end=False ) else: return along_first if along_second is None: return _intersection_helpers.Intersection( intersection.index_first, 1.0, intersection.index_second, 1.0, interior_curve=CLASSIFICATION_T.COINCIDENT, ) else: return along_second
[ "def", "get_next_coincident", "(", "intersection", ",", "intersections", ")", ":", "# Moving along the first or second edge will produce the same result, but", "# since we \"rotate\" all intersections to the beginning of an edge, the", "# index may not match the first or second (or both).", "along_first", "=", "get_next_first", "(", "intersection", ",", "intersections", ",", "to_end", "=", "False", ")", "if", "along_first", "is", "None", ":", "along_second", "=", "get_next_second", "(", "intersection", ",", "intersections", ",", "to_end", "=", "False", ")", "else", ":", "return", "along_first", "if", "along_second", "is", "None", ":", "return", "_intersection_helpers", ".", "Intersection", "(", "intersection", ".", "index_first", ",", "1.0", ",", "intersection", ".", "index_second", ",", "1.0", ",", "interior_curve", "=", "CLASSIFICATION_T", ".", "COINCIDENT", ",", ")", "else", ":", "return", "along_second" ]
Gets the next node along the current (coincident) edge. .. note:: This is a helper used only by :func:`get_next`, which in turn is only used by :func:`basic_interior_combine`, which itself is only used by :func:`combine_intersections`. Along with :func:`get_next_first` and :func:`get_next_second`, this function does the majority of the heavy lifting in :func:`get_next`. This function moves immediately to the "next" intersection along the "current" edge. An intersection labeled as ``COINCIDENT`` can only occur at the beginning of a segment. The end will correspond to the ``s`` or ``t`` parameter being equal to ``1.0`` and so it will get "rotated" to the front of the next edge, where it will be classified according to a different edge pair. It's also worth noting that some coincident segments will correspond to curves moving in opposite directions. In that case, there is no "interior" intersection (the curves are facing away) and so that segment won't be part of an intersection. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. Returns: .Intersection: The "next" point along a surface of intersection. This will produce the next intersection along the current (second) edge or the end of the same edge.
[ "Gets", "the", "next", "node", "along", "the", "current", "(", "coincident", ")", "edge", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2328-L2384
dhermes/bezier
src/bezier/_surface_helpers.py
get_next
def get_next(intersection, intersections, unused): """Gets the next node along a given edge. .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. This function does the majority of the heavy lifting for :func:`basic_interior_combine`. .. note:: This function returns :class:`.Intersection` objects even when the point isn't strictly an intersection. This is "incorrect" in some sense, but for now, we don't bother implementing a class similar to, but different from, :class:`.Intersection` to satisfy this need. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. unused (List[.Intersection]): List of nodes that haven't been used yet in an intersection curved polygon Returns: .Intersection: The "next" point along a surface of intersection. This will produce the next intersection along the current edge or the end of the current edge. Raises: ValueError: If the intersection is not classified as :attr:`~.IntersectionClassification.FIRST`, :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.SECOND`, :attr:`~.IntersectionClassification.TANGENT_SECOND` or :attr:`~.IntersectionClassification.COINCIDENT`. """ result = None if is_first(intersection.interior_curve): result = get_next_first(intersection, intersections) elif is_second(intersection.interior_curve): result = get_next_second(intersection, intersections) elif intersection.interior_curve == CLASSIFICATION_T.COINCIDENT: result = get_next_coincident(intersection, intersections) else: raise ValueError( 'Cannot get next node if not starting from "FIRST", ' '"TANGENT_FIRST", "SECOND", "TANGENT_SECOND" or "COINCIDENT".' ) if result in unused: unused.remove(result) return result
python
def get_next(intersection, intersections, unused): """Gets the next node along a given edge. .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. This function does the majority of the heavy lifting for :func:`basic_interior_combine`. .. note:: This function returns :class:`.Intersection` objects even when the point isn't strictly an intersection. This is "incorrect" in some sense, but for now, we don't bother implementing a class similar to, but different from, :class:`.Intersection` to satisfy this need. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. unused (List[.Intersection]): List of nodes that haven't been used yet in an intersection curved polygon Returns: .Intersection: The "next" point along a surface of intersection. This will produce the next intersection along the current edge or the end of the current edge. Raises: ValueError: If the intersection is not classified as :attr:`~.IntersectionClassification.FIRST`, :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.SECOND`, :attr:`~.IntersectionClassification.TANGENT_SECOND` or :attr:`~.IntersectionClassification.COINCIDENT`. """ result = None if is_first(intersection.interior_curve): result = get_next_first(intersection, intersections) elif is_second(intersection.interior_curve): result = get_next_second(intersection, intersections) elif intersection.interior_curve == CLASSIFICATION_T.COINCIDENT: result = get_next_coincident(intersection, intersections) else: raise ValueError( 'Cannot get next node if not starting from "FIRST", ' '"TANGENT_FIRST", "SECOND", "TANGENT_SECOND" or "COINCIDENT".' ) if result in unused: unused.remove(result) return result
[ "def", "get_next", "(", "intersection", ",", "intersections", ",", "unused", ")", ":", "result", "=", "None", "if", "is_first", "(", "intersection", ".", "interior_curve", ")", ":", "result", "=", "get_next_first", "(", "intersection", ",", "intersections", ")", "elif", "is_second", "(", "intersection", ".", "interior_curve", ")", ":", "result", "=", "get_next_second", "(", "intersection", ",", "intersections", ")", "elif", "intersection", ".", "interior_curve", "==", "CLASSIFICATION_T", ".", "COINCIDENT", ":", "result", "=", "get_next_coincident", "(", "intersection", ",", "intersections", ")", "else", ":", "raise", "ValueError", "(", "'Cannot get next node if not starting from \"FIRST\", '", "'\"TANGENT_FIRST\", \"SECOND\", \"TANGENT_SECOND\" or \"COINCIDENT\".'", ")", "if", "result", "in", "unused", ":", "unused", ".", "remove", "(", "result", ")", "return", "result" ]
Gets the next node along a given edge. .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. This function does the majority of the heavy lifting for :func:`basic_interior_combine`. .. note:: This function returns :class:`.Intersection` objects even when the point isn't strictly an intersection. This is "incorrect" in some sense, but for now, we don't bother implementing a class similar to, but different from, :class:`.Intersection` to satisfy this need. Args: intersection (.Intersection): The current intersection. intersections (List[.Intersection]): List of all detected intersections, provided as a reference for potential points to arrive at. unused (List[.Intersection]): List of nodes that haven't been used yet in an intersection curved polygon Returns: .Intersection: The "next" point along a surface of intersection. This will produce the next intersection along the current edge or the end of the current edge. Raises: ValueError: If the intersection is not classified as :attr:`~.IntersectionClassification.FIRST`, :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.SECOND`, :attr:`~.IntersectionClassification.TANGENT_SECOND` or :attr:`~.IntersectionClassification.COINCIDENT`.
[ "Gets", "the", "next", "node", "along", "a", "given", "edge", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2419-L2472
dhermes/bezier
src/bezier/_surface_helpers.py
ends_to_curve
def ends_to_curve(start_node, end_node): """Convert a "pair" of intersection nodes to a curve segment. .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. .. note:: This function could specialize to the first or second segment attached to ``start_node`` and ``end_node``. We determine first / second based on the classification of ``start_node``, but the callers of this function could provide that information / isolate the base curve and the two parameters for us. .. note:: This only checks the classification of the ``start_node``. Args: start_node (.Intersection): The beginning of a segment. end_node (.Intersection): The end of (the same) segment. Returns: Tuple[int, float, float]: The 3-tuple of: * The edge index along the first surface (if in ``{0, 1, 2}``) or the edge index along the second surface shifted to the right by 3 (if in ``{3, 4, 5}``) * The start parameter along the edge * The end parameter along the edge Raises: ValueError: If the ``start_node`` and ``end_node`` disagree on the first curve when classified as "FIRST". ValueError: If the ``start_node`` and ``end_node`` disagree on the second curve when classified as "SECOND". ValueError: If the ``start_node`` and ``end_node`` disagree on the both curves when classified as "COINCIDENT". ValueError: If the ``start_node`` is not classified as :attr:`~.IntersectionClassification.FIRST`, :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.SECOND`, :attr:`~.IntersectionClassification.TANGENT_SECOND` or :attr:`~.IntersectionClassification.COINCIDENT`. """ if is_first(start_node.interior_curve): if end_node.index_first != start_node.index_first: raise ValueError(_WRONG_CURVE) return start_node.index_first, start_node.s, end_node.s elif is_second(start_node.interior_curve): if end_node.index_second != start_node.index_second: raise ValueError(_WRONG_CURVE) return start_node.index_second + 3, start_node.t, end_node.t elif start_node.interior_curve == CLASSIFICATION_T.COINCIDENT: if end_node.index_first == start_node.index_first: return start_node.index_first, start_node.s, end_node.s elif end_node.index_second == start_node.index_second: return start_node.index_second + 3, start_node.t, end_node.t else: raise ValueError(_WRONG_CURVE) else: raise ValueError( 'Segment start must be classified as "FIRST", "TANGENT_FIRST", ' '"SECOND", "TANGENT_SECOND" or "COINCIDENT".' )
python
def ends_to_curve(start_node, end_node): """Convert a "pair" of intersection nodes to a curve segment. .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. .. note:: This function could specialize to the first or second segment attached to ``start_node`` and ``end_node``. We determine first / second based on the classification of ``start_node``, but the callers of this function could provide that information / isolate the base curve and the two parameters for us. .. note:: This only checks the classification of the ``start_node``. Args: start_node (.Intersection): The beginning of a segment. end_node (.Intersection): The end of (the same) segment. Returns: Tuple[int, float, float]: The 3-tuple of: * The edge index along the first surface (if in ``{0, 1, 2}``) or the edge index along the second surface shifted to the right by 3 (if in ``{3, 4, 5}``) * The start parameter along the edge * The end parameter along the edge Raises: ValueError: If the ``start_node`` and ``end_node`` disagree on the first curve when classified as "FIRST". ValueError: If the ``start_node`` and ``end_node`` disagree on the second curve when classified as "SECOND". ValueError: If the ``start_node`` and ``end_node`` disagree on the both curves when classified as "COINCIDENT". ValueError: If the ``start_node`` is not classified as :attr:`~.IntersectionClassification.FIRST`, :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.SECOND`, :attr:`~.IntersectionClassification.TANGENT_SECOND` or :attr:`~.IntersectionClassification.COINCIDENT`. """ if is_first(start_node.interior_curve): if end_node.index_first != start_node.index_first: raise ValueError(_WRONG_CURVE) return start_node.index_first, start_node.s, end_node.s elif is_second(start_node.interior_curve): if end_node.index_second != start_node.index_second: raise ValueError(_WRONG_CURVE) return start_node.index_second + 3, start_node.t, end_node.t elif start_node.interior_curve == CLASSIFICATION_T.COINCIDENT: if end_node.index_first == start_node.index_first: return start_node.index_first, start_node.s, end_node.s elif end_node.index_second == start_node.index_second: return start_node.index_second + 3, start_node.t, end_node.t else: raise ValueError(_WRONG_CURVE) else: raise ValueError( 'Segment start must be classified as "FIRST", "TANGENT_FIRST", ' '"SECOND", "TANGENT_SECOND" or "COINCIDENT".' )
[ "def", "ends_to_curve", "(", "start_node", ",", "end_node", ")", ":", "if", "is_first", "(", "start_node", ".", "interior_curve", ")", ":", "if", "end_node", ".", "index_first", "!=", "start_node", ".", "index_first", ":", "raise", "ValueError", "(", "_WRONG_CURVE", ")", "return", "start_node", ".", "index_first", ",", "start_node", ".", "s", ",", "end_node", ".", "s", "elif", "is_second", "(", "start_node", ".", "interior_curve", ")", ":", "if", "end_node", ".", "index_second", "!=", "start_node", ".", "index_second", ":", "raise", "ValueError", "(", "_WRONG_CURVE", ")", "return", "start_node", ".", "index_second", "+", "3", ",", "start_node", ".", "t", ",", "end_node", ".", "t", "elif", "start_node", ".", "interior_curve", "==", "CLASSIFICATION_T", ".", "COINCIDENT", ":", "if", "end_node", ".", "index_first", "==", "start_node", ".", "index_first", ":", "return", "start_node", ".", "index_first", ",", "start_node", ".", "s", ",", "end_node", ".", "s", "elif", "end_node", ".", "index_second", "==", "start_node", ".", "index_second", ":", "return", "start_node", ".", "index_second", "+", "3", ",", "start_node", ".", "t", ",", "end_node", ".", "t", "else", ":", "raise", "ValueError", "(", "_WRONG_CURVE", ")", "else", ":", "raise", "ValueError", "(", "'Segment start must be classified as \"FIRST\", \"TANGENT_FIRST\", '", "'\"SECOND\", \"TANGENT_SECOND\" or \"COINCIDENT\".'", ")" ]
Convert a "pair" of intersection nodes to a curve segment. .. note:: This is a helper used only by :func:`basic_interior_combine`, which in turn is only used by :func:`combine_intersections`. .. note:: This function could specialize to the first or second segment attached to ``start_node`` and ``end_node``. We determine first / second based on the classification of ``start_node``, but the callers of this function could provide that information / isolate the base curve and the two parameters for us. .. note:: This only checks the classification of the ``start_node``. Args: start_node (.Intersection): The beginning of a segment. end_node (.Intersection): The end of (the same) segment. Returns: Tuple[int, float, float]: The 3-tuple of: * The edge index along the first surface (if in ``{0, 1, 2}``) or the edge index along the second surface shifted to the right by 3 (if in ``{3, 4, 5}``) * The start parameter along the edge * The end parameter along the edge Raises: ValueError: If the ``start_node`` and ``end_node`` disagree on the first curve when classified as "FIRST". ValueError: If the ``start_node`` and ``end_node`` disagree on the second curve when classified as "SECOND". ValueError: If the ``start_node`` and ``end_node`` disagree on the both curves when classified as "COINCIDENT". ValueError: If the ``start_node`` is not classified as :attr:`~.IntersectionClassification.FIRST`, :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.SECOND`, :attr:`~.IntersectionClassification.TANGENT_SECOND` or :attr:`~.IntersectionClassification.COINCIDENT`.
[ "Convert", "a", "pair", "of", "intersection", "nodes", "to", "a", "curve", "segment", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2475-L2548
dhermes/bezier
src/bezier/_surface_helpers.py
no_intersections
def no_intersections(nodes1, degree1, nodes2, degree2): r"""Determine if one surface is in the other. Helper for :func:`combine_intersections` that handles the case of no points of intersection. In this case, either the surfaces are disjoint or one is fully contained in the other. To check containment, it's enough to check if one of the corners is contained in the other surface. Args: nodes1 (numpy.ndarray): The nodes defining the first surface in the intersection (assumed in :math:\mathbf{R}^2`). degree1 (int): The degree of the surface given by ``nodes1``. nodes2 (numpy.ndarray): The nodes defining the second surface in the intersection (assumed in :math:\mathbf{R}^2`). degree2 (int): The degree of the surface given by ``nodes2``. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * Edges info list; will be empty or :data:`None` * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. """ # NOTE: This is a circular import. from bezier import _surface_intersection located = _surface_intersection.locate_point( nodes2, degree2, nodes1[0, 0], nodes1[1, 0] ) if located is not None: return None, True located = _surface_intersection.locate_point( nodes1, degree1, nodes2[0, 0], nodes2[1, 0] ) if located is not None: return None, False return [], None
python
def no_intersections(nodes1, degree1, nodes2, degree2): r"""Determine if one surface is in the other. Helper for :func:`combine_intersections` that handles the case of no points of intersection. In this case, either the surfaces are disjoint or one is fully contained in the other. To check containment, it's enough to check if one of the corners is contained in the other surface. Args: nodes1 (numpy.ndarray): The nodes defining the first surface in the intersection (assumed in :math:\mathbf{R}^2`). degree1 (int): The degree of the surface given by ``nodes1``. nodes2 (numpy.ndarray): The nodes defining the second surface in the intersection (assumed in :math:\mathbf{R}^2`). degree2 (int): The degree of the surface given by ``nodes2``. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * Edges info list; will be empty or :data:`None` * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. """ # NOTE: This is a circular import. from bezier import _surface_intersection located = _surface_intersection.locate_point( nodes2, degree2, nodes1[0, 0], nodes1[1, 0] ) if located is not None: return None, True located = _surface_intersection.locate_point( nodes1, degree1, nodes2[0, 0], nodes2[1, 0] ) if located is not None: return None, False return [], None
[ "def", "no_intersections", "(", "nodes1", ",", "degree1", ",", "nodes2", ",", "degree2", ")", ":", "# NOTE: This is a circular import.", "from", "bezier", "import", "_surface_intersection", "located", "=", "_surface_intersection", ".", "locate_point", "(", "nodes2", ",", "degree2", ",", "nodes1", "[", "0", ",", "0", "]", ",", "nodes1", "[", "1", ",", "0", "]", ")", "if", "located", "is", "not", "None", ":", "return", "None", ",", "True", "located", "=", "_surface_intersection", ".", "locate_point", "(", "nodes1", ",", "degree1", ",", "nodes2", "[", "0", ",", "0", "]", ",", "nodes2", "[", "1", ",", "0", "]", ")", "if", "located", "is", "not", "None", ":", "return", "None", ",", "False", "return", "[", "]", ",", "None" ]
r"""Determine if one surface is in the other. Helper for :func:`combine_intersections` that handles the case of no points of intersection. In this case, either the surfaces are disjoint or one is fully contained in the other. To check containment, it's enough to check if one of the corners is contained in the other surface. Args: nodes1 (numpy.ndarray): The nodes defining the first surface in the intersection (assumed in :math:\mathbf{R}^2`). degree1 (int): The degree of the surface given by ``nodes1``. nodes2 (numpy.ndarray): The nodes defining the second surface in the intersection (assumed in :math:\mathbf{R}^2`). degree2 (int): The degree of the surface given by ``nodes2``. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * Edges info list; will be empty or :data:`None` * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other.
[ "r", "Determine", "if", "one", "surface", "is", "in", "the", "other", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2551-L2591
dhermes/bezier
src/bezier/_surface_helpers.py
tangent_only_intersections
def tangent_only_intersections(all_types): """Determine intersection in the case of only-tangent intersections. If the only intersections are tangencies, then either the surfaces are tangent but don't meet ("kissing" edges) or one surface is internally tangent to the other. Thus we expect every intersection to be classified as :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.TANGENT_SECOND`, :attr:`~.IntersectionClassification.OPPOSED`, :attr:`~.IntersectionClassification.IGNORED_CORNER` or :attr:`~.IntersectionClassification.COINCIDENT_UNUSED`. What's more, we expect all intersections to be classified the same for a given pairing. Args: all_types (Set[.IntersectionClassification]): The set of all intersection classifications encountered among the intersections for the given surface-surface pair. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * Edges info list; will be empty or :data:`None` * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. Raises: ValueError: If there are intersections of more than one type among :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.TANGENT_SECOND`, :attr:`~.IntersectionClassification.OPPOSED`, :attr:`~.IntersectionClassification.IGNORED_CORNER` or :attr:`~.IntersectionClassification.COINCIDENT_UNUSED`. ValueError: If there is a unique classification, but it isn't one of the tangent types. """ if len(all_types) != 1: raise ValueError("Unexpected value, types should all match", all_types) point_type = all_types.pop() if point_type == CLASSIFICATION_T.OPPOSED: return [], None elif point_type == CLASSIFICATION_T.IGNORED_CORNER: return [], None elif point_type == CLASSIFICATION_T.TANGENT_FIRST: return None, True elif point_type == CLASSIFICATION_T.TANGENT_SECOND: return None, False elif point_type == CLASSIFICATION_T.COINCIDENT_UNUSED: return [], None else: raise ValueError("Point type not for tangency", point_type)
python
def tangent_only_intersections(all_types): """Determine intersection in the case of only-tangent intersections. If the only intersections are tangencies, then either the surfaces are tangent but don't meet ("kissing" edges) or one surface is internally tangent to the other. Thus we expect every intersection to be classified as :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.TANGENT_SECOND`, :attr:`~.IntersectionClassification.OPPOSED`, :attr:`~.IntersectionClassification.IGNORED_CORNER` or :attr:`~.IntersectionClassification.COINCIDENT_UNUSED`. What's more, we expect all intersections to be classified the same for a given pairing. Args: all_types (Set[.IntersectionClassification]): The set of all intersection classifications encountered among the intersections for the given surface-surface pair. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * Edges info list; will be empty or :data:`None` * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. Raises: ValueError: If there are intersections of more than one type among :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.TANGENT_SECOND`, :attr:`~.IntersectionClassification.OPPOSED`, :attr:`~.IntersectionClassification.IGNORED_CORNER` or :attr:`~.IntersectionClassification.COINCIDENT_UNUSED`. ValueError: If there is a unique classification, but it isn't one of the tangent types. """ if len(all_types) != 1: raise ValueError("Unexpected value, types should all match", all_types) point_type = all_types.pop() if point_type == CLASSIFICATION_T.OPPOSED: return [], None elif point_type == CLASSIFICATION_T.IGNORED_CORNER: return [], None elif point_type == CLASSIFICATION_T.TANGENT_FIRST: return None, True elif point_type == CLASSIFICATION_T.TANGENT_SECOND: return None, False elif point_type == CLASSIFICATION_T.COINCIDENT_UNUSED: return [], None else: raise ValueError("Point type not for tangency", point_type)
[ "def", "tangent_only_intersections", "(", "all_types", ")", ":", "if", "len", "(", "all_types", ")", "!=", "1", ":", "raise", "ValueError", "(", "\"Unexpected value, types should all match\"", ",", "all_types", ")", "point_type", "=", "all_types", ".", "pop", "(", ")", "if", "point_type", "==", "CLASSIFICATION_T", ".", "OPPOSED", ":", "return", "[", "]", ",", "None", "elif", "point_type", "==", "CLASSIFICATION_T", ".", "IGNORED_CORNER", ":", "return", "[", "]", ",", "None", "elif", "point_type", "==", "CLASSIFICATION_T", ".", "TANGENT_FIRST", ":", "return", "None", ",", "True", "elif", "point_type", "==", "CLASSIFICATION_T", ".", "TANGENT_SECOND", ":", "return", "None", ",", "False", "elif", "point_type", "==", "CLASSIFICATION_T", ".", "COINCIDENT_UNUSED", ":", "return", "[", "]", ",", "None", "else", ":", "raise", "ValueError", "(", "\"Point type not for tangency\"", ",", "point_type", ")" ]
Determine intersection in the case of only-tangent intersections. If the only intersections are tangencies, then either the surfaces are tangent but don't meet ("kissing" edges) or one surface is internally tangent to the other. Thus we expect every intersection to be classified as :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.TANGENT_SECOND`, :attr:`~.IntersectionClassification.OPPOSED`, :attr:`~.IntersectionClassification.IGNORED_CORNER` or :attr:`~.IntersectionClassification.COINCIDENT_UNUSED`. What's more, we expect all intersections to be classified the same for a given pairing. Args: all_types (Set[.IntersectionClassification]): The set of all intersection classifications encountered among the intersections for the given surface-surface pair. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * Edges info list; will be empty or :data:`None` * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. Raises: ValueError: If there are intersections of more than one type among :attr:`~.IntersectionClassification.TANGENT_FIRST`, :attr:`~.IntersectionClassification.TANGENT_SECOND`, :attr:`~.IntersectionClassification.OPPOSED`, :attr:`~.IntersectionClassification.IGNORED_CORNER` or :attr:`~.IntersectionClassification.COINCIDENT_UNUSED`. ValueError: If there is a unique classification, but it isn't one of the tangent types.
[ "Determine", "intersection", "in", "the", "case", "of", "only", "-", "tangent", "intersections", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2594-L2653
dhermes/bezier
src/bezier/_surface_helpers.py
basic_interior_combine
def basic_interior_combine(intersections, max_edges=10): """Combine intersections that don't involve tangencies. .. note:: This is a helper used only by :func:`combine_intersections`. .. note:: This helper assumes ``intersections`` isn't empty, but doesn't enforce it. Args: intersections (List[.Intersection]): Intersections from each of the 9 edge-edge pairs from a surface-surface pairing. max_edges (Optional[int]): The maximum number of allowed / expected edges per intersection. This is to avoid infinite loops. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * List of "edge info" lists. Each list represents a curved polygon and contains 3-tuples of edge index, start and end (see the output of :func:`ends_to_curve`). * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. Raises: RuntimeError: If the number of edges in a curved polygon exceeds ``max_edges``. This is interpreted as a sign that the algorithm failed. """ unused = intersections[:] result = [] while unused: start = unused.pop() curr_node = start next_node = get_next(curr_node, intersections, unused) edge_ends = [(curr_node, next_node)] while next_node is not start: curr_node = to_front(next_node, intersections, unused) # NOTE: We also check to break when moving a corner node # to the front. This is because ``intersections`` # de-duplicates corners by selecting the one # (of 2 or 4 choices) at the front of segment(s). if curr_node is start: break next_node = get_next(curr_node, intersections, unused) edge_ends.append((curr_node, next_node)) if len(edge_ends) > max_edges: raise RuntimeError( "Unexpected number of edges", len(edge_ends) ) edge_info = tuple( ends_to_curve(start_node, end_node) for start_node, end_node in edge_ends ) result.append(edge_info) if len(result) == 1: if result[0] in FIRST_SURFACE_INFO: return None, True elif result[0] in SECOND_SURFACE_INFO: return None, False return result, None
python
def basic_interior_combine(intersections, max_edges=10): """Combine intersections that don't involve tangencies. .. note:: This is a helper used only by :func:`combine_intersections`. .. note:: This helper assumes ``intersections`` isn't empty, but doesn't enforce it. Args: intersections (List[.Intersection]): Intersections from each of the 9 edge-edge pairs from a surface-surface pairing. max_edges (Optional[int]): The maximum number of allowed / expected edges per intersection. This is to avoid infinite loops. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * List of "edge info" lists. Each list represents a curved polygon and contains 3-tuples of edge index, start and end (see the output of :func:`ends_to_curve`). * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. Raises: RuntimeError: If the number of edges in a curved polygon exceeds ``max_edges``. This is interpreted as a sign that the algorithm failed. """ unused = intersections[:] result = [] while unused: start = unused.pop() curr_node = start next_node = get_next(curr_node, intersections, unused) edge_ends = [(curr_node, next_node)] while next_node is not start: curr_node = to_front(next_node, intersections, unused) # NOTE: We also check to break when moving a corner node # to the front. This is because ``intersections`` # de-duplicates corners by selecting the one # (of 2 or 4 choices) at the front of segment(s). if curr_node is start: break next_node = get_next(curr_node, intersections, unused) edge_ends.append((curr_node, next_node)) if len(edge_ends) > max_edges: raise RuntimeError( "Unexpected number of edges", len(edge_ends) ) edge_info = tuple( ends_to_curve(start_node, end_node) for start_node, end_node in edge_ends ) result.append(edge_info) if len(result) == 1: if result[0] in FIRST_SURFACE_INFO: return None, True elif result[0] in SECOND_SURFACE_INFO: return None, False return result, None
[ "def", "basic_interior_combine", "(", "intersections", ",", "max_edges", "=", "10", ")", ":", "unused", "=", "intersections", "[", ":", "]", "result", "=", "[", "]", "while", "unused", ":", "start", "=", "unused", ".", "pop", "(", ")", "curr_node", "=", "start", "next_node", "=", "get_next", "(", "curr_node", ",", "intersections", ",", "unused", ")", "edge_ends", "=", "[", "(", "curr_node", ",", "next_node", ")", "]", "while", "next_node", "is", "not", "start", ":", "curr_node", "=", "to_front", "(", "next_node", ",", "intersections", ",", "unused", ")", "# NOTE: We also check to break when moving a corner node", "# to the front. This is because ``intersections``", "# de-duplicates corners by selecting the one", "# (of 2 or 4 choices) at the front of segment(s).", "if", "curr_node", "is", "start", ":", "break", "next_node", "=", "get_next", "(", "curr_node", ",", "intersections", ",", "unused", ")", "edge_ends", ".", "append", "(", "(", "curr_node", ",", "next_node", ")", ")", "if", "len", "(", "edge_ends", ")", ">", "max_edges", ":", "raise", "RuntimeError", "(", "\"Unexpected number of edges\"", ",", "len", "(", "edge_ends", ")", ")", "edge_info", "=", "tuple", "(", "ends_to_curve", "(", "start_node", ",", "end_node", ")", "for", "start_node", ",", "end_node", "in", "edge_ends", ")", "result", ".", "append", "(", "edge_info", ")", "if", "len", "(", "result", ")", "==", "1", ":", "if", "result", "[", "0", "]", "in", "FIRST_SURFACE_INFO", ":", "return", "None", ",", "True", "elif", "result", "[", "0", "]", "in", "SECOND_SURFACE_INFO", ":", "return", "None", ",", "False", "return", "result", ",", "None" ]
Combine intersections that don't involve tangencies. .. note:: This is a helper used only by :func:`combine_intersections`. .. note:: This helper assumes ``intersections`` isn't empty, but doesn't enforce it. Args: intersections (List[.Intersection]): Intersections from each of the 9 edge-edge pairs from a surface-surface pairing. max_edges (Optional[int]): The maximum number of allowed / expected edges per intersection. This is to avoid infinite loops. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * List of "edge info" lists. Each list represents a curved polygon and contains 3-tuples of edge index, start and end (see the output of :func:`ends_to_curve`). * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. Raises: RuntimeError: If the number of edges in a curved polygon exceeds ``max_edges``. This is interpreted as a sign that the algorithm failed.
[ "Combine", "intersections", "that", "don", "t", "involve", "tangencies", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2656-L2723
dhermes/bezier
src/bezier/_surface_helpers.py
combine_intersections
def combine_intersections( intersections, nodes1, degree1, nodes2, degree2, all_types ): r"""Combine curve-curve intersections into curved polygon(s). .. note:: This is a helper used only by :meth:`.Surface.intersect`. Does so assuming each intersection lies on an edge of one of two :class:`.Surface`-s. .. note :: This assumes that each ``intersection`` has been classified via :func:`classify_intersection` and only the intersections classified as ``FIRST`` and ``SECOND`` were kept. Args: intersections (List[.Intersection]): Intersections from each of the 9 edge-edge pairs from a surface-surface pairing. nodes1 (numpy.ndarray): The nodes defining the first surface in the intersection (assumed in :math:\mathbf{R}^2`). degree1 (int): The degree of the surface given by ``nodes1``. nodes2 (numpy.ndarray): The nodes defining the second surface in the intersection (assumed in :math:\mathbf{R}^2`). degree2 (int): The degree of the surface given by ``nodes2``. all_types (Set[.IntersectionClassification]): The set of all intersection classifications encountered among the intersections for the given surface-surface pair. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * List of "edge info" lists. Each list represents a curved polygon and contains 3-tuples of edge index, start and end (see the output of :func:`ends_to_curve`). * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. """ if intersections: return basic_interior_combine(intersections) elif all_types: return tangent_only_intersections(all_types) else: return no_intersections(nodes1, degree1, nodes2, degree2)
python
def combine_intersections( intersections, nodes1, degree1, nodes2, degree2, all_types ): r"""Combine curve-curve intersections into curved polygon(s). .. note:: This is a helper used only by :meth:`.Surface.intersect`. Does so assuming each intersection lies on an edge of one of two :class:`.Surface`-s. .. note :: This assumes that each ``intersection`` has been classified via :func:`classify_intersection` and only the intersections classified as ``FIRST`` and ``SECOND`` were kept. Args: intersections (List[.Intersection]): Intersections from each of the 9 edge-edge pairs from a surface-surface pairing. nodes1 (numpy.ndarray): The nodes defining the first surface in the intersection (assumed in :math:\mathbf{R}^2`). degree1 (int): The degree of the surface given by ``nodes1``. nodes2 (numpy.ndarray): The nodes defining the second surface in the intersection (assumed in :math:\mathbf{R}^2`). degree2 (int): The degree of the surface given by ``nodes2``. all_types (Set[.IntersectionClassification]): The set of all intersection classifications encountered among the intersections for the given surface-surface pair. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * List of "edge info" lists. Each list represents a curved polygon and contains 3-tuples of edge index, start and end (see the output of :func:`ends_to_curve`). * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other. """ if intersections: return basic_interior_combine(intersections) elif all_types: return tangent_only_intersections(all_types) else: return no_intersections(nodes1, degree1, nodes2, degree2)
[ "def", "combine_intersections", "(", "intersections", ",", "nodes1", ",", "degree1", ",", "nodes2", ",", "degree2", ",", "all_types", ")", ":", "if", "intersections", ":", "return", "basic_interior_combine", "(", "intersections", ")", "elif", "all_types", ":", "return", "tangent_only_intersections", "(", "all_types", ")", "else", ":", "return", "no_intersections", "(", "nodes1", ",", "degree1", ",", "nodes2", ",", "degree2", ")" ]
r"""Combine curve-curve intersections into curved polygon(s). .. note:: This is a helper used only by :meth:`.Surface.intersect`. Does so assuming each intersection lies on an edge of one of two :class:`.Surface`-s. .. note :: This assumes that each ``intersection`` has been classified via :func:`classify_intersection` and only the intersections classified as ``FIRST`` and ``SECOND`` were kept. Args: intersections (List[.Intersection]): Intersections from each of the 9 edge-edge pairs from a surface-surface pairing. nodes1 (numpy.ndarray): The nodes defining the first surface in the intersection (assumed in :math:\mathbf{R}^2`). degree1 (int): The degree of the surface given by ``nodes1``. nodes2 (numpy.ndarray): The nodes defining the second surface in the intersection (assumed in :math:\mathbf{R}^2`). degree2 (int): The degree of the surface given by ``nodes2``. all_types (Set[.IntersectionClassification]): The set of all intersection classifications encountered among the intersections for the given surface-surface pair. Returns: Tuple[Optional[list], Optional[bool]]: Pair (2-tuple) of * List of "edge info" lists. Each list represents a curved polygon and contains 3-tuples of edge index, start and end (see the output of :func:`ends_to_curve`). * "Contained" boolean. If not :data:`None`, indicates that one of the surfaces is contained in the other.
[ "r", "Combine", "curve", "-", "curve", "intersections", "into", "curved", "polygon", "(", "s", ")", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2726-L2773
dhermes/bezier
src/bezier/_surface_helpers.py
_evaluate_barycentric
def _evaluate_barycentric(nodes, degree, lambda1, lambda2, lambda3): r"""Compute a point on a surface. Evaluates :math:`B\left(\lambda_1, \lambda_2, \lambda_3\right)` for a B |eacute| zier surface / triangle defined by ``nodes``. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. lambda1 (float): Parameter along the reference triangle. lambda2 (float): Parameter along the reference triangle. lambda3 (float): Parameter along the reference triangle. Returns: numpy.ndarray: The evaluated point as a ``D x 1`` array (where ``D`` is the ambient dimension where ``nodes`` reside). """ dimension, num_nodes = nodes.shape binom_val = 1.0 result = np.zeros((dimension, 1), order="F") index = num_nodes - 1 result[:, 0] += nodes[:, index] # curve evaluate_multi_barycentric() takes arrays. lambda1 = np.asfortranarray([lambda1]) lambda2 = np.asfortranarray([lambda2]) for k in six.moves.xrange(degree - 1, -1, -1): # We want to go from (d C (k + 1)) to (d C k). binom_val = (binom_val * (k + 1)) / (degree - k) index -= 1 # Step to last element in column. # k = d - 1, d - 2, ... # d - k = 1, 2, ... # We know column k has (d - k + 1) elements. new_index = index - degree + k # First element in column. col_nodes = nodes[:, new_index : index + 1] # noqa: E203 col_nodes = np.asfortranarray(col_nodes) col_result = _curve_helpers.evaluate_multi_barycentric( col_nodes, lambda1, lambda2 ) result *= lambda3 result += binom_val * col_result # Update index for next iteration. index = new_index return result
python
def _evaluate_barycentric(nodes, degree, lambda1, lambda2, lambda3): r"""Compute a point on a surface. Evaluates :math:`B\left(\lambda_1, \lambda_2, \lambda_3\right)` for a B |eacute| zier surface / triangle defined by ``nodes``. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. lambda1 (float): Parameter along the reference triangle. lambda2 (float): Parameter along the reference triangle. lambda3 (float): Parameter along the reference triangle. Returns: numpy.ndarray: The evaluated point as a ``D x 1`` array (where ``D`` is the ambient dimension where ``nodes`` reside). """ dimension, num_nodes = nodes.shape binom_val = 1.0 result = np.zeros((dimension, 1), order="F") index = num_nodes - 1 result[:, 0] += nodes[:, index] # curve evaluate_multi_barycentric() takes arrays. lambda1 = np.asfortranarray([lambda1]) lambda2 = np.asfortranarray([lambda2]) for k in six.moves.xrange(degree - 1, -1, -1): # We want to go from (d C (k + 1)) to (d C k). binom_val = (binom_val * (k + 1)) / (degree - k) index -= 1 # Step to last element in column. # k = d - 1, d - 2, ... # d - k = 1, 2, ... # We know column k has (d - k + 1) elements. new_index = index - degree + k # First element in column. col_nodes = nodes[:, new_index : index + 1] # noqa: E203 col_nodes = np.asfortranarray(col_nodes) col_result = _curve_helpers.evaluate_multi_barycentric( col_nodes, lambda1, lambda2 ) result *= lambda3 result += binom_val * col_result # Update index for next iteration. index = new_index return result
[ "def", "_evaluate_barycentric", "(", "nodes", ",", "degree", ",", "lambda1", ",", "lambda2", ",", "lambda3", ")", ":", "dimension", ",", "num_nodes", "=", "nodes", ".", "shape", "binom_val", "=", "1.0", "result", "=", "np", ".", "zeros", "(", "(", "dimension", ",", "1", ")", ",", "order", "=", "\"F\"", ")", "index", "=", "num_nodes", "-", "1", "result", "[", ":", ",", "0", "]", "+=", "nodes", "[", ":", ",", "index", "]", "# curve evaluate_multi_barycentric() takes arrays.", "lambda1", "=", "np", ".", "asfortranarray", "(", "[", "lambda1", "]", ")", "lambda2", "=", "np", ".", "asfortranarray", "(", "[", "lambda2", "]", ")", "for", "k", "in", "six", ".", "moves", ".", "xrange", "(", "degree", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "# We want to go from (d C (k + 1)) to (d C k).", "binom_val", "=", "(", "binom_val", "*", "(", "k", "+", "1", ")", ")", "/", "(", "degree", "-", "k", ")", "index", "-=", "1", "# Step to last element in column.", "# k = d - 1, d - 2, ...", "# d - k = 1, 2, ...", "# We know column k has (d - k + 1) elements.", "new_index", "=", "index", "-", "degree", "+", "k", "# First element in column.", "col_nodes", "=", "nodes", "[", ":", ",", "new_index", ":", "index", "+", "1", "]", "# noqa: E203", "col_nodes", "=", "np", ".", "asfortranarray", "(", "col_nodes", ")", "col_result", "=", "_curve_helpers", ".", "evaluate_multi_barycentric", "(", "col_nodes", ",", "lambda1", ",", "lambda2", ")", "result", "*=", "lambda3", "result", "+=", "binom_val", "*", "col_result", "# Update index for next iteration.", "index", "=", "new_index", "return", "result" ]
r"""Compute a point on a surface. Evaluates :math:`B\left(\lambda_1, \lambda_2, \lambda_3\right)` for a B |eacute| zier surface / triangle defined by ``nodes``. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. lambda1 (float): Parameter along the reference triangle. lambda2 (float): Parameter along the reference triangle. lambda3 (float): Parameter along the reference triangle. Returns: numpy.ndarray: The evaluated point as a ``D x 1`` array (where ``D`` is the ambient dimension where ``nodes`` reside).
[ "r", "Compute", "a", "point", "on", "a", "surface", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2776-L2823
dhermes/bezier
src/bezier/_surface_helpers.py
_evaluate_barycentric_multi
def _evaluate_barycentric_multi(nodes, degree, param_vals, dimension): r"""Compute multiple points on the surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. param_vals (numpy.ndarray): Array of parameter values (as a ``N x 3`` array). dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: The evaluated points, where columns correspond to rows of ``param_vals`` and the rows to the dimension of the underlying surface. """ num_vals, _ = param_vals.shape result = np.empty((dimension, num_vals), order="F") for index, (lambda1, lambda2, lambda3) in enumerate(param_vals): result[:, index] = evaluate_barycentric( nodes, degree, lambda1, lambda2, lambda3 )[:, 0] return result
python
def _evaluate_barycentric_multi(nodes, degree, param_vals, dimension): r"""Compute multiple points on the surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. param_vals (numpy.ndarray): Array of parameter values (as a ``N x 3`` array). dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: The evaluated points, where columns correspond to rows of ``param_vals`` and the rows to the dimension of the underlying surface. """ num_vals, _ = param_vals.shape result = np.empty((dimension, num_vals), order="F") for index, (lambda1, lambda2, lambda3) in enumerate(param_vals): result[:, index] = evaluate_barycentric( nodes, degree, lambda1, lambda2, lambda3 )[:, 0] return result
[ "def", "_evaluate_barycentric_multi", "(", "nodes", ",", "degree", ",", "param_vals", ",", "dimension", ")", ":", "num_vals", ",", "_", "=", "param_vals", ".", "shape", "result", "=", "np", ".", "empty", "(", "(", "dimension", ",", "num_vals", ")", ",", "order", "=", "\"F\"", ")", "for", "index", ",", "(", "lambda1", ",", "lambda2", ",", "lambda3", ")", "in", "enumerate", "(", "param_vals", ")", ":", "result", "[", ":", ",", "index", "]", "=", "evaluate_barycentric", "(", "nodes", ",", "degree", ",", "lambda1", ",", "lambda2", ",", "lambda3", ")", "[", ":", ",", "0", "]", "return", "result" ]
r"""Compute multiple points on the surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. param_vals (numpy.ndarray): Array of parameter values (as a ``N x 3`` array). dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: The evaluated points, where columns correspond to rows of ``param_vals`` and the rows to the dimension of the underlying surface.
[ "r", "Compute", "multiple", "points", "on", "the", "surface", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2826-L2852
dhermes/bezier
src/bezier/_surface_helpers.py
_evaluate_cartesian_multi
def _evaluate_cartesian_multi(nodes, degree, param_vals, dimension): r"""Compute multiple points on the surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. param_vals (numpy.ndarray): Array of parameter values (as a ``N x 2`` array). dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: The evaluated points, where columns correspond to rows of ``param_vals`` and the rows to the dimension of the underlying surface. """ num_vals, _ = param_vals.shape result = np.empty((dimension, num_vals), order="F") for index, (s, t) in enumerate(param_vals): result[:, index] = evaluate_barycentric( nodes, degree, 1.0 - s - t, s, t )[:, 0] return result
python
def _evaluate_cartesian_multi(nodes, degree, param_vals, dimension): r"""Compute multiple points on the surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. param_vals (numpy.ndarray): Array of parameter values (as a ``N x 2`` array). dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: The evaluated points, where columns correspond to rows of ``param_vals`` and the rows to the dimension of the underlying surface. """ num_vals, _ = param_vals.shape result = np.empty((dimension, num_vals), order="F") for index, (s, t) in enumerate(param_vals): result[:, index] = evaluate_barycentric( nodes, degree, 1.0 - s - t, s, t )[:, 0] return result
[ "def", "_evaluate_cartesian_multi", "(", "nodes", ",", "degree", ",", "param_vals", ",", "dimension", ")", ":", "num_vals", ",", "_", "=", "param_vals", ".", "shape", "result", "=", "np", ".", "empty", "(", "(", "dimension", ",", "num_vals", ")", ",", "order", "=", "\"F\"", ")", "for", "index", ",", "(", "s", ",", "t", ")", "in", "enumerate", "(", "param_vals", ")", ":", "result", "[", ":", ",", "index", "]", "=", "evaluate_barycentric", "(", "nodes", ",", "degree", ",", "1.0", "-", "s", "-", "t", ",", "s", ",", "t", ")", "[", ":", ",", "0", "]", "return", "result" ]
r"""Compute multiple points on the surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. param_vals (numpy.ndarray): Array of parameter values (as a ``N x 2`` array). dimension (int): The dimension the surface lives in. Returns: numpy.ndarray: The evaluated points, where columns correspond to rows of ``param_vals`` and the rows to the dimension of the underlying surface.
[ "r", "Compute", "multiple", "points", "on", "the", "surface", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2855-L2881
dhermes/bezier
src/bezier/_surface_helpers.py
_compute_edge_nodes
def _compute_edge_nodes(nodes, degree): """Compute the nodes of each edges of a surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]: The nodes in the edges of the surface. """ dimension, _ = np.shape(nodes) nodes1 = np.empty((dimension, degree + 1), order="F") nodes2 = np.empty((dimension, degree + 1), order="F") nodes3 = np.empty((dimension, degree + 1), order="F") curr2 = degree curr3 = -1 for i in six.moves.xrange(degree + 1): nodes1[:, i] = nodes[:, i] nodes2[:, i] = nodes[:, curr2] nodes3[:, i] = nodes[:, curr3] # Update the indices. curr2 += degree - i curr3 -= i + 2 return nodes1, nodes2, nodes3
python
def _compute_edge_nodes(nodes, degree): """Compute the nodes of each edges of a surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]: The nodes in the edges of the surface. """ dimension, _ = np.shape(nodes) nodes1 = np.empty((dimension, degree + 1), order="F") nodes2 = np.empty((dimension, degree + 1), order="F") nodes3 = np.empty((dimension, degree + 1), order="F") curr2 = degree curr3 = -1 for i in six.moves.xrange(degree + 1): nodes1[:, i] = nodes[:, i] nodes2[:, i] = nodes[:, curr2] nodes3[:, i] = nodes[:, curr3] # Update the indices. curr2 += degree - i curr3 -= i + 2 return nodes1, nodes2, nodes3
[ "def", "_compute_edge_nodes", "(", "nodes", ",", "degree", ")", ":", "dimension", ",", "_", "=", "np", ".", "shape", "(", "nodes", ")", "nodes1", "=", "np", ".", "empty", "(", "(", "dimension", ",", "degree", "+", "1", ")", ",", "order", "=", "\"F\"", ")", "nodes2", "=", "np", ".", "empty", "(", "(", "dimension", ",", "degree", "+", "1", ")", ",", "order", "=", "\"F\"", ")", "nodes3", "=", "np", ".", "empty", "(", "(", "dimension", ",", "degree", "+", "1", ")", ",", "order", "=", "\"F\"", ")", "curr2", "=", "degree", "curr3", "=", "-", "1", "for", "i", "in", "six", ".", "moves", ".", "xrange", "(", "degree", "+", "1", ")", ":", "nodes1", "[", ":", ",", "i", "]", "=", "nodes", "[", ":", ",", "i", "]", "nodes2", "[", ":", ",", "i", "]", "=", "nodes", "[", ":", ",", "curr2", "]", "nodes3", "[", ":", ",", "i", "]", "=", "nodes", "[", ":", ",", "curr3", "]", "# Update the indices.", "curr2", "+=", "degree", "-", "i", "curr3", "-=", "i", "+", "2", "return", "nodes1", ",", "nodes2", ",", "nodes3" ]
Compute the nodes of each edges of a surface. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control point nodes that define the surface. degree (int): The degree of the surface define by ``nodes``. Returns: Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]: The nodes in the edges of the surface.
[ "Compute", "the", "nodes", "of", "each", "edges", "of", "a", "surface", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2884-L2913
dhermes/bezier
src/bezier/_surface_helpers.py
shoelace_for_area
def shoelace_for_area(nodes): r"""Compute an auxiliary "shoelace" sum used to compute area. .. note:: This is a helper for :func:`_compute_area`. Defining :math:`\left[i, j\right] = x_i y_j - y_i x_j` as a shoelace term illuminates the name of this helper. On a degree one curve, this function will return .. math:: \frac{1}{2}\left[0, 1\right]. on a degree two curve it will return .. math:: \frac{1}{6}\left(2 \left[0, 1\right] + 2 \left[1, 2\right] + \left[0, 2\right]\right) and so on. For a given :math:`\left[i, j\right]`, the coefficient comes from integrating :math:`b_{i, d}, b_{j, d}` on :math:`\left[0, 1\right]` (where :math:`b_{i, d}, b_{j, d}` are Bernstein basis polynomials). Returns: float: The computed sum of shoelace terms. Raises: .UnsupportedDegree: If the degree is not 1, 2, 3 or 4. """ _, num_nodes = nodes.shape if num_nodes == 2: shoelace = SHOELACE_LINEAR scale_factor = 2.0 elif num_nodes == 3: shoelace = SHOELACE_QUADRATIC scale_factor = 6.0 elif num_nodes == 4: shoelace = SHOELACE_CUBIC scale_factor = 20.0 elif num_nodes == 5: shoelace = SHOELACE_QUARTIC scale_factor = 70.0 else: raise _helpers.UnsupportedDegree(num_nodes - 1, supported=(1, 2, 3, 4)) result = 0.0 for multiplier, index1, index2 in shoelace: result += multiplier * ( nodes[0, index1] * nodes[1, index2] - nodes[1, index1] * nodes[0, index2] ) return result / scale_factor
python
def shoelace_for_area(nodes): r"""Compute an auxiliary "shoelace" sum used to compute area. .. note:: This is a helper for :func:`_compute_area`. Defining :math:`\left[i, j\right] = x_i y_j - y_i x_j` as a shoelace term illuminates the name of this helper. On a degree one curve, this function will return .. math:: \frac{1}{2}\left[0, 1\right]. on a degree two curve it will return .. math:: \frac{1}{6}\left(2 \left[0, 1\right] + 2 \left[1, 2\right] + \left[0, 2\right]\right) and so on. For a given :math:`\left[i, j\right]`, the coefficient comes from integrating :math:`b_{i, d}, b_{j, d}` on :math:`\left[0, 1\right]` (where :math:`b_{i, d}, b_{j, d}` are Bernstein basis polynomials). Returns: float: The computed sum of shoelace terms. Raises: .UnsupportedDegree: If the degree is not 1, 2, 3 or 4. """ _, num_nodes = nodes.shape if num_nodes == 2: shoelace = SHOELACE_LINEAR scale_factor = 2.0 elif num_nodes == 3: shoelace = SHOELACE_QUADRATIC scale_factor = 6.0 elif num_nodes == 4: shoelace = SHOELACE_CUBIC scale_factor = 20.0 elif num_nodes == 5: shoelace = SHOELACE_QUARTIC scale_factor = 70.0 else: raise _helpers.UnsupportedDegree(num_nodes - 1, supported=(1, 2, 3, 4)) result = 0.0 for multiplier, index1, index2 in shoelace: result += multiplier * ( nodes[0, index1] * nodes[1, index2] - nodes[1, index1] * nodes[0, index2] ) return result / scale_factor
[ "def", "shoelace_for_area", "(", "nodes", ")", ":", "_", ",", "num_nodes", "=", "nodes", ".", "shape", "if", "num_nodes", "==", "2", ":", "shoelace", "=", "SHOELACE_LINEAR", "scale_factor", "=", "2.0", "elif", "num_nodes", "==", "3", ":", "shoelace", "=", "SHOELACE_QUADRATIC", "scale_factor", "=", "6.0", "elif", "num_nodes", "==", "4", ":", "shoelace", "=", "SHOELACE_CUBIC", "scale_factor", "=", "20.0", "elif", "num_nodes", "==", "5", ":", "shoelace", "=", "SHOELACE_QUARTIC", "scale_factor", "=", "70.0", "else", ":", "raise", "_helpers", ".", "UnsupportedDegree", "(", "num_nodes", "-", "1", ",", "supported", "=", "(", "1", ",", "2", ",", "3", ",", "4", ")", ")", "result", "=", "0.0", "for", "multiplier", ",", "index1", ",", "index2", "in", "shoelace", ":", "result", "+=", "multiplier", "*", "(", "nodes", "[", "0", ",", "index1", "]", "*", "nodes", "[", "1", ",", "index2", "]", "-", "nodes", "[", "1", ",", "index1", "]", "*", "nodes", "[", "0", ",", "index2", "]", ")", "return", "result", "/", "scale_factor" ]
r"""Compute an auxiliary "shoelace" sum used to compute area. .. note:: This is a helper for :func:`_compute_area`. Defining :math:`\left[i, j\right] = x_i y_j - y_i x_j` as a shoelace term illuminates the name of this helper. On a degree one curve, this function will return .. math:: \frac{1}{2}\left[0, 1\right]. on a degree two curve it will return .. math:: \frac{1}{6}\left(2 \left[0, 1\right] + 2 \left[1, 2\right] + \left[0, 2\right]\right) and so on. For a given :math:`\left[i, j\right]`, the coefficient comes from integrating :math:`b_{i, d}, b_{j, d}` on :math:`\left[0, 1\right]` (where :math:`b_{i, d}, b_{j, d}` are Bernstein basis polynomials). Returns: float: The computed sum of shoelace terms. Raises: .UnsupportedDegree: If the degree is not 1, 2, 3 or 4.
[ "r", "Compute", "an", "auxiliary", "shoelace", "sum", "used", "to", "compute", "area", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_surface_helpers.py#L2916-L2973
dhermes/bezier
docs/make_images.py
save_image
def save_image(figure, filename): """Save an image to the docs images directory. Args: filename (str): The name of the file (not containing directory info). """ path = os.path.join(IMAGES_DIR, filename) figure.savefig(path, bbox_inches="tight") plt.close(figure)
python
def save_image(figure, filename): """Save an image to the docs images directory. Args: filename (str): The name of the file (not containing directory info). """ path = os.path.join(IMAGES_DIR, filename) figure.savefig(path, bbox_inches="tight") plt.close(figure)
[ "def", "save_image", "(", "figure", ",", "filename", ")", ":", "path", "=", "os", ".", "path", ".", "join", "(", "IMAGES_DIR", ",", "filename", ")", "figure", ".", "savefig", "(", "path", ",", "bbox_inches", "=", "\"tight\"", ")", "plt", ".", "close", "(", "figure", ")" ]
Save an image to the docs images directory. Args: filename (str): The name of the file (not containing directory info).
[ "Save", "an", "image", "to", "the", "docs", "images", "directory", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L48-L57
dhermes/bezier
docs/make_images.py
stack1d
def stack1d(*points): """Fill out the columns of matrix with a series of points. This is because ``np.hstack()`` will just make another 1D vector out of them and ``np.vstack()`` will put them in the rows. Args: points (Tuple[numpy.ndarray, ...]): Tuple of 1D points (i.e. arrays with shape ``(2,)``. Returns: numpy.ndarray: The array with each point in ``points`` as its columns. """ result = np.empty((2, len(points)), order="F") for index, point in enumerate(points): result[:, index] = point return result
python
def stack1d(*points): """Fill out the columns of matrix with a series of points. This is because ``np.hstack()`` will just make another 1D vector out of them and ``np.vstack()`` will put them in the rows. Args: points (Tuple[numpy.ndarray, ...]): Tuple of 1D points (i.e. arrays with shape ``(2,)``. Returns: numpy.ndarray: The array with each point in ``points`` as its columns. """ result = np.empty((2, len(points)), order="F") for index, point in enumerate(points): result[:, index] = point return result
[ "def", "stack1d", "(", "*", "points", ")", ":", "result", "=", "np", ".", "empty", "(", "(", "2", ",", "len", "(", "points", ")", ")", ",", "order", "=", "\"F\"", ")", "for", "index", ",", "point", "in", "enumerate", "(", "points", ")", ":", "result", "[", ":", ",", "index", "]", "=", "point", "return", "result" ]
Fill out the columns of matrix with a series of points. This is because ``np.hstack()`` will just make another 1D vector out of them and ``np.vstack()`` will put them in the rows. Args: points (Tuple[numpy.ndarray, ...]): Tuple of 1D points (i.e. arrays with shape ``(2,)``. Returns: numpy.ndarray: The array with each point in ``points`` as its columns.
[ "Fill", "out", "the", "columns", "of", "matrix", "with", "a", "series", "of", "points", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L60-L77
dhermes/bezier
docs/make_images.py
linearization_error
def linearization_error(nodes): """Image for :func:`.linearization_error` docstring.""" if NO_IMAGES: return curve = bezier.Curve.from_nodes(nodes) line = bezier.Curve.from_nodes(nodes[:, (0, -1)]) midpoints = np.hstack([curve.evaluate(0.5), line.evaluate(0.5)]) ax = curve.plot(256) line.plot(256, ax=ax) ax.plot( midpoints[0, :], midpoints[1, :], color="black", linestyle="dashed" ) ax.axis("scaled") save_image(ax.figure, "linearization_error.png")
python
def linearization_error(nodes): """Image for :func:`.linearization_error` docstring.""" if NO_IMAGES: return curve = bezier.Curve.from_nodes(nodes) line = bezier.Curve.from_nodes(nodes[:, (0, -1)]) midpoints = np.hstack([curve.evaluate(0.5), line.evaluate(0.5)]) ax = curve.plot(256) line.plot(256, ax=ax) ax.plot( midpoints[0, :], midpoints[1, :], color="black", linestyle="dashed" ) ax.axis("scaled") save_image(ax.figure, "linearization_error.png")
[ "def", "linearization_error", "(", "nodes", ")", ":", "if", "NO_IMAGES", ":", "return", "curve", "=", "bezier", ".", "Curve", ".", "from_nodes", "(", "nodes", ")", "line", "=", "bezier", ".", "Curve", ".", "from_nodes", "(", "nodes", "[", ":", ",", "(", "0", ",", "-", "1", ")", "]", ")", "midpoints", "=", "np", ".", "hstack", "(", "[", "curve", ".", "evaluate", "(", "0.5", ")", ",", "line", ".", "evaluate", "(", "0.5", ")", "]", ")", "ax", "=", "curve", ".", "plot", "(", "256", ")", "line", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "ax", ".", "plot", "(", "midpoints", "[", "0", ",", ":", "]", ",", "midpoints", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"dashed\"", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "save_image", "(", "ax", ".", "figure", ",", "\"linearization_error.png\"", ")" ]
Image for :func:`.linearization_error` docstring.
[ "Image", "for", ":", "func", ":", ".", "linearization_error", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L80-L94
dhermes/bezier
docs/make_images.py
newton_refine1
def newton_refine1(s, new_s, curve1, t, new_t, curve2): """Image for :func:`.newton_refine` docstring.""" if NO_IMAGES: return points = np.hstack([curve1.evaluate(s), curve2.evaluate(t)]) points_new = np.hstack([curve1.evaluate(new_s), curve2.evaluate(new_t)]) ax = curve1.plot(256) curve2.plot(256, ax=ax) ax.plot( points[0, :], points[1, :], color="black", linestyle="None", marker="o", markeredgewidth=1, markerfacecolor="None", ) ax.plot( points_new[0, :], points_new[1, :], color="black", linestyle="None", marker="o", ) ax.axis("scaled") save_image(ax.figure, "newton_refine1.png")
python
def newton_refine1(s, new_s, curve1, t, new_t, curve2): """Image for :func:`.newton_refine` docstring.""" if NO_IMAGES: return points = np.hstack([curve1.evaluate(s), curve2.evaluate(t)]) points_new = np.hstack([curve1.evaluate(new_s), curve2.evaluate(new_t)]) ax = curve1.plot(256) curve2.plot(256, ax=ax) ax.plot( points[0, :], points[1, :], color="black", linestyle="None", marker="o", markeredgewidth=1, markerfacecolor="None", ) ax.plot( points_new[0, :], points_new[1, :], color="black", linestyle="None", marker="o", ) ax.axis("scaled") save_image(ax.figure, "newton_refine1.png")
[ "def", "newton_refine1", "(", "s", ",", "new_s", ",", "curve1", ",", "t", ",", "new_t", ",", "curve2", ")", ":", "if", "NO_IMAGES", ":", "return", "points", "=", "np", ".", "hstack", "(", "[", "curve1", ".", "evaluate", "(", "s", ")", ",", "curve2", ".", "evaluate", "(", "t", ")", "]", ")", "points_new", "=", "np", ".", "hstack", "(", "[", "curve1", ".", "evaluate", "(", "new_s", ")", ",", "curve2", ".", "evaluate", "(", "new_t", ")", "]", ")", "ax", "=", "curve1", ".", "plot", "(", "256", ")", "curve2", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "ax", ".", "plot", "(", "points", "[", "0", ",", ":", "]", ",", "points", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ",", "markeredgewidth", "=", "1", ",", "markerfacecolor", "=", "\"None\"", ",", ")", "ax", ".", "plot", "(", "points_new", "[", "0", ",", ":", "]", ",", "points_new", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ",", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "save_image", "(", "ax", ".", "figure", ",", "\"newton_refine1.png\"", ")" ]
Image for :func:`.newton_refine` docstring.
[ "Image", "for", ":", "func", ":", ".", "newton_refine", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L97-L123
dhermes/bezier
docs/make_images.py
newton_refine2
def newton_refine2(s_vals, curve1, curve2): """Image for :func:`.newton_refine` docstring.""" if NO_IMAGES: return ax = curve1.plot(256) ax.lines[-1].zorder = 1 curve2.plot(256, ax=ax) ax.lines[-1].zorder = 1 points = curve1.evaluate_multi(np.asfortranarray(s_vals)) colors = seaborn.dark_palette("blue", 5) ax.scatter( points[0, :], points[1, :], c=colors, s=20, alpha=0.75, zorder=2 ) ax.axis("scaled") ax.set_xlim(0.0, 1.0) ax.set_ylim(0.0, 1.0) save_image(ax.figure, "newton_refine2.png")
python
def newton_refine2(s_vals, curve1, curve2): """Image for :func:`.newton_refine` docstring.""" if NO_IMAGES: return ax = curve1.plot(256) ax.lines[-1].zorder = 1 curve2.plot(256, ax=ax) ax.lines[-1].zorder = 1 points = curve1.evaluate_multi(np.asfortranarray(s_vals)) colors = seaborn.dark_palette("blue", 5) ax.scatter( points[0, :], points[1, :], c=colors, s=20, alpha=0.75, zorder=2 ) ax.axis("scaled") ax.set_xlim(0.0, 1.0) ax.set_ylim(0.0, 1.0) save_image(ax.figure, "newton_refine2.png")
[ "def", "newton_refine2", "(", "s_vals", ",", "curve1", ",", "curve2", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "curve1", ".", "plot", "(", "256", ")", "ax", ".", "lines", "[", "-", "1", "]", ".", "zorder", "=", "1", "curve2", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "ax", ".", "lines", "[", "-", "1", "]", ".", "zorder", "=", "1", "points", "=", "curve1", ".", "evaluate_multi", "(", "np", ".", "asfortranarray", "(", "s_vals", ")", ")", "colors", "=", "seaborn", ".", "dark_palette", "(", "\"blue\"", ",", "5", ")", "ax", ".", "scatter", "(", "points", "[", "0", ",", ":", "]", ",", "points", "[", "1", ",", ":", "]", ",", "c", "=", "colors", ",", "s", "=", "20", ",", "alpha", "=", "0.75", ",", "zorder", "=", "2", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "0.0", ",", "1.0", ")", "ax", ".", "set_ylim", "(", "0.0", ",", "1.0", ")", "save_image", "(", "ax", ".", "figure", ",", "\"newton_refine2.png\"", ")" ]
Image for :func:`.newton_refine` docstring.
[ "Image", "for", ":", "func", ":", ".", "newton_refine", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L126-L143
dhermes/bezier
docs/make_images.py
segment_intersection1
def segment_intersection1(start0, end0, start1, end1, s): """Image for :func:`.segment_intersection` docstring.""" if NO_IMAGES: return line0 = bezier.Curve.from_nodes(stack1d(start0, end0)) line1 = bezier.Curve.from_nodes(stack1d(start1, end1)) ax = line0.plot(2) line1.plot(256, ax=ax) (x_val,), (y_val,) = line0.evaluate(s) ax.plot([x_val], [y_val], color="black", marker="o") ax.axis("scaled") save_image(ax.figure, "segment_intersection1.png")
python
def segment_intersection1(start0, end0, start1, end1, s): """Image for :func:`.segment_intersection` docstring.""" if NO_IMAGES: return line0 = bezier.Curve.from_nodes(stack1d(start0, end0)) line1 = bezier.Curve.from_nodes(stack1d(start1, end1)) ax = line0.plot(2) line1.plot(256, ax=ax) (x_val,), (y_val,) = line0.evaluate(s) ax.plot([x_val], [y_val], color="black", marker="o") ax.axis("scaled") save_image(ax.figure, "segment_intersection1.png")
[ "def", "segment_intersection1", "(", "start0", ",", "end0", ",", "start1", ",", "end1", ",", "s", ")", ":", "if", "NO_IMAGES", ":", "return", "line0", "=", "bezier", ".", "Curve", ".", "from_nodes", "(", "stack1d", "(", "start0", ",", "end0", ")", ")", "line1", "=", "bezier", ".", "Curve", ".", "from_nodes", "(", "stack1d", "(", "start1", ",", "end1", ")", ")", "ax", "=", "line0", ".", "plot", "(", "2", ")", "line1", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "(", "x_val", ",", ")", ",", "(", "y_val", ",", ")", "=", "line0", ".", "evaluate", "(", "s", ")", "ax", ".", "plot", "(", "[", "x_val", "]", ",", "[", "y_val", "]", ",", "color", "=", "\"black\"", ",", "marker", "=", "\"o\"", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "save_image", "(", "ax", ".", "figure", ",", "\"segment_intersection1.png\"", ")" ]
Image for :func:`.segment_intersection` docstring.
[ "Image", "for", ":", "func", ":", ".", "segment_intersection", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L166-L178
dhermes/bezier
docs/make_images.py
segment_intersection2
def segment_intersection2(start0, end0, start1, end1): """Image for :func:`.segment_intersection` docstring.""" if NO_IMAGES: return line0 = bezier.Curve.from_nodes(stack1d(start0, end0)) line1 = bezier.Curve.from_nodes(stack1d(start1, end1)) ax = line0.plot(2) line1.plot(2, ax=ax) ax.axis("scaled") save_image(ax.figure, "segment_intersection2.png")
python
def segment_intersection2(start0, end0, start1, end1): """Image for :func:`.segment_intersection` docstring.""" if NO_IMAGES: return line0 = bezier.Curve.from_nodes(stack1d(start0, end0)) line1 = bezier.Curve.from_nodes(stack1d(start1, end1)) ax = line0.plot(2) line1.plot(2, ax=ax) ax.axis("scaled") save_image(ax.figure, "segment_intersection2.png")
[ "def", "segment_intersection2", "(", "start0", ",", "end0", ",", "start1", ",", "end1", ")", ":", "if", "NO_IMAGES", ":", "return", "line0", "=", "bezier", ".", "Curve", ".", "from_nodes", "(", "stack1d", "(", "start0", ",", "end0", ")", ")", "line1", "=", "bezier", ".", "Curve", ".", "from_nodes", "(", "stack1d", "(", "start1", ",", "end1", ")", ")", "ax", "=", "line0", ".", "plot", "(", "2", ")", "line1", ".", "plot", "(", "2", ",", "ax", "=", "ax", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "save_image", "(", "ax", ".", "figure", ",", "\"segment_intersection2.png\"", ")" ]
Image for :func:`.segment_intersection` docstring.
[ "Image", "for", ":", "func", ":", ".", "segment_intersection", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L181-L191
dhermes/bezier
docs/make_images.py
helper_parallel_lines
def helper_parallel_lines(start0, end0, start1, end1, filename): """Image for :func:`.parallel_lines_parameters` docstring.""" if NO_IMAGES: return figure = plt.figure() ax = figure.gca() points = stack1d(start0, end0, start1, end1) ax.plot(points[0, :2], points[1, :2], marker="o") ax.plot(points[0, 2:], points[1, 2:], marker="o") ax.axis("scaled") _plot_helpers.add_plot_boundary(ax) save_image(figure, filename)
python
def helper_parallel_lines(start0, end0, start1, end1, filename): """Image for :func:`.parallel_lines_parameters` docstring.""" if NO_IMAGES: return figure = plt.figure() ax = figure.gca() points = stack1d(start0, end0, start1, end1) ax.plot(points[0, :2], points[1, :2], marker="o") ax.plot(points[0, 2:], points[1, 2:], marker="o") ax.axis("scaled") _plot_helpers.add_plot_boundary(ax) save_image(figure, filename)
[ "def", "helper_parallel_lines", "(", "start0", ",", "end0", ",", "start1", ",", "end1", ",", "filename", ")", ":", "if", "NO_IMAGES", ":", "return", "figure", "=", "plt", ".", "figure", "(", ")", "ax", "=", "figure", ".", "gca", "(", ")", "points", "=", "stack1d", "(", "start0", ",", "end0", ",", "start1", ",", "end1", ")", "ax", ".", "plot", "(", "points", "[", "0", ",", ":", "2", "]", ",", "points", "[", "1", ",", ":", "2", "]", ",", "marker", "=", "\"o\"", ")", "ax", ".", "plot", "(", "points", "[", "0", ",", "2", ":", "]", ",", "points", "[", "1", ",", "2", ":", "]", ",", "marker", "=", "\"o\"", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "_plot_helpers", ".", "add_plot_boundary", "(", "ax", ")", "save_image", "(", "figure", ",", "filename", ")" ]
Image for :func:`.parallel_lines_parameters` docstring.
[ "Image", "for", ":", "func", ":", ".", "parallel_lines_parameters", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L194-L206
dhermes/bezier
docs/make_images.py
curve_constructor
def curve_constructor(curve): """Image for :class`.Curve` docstring.""" if NO_IMAGES: return ax = curve.plot(256) line = ax.lines[0] nodes = curve._nodes ax.plot( nodes[0, :], nodes[1, :], color="black", linestyle="None", marker="o" ) add_patch(ax, nodes, line.get_color()) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.0625, 0.5625) save_image(ax.figure, "curve_constructor.png")
python
def curve_constructor(curve): """Image for :class`.Curve` docstring.""" if NO_IMAGES: return ax = curve.plot(256) line = ax.lines[0] nodes = curve._nodes ax.plot( nodes[0, :], nodes[1, :], color="black", linestyle="None", marker="o" ) add_patch(ax, nodes, line.get_color()) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.0625, 0.5625) save_image(ax.figure, "curve_constructor.png")
[ "def", "curve_constructor", "(", "curve", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "curve", ".", "plot", "(", "256", ")", "line", "=", "ax", ".", "lines", "[", "0", "]", "nodes", "=", "curve", ".", "_nodes", "ax", ".", "plot", "(", "nodes", "[", "0", ",", ":", "]", ",", "nodes", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ")", "add_patch", "(", "ax", ",", "nodes", ",", "line", ".", "get_color", "(", ")", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.125", ",", "1.125", ")", "ax", ".", "set_ylim", "(", "-", "0.0625", ",", "0.5625", ")", "save_image", "(", "ax", ".", "figure", ",", "\"curve_constructor.png\"", ")" ]
Image for :class`.Curve` docstring.
[ "Image", "for", ":", "class", ".", "Curve", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L227-L242
dhermes/bezier
docs/make_images.py
curve_evaluate
def curve_evaluate(curve): """Image for :meth`.Curve.evaluate` docstring.""" if NO_IMAGES: return ax = curve.plot(256) points = curve.evaluate_multi(np.asfortranarray([0.75])) ax.plot( points[0, :], points[1, :], color="black", linestyle="None", marker="o" ) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.0625, 0.5625) save_image(ax.figure, "curve_evaluate.png")
python
def curve_evaluate(curve): """Image for :meth`.Curve.evaluate` docstring.""" if NO_IMAGES: return ax = curve.plot(256) points = curve.evaluate_multi(np.asfortranarray([0.75])) ax.plot( points[0, :], points[1, :], color="black", linestyle="None", marker="o" ) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.0625, 0.5625) save_image(ax.figure, "curve_evaluate.png")
[ "def", "curve_evaluate", "(", "curve", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "curve", ".", "plot", "(", "256", ")", "points", "=", "curve", ".", "evaluate_multi", "(", "np", ".", "asfortranarray", "(", "[", "0.75", "]", ")", ")", "ax", ".", "plot", "(", "points", "[", "0", ",", ":", "]", ",", "points", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.125", ",", "1.125", ")", "ax", ".", "set_ylim", "(", "-", "0.0625", ",", "0.5625", ")", "save_image", "(", "ax", ".", "figure", ",", "\"curve_evaluate.png\"", ")" ]
Image for :meth`.Curve.evaluate` docstring.
[ "Image", "for", ":", "meth", ".", "Curve", ".", "evaluate", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L245-L258
dhermes/bezier
docs/make_images.py
curve_subdivide
def curve_subdivide(curve, left, right): """Image for :meth`.Curve.subdivide` docstring.""" if NO_IMAGES: return figure = plt.figure() ax = figure.gca() add_patch(ax, curve._nodes, "gray") ax = left.plot(256, ax=ax) line = ax.lines[-1] add_patch(ax, left._nodes, line.get_color()) right.plot(256, ax=ax) line = ax.lines[-1] add_patch(ax, right._nodes, line.get_color()) ax.axis("scaled") ax.set_xlim(-0.125, 2.125) ax.set_ylim(-0.125, 3.125) save_image(ax.figure, "curve_subdivide.png")
python
def curve_subdivide(curve, left, right): """Image for :meth`.Curve.subdivide` docstring.""" if NO_IMAGES: return figure = plt.figure() ax = figure.gca() add_patch(ax, curve._nodes, "gray") ax = left.plot(256, ax=ax) line = ax.lines[-1] add_patch(ax, left._nodes, line.get_color()) right.plot(256, ax=ax) line = ax.lines[-1] add_patch(ax, right._nodes, line.get_color()) ax.axis("scaled") ax.set_xlim(-0.125, 2.125) ax.set_ylim(-0.125, 3.125) save_image(ax.figure, "curve_subdivide.png")
[ "def", "curve_subdivide", "(", "curve", ",", "left", ",", "right", ")", ":", "if", "NO_IMAGES", ":", "return", "figure", "=", "plt", ".", "figure", "(", ")", "ax", "=", "figure", ".", "gca", "(", ")", "add_patch", "(", "ax", ",", "curve", ".", "_nodes", ",", "\"gray\"", ")", "ax", "=", "left", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "line", "=", "ax", ".", "lines", "[", "-", "1", "]", "add_patch", "(", "ax", ",", "left", ".", "_nodes", ",", "line", ".", "get_color", "(", ")", ")", "right", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "line", "=", "ax", ".", "lines", "[", "-", "1", "]", "add_patch", "(", "ax", ",", "right", ".", "_nodes", ",", "line", ".", "get_color", "(", ")", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.125", ",", "2.125", ")", "ax", ".", "set_ylim", "(", "-", "0.125", ",", "3.125", ")", "save_image", "(", "ax", ".", "figure", ",", "\"curve_subdivide.png\"", ")" ]
Image for :meth`.Curve.subdivide` docstring.
[ "Image", "for", ":", "meth", ".", "Curve", ".", "subdivide", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L261-L278
dhermes/bezier
docs/make_images.py
curve_intersect
def curve_intersect(curve1, curve2, s_vals): """Image for :meth`.Curve.intersect` docstring.""" if NO_IMAGES: return ax = curve1.plot(256) curve2.plot(256, ax=ax) intersections = curve1.evaluate_multi(s_vals) ax.plot( intersections[0, :], intersections[1, :], color="black", linestyle="None", marker="o", ) ax.axis("scaled") ax.set_xlim(0.0, 0.75) ax.set_ylim(0.0, 0.75) save_image(ax.figure, "curve_intersect.png")
python
def curve_intersect(curve1, curve2, s_vals): """Image for :meth`.Curve.intersect` docstring.""" if NO_IMAGES: return ax = curve1.plot(256) curve2.plot(256, ax=ax) intersections = curve1.evaluate_multi(s_vals) ax.plot( intersections[0, :], intersections[1, :], color="black", linestyle="None", marker="o", ) ax.axis("scaled") ax.set_xlim(0.0, 0.75) ax.set_ylim(0.0, 0.75) save_image(ax.figure, "curve_intersect.png")
[ "def", "curve_intersect", "(", "curve1", ",", "curve2", ",", "s_vals", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "curve1", ".", "plot", "(", "256", ")", "curve2", ".", "plot", "(", "256", ",", "ax", "=", "ax", ")", "intersections", "=", "curve1", ".", "evaluate_multi", "(", "s_vals", ")", "ax", ".", "plot", "(", "intersections", "[", "0", ",", ":", "]", ",", "intersections", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ",", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "0.0", ",", "0.75", ")", "ax", ".", "set_ylim", "(", "0.0", ",", "0.75", ")", "save_image", "(", "ax", ".", "figure", ",", "\"curve_intersect.png\"", ")" ]
Image for :meth`.Curve.intersect` docstring.
[ "Image", "for", ":", "meth", ".", "Curve", ".", "intersect", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L281-L299
dhermes/bezier
docs/make_images.py
surface_constructor
def surface_constructor(surface): """Image for :class`.Surface` docstring.""" if NO_IMAGES: return ax = surface.plot(256, with_nodes=True) line = ax.lines[0] nodes = surface._nodes add_patch(ax, nodes[:, (0, 1, 2, 5)], line.get_color()) delta = 1.0 / 32.0 ax.text( nodes[0, 0], nodes[1, 0], r"$v_0$", fontsize=20, verticalalignment="top", horizontalalignment="right", ) ax.text( nodes[0, 1], nodes[1, 1], r"$v_1$", fontsize=20, verticalalignment="top", horizontalalignment="center", ) ax.text( nodes[0, 2], nodes[1, 2], r"$v_2$", fontsize=20, verticalalignment="top", horizontalalignment="left", ) ax.text( nodes[0, 3] - delta, nodes[1, 3], r"$v_3$", fontsize=20, verticalalignment="center", horizontalalignment="right", ) ax.text( nodes[0, 4] + delta, nodes[1, 4], r"$v_4$", fontsize=20, verticalalignment="center", horizontalalignment="left", ) ax.text( nodes[0, 5], nodes[1, 5] + delta, r"$v_5$", fontsize=20, verticalalignment="bottom", horizontalalignment="center", ) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.125, 1.125) save_image(ax.figure, "surface_constructor.png")
python
def surface_constructor(surface): """Image for :class`.Surface` docstring.""" if NO_IMAGES: return ax = surface.plot(256, with_nodes=True) line = ax.lines[0] nodes = surface._nodes add_patch(ax, nodes[:, (0, 1, 2, 5)], line.get_color()) delta = 1.0 / 32.0 ax.text( nodes[0, 0], nodes[1, 0], r"$v_0$", fontsize=20, verticalalignment="top", horizontalalignment="right", ) ax.text( nodes[0, 1], nodes[1, 1], r"$v_1$", fontsize=20, verticalalignment="top", horizontalalignment="center", ) ax.text( nodes[0, 2], nodes[1, 2], r"$v_2$", fontsize=20, verticalalignment="top", horizontalalignment="left", ) ax.text( nodes[0, 3] - delta, nodes[1, 3], r"$v_3$", fontsize=20, verticalalignment="center", horizontalalignment="right", ) ax.text( nodes[0, 4] + delta, nodes[1, 4], r"$v_4$", fontsize=20, verticalalignment="center", horizontalalignment="left", ) ax.text( nodes[0, 5], nodes[1, 5] + delta, r"$v_5$", fontsize=20, verticalalignment="bottom", horizontalalignment="center", ) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.125, 1.125) save_image(ax.figure, "surface_constructor.png")
[ "def", "surface_constructor", "(", "surface", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "surface", ".", "plot", "(", "256", ",", "with_nodes", "=", "True", ")", "line", "=", "ax", ".", "lines", "[", "0", "]", "nodes", "=", "surface", ".", "_nodes", "add_patch", "(", "ax", ",", "nodes", "[", ":", ",", "(", "0", ",", "1", ",", "2", ",", "5", ")", "]", ",", "line", ".", "get_color", "(", ")", ")", "delta", "=", "1.0", "/", "32.0", "ax", ".", "text", "(", "nodes", "[", "0", ",", "0", "]", ",", "nodes", "[", "1", ",", "0", "]", ",", "r\"$v_0$\"", ",", "fontsize", "=", "20", ",", "verticalalignment", "=", "\"top\"", ",", "horizontalalignment", "=", "\"right\"", ",", ")", "ax", ".", "text", "(", "nodes", "[", "0", ",", "1", "]", ",", "nodes", "[", "1", ",", "1", "]", ",", "r\"$v_1$\"", ",", "fontsize", "=", "20", ",", "verticalalignment", "=", "\"top\"", ",", "horizontalalignment", "=", "\"center\"", ",", ")", "ax", ".", "text", "(", "nodes", "[", "0", ",", "2", "]", ",", "nodes", "[", "1", ",", "2", "]", ",", "r\"$v_2$\"", ",", "fontsize", "=", "20", ",", "verticalalignment", "=", "\"top\"", ",", "horizontalalignment", "=", "\"left\"", ",", ")", "ax", ".", "text", "(", "nodes", "[", "0", ",", "3", "]", "-", "delta", ",", "nodes", "[", "1", ",", "3", "]", ",", "r\"$v_3$\"", ",", "fontsize", "=", "20", ",", "verticalalignment", "=", "\"center\"", ",", "horizontalalignment", "=", "\"right\"", ",", ")", "ax", ".", "text", "(", "nodes", "[", "0", ",", "4", "]", "+", "delta", ",", "nodes", "[", "1", ",", "4", "]", ",", "r\"$v_4$\"", ",", "fontsize", "=", "20", ",", "verticalalignment", "=", "\"center\"", ",", "horizontalalignment", "=", "\"left\"", ",", ")", "ax", ".", "text", "(", "nodes", "[", "0", ",", "5", "]", ",", "nodes", "[", "1", ",", "5", "]", "+", "delta", ",", "r\"$v_5$\"", ",", "fontsize", "=", "20", ",", "verticalalignment", "=", "\"bottom\"", ",", "horizontalalignment", "=", "\"center\"", ",", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.125", ",", "1.125", ")", "ax", ".", "set_ylim", "(", "-", "0.125", ",", "1.125", ")", "save_image", "(", "ax", ".", "figure", ",", "\"surface_constructor.png\"", ")" ]
Image for :class`.Surface` docstring.
[ "Image", "for", ":", "class", ".", "Surface", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L302-L363
dhermes/bezier
docs/make_images.py
surface_evaluate_barycentric
def surface_evaluate_barycentric(surface, point): """Image for :meth`.Surface.evaluate_barycentric` docstring.""" if NO_IMAGES: return ax = surface.plot(256) ax.plot( point[0, :], point[1, :], color="black", linestyle="None", marker="o" ) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.125, 1.125) save_image(ax.figure, "surface_evaluate_barycentric.png")
python
def surface_evaluate_barycentric(surface, point): """Image for :meth`.Surface.evaluate_barycentric` docstring.""" if NO_IMAGES: return ax = surface.plot(256) ax.plot( point[0, :], point[1, :], color="black", linestyle="None", marker="o" ) ax.axis("scaled") ax.set_xlim(-0.125, 1.125) ax.set_ylim(-0.125, 1.125) save_image(ax.figure, "surface_evaluate_barycentric.png")
[ "def", "surface_evaluate_barycentric", "(", "surface", ",", "point", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "surface", ".", "plot", "(", "256", ")", "ax", ".", "plot", "(", "point", "[", "0", ",", ":", "]", ",", "point", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.125", ",", "1.125", ")", "ax", ".", "set_ylim", "(", "-", "0.125", ",", "1.125", ")", "save_image", "(", "ax", ".", "figure", ",", "\"surface_evaluate_barycentric.png\"", ")" ]
Image for :meth`.Surface.evaluate_barycentric` docstring.
[ "Image", "for", ":", "meth", ".", "Surface", ".", "evaluate_barycentric", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L366-L378
dhermes/bezier
docs/make_images.py
surface_evaluate_cartesian_multi
def surface_evaluate_cartesian_multi(surface, points): """Image for :meth`.Surface.evaluate_cartesian_multi` docstring.""" if NO_IMAGES: return ax = surface.plot(256) ax.plot( points[0, :], points[1, :], color="black", linestyle="None", marker="o" ) delta = 1.0 / 32.0 font_size = 18 ax.text( points[0, 0], points[1, 0], r"$w_0$", fontsize=font_size, verticalalignment="top", horizontalalignment="right", ) ax.text( points[0, 1] + 2 * delta, points[1, 1], r"$w_1$", fontsize=font_size, verticalalignment="center", horizontalalignment="left", ) ax.text( points[0, 2], points[1, 2] + delta, r"$w_2$", fontsize=font_size, verticalalignment="bottom", horizontalalignment="left", ) ax.axis("scaled") ax.set_xlim(-3.125, 2.375) ax.set_ylim(-0.25, 2.125) save_image(ax.figure, "surface_evaluate_cartesian_multi.png")
python
def surface_evaluate_cartesian_multi(surface, points): """Image for :meth`.Surface.evaluate_cartesian_multi` docstring.""" if NO_IMAGES: return ax = surface.plot(256) ax.plot( points[0, :], points[1, :], color="black", linestyle="None", marker="o" ) delta = 1.0 / 32.0 font_size = 18 ax.text( points[0, 0], points[1, 0], r"$w_0$", fontsize=font_size, verticalalignment="top", horizontalalignment="right", ) ax.text( points[0, 1] + 2 * delta, points[1, 1], r"$w_1$", fontsize=font_size, verticalalignment="center", horizontalalignment="left", ) ax.text( points[0, 2], points[1, 2] + delta, r"$w_2$", fontsize=font_size, verticalalignment="bottom", horizontalalignment="left", ) ax.axis("scaled") ax.set_xlim(-3.125, 2.375) ax.set_ylim(-0.25, 2.125) save_image(ax.figure, "surface_evaluate_cartesian_multi.png")
[ "def", "surface_evaluate_cartesian_multi", "(", "surface", ",", "points", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "surface", ".", "plot", "(", "256", ")", "ax", ".", "plot", "(", "points", "[", "0", ",", ":", "]", ",", "points", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"None\"", ",", "marker", "=", "\"o\"", ")", "delta", "=", "1.0", "/", "32.0", "font_size", "=", "18", "ax", ".", "text", "(", "points", "[", "0", ",", "0", "]", ",", "points", "[", "1", ",", "0", "]", ",", "r\"$w_0$\"", ",", "fontsize", "=", "font_size", ",", "verticalalignment", "=", "\"top\"", ",", "horizontalalignment", "=", "\"right\"", ",", ")", "ax", ".", "text", "(", "points", "[", "0", ",", "1", "]", "+", "2", "*", "delta", ",", "points", "[", "1", ",", "1", "]", ",", "r\"$w_1$\"", ",", "fontsize", "=", "font_size", ",", "verticalalignment", "=", "\"center\"", ",", "horizontalalignment", "=", "\"left\"", ",", ")", "ax", ".", "text", "(", "points", "[", "0", ",", "2", "]", ",", "points", "[", "1", ",", "2", "]", "+", "delta", ",", "r\"$w_2$\"", ",", "fontsize", "=", "font_size", ",", "verticalalignment", "=", "\"bottom\"", ",", "horizontalalignment", "=", "\"left\"", ",", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "3.125", ",", "2.375", ")", "ax", ".", "set_ylim", "(", "-", "0.25", ",", "2.125", ")", "save_image", "(", "ax", ".", "figure", ",", "\"surface_evaluate_cartesian_multi.png\"", ")" ]
Image for :meth`.Surface.evaluate_cartesian_multi` docstring.
[ "Image", "for", ":", "meth", ".", "Surface", ".", "evaluate_cartesian_multi", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L381-L419
dhermes/bezier
docs/make_images.py
surface_is_valid1
def surface_is_valid1(surface): """Image for :meth`.Surface.is_valid` docstring.""" if NO_IMAGES: return ax = surface.plot(256) ax.axis("scaled") ax.set_xlim(-0.125, 2.125) ax.set_ylim(-0.125, 2.125) save_image(ax.figure, "surface_is_valid1.png")
python
def surface_is_valid1(surface): """Image for :meth`.Surface.is_valid` docstring.""" if NO_IMAGES: return ax = surface.plot(256) ax.axis("scaled") ax.set_xlim(-0.125, 2.125) ax.set_ylim(-0.125, 2.125) save_image(ax.figure, "surface_is_valid1.png")
[ "def", "surface_is_valid1", "(", "surface", ")", ":", "if", "NO_IMAGES", ":", "return", "ax", "=", "surface", ".", "plot", "(", "256", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.125", ",", "2.125", ")", "ax", ".", "set_ylim", "(", "-", "0.125", ",", "2.125", ")", "save_image", "(", "ax", ".", "figure", ",", "\"surface_is_valid1.png\"", ")" ]
Image for :meth`.Surface.is_valid` docstring.
[ "Image", "for", ":", "meth", ".", "Surface", ".", "is_valid", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L471-L480
dhermes/bezier
docs/make_images.py
surface_is_valid3
def surface_is_valid3(surface): """Image for :meth`.Surface.is_valid` docstring.""" if NO_IMAGES: return edge1, edge2, edge3 = surface.edges N = 128 # Compute points on each edge. std_s = np.linspace(0.0, 1.0, N + 1) points1 = edge1.evaluate_multi(std_s) points2 = edge2.evaluate_multi(std_s) points3 = edge3.evaluate_multi(std_s) # Compute the actual boundary where the Jacobian is 0. s_vals = np.linspace(0.0, 0.2, N) t_discrim = np.sqrt((1.0 - s_vals) * (1.0 - 5.0 * s_vals)) t_top = 0.5 * (1.0 - s_vals + t_discrim) t_bottom = 0.5 * (1.0 - s_vals - t_discrim) jacobian_zero_params = np.zeros((2 * N - 1, 2), order="F") jacobian_zero_params[:N, 0] = s_vals jacobian_zero_params[:N, 1] = t_top jacobian_zero_params[N:, 0] = s_vals[-2::-1] jacobian_zero_params[N:, 1] = t_bottom[-2::-1] jac_edge = surface.evaluate_cartesian_multi(jacobian_zero_params) # Add the surface to the plot and add a dashed line # for each "true" edge. figure = plt.figure() ax = figure.gca() line, = ax.plot(jac_edge[0, :], jac_edge[1, :]) color = line.get_color() ax.plot(points1[0, :], points1[1, :], color="black", linestyle="dashed") ax.plot(points2[0, :], points2[1, :], color="black", linestyle="dashed") ax.plot(points3[0, :], points3[1, :], color="black", linestyle="dashed") polygon = np.hstack([points1[:, 1:], points2[:, 1:], jac_edge[:, 1:]]) add_patch(ax, polygon, color, with_nodes=False) ax.axis("scaled") ax.set_xlim(-0.0625, 1.0625) ax.set_ylim(-0.0625, 1.0625) save_image(ax.figure, "surface_is_valid3.png")
python
def surface_is_valid3(surface): """Image for :meth`.Surface.is_valid` docstring.""" if NO_IMAGES: return edge1, edge2, edge3 = surface.edges N = 128 # Compute points on each edge. std_s = np.linspace(0.0, 1.0, N + 1) points1 = edge1.evaluate_multi(std_s) points2 = edge2.evaluate_multi(std_s) points3 = edge3.evaluate_multi(std_s) # Compute the actual boundary where the Jacobian is 0. s_vals = np.linspace(0.0, 0.2, N) t_discrim = np.sqrt((1.0 - s_vals) * (1.0 - 5.0 * s_vals)) t_top = 0.5 * (1.0 - s_vals + t_discrim) t_bottom = 0.5 * (1.0 - s_vals - t_discrim) jacobian_zero_params = np.zeros((2 * N - 1, 2), order="F") jacobian_zero_params[:N, 0] = s_vals jacobian_zero_params[:N, 1] = t_top jacobian_zero_params[N:, 0] = s_vals[-2::-1] jacobian_zero_params[N:, 1] = t_bottom[-2::-1] jac_edge = surface.evaluate_cartesian_multi(jacobian_zero_params) # Add the surface to the plot and add a dashed line # for each "true" edge. figure = plt.figure() ax = figure.gca() line, = ax.plot(jac_edge[0, :], jac_edge[1, :]) color = line.get_color() ax.plot(points1[0, :], points1[1, :], color="black", linestyle="dashed") ax.plot(points2[0, :], points2[1, :], color="black", linestyle="dashed") ax.plot(points3[0, :], points3[1, :], color="black", linestyle="dashed") polygon = np.hstack([points1[:, 1:], points2[:, 1:], jac_edge[:, 1:]]) add_patch(ax, polygon, color, with_nodes=False) ax.axis("scaled") ax.set_xlim(-0.0625, 1.0625) ax.set_ylim(-0.0625, 1.0625) save_image(ax.figure, "surface_is_valid3.png")
[ "def", "surface_is_valid3", "(", "surface", ")", ":", "if", "NO_IMAGES", ":", "return", "edge1", ",", "edge2", ",", "edge3", "=", "surface", ".", "edges", "N", "=", "128", "# Compute points on each edge.", "std_s", "=", "np", ".", "linspace", "(", "0.0", ",", "1.0", ",", "N", "+", "1", ")", "points1", "=", "edge1", ".", "evaluate_multi", "(", "std_s", ")", "points2", "=", "edge2", ".", "evaluate_multi", "(", "std_s", ")", "points3", "=", "edge3", ".", "evaluate_multi", "(", "std_s", ")", "# Compute the actual boundary where the Jacobian is 0.", "s_vals", "=", "np", ".", "linspace", "(", "0.0", ",", "0.2", ",", "N", ")", "t_discrim", "=", "np", ".", "sqrt", "(", "(", "1.0", "-", "s_vals", ")", "*", "(", "1.0", "-", "5.0", "*", "s_vals", ")", ")", "t_top", "=", "0.5", "*", "(", "1.0", "-", "s_vals", "+", "t_discrim", ")", "t_bottom", "=", "0.5", "*", "(", "1.0", "-", "s_vals", "-", "t_discrim", ")", "jacobian_zero_params", "=", "np", ".", "zeros", "(", "(", "2", "*", "N", "-", "1", ",", "2", ")", ",", "order", "=", "\"F\"", ")", "jacobian_zero_params", "[", ":", "N", ",", "0", "]", "=", "s_vals", "jacobian_zero_params", "[", ":", "N", ",", "1", "]", "=", "t_top", "jacobian_zero_params", "[", "N", ":", ",", "0", "]", "=", "s_vals", "[", "-", "2", ":", ":", "-", "1", "]", "jacobian_zero_params", "[", "N", ":", ",", "1", "]", "=", "t_bottom", "[", "-", "2", ":", ":", "-", "1", "]", "jac_edge", "=", "surface", ".", "evaluate_cartesian_multi", "(", "jacobian_zero_params", ")", "# Add the surface to the plot and add a dashed line", "# for each \"true\" edge.", "figure", "=", "plt", ".", "figure", "(", ")", "ax", "=", "figure", ".", "gca", "(", ")", "line", ",", "=", "ax", ".", "plot", "(", "jac_edge", "[", "0", ",", ":", "]", ",", "jac_edge", "[", "1", ",", ":", "]", ")", "color", "=", "line", ".", "get_color", "(", ")", "ax", ".", "plot", "(", "points1", "[", "0", ",", ":", "]", ",", "points1", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"dashed\"", ")", "ax", ".", "plot", "(", "points2", "[", "0", ",", ":", "]", ",", "points2", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"dashed\"", ")", "ax", ".", "plot", "(", "points3", "[", "0", ",", ":", "]", ",", "points3", "[", "1", ",", ":", "]", ",", "color", "=", "\"black\"", ",", "linestyle", "=", "\"dashed\"", ")", "polygon", "=", "np", ".", "hstack", "(", "[", "points1", "[", ":", ",", "1", ":", "]", ",", "points2", "[", ":", ",", "1", ":", "]", ",", "jac_edge", "[", ":", ",", "1", ":", "]", "]", ")", "add_patch", "(", "ax", ",", "polygon", ",", "color", ",", "with_nodes", "=", "False", ")", "ax", ".", "axis", "(", "\"scaled\"", ")", "ax", ".", "set_xlim", "(", "-", "0.0625", ",", "1.0625", ")", "ax", ".", "set_ylim", "(", "-", "0.0625", ",", "1.0625", ")", "save_image", "(", "ax", ".", "figure", ",", "\"surface_is_valid3.png\"", ")" ]
Image for :meth`.Surface.is_valid` docstring.
[ "Image", "for", ":", "meth", ".", "Surface", ".", "is_valid", "docstring", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/make_images.py#L495-L532